Implement appmenu on Wayland with org_kde_kwin_appmenu protocol

This commit is contained in:
Ilya Fedin 2021-05-13 08:16:21 +04:00 committed by John Preston
parent 434ef34378
commit 0b86feeeb5
4 changed files with 77 additions and 10 deletions

View file

@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <surface.h> #include <surface.h>
#include <xdgforeign.h> #include <xdgforeign.h>
#include <plasmashell.h> #include <plasmashell.h>
#include <appmenu.h>
using namespace KWayland::Client; using namespace KWayland::Client;
@ -36,6 +37,10 @@ public:
return _plasmaShell.get(); return _plasmaShell.get();
} }
[[nodiscard]] AppMenuManager *appMenuManager() {
return _appMenuManager.get();
}
[[nodiscard]] QEventLoop &interfacesLoop() { [[nodiscard]] QEventLoop &interfacesLoop() {
return _interfacesLoop; return _interfacesLoop;
} }
@ -51,6 +56,7 @@ private:
Registry _applicationRegistry; Registry _applicationRegistry;
std::unique_ptr<XdgExporter> _xdgExporter; std::unique_ptr<XdgExporter> _xdgExporter;
std::unique_ptr<PlasmaShell> _plasmaShell; std::unique_ptr<PlasmaShell> _plasmaShell;
std::unique_ptr<AppMenuManager> _appMenuManager;
QEventLoop _interfacesLoop; QEventLoop _interfacesLoop;
bool _interfacesAnnounced = false; bool _interfacesAnnounced = false;
}; };
@ -117,6 +123,21 @@ WaylandIntegration::Private::Private()
&PlasmaShell::destroy); &PlasmaShell::destroy);
}); });
connect(
&_applicationRegistry,
&Registry::appMenuAnnounced,
[=](uint name, uint version) {
_appMenuManager = std::unique_ptr<AppMenuManager>{
_applicationRegistry.createAppMenuManager(name, version),
};
connect(
_applicationConnection,
&ConnectionThread::connectionDied,
_appMenuManager.get(),
&AppMenuManager::destroy);
});
_connection.initConnection(); _connection.initConnection();
} }
@ -187,5 +208,27 @@ void WaylandIntegration::skipTaskbar(QWindow *window, bool skip) {
plasmaSurface->setSkipTaskbar(skip); plasmaSurface->setSkipTaskbar(skip);
} }
void WaylandIntegration::registerAppMenu(
QWindow *window,
const QString &serviceName,
const QString &objectPath) {
const auto manager = _private->appMenuManager();
if (!manager) {
return;
}
const auto surface = Surface::fromWindow(window);
if (!surface) {
return;
}
const auto appMenu = manager->create(surface, surface);
if (!appMenu) {
return;
}
appMenu->setAddress(serviceName, objectPath);
}
} // namespace internal } // namespace internal
} // namespace Platform } // namespace Platform

View file

@ -18,6 +18,10 @@ public:
QString nativeHandle(QWindow *window); QString nativeHandle(QWindow *window);
bool skipTaskbarSupported(); bool skipTaskbarSupported();
void skipTaskbar(QWindow *window, bool skip); void skipTaskbar(QWindow *window, bool skip);
void registerAppMenu(
QWindow *window,
const QString &serviceName,
const QString &objectPath);
private: private:
WaylandIntegration(); WaylandIntegration();

View file

@ -44,5 +44,11 @@ bool WaylandIntegration::skipTaskbarSupported() {
void WaylandIntegration::skipTaskbar(QWindow *window, bool skip) { void WaylandIntegration::skipTaskbar(QWindow *window, bool skip) {
} }
void WaylandIntegration::registerAppMenu(
QWindow *window,
const QString &serviceName,
const QString &objectPath) {
}
} // namespace internal } // namespace internal
} // namespace Platform } // namespace Platform

View file

@ -503,7 +503,15 @@ bool IsAppMenuSupported() {
// This call must be made from the same bus connection as DBusMenuExporter // This call must be made from the same bus connection as DBusMenuExporter
// So it must use QDBusConnection // So it must use QDBusConnection
void RegisterAppMenu(uint winId, const QString &menuPath) { void RegisterAppMenu(QWindow *window, const QString &menuPath) {
if (const auto integration = WaylandIntegration::Instance()) {
integration->registerAppMenu(
window,
QDBusConnection::sessionBus().baseService(),
menuPath);
return;
}
auto message = QDBusMessage::createMethodCall( auto message = QDBusMessage::createMethodCall(
kAppMenuService.utf16(), kAppMenuService.utf16(),
kAppMenuObjectPath.utf16(), kAppMenuObjectPath.utf16(),
@ -511,7 +519,7 @@ void RegisterAppMenu(uint winId, const QString &menuPath) {
qsl("RegisterWindow")); qsl("RegisterWindow"));
message.setArguments({ message.setArguments({
winId, window->winId(),
QVariant::fromValue(QDBusObjectPath(menuPath)) QVariant::fromValue(QDBusObjectPath(menuPath))
}); });
@ -520,7 +528,11 @@ void RegisterAppMenu(uint winId, const QString &menuPath) {
// This call must be made from the same bus connection as DBusMenuExporter // This call must be made from the same bus connection as DBusMenuExporter
// So it must use QDBusConnection // So it must use QDBusConnection
void UnregisterAppMenu(uint winId) { void UnregisterAppMenu(QWindow *window) {
if (const auto integration = WaylandIntegration::Instance()) {
return;
}
auto message = QDBusMessage::createMethodCall( auto message = QDBusMessage::createMethodCall(
kAppMenuService.utf16(), kAppMenuService.utf16(),
kAppMenuObjectPath.utf16(), kAppMenuObjectPath.utf16(),
@ -528,7 +540,7 @@ void UnregisterAppMenu(uint winId) {
qsl("UnregisterWindow")); qsl("UnregisterWindow"));
message.setArguments({ message.setArguments({
winId window->winId()
}); });
QDBusConnection::sessionBus().send(message); QDBusConnection::sessionBus().send(message);
@ -806,9 +818,9 @@ void MainWindow::handleAppMenuOwnerChanged(
} }
if (_appMenuSupported && _mainMenuExporter) { if (_appMenuSupported && _mainMenuExporter) {
RegisterAppMenu(winId(), kMainMenuObjectPath.utf16()); RegisterAppMenu(windowHandle(), kMainMenuObjectPath.utf16());
} else { } else {
UnregisterAppMenu(winId()); UnregisterAppMenu(windowHandle());
} }
} }
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
@ -1133,7 +1145,7 @@ void MainWindow::createGlobalMenu() {
psMainMenu); psMainMenu);
if (_appMenuSupported) { if (_appMenuSupported) {
RegisterAppMenu(winId(), kMainMenuObjectPath.utf16()); RegisterAppMenu(windowHandle(), kMainMenuObjectPath.utf16());
} }
updateGlobalMenu(); updateGlobalMenu();
@ -1269,9 +1281,11 @@ void MainWindow::handleVisibleChangedHook(bool visible) {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION #ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
if (_appMenuSupported && _mainMenuExporter) { if (_appMenuSupported && _mainMenuExporter) {
if (visible) { if (visible) {
RegisterAppMenu(winId(), kMainMenuObjectPath.utf16()); base::call_delayed(1, this, [=] {
RegisterAppMenu(windowHandle(), kMainMenuObjectPath.utf16());
});
} else { } else {
UnregisterAppMenu(winId()); UnregisterAppMenu(windowHandle());
} }
} }
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
@ -1299,7 +1313,7 @@ MainWindow::~MainWindow() {
delete _sniTrayIcon; delete _sniTrayIcon;
if (_appMenuSupported) { if (_appMenuSupported) {
UnregisterAppMenu(winId()); UnregisterAppMenu(windowHandle());
} }
delete _mainMenuExporter; delete _mainMenuExporter;