From 4383dfa5a1847ecd313a121741739cf678f1f569 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sun, 19 Aug 2018 14:32:18 +0800 Subject: [PATCH] Working notification. Also, fixes #6. --- qml/Room.qml | 2 +- qml/form/RoomForm.qml | 3 +++ qml/form/RoomListForm.qml | 17 ++++------------- qml/main.qml | 18 +----------------- src/controller.cpp | 12 ++++++++++++ src/controller.h | 5 +++++ src/matriqueroom.h | 2 +- src/roomlistmodel.cpp | 32 +++++++++++++++----------------- src/roomlistmodel.h | 5 +++-- 9 files changed, 45 insertions(+), 51 deletions(-) diff --git a/qml/Room.qml b/qml/Room.qml index c62f3e6..32d990d 100644 --- a/qml/Room.qml +++ b/qml/Room.qml @@ -16,7 +16,7 @@ Page { connection: page.connection onRoomAdded: setting.lazyLoad ? {} : room.getPreviousContent(20) - onNewMessage: trayIcon.showMessage("New message", "New message for room " + room.displayName) + onNewMessage: matriqueController.showMessage(roomName, content, icon) } RowLayout { diff --git a/qml/form/RoomForm.qml b/qml/form/RoomForm.qml index 9aed121..1efbc16 100644 --- a/qml/form/RoomForm.qml +++ b/qml/form/RoomForm.qml @@ -318,6 +318,9 @@ Item { bottomPadding: 0 selectByMouse: true + text: currentRoom ? currentRoom.cachedInput : "" + onTextChanged: currentRoom.cachedInput = text + Keys.onReturnPressed: { if (inputField.text) { inputField.postMessage(inputField.text) diff --git a/qml/form/RoomListForm.qml b/qml/form/RoomListForm.qml index 09f8a1d..7086a7c 100644 --- a/qml/form/RoomListForm.qml +++ b/qml/form/RoomListForm.qml @@ -30,13 +30,15 @@ Item { id: searchField width: parent.width - 18 height: 36 - color: "black" - leftPadding: mini ? 4 : 16 + color: "white" + leftPadding: mini ? 4 : 32 topPadding: 0 bottomPadding: 0 anchors.centerIn: parent background: Row { + visible: !parent.text + MaterialIcon { icon: "\ue8b6" color: "white" @@ -55,17 +57,6 @@ Item { verticalAlignment: Text.AlignVCenter } } - - Rectangle { - z: -2 - width: searchField.activeFocus || searchField.text ? parent.width : 0 - height: parent.height - color: "white" - - Behavior on width { - PropertyAnimation { easing.type: Easing.InOutCubic; duration: 200 } - } - } } } diff --git a/qml/main.qml b/qml/main.qml index 5174a76..cdc3450 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -4,7 +4,6 @@ import QtQuick.Layouts 1.3 import QtQuick.Controls.Material 2.2 import QtGraphicalEffects 1.0 import Qt.labs.settings 1.0 as Settings -import Qt.labs.platform 1.0 as Platform import Matrique 0.1 import "component" @@ -40,21 +39,6 @@ ApplicationWindow { property alias miniMode: settingPage.miniMode } - Platform.SystemTrayIcon { - id: trayIcon - - visible: true - iconSource: "qrc:/asset/img/icon.png" - - onActivated: window.active ? window.hide() : raiseWindow() - - function raiseWindow() { - window.show() - window.raise() - window.requestActivate() - } - } - Controller { id: matriqueController } @@ -87,7 +71,7 @@ ApplicationWindow { parent: null - connection: window.connection + connection: matriqueController.isLogin ? window.connection : undefined } Setting { diff --git a/src/controller.cpp b/src/controller.cpp index b46fa27..bc5941e 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -8,8 +8,15 @@ #include "csapi/joining.h" #include +#include Controller::Controller(QObject* parent) : QObject(parent) { + tray->setIcon(QIcon(":/asset/img/icon.png")); + tray->setToolTip("Matrique"); + trayMenu->addAction("Quit", [=] { QApplication::quit(); }); + tray->setContextMenu(trayMenu); + tray->show(); + Connection::setRoomType(); connect(m_connection, &Connection::connected, this, &Controller::connected); @@ -102,3 +109,8 @@ void Controller::playAudio(QUrl localFile) { player->play(); connect(player, &QMediaPlayer::stateChanged, [=] { player->deleteLater(); }); } + +void Controller::showMessage(const QString& title, const QString& msg, + const QIcon& icon) { + tray->showMessage(title, msg, icon); +} diff --git a/src/controller.h b/src/controller.h index 0b76732..a6b0cb0 100644 --- a/src/controller.h +++ b/src/controller.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include using namespace QMatrixClient; @@ -78,6 +80,8 @@ class Controller : public QObject { private: QClipboard* m_clipboard = QApplication::clipboard(); + QSystemTrayIcon* tray = new QSystemTrayIcon(); + QMenu* trayMenu = new QMenu(); bool m_isLogin = false; QString m_userID; @@ -104,6 +108,7 @@ class Controller : public QObject { void createDirectChat(const QString& userID); void copyToClipboard(const QString& text); void playAudio(QUrl localFile); + void showMessage(const QString& title, const QString& msg, const QIcon& icon); }; #endif // CONTROLLER_H diff --git a/src/matriqueroom.h b/src/matriqueroom.h index 6525b60..50ac03c 100644 --- a/src/matriqueroom.h +++ b/src/matriqueroom.h @@ -24,7 +24,7 @@ class MatriqueRoom : public Room { } private: - QString m_cachedInput = ""; + QString m_cachedInput; QString getMIME(const QUrl& fileUrl) const; void postFile(const QUrl& localFile, const QUrl& mxcUrl); diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index 24fb3a2..621689e 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -1,5 +1,7 @@ #include "roomlistmodel.h" +#include "user.h" + #include "events/roomevent.h" #include @@ -14,10 +16,12 @@ RoomListModel::~RoomListModel() {} void RoomListModel::setConnection(Connection* connection) { Q_ASSERT(connection); + if (connection == m_connection) return; + using QMatrixClient::Room; m_connection = connection; - if (!connection->accessToken().isEmpty()) doResetModel(); + doResetModel(); connect(connection, &Connection::connected, this, &RoomListModel::doResetModel); @@ -43,7 +47,6 @@ void RoomListModel::doAddRoom(Room* r) { if (auto* room = static_cast(r)) { m_rooms.append(room); connectRoomSignals(room); -// qCritical() << room->cachedInput(); emit roomAdded(room); } else { qCritical() << "Attempt to add nullptr to the room list"; @@ -61,21 +64,16 @@ void RoomListModel::connectRoomSignals(MatriqueRoom* room) { connect(room, &Room::joinStateChanged, this, [=] { refresh(room); }); connect(room, &Room::avatarChanged, this, [=] { refresh(room, {AvatarRole}); }); - - connect(room, &Room::unreadMessagesChanged, this, [=](Room* r) { - if (r->hasUnreadMessages()) emit newMessage(static_cast(r)); - }); - // connect( - // room, &QMatrixClient::Room::aboutToAddNewMessages, this, - // [=](QMatrixClient::RoomEventsRange eventsRange) { - // for (QMatrixClient::RoomEvents events : eventsRange.const_iterator) - // { - // for (QMatrixClient::RoomEvent event : events) { - // qDebug() << event.fullJson(); - // } - // } - // emit newMessage(room); - // }); + connect(room, &QMatrixClient::Room::aboutToAddNewMessages, this, + [=](QMatrixClient::RoomEventsRange eventsRange) { + RoomEvent* event = (eventsRange.end() - 1)->get(); + User* sender = room->user(event->senderId()); + if (sender == room->localUser()) return; + emit newMessage(room->displayName(), + sender->displayname() + ": " + + event->contentJson().value("body").toString(), + QPixmap::fromImage(room->avatar(64))); + }); } void RoomListModel::updateRoom(Room* room, Room* prev) { diff --git a/src/roomlistmodel.h b/src/roomlistmodel.h index ffe4fed..b1d5a05 100644 --- a/src/roomlistmodel.h +++ b/src/roomlistmodel.h @@ -2,8 +2,9 @@ #define ROOMLISTMODEL_H #include "connection.h" -#include "room.h" +#include "events/roomevent.h" #include "matriqueroom.h" +#include "room.h" #include @@ -69,7 +70,7 @@ class RoomListModel : public QAbstractListModel { signals: void connectionChanged(); void roomAdded(MatriqueRoom* room); - void newMessage(MatriqueRoom* room); + void newMessage(const QString& roomName, const QString& content, const QIcon& icon); }; #endif // ROOMLISTMODEL_H