diff --git a/imports/Spectral/Page/SettingForm.ui.qml b/imports/Spectral/Page/SettingForm.ui.qml index 2355730..8f22f51 100644 --- a/imports/Spectral/Page/SettingForm.ui.qml +++ b/imports/Spectral/Page/SettingForm.ui.qml @@ -67,6 +67,13 @@ Page { onCheckedChanged: MSettings.pressAndHold = checked } + Switch { + text: "Show tray icon" + checked: MSettings.showTray + + onCheckedChanged: MSettings.showTray = checked + } + Switch { text: "Confirm on Exit" checked: MSettings.confirmOnExit diff --git a/imports/Spectral/Setting/Setting.qml b/imports/Spectral/Setting/Setting.qml index 84fce5e..17e69b9 100644 --- a/imports/Spectral/Setting/Setting.qml +++ b/imports/Spectral/Setting/Setting.qml @@ -4,6 +4,7 @@ import Qt.labs.settings 1.0 Settings { property bool pressAndHold + property bool showTray: true property bool confirmOnExit: true property bool darkTheme diff --git a/qml/main.qml b/qml/main.qml index b53cae3..d373823 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -3,6 +3,7 @@ import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 import QtQuick.Controls.Material 2.2 import Qt.labs.settings 1.0 +import Qt.labs.platform 1.0 as Platform import Spectral.Component 2.0 import Spectral.Page 2.0 @@ -29,15 +30,29 @@ ApplicationWindow { Material.accent: spectralController.color(currentConnection ? currentConnection.localUserId : "") + Platform.SystemTrayIcon { + visible: MSettings.showTray + iconSource: "qrc:/assets/img/icon.png" + + menu: Platform.Menu { + Platform.MenuItem { + text: qsTr("Hide Window") + onTriggered: hideWindow() + } + Platform.MenuItem { + text: qsTr("Quit") + onTriggered: Qt.quit() + } + } + + onActivated: showWindow() + } + Controller { id: spectralController - onShowWindow: { - window.show() - window.raise() - window.requestActivate() - } - onHideWindow: window.hide() + quitOnLastWindowClosed: !MSettings.showTray + onNotificationClicked: { roomPage.enteredRoom = currentConnection.room(roomId) showWindow() @@ -387,6 +402,16 @@ ApplicationWindow { value: currentConnection } + function showWindow() { + window.show() + window.raise() + window.requestActivate() + } + + function hideWindow() { + window.hide() + } + Component.onCompleted: { spectralController.initiated.connect(function() { if (spectralController.accountCount == 0) stackView.push(loginPage) diff --git a/src/controller.cpp b/src/controller.cpp index 3b10d1b..287786e 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -28,19 +28,11 @@ #include Controller::Controller(QObject* parent) - : QObject(parent), tray(this), notificationsManager(this) { + : QObject(parent), notificationsManager(this) { + QApplication::setQuitOnLastWindowClosed(false); + connect(¬ificationsManager, &NotificationsManager::notificationClicked, this, &Controller::notificationClicked); - tray.setIcon(QIcon(":/assets/img/icon.png")); - tray.setToolTip("Spectral"); - connect(&tray, &QSystemTrayIcon::activated, - [this](QSystemTrayIcon::ActivationReason r) { - if (r != QSystemTrayIcon::Context) emit showWindow(); - }); - trayMenu.addAction("Hide Window", [=] { emit hideWindow(); }); - trayMenu.addAction("Quit", [=] { QApplication::quit(); }); - tray.setContextMenu(&trayMenu); - tray.show(); Connection::setRoomType(); Connection::setUserType(); diff --git a/src/controller.h b/src/controller.h index 18d2532..43b889c 100644 --- a/src/controller.h +++ b/src/controller.h @@ -18,6 +18,8 @@ class Controller : public QObject { Q_OBJECT Q_PROPERTY(int accountCount READ accountCount NOTIFY connectionAdded NOTIFY connectionDropped) + Q_PROPERTY(bool quitOnLastWindowClosed READ quitOnLastWindowClosed WRITE + setQuitOnLastWindowClosed NOTIFY quitOnLastWindowClosedChanged) public: explicit Controller(QObject* parent = nullptr); @@ -35,13 +37,21 @@ class Controller : public QObject { // All the Q_PROPERTYs. int accountCount() { return m_connections.count(); } + bool quitOnLastWindowClosed() { + return QApplication::quitOnLastWindowClosed(); + } + void setQuitOnLastWindowClosed(bool value) { + if (quitOnLastWindowClosed() != value) { + QApplication::setQuitOnLastWindowClosed(value); + emit quitOnLastWindowClosedChanged(); + } + } + Q_INVOKABLE QColor color(QString userId); Q_INVOKABLE void setColor(QString userId, QColor newColor); private: QClipboard* m_clipboard = QApplication::clipboard(); - QSystemTrayIcon tray; - QMenu trayMenu; NotificationsManager notificationsManager; QVector m_connections; @@ -57,12 +67,11 @@ class Controller : public QObject { signals: void busyChanged(); void errorOccured(QString error, QString detail); - void showWindow(); - void hideWindow(); void connectionAdded(Connection* conn); void connectionDropped(Connection* conn); void initiated(); void notificationClicked(const QString roomId, const QString eventId); + void quitOnLastWindowClosedChanged(); public slots: void logout(Connection* conn); @@ -73,7 +82,8 @@ class Controller : public QObject { void playAudio(QUrl localFile); void postNotification(const QString& roomId, const QString& eventId, const QString& roomName, const QString& senderName, - const QString& text, const QImage& icon, const QUrl& iconPath); + const QString& text, const QImage& icon, + const QUrl& iconPath); static QImage safeImage(QImage image); }; diff --git a/src/main.cpp b/src/main.cpp index 365321b..2107a44 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -33,8 +33,6 @@ int main(int argc, char *argv[]) { app.setApplicationName("Spectral"); app.setWindowIcon(QIcon(":/assets/img/icon.png")); - app.setQuitOnLastWindowClosed(false); - qmlRegisterType("SortFilterProxyModel", 0, 2, "SortFilterProxyModel"); qmlRegisterType("Spectral", 0, 1, "ImageItem");