Skip to content

Commit

Permalink
Refactor tray window opening code for clarity and efficiency
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Cambra <claudio.cambra@gmail.com>
  • Loading branch information
claucambra committed Jun 30, 2022
1 parent 277aefc commit 53711d4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
7 changes: 1 addition & 6 deletions src/gui/owncloudgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -157,9 +154,7 @@ void ownCloudGui::slotOpenSettingsDialog()

void ownCloudGui::slotOpenMainDialog()
{
if (!_tray->isOpen()) {
_tray->showWindow();
}
_tray->showWindow();
}

void ownCloudGui::slotTrayClicked(QSystemTrayIcon::ActivationReason reason)
Expand Down
35 changes: 32 additions & 3 deletions src/gui/systray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QQuickWindow*>(trayWindowComponent.create());
}
hideWindow();
_trayWindow->hide();
emit activated(QSystemTrayIcon::ActivationReason::Unknown);

const auto folderMap = FolderMan::instance()->map();
Expand All @@ -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();
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/gui/systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -113,6 +111,9 @@ class Systray
public slots:
void slotNewUserSelected();

void showWindow();
void hideWindow();

private slots:
void slotUnpauseAllFolders();
void slotPauseAllFolders();
Expand Down Expand Up @@ -140,6 +141,7 @@ private slots:
bool _syncIsPaused = true;
QPointer<QQmlApplicationEngine> _trayEngine;
QPointer<QMenu> _contextMenu;
QPointer<QQuickWindow> _trayWindow;

AccessManagerFactory _accessManagerFactory;

Expand Down
18 changes: 2 additions & 16 deletions src/gui/tray/Window.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 53711d4

Please sign in to comment.