From 59430cce89471cdbcb3abd870a9fc1ca5883998b Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Fri, 10 Dec 2021 21:26:35 +0800 Subject: [PATCH] Add support for minimizing to system tray on startup If the user wants to automatically launch NeoChat when the system starts up, the user may also want to minimize the window to system tray on startup. So a new option named "Minimize to system tray on startup" is added. The option is only visible on desktop platforms, and is only enabled when "Close to system tray" is checked. In order to restore window geometry for the first time the user opens the window if the option is checked, 1. a new function named `restoreWindowGeometry` is added, and `restoreWindowGeometryConnections` will be enabled if the option is checked, and will be disabled after the window debuts. 2. `saveWindowGeometryConnections` will be enabled if the option is not checked, and will be disabled if checked and enabled after the window debuts. --- .../NeoChat/Settings/GeneralSettingsPage.qml | 10 +++++ qml/main.qml | 43 ++++++++++++++++--- src/controller.cpp | 8 ++++ src/controller.h | 1 + src/main.cpp | 7 ++- src/neochatconfig.kcfg | 4 ++ 6 files changed, 62 insertions(+), 11 deletions(-) diff --git a/imports/NeoChat/Settings/GeneralSettingsPage.qml b/imports/NeoChat/Settings/GeneralSettingsPage.qml index a1758fcfd..dfcad3319 100644 --- a/imports/NeoChat/Settings/GeneralSettingsPage.qml +++ b/imports/NeoChat/Settings/GeneralSettingsPage.qml @@ -25,6 +25,16 @@ Kirigami.ScrollablePage { Config.save() } } + QQC2.CheckBox { + text: i18n("Minimize to system tray on startup") + checked: Config.minimizeToSystemTrayOnStartup + visible: Controller.supportSystemTray && !Kirigami.Settings.isMobile + enabled: Config.systemTray && !Config.isMinimizeToSystemTrayOnStartupImmutable + onToggled: { + Config.minimizeToSystemTrayOnStartup = checked + Config.save() + } + } QQC2.CheckBox { // TODO: When there are enough notification and timeline event // settings, make 2 separate groups with FormData labels. diff --git a/qml/main.qml b/qml/main.qml index 10d8cb9d7..c927940c0 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -22,10 +22,9 @@ Kirigami.ApplicationWindow { minimumWidth: Kirigami.Units.gridUnit * 15 minimumHeight: Kirigami.Units.gridUnit * 20 + visible: false // Will be overridden in Component.onCompleted wideScreen: width > columnWidth * 5 - onClosing: Controller.saveWindowGeometry(root) - pageStack.initialPage: LoadingPage {} pageStack.globalToolBar.canContainHandles: true @@ -55,10 +54,17 @@ Kirigami.ApplicationWindow { onTriggered: Controller.saveWindowGeometry(root) } - onWidthChanged: saveWindowGeometryTimer.restart() - onHeightChanged: saveWindowGeometryTimer.restart() - onXChanged: saveWindowGeometryTimer.restart() - onYChanged: saveWindowGeometryTimer.restart() + Connections { + id: saveWindowGeometryConnections + enabled: false // Disable on startup to avoid writing wrong values if the window is hidden + target: root + + function onClosing() { Controller.saveWindowGeometry(root); } + function onWidthChanged() { saveWindowGeometryTimer.restart(); } + function onHeightChanged() { saveWindowGeometryTimer.restart(); } + function onXChanged() { saveWindowGeometryTimer.restart(); } + function onYChanged() { saveWindowGeometryTimer.restart(); } + } Shortcut { sequence: "Ctrl+K" @@ -271,7 +277,15 @@ Kirigami.ApplicationWindow { ] } - Component.onCompleted: Controller.setBlur(pageStack, Config.blur && !Config.compactLayout); + Component.onCompleted: { + Controller.setBlur(pageStack, Config.blur && !Config.compactLayout); + if (Config.minimizeToSystemTrayOnStartup && !Kirigami.Settings.isMobile && Controller.supportSystemTray && Config.systemTray) { + restoreWindowGeometryConnections.enabled = true; // To restore window size and position + } else { + visible = true; + saveWindowGeometryConnections.enabled = true; + } + } Connections { target: Config function onBlurChanged() { @@ -349,6 +363,21 @@ Kirigami.ApplicationWindow { } } + Connections { + id: restoreWindowGeometryConnections + enabled: false + target: root + + function onVisibleChanged() { + if (!visible) { + return; + } + Controller.restoreWindowGeometry(root); + restoreWindowGeometryConnections.enabled = false; // Only restore window geometry for the first time + saveWindowGeometryConnections.enabled = true; + } + } + Connections { target: Controller.activeConnection function onDirectChatAvailable(directChat) { diff --git a/src/controller.cpp b/src/controller.cpp index 0cc984ca1..2372dee13 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -580,6 +580,14 @@ void Controller::setActiveConnection(Connection *connection) Q_EMIT activeConnectionChanged(); } +void Controller::restoreWindowGeometry(QQuickWindow *window) +{ + KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); + KConfigGroup windowGroup(&dataResource, "Window"); + KWindowConfig::restoreWindowSize(window, windowGroup); + KWindowConfig::restoreWindowPosition(window, windowGroup); +} + void Controller::saveWindowGeometry(QQuickWindow *window) { KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); diff --git a/src/controller.h b/src/controller.h index 1741bea31..39240631d 100644 --- a/src/controller.h +++ b/src/controller.h @@ -143,6 +143,7 @@ public Q_SLOTS: static void playAudio(const QUrl &localFile); void changeAvatar(Quotient::Connection *conn, const QUrl &localFile); static void markAllMessagesAsRead(Quotient::Connection *conn); + void restoreWindowGeometry(QQuickWindow *); void saveWindowGeometry(QQuickWindow *); private: diff --git a/src/main.cpp b/src/main.cpp index 23aedb020..f8820374d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -255,10 +255,9 @@ int main(int argc, char *argv[]) for (auto obj : rootObjects) { auto view = qobject_cast(obj); if (view) { - KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); - KConfigGroup windowGroup(&dataResource, "Window"); - KWindowConfig::restoreWindowSize(view, windowGroup); - KWindowConfig::restoreWindowPosition(view, windowGroup); + if (view->isVisible()) { + Controller::instance().restoreWindowGeometry(view); + } break; } } diff --git a/src/neochatconfig.kcfg b/src/neochatconfig.kcfg index f3771e8b0..12fa3b0e4 100644 --- a/src/neochatconfig.kcfg +++ b/src/neochatconfig.kcfg @@ -73,6 +73,10 @@ true + + + false + true