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
}
Switch {
text: "Show tray icon"
checked: MSettings.showTray
onCheckedChanged: MSettings.showTray = checked
}
Switch {
text: "Confirm on Exit"
checked: MSettings.confirmOnExit

View File

@ -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

View File

@ -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)

View File

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

View File

@ -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<Connection*> 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);
};

View File

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