From ce9e4a99c0467617bfcecea0c7639a9ed7018198 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Thu, 30 Jun 2022 17:42:39 +0200 Subject: [PATCH] Refactor tray window opening code for clarity and efficiency Signed-off-by: Claudio Cambra --- src/gui/owncloudgui.cpp | 7 +------ src/gui/systray.cpp | 40 ++++++++++++++++++++++++++++++++++++---- src/gui/systray.h | 17 ++++++++--------- src/gui/tray/Window.qml | 28 ++++------------------------ 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/gui/owncloudgui.cpp b/src/gui/owncloudgui.cpp index aa5066d608e76..97a26afa4892e 100644 --- a/src/gui/owncloudgui.cpp +++ b/src/gui/owncloudgui.cpp @@ -82,9 +82,6 @@ ownCloudGui::ownCloudGui(Application *parent) connect(_tray.data(), &Systray::openAccountWizard, this, &ownCloudGui::slotNewAccountWizard); - connect(_tray.data(), &Systray::openMainDialog, - this, &ownCloudGui::slotOpenMainDialog); - connect(_tray.data(), &Systray::openSettings, this, &ownCloudGui::slotShowSettings); @@ -157,9 +154,7 @@ void ownCloudGui::slotOpenSettingsDialog() void ownCloudGui::slotOpenMainDialog() { - if (!_tray->isOpen()) { - _tray->showWindow(); - } + _tray->showWindow(); } void ownCloudGui::slotTrayClicked(QSystemTrayIcon::ActivationReason reason) diff --git a/src/gui/systray.cpp b/src/gui/systray.cpp index 9db7ef835c588..8ec5f3374cfb8 100644 --- a/src/gui/systray.cpp +++ b/src/gui/systray.cpp @@ -120,7 +120,7 @@ Systray::Systray() #if defined(Q_OS_MACOS) || defined(Q_OS_WIN) connect(AccountManager::instance(), &AccountManager::accountAdded, - this, [this]{ emit showWindow(); }); + this, [this]{ showWindow(); }); #else // Since the positioning of the QSystemTrayIcon is borked on non-Windows and non-macOS desktop environments, // we hardcode the position of the tray to be in the center when we add a new account from somewhere like @@ -128,7 +128,7 @@ Systray::Systray() // is placed connect(AccountManager::instance(), &AccountManager::accountAdded, - this, [this]{ emit showWindow(WindowPosition::Center); }); + this, [this]{ showWindow(WindowPosition::Center); }); #endif } @@ -138,7 +138,9 @@ void Systray::create() if (!AccountManager::instance()->accounts().isEmpty()) { _trayEngine->rootContext()->setContextProperty("activityModel", UserModel::instance()->currentActivityModel()); } - _trayEngine->load(QStringLiteral("qrc:/qml/src/gui/tray/Window.qml")); + + QQmlComponent trayWindowComponent(_trayEngine, QStringLiteral("qrc:/qml/src/gui/tray/Window.qml")); + _trayWindow.reset(qobject_cast(trayWindowComponent.create())); } hideWindow(); emit activated(QSystemTrayIcon::ActivationReason::Unknown); @@ -152,6 +154,36 @@ void Systray::create() } } +void Systray::showWindow(WindowPosition position) +{ + if(isOpen() || !_trayWindow) { + return; + } + + if(position == WindowPosition::Center) { + positionWindowAtScreenCenter(_trayWindow.data()); + } else { + positionWindowAtTray(_trayWindow.data()); + } + _trayWindow->show(); + _trayWindow->raise(); + _trayWindow->requestActivate(); + + setIsOpen(true); + + UserModel::instance()->fetchCurrentActivityModel(); +} + +void Systray::hideWindow() +{ + if(!isOpen() || !_trayWindow) { + return; + } + + _trayWindow->hide(); + setIsOpen(false); +} + void Systray::setupContextMenu() { const auto oldContextMenu = _contextMenu.data(); @@ -169,7 +201,7 @@ void Systray::setupContextMenu() if (AccountManager::instance()->accounts().isEmpty()) { _contextMenu->addAction(tr("Add account"), this, &Systray::openAccountWizard); } else { - _contextMenu->addAction(tr("Open main dialog"), this, &Systray::openMainDialog); + _contextMenu->addAction(tr("Open main dialog"), this, [this]{ showWindow(); }); } auto pauseAction = _contextMenu->addAction(tr("Pause sync"), this, &Systray::slotPauseAllFolders); diff --git a/src/gui/systray.h b/src/gui/systray.h index 716f13e438653..55bce5792988d 100644 --- a/src/gui/systray.h +++ b/src/gui/systray.h @@ -75,7 +75,7 @@ class Systray enum class TaskBarPosition { Bottom, Left, Top, Right }; Q_ENUM(TaskBarPosition); - + enum class NotificationPosition { Default, TopLeft, TopRight, BottomLeft, BottomRight }; Q_ENUM(NotificationPosition); @@ -98,15 +98,10 @@ class Systray signals: void currentUserChanged(); void openAccountWizard(); - void openMainDialog(); void openSettings(); void openHelp(); void shutdown(); - // These window signals are listened to in Window.qml - void hideWindow(); - void showWindow(WindowPosition position = WindowPosition::Default); - void openShareDialog(const QString &sharePath, const QString &localPath); void showFileActivityDialog(const QString &objectName, const int objectId); void sendChatMessage(const QString &token, const QString &message, const QString &replyTo); @@ -117,15 +112,18 @@ class Systray public slots: void slotNewUserSelected(); + + void forceWindowInit(QQuickWindow *window) const; void positionWindowAtTray(QQuickWindow *window) const; void positionWindowAtScreenCenter(QQuickWindow *window) const; + void positionNotificationWindow(QQuickWindow *window) const; + + void showWindow(WindowPosition position = WindowPosition::Default); + void hideWindow(); void setSyncIsPaused(const bool syncIsPaused); void setIsOpen(const bool isOpen); - void forceWindowInit(QQuickWindow *window) const; - void positionNotificationWindow(QQuickWindow *window) const; - private slots: void slotUnpauseAllFolders(); void slotPauseAllFolders(); @@ -153,6 +151,7 @@ private slots: bool _syncIsPaused = true; QPointer _trayEngine; QPointer _contextMenu; + QSharedPointer _trayWindow; AccessManagerFactory _accessManagerFactory; diff --git a/src/gui/tray/Window.qml b/src/gui/tray/Window.qml index 6419e651ed9c1..1e84e1847c686 100644 --- a/src/gui/tray/Window.qml +++ b/src/gui/tray/Window.qml @@ -77,31 +77,11 @@ Window { Connections { target: Systray - function onShowWindow(position) { - if(trayWindow.visible) { - return; + function onIsOpenChanged() { + if(Systray.isOpen) { + accountMenu.close(); + appsMenu.close(); } - - accountMenu.close(); - appsMenu.close(); - - if(position === Systray.WindowPosition.Center) { - Systray.positionWindowAtScreenCenter(trayWindow); - } else { - Systray.positionWindowAtTray(trayWindow); - } - - trayWindow.show(); - trayWindow.raise(); - trayWindow.requestActivate(); - - Systray.isOpen = true; - UserModel.fetchCurrentActivityModel(); - } - - function onHideWindow() { - trayWindow.hide(); - Systray.isOpen = false; } function onShowFileActivityDialog(objectName, objectId) {