Move tray icon outside C++ and allow hiding tray icon.

This commit is contained in:
Black Hat 2018-10-21 10:17:21 +08:00
parent 73ce9c2bf1
commit aae84ba65f
6 changed files with 58 additions and 25 deletions

View File

@ -67,6 +67,13 @@ Page {
onCheckedChanged: MSettings.pressAndHold = checked onCheckedChanged: MSettings.pressAndHold = checked
} }
Switch {
text: "Show tray icon"
checked: MSettings.showTray
onCheckedChanged: MSettings.showTray = checked
}
Switch { Switch {
text: "Confirm on Exit" text: "Confirm on Exit"
checked: MSettings.confirmOnExit checked: MSettings.confirmOnExit

View File

@ -4,6 +4,7 @@ import Qt.labs.settings 1.0
Settings { Settings {
property bool pressAndHold property bool pressAndHold
property bool showTray: true
property bool confirmOnExit: true property bool confirmOnExit: true
property bool darkTheme property bool darkTheme

View File

@ -3,6 +3,7 @@ import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3 import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2 import QtQuick.Controls.Material 2.2
import Qt.labs.settings 1.0 import Qt.labs.settings 1.0
import Qt.labs.platform 1.0 as Platform
import Spectral.Component 2.0 import Spectral.Component 2.0
import Spectral.Page 2.0 import Spectral.Page 2.0
@ -29,15 +30,29 @@ ApplicationWindow {
Material.accent: spectralController.color(currentConnection ? currentConnection.localUserId : "") 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 { Controller {
id: spectralController id: spectralController
onShowWindow: { quitOnLastWindowClosed: !MSettings.showTray
window.show()
window.raise()
window.requestActivate()
}
onHideWindow: window.hide()
onNotificationClicked: { onNotificationClicked: {
roomPage.enteredRoom = currentConnection.room(roomId) roomPage.enteredRoom = currentConnection.room(roomId)
showWindow() showWindow()
@ -387,6 +402,16 @@ ApplicationWindow {
value: currentConnection value: currentConnection
} }
function showWindow() {
window.show()
window.raise()
window.requestActivate()
}
function hideWindow() {
window.hide()
}
Component.onCompleted: { Component.onCompleted: {
spectralController.initiated.connect(function() { spectralController.initiated.connect(function() {
if (spectralController.accountCount == 0) stackView.push(loginPage) if (spectralController.accountCount == 0) stackView.push(loginPage)

View File

@ -28,19 +28,11 @@
#include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkReply>
Controller::Controller(QObject* parent) Controller::Controller(QObject* parent)
: QObject(parent), tray(this), notificationsManager(this) { : QObject(parent), notificationsManager(this) {
QApplication::setQuitOnLastWindowClosed(false);
connect(&notificationsManager, &NotificationsManager::notificationClicked, connect(&notificationsManager, &NotificationsManager::notificationClicked,
this, &Controller::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<SpectralRoom>(); Connection::setRoomType<SpectralRoom>();
Connection::setUserType<SpectralUser>(); Connection::setUserType<SpectralUser>();

View File

@ -18,6 +18,8 @@ class Controller : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int accountCount READ accountCount NOTIFY connectionAdded NOTIFY Q_PROPERTY(int accountCount READ accountCount NOTIFY connectionAdded NOTIFY
connectionDropped) connectionDropped)
Q_PROPERTY(bool quitOnLastWindowClosed READ quitOnLastWindowClosed WRITE
setQuitOnLastWindowClosed NOTIFY quitOnLastWindowClosedChanged)
public: public:
explicit Controller(QObject* parent = nullptr); explicit Controller(QObject* parent = nullptr);
@ -35,13 +37,21 @@ class Controller : public QObject {
// All the Q_PROPERTYs. // All the Q_PROPERTYs.
int accountCount() { return m_connections.count(); } 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 QColor color(QString userId);
Q_INVOKABLE void setColor(QString userId, QColor newColor); Q_INVOKABLE void setColor(QString userId, QColor newColor);
private: private:
QClipboard* m_clipboard = QApplication::clipboard(); QClipboard* m_clipboard = QApplication::clipboard();
QSystemTrayIcon tray;
QMenu trayMenu;
NotificationsManager notificationsManager; NotificationsManager notificationsManager;
QVector<Connection*> m_connections; QVector<Connection*> m_connections;
@ -57,12 +67,11 @@ class Controller : public QObject {
signals: signals:
void busyChanged(); void busyChanged();
void errorOccured(QString error, QString detail); void errorOccured(QString error, QString detail);
void showWindow();
void hideWindow();
void connectionAdded(Connection* conn); void connectionAdded(Connection* conn);
void connectionDropped(Connection* conn); void connectionDropped(Connection* conn);
void initiated(); void initiated();
void notificationClicked(const QString roomId, const QString eventId); void notificationClicked(const QString roomId, const QString eventId);
void quitOnLastWindowClosedChanged();
public slots: public slots:
void logout(Connection* conn); void logout(Connection* conn);
@ -73,7 +82,8 @@ class Controller : public QObject {
void playAudio(QUrl localFile); void playAudio(QUrl localFile);
void postNotification(const QString& roomId, const QString& eventId, void postNotification(const QString& roomId, const QString& eventId,
const QString& roomName, const QString& senderName, 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); static QImage safeImage(QImage image);
}; };

View File

@ -1,4 +1,4 @@
#include <QApplication> #include <QGuiApplication>
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext> #include <QQmlContext>
@ -33,8 +33,6 @@ int main(int argc, char *argv[]) {
app.setApplicationName("Spectral"); app.setApplicationName("Spectral");
app.setWindowIcon(QIcon(":/assets/img/icon.png")); app.setWindowIcon(QIcon(":/assets/img/icon.png"));
app.setQuitOnLastWindowClosed(false);
qmlRegisterType<qqsfpm::QQmlSortFilterProxyModel>("SortFilterProxyModel", 0, qmlRegisterType<qqsfpm::QQmlSortFilterProxyModel>("SortFilterProxyModel", 0,
2, "SortFilterProxyModel"); 2, "SortFilterProxyModel");
qmlRegisterType<ImageItem>("Spectral", 0, 1, "ImageItem"); qmlRegisterType<ImageItem>("Spectral", 0, 1, "ImageItem");