Skip to content

Commit 61c63cb

Browse files
committed
Refactor tray window opening code for clarity and efficiency
Signed-off-by: Claudio Cambra <claudio.cambra@gmail.com>
1 parent 277aefc commit 61c63cb

4 files changed

Lines changed: 39 additions & 27 deletions

File tree

src/gui/owncloudgui.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ ownCloudGui::ownCloudGui(Application *parent)
8282
connect(_tray.data(), &Systray::openAccountWizard,
8383
this, &ownCloudGui::slotNewAccountWizard);
8484

85-
connect(_tray.data(), &Systray::openMainDialog,
86-
this, &ownCloudGui::slotOpenMainDialog);
87-
8885
connect(_tray.data(), &Systray::openSettings,
8986
this, &ownCloudGui::slotShowSettings);
9087

@@ -157,9 +154,7 @@ void ownCloudGui::slotOpenSettingsDialog()
157154

158155
void ownCloudGui::slotOpenMainDialog()
159156
{
160-
if (!_tray->isOpen()) {
161-
_tray->showWindow();
162-
}
157+
_tray->showWindow();
163158
}
164159

165160
void ownCloudGui::slotTrayClicked(QSystemTrayIcon::ActivationReason reason)

src/gui/systray.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ void Systray::create()
126126
if (!AccountManager::instance()->accounts().isEmpty()) {
127127
_trayEngine->rootContext()->setContextProperty("activityModel", UserModel::instance()->currentActivityModel());
128128
}
129-
_trayEngine->load(QStringLiteral("qrc:/qml/src/gui/tray/Window.qml"));
129+
130+
QQmlComponent trayWindowComponent(_trayEngine, QStringLiteral("qrc:/qml/src/gui/tray/Window.qml"));
131+
_trayWindow = qobject_cast<QQuickWindow*>(trayWindowComponent.create());
130132
}
131133
hideWindow();
132134
emit activated(QSystemTrayIcon::ActivationReason::Unknown);
@@ -140,6 +142,33 @@ void Systray::create()
140142
}
141143
}
142144

145+
void Systray::showWindow()
146+
{
147+
if(isOpen() || !_trayWindow) {
148+
return;
149+
}
150+
151+
positionWindow(_trayWindow.data());
152+
_trayWindow->show();
153+
_trayWindow->raise();
154+
_trayWindow->requestActivate();
155+
156+
UserModel::instance()->fetchCurrentActivityModel();
157+
158+
setOpened();
159+
Q_EMIT windowOpened();
160+
}
161+
162+
void Systray::hideWindow()
163+
{
164+
if(!isOpen() || !_trayWindow) {
165+
return;
166+
}
167+
168+
_trayWindow->hide();
169+
setClosed();
170+
}
171+
143172
void Systray::setupContextMenu()
144173
{
145174
const auto oldContextMenu = _contextMenu.data();
@@ -157,7 +186,7 @@ void Systray::setupContextMenu()
157186
if (AccountManager::instance()->accounts().isEmpty()) {
158187
_contextMenu->addAction(tr("Add account"), this, &Systray::openAccountWizard);
159188
} else {
160-
_contextMenu->addAction(tr("Open main dialog"), this, &Systray::openMainDialog);
189+
_contextMenu->addAction(tr("Open main dialog"), this, &Systray::showWindow);
161190
}
162191

163192
auto pauseAction = _contextMenu->addAction(tr("Pause sync"), this, &Systray::slotPauseAllFolders);

src/gui/systray.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ class Systray
9898
signals:
9999
void currentUserChanged();
100100
void openAccountWizard();
101-
void openMainDialog();
102101
void openSettings();
103102
void openHelp();
104103
void shutdown();
105104

106-
void hideWindow();
107-
void showWindow();
105+
void windowOpened();
108106
void openShareDialog(const QString &sharePath, const QString &localPath);
109107
void showFileActivityDialog(const QString &objectName, const int objectId);
110108
void sendChatMessage(const QString &token, const QString &message, const QString &replyTo);
@@ -113,6 +111,9 @@ class Systray
113111
public slots:
114112
void slotNewUserSelected();
115113

114+
void showWindow();
115+
void hideWindow();
116+
116117
private slots:
117118
void slotUnpauseAllFolders();
118119
void slotPauseAllFolders();
@@ -140,6 +141,7 @@ private slots:
140141
bool _syncIsPaused = true;
141142
QPointer<QQmlApplicationEngine> _trayEngine;
142143
QPointer<QMenu> _contextMenu;
144+
QPointer<QQuickWindow> _trayWindow;
143145

144146
AccessManagerFactory _accessManagerFactory;
145147

src/gui/tray/Window.qml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ Window {
4343
}
4444
}
4545

46-
onClosing: {
47-
Systray.setClosed()
48-
}
46+
onClosing: Systray.setClosed()
4947

5048
onVisibleChanged: {
5149
// HACK: reload account Instantiator immediately by restting it - could be done better I guess
@@ -78,21 +76,9 @@ Window {
7876

7977
Connections {
8078
target: Systray
81-
function onShowWindow() {
79+
function onWindowOpened() {
8280
accountMenu.close();
8381
appsMenu.close();
84-
Systray.positionWindow(trayWindow);
85-
86-
trayWindow.show();
87-
trayWindow.raise();
88-
trayWindow.requestActivate();
89-
90-
Systray.setOpened();
91-
UserModel.fetchCurrentActivityModel();
92-
}
93-
function onHideWindow() {
94-
trayWindow.hide();
95-
Systray.setClosed();
9682
}
9783

9884
function onShowFileActivityDialog(objectName, objectId) {

0 commit comments

Comments
 (0)