mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-16 14:17:12 +02:00
Implement appmenu on Wayland with org_kde_kwin_appmenu protocol
This commit is contained in:
parent
434ef34378
commit
0b86feeeb5
4 changed files with 77 additions and 10 deletions
|
@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
#include <surface.h>
|
||||
#include <xdgforeign.h>
|
||||
#include <plasmashell.h>
|
||||
#include <appmenu.h>
|
||||
|
||||
using namespace KWayland::Client;
|
||||
|
||||
|
@ -36,6 +37,10 @@ public:
|
|||
return _plasmaShell.get();
|
||||
}
|
||||
|
||||
[[nodiscard]] AppMenuManager *appMenuManager() {
|
||||
return _appMenuManager.get();
|
||||
}
|
||||
|
||||
[[nodiscard]] QEventLoop &interfacesLoop() {
|
||||
return _interfacesLoop;
|
||||
}
|
||||
|
@ -51,6 +56,7 @@ private:
|
|||
Registry _applicationRegistry;
|
||||
std::unique_ptr<XdgExporter> _xdgExporter;
|
||||
std::unique_ptr<PlasmaShell> _plasmaShell;
|
||||
std::unique_ptr<AppMenuManager> _appMenuManager;
|
||||
QEventLoop _interfacesLoop;
|
||||
bool _interfacesAnnounced = false;
|
||||
};
|
||||
|
@ -117,6 +123,21 @@ WaylandIntegration::Private::Private()
|
|||
&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();
|
||||
}
|
||||
|
||||
|
@ -187,5 +208,27 @@ void WaylandIntegration::skipTaskbar(QWindow *window, bool 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 Platform
|
||||
|
|
|
@ -18,6 +18,10 @@ public:
|
|||
QString nativeHandle(QWindow *window);
|
||||
bool skipTaskbarSupported();
|
||||
void skipTaskbar(QWindow *window, bool skip);
|
||||
void registerAppMenu(
|
||||
QWindow *window,
|
||||
const QString &serviceName,
|
||||
const QString &objectPath);
|
||||
|
||||
private:
|
||||
WaylandIntegration();
|
||||
|
|
|
@ -44,5 +44,11 @@ bool WaylandIntegration::skipTaskbarSupported() {
|
|||
void WaylandIntegration::skipTaskbar(QWindow *window, bool skip) {
|
||||
}
|
||||
|
||||
void WaylandIntegration::registerAppMenu(
|
||||
QWindow *window,
|
||||
const QString &serviceName,
|
||||
const QString &objectPath) {
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace Platform
|
||||
|
|
|
@ -503,7 +503,15 @@ bool IsAppMenuSupported() {
|
|||
|
||||
// This call must be made from the same bus connection as DBusMenuExporter
|
||||
// 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(
|
||||
kAppMenuService.utf16(),
|
||||
kAppMenuObjectPath.utf16(),
|
||||
|
@ -511,7 +519,7 @@ void RegisterAppMenu(uint winId, const QString &menuPath) {
|
|||
qsl("RegisterWindow"));
|
||||
|
||||
message.setArguments({
|
||||
winId,
|
||||
window->winId(),
|
||||
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
|
||||
// So it must use QDBusConnection
|
||||
void UnregisterAppMenu(uint winId) {
|
||||
void UnregisterAppMenu(QWindow *window) {
|
||||
if (const auto integration = WaylandIntegration::Instance()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto message = QDBusMessage::createMethodCall(
|
||||
kAppMenuService.utf16(),
|
||||
kAppMenuObjectPath.utf16(),
|
||||
|
@ -528,7 +540,7 @@ void UnregisterAppMenu(uint winId) {
|
|||
qsl("UnregisterWindow"));
|
||||
|
||||
message.setArguments({
|
||||
winId
|
||||
window->winId()
|
||||
});
|
||||
|
||||
QDBusConnection::sessionBus().send(message);
|
||||
|
@ -806,9 +818,9 @@ void MainWindow::handleAppMenuOwnerChanged(
|
|||
}
|
||||
|
||||
if (_appMenuSupported && _mainMenuExporter) {
|
||||
RegisterAppMenu(winId(), kMainMenuObjectPath.utf16());
|
||||
RegisterAppMenu(windowHandle(), kMainMenuObjectPath.utf16());
|
||||
} else {
|
||||
UnregisterAppMenu(winId());
|
||||
UnregisterAppMenu(windowHandle());
|
||||
}
|
||||
}
|
||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||
|
@ -1133,7 +1145,7 @@ void MainWindow::createGlobalMenu() {
|
|||
psMainMenu);
|
||||
|
||||
if (_appMenuSupported) {
|
||||
RegisterAppMenu(winId(), kMainMenuObjectPath.utf16());
|
||||
RegisterAppMenu(windowHandle(), kMainMenuObjectPath.utf16());
|
||||
}
|
||||
|
||||
updateGlobalMenu();
|
||||
|
@ -1269,9 +1281,11 @@ void MainWindow::handleVisibleChangedHook(bool visible) {
|
|||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||
if (_appMenuSupported && _mainMenuExporter) {
|
||||
if (visible) {
|
||||
RegisterAppMenu(winId(), kMainMenuObjectPath.utf16());
|
||||
base::call_delayed(1, this, [=] {
|
||||
RegisterAppMenu(windowHandle(), kMainMenuObjectPath.utf16());
|
||||
});
|
||||
} else {
|
||||
UnregisterAppMenu(winId());
|
||||
UnregisterAppMenu(windowHandle());
|
||||
}
|
||||
}
|
||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||
|
@ -1299,7 +1313,7 @@ MainWindow::~MainWindow() {
|
|||
delete _sniTrayIcon;
|
||||
|
||||
if (_appMenuSupported) {
|
||||
UnregisterAppMenu(winId());
|
||||
UnregisterAppMenu(windowHandle());
|
||||
}
|
||||
|
||||
delete _mainMenuExporter;
|
||||
|
|
Loading…
Add table
Reference in a new issue