Skip to content

Commit

Permalink
Merge pull request #4688 from nextcloud/bugfix/refactor-tray-window-open
Browse files Browse the repository at this point in the history
Refactor tray window opening code for clarity and efficiency
  • Loading branch information
claucambra authored Jul 12, 2022
2 parents 53aba48 + ce9e4a9 commit fc16d82
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 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
40 changes: 36 additions & 4 deletions src/gui/systray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ 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
// the wizard. Otherwise with the conventional method we end up with the tray appearing wherever the cursor
// is placed

connect(AccountManager::instance(), &AccountManager::accountAdded,
this, [this]{ emit showWindow(WindowPosition::Center); });
this, [this]{ showWindow(WindowPosition::Center); });
#endif
}

Expand All @@ -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<QQuickWindow*>(trayWindowComponent.create()));
}
hideWindow();
emit activated(QSystemTrayIcon::ActivationReason::Unknown);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions src/gui/systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -153,6 +151,7 @@ private slots:
bool _syncIsPaused = true;
QPointer<QQmlApplicationEngine> _trayEngine;
QPointer<QMenu> _contextMenu;
QSharedPointer<QQuickWindow> _trayWindow;

AccessManagerFactory _accessManagerFactory;

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

0 comments on commit fc16d82

Please sign in to comment.