From 53711d4dff9de4e062d2d4ee2a2e9bc8819dba6a 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 | 35 ++++++++++++++++++++++++++++++++--- src/gui/systray.h | 8 +++++--- src/gui/tray/Window.qml | 18 ++---------------- 4 files changed, 40 insertions(+), 28 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 128937cde5bd6..b0cb1caeef50d 100644 --- a/src/gui/systray.cpp +++ b/src/gui/systray.cpp @@ -126,9 +126,11 @@ 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 = qobject_cast(trayWindowComponent.create()); } - hideWindow(); + _trayWindow->hide(); emit activated(QSystemTrayIcon::ActivationReason::Unknown); const auto folderMap = FolderMan::instance()->map(); @@ -140,6 +142,33 @@ void Systray::create() } } +void Systray::showWindow() +{ + if(isOpen() || !_trayWindow) { + return; + } + + positionWindow(_trayWindow.data()); + _trayWindow->show(); + _trayWindow->raise(); + _trayWindow->requestActivate(); + + UserModel::instance()->fetchCurrentActivityModel(); + + setOpened(); + Q_EMIT windowOpened(); +} + +void Systray::hideWindow() +{ + if(!isOpen() || !_trayWindow) { + return; + } + + _trayWindow->hide(); + setClosed(); +} + void Systray::setupContextMenu() { const auto oldContextMenu = _contextMenu.data(); @@ -157,7 +186,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, &Systray::showWindow); } auto pauseAction = _contextMenu->addAction(tr("Pause sync"), this, &Systray::slotPauseAllFolders); diff --git a/src/gui/systray.h b/src/gui/systray.h index 18f01cd937aea..c8e076711b00f 100644 --- a/src/gui/systray.h +++ b/src/gui/systray.h @@ -98,13 +98,11 @@ class Systray signals: void currentUserChanged(); void openAccountWizard(); - void openMainDialog(); void openSettings(); void openHelp(); void shutdown(); - void hideWindow(); - void showWindow(); + void windowOpened(); 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); @@ -113,6 +111,9 @@ class Systray public slots: void slotNewUserSelected(); + void showWindow(); + void hideWindow(); + private slots: void slotUnpauseAllFolders(); void slotPauseAllFolders(); @@ -140,6 +141,7 @@ private slots: bool _syncIsPaused = true; QPointer _trayEngine; QPointer _contextMenu; + QPointer _trayWindow; AccessManagerFactory _accessManagerFactory; diff --git a/src/gui/tray/Window.qml b/src/gui/tray/Window.qml index 3b5d80ff38aeb..5b9f6d5b965f1 100644 --- a/src/gui/tray/Window.qml +++ b/src/gui/tray/Window.qml @@ -43,9 +43,7 @@ Window { } } - onClosing: { - Systray.setClosed() - } + onClosing: Systray.setClosed() onVisibleChanged: { // HACK: reload account Instantiator immediately by restting it - could be done better I guess @@ -78,21 +76,9 @@ Window { Connections { target: Systray - function onShowWindow() { + function onWindowOpened() { accountMenu.close(); appsMenu.close(); - Systray.positionWindow(trayWindow); - - trayWindow.show(); - trayWindow.raise(); - trayWindow.requestActivate(); - - Systray.setOpened(); - UserModel.fetchCurrentActivityModel(); - } - function onHideWindow() { - trayWindow.hide(); - Systray.setClosed(); } function onShowFileActivityDialog(objectName, objectId) {