Add typing notification.

This commit is contained in:
Black Hat 2018-08-20 22:40:36 +08:00
parent 85c2e54219
commit fa4db065f2
3 changed files with 77 additions and 2 deletions

View File

@ -319,7 +319,10 @@ Item {
selectByMouse: true selectByMouse: true
text: currentRoom ? currentRoom.cachedInput : "" text: currentRoom ? currentRoom.cachedInput : ""
onTextChanged: currentRoom.cachedInput = text onTextChanged: {
currentRoom.isTyping = true
currentRoom.cachedInput = text
}
Keys.onReturnPressed: { Keys.onReturnPressed: {
if (inputField.text) { if (inputField.text) {
@ -332,6 +335,9 @@ Item {
color: Material.theme == Material.Light ? "#eaeaea" : "#242424" color: Material.theme == Material.Light ? "#eaeaea" : "#242424"
} }
ToolTip.visible: currentRoom && currentRoom.hasUsersTyping
ToolTip.text: currentRoom ? currentRoom.usersTyping : ""
function postMessage(text) { function postMessage(text) {
if (text.trim().length === 0) { return } if (text.trim().length === 0) { return }
if(!currentRoom) { return } if(!currentRoom) { return }

View File

@ -1,14 +1,36 @@
#include "matriqueroom.h" #include "matriqueroom.h"
#include "connection.h" #include "connection.h"
#include "user.h"
#include "csapi/leaving.h" #include "csapi/leaving.h"
#include "csapi/typing.h"
#include "events/typingevent.h"
#include <QFileDialog> #include <QFileDialog>
#include <QMimeDatabase> #include <QMimeDatabase>
MatriqueRoom::MatriqueRoom(Connection* connection, QString roomId, MatriqueRoom::MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState) JoinState joinState)
: Room(connection, std::move(roomId), joinState) {} : Room(connection, std::move(roomId), joinState) {
m_timeoutTimer->setSingleShot(true);
m_timeoutTimer->setInterval(2000);
m_repeatTimer->setInterval(5000);
connect(m_timeoutTimer, &QTimer::timeout, [=] { setIsTyping(false); });
connect(m_repeatTimer, &QTimer::timeout,
[=] { sendTypingNotification(true); });
connect(this, &MatriqueRoom::isTypingChanged, [=] {
if (m_isTyping) {
m_timeoutTimer->start();
m_repeatTimer->start();
sendTypingNotification(true);
} else {
m_timeoutTimer->stop();
m_repeatTimer->stop();
sendTypingNotification(false);
}
});
}
void MatriqueRoom::chooseAndUploadFile() { void MatriqueRoom::chooseAndUploadFile() {
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as")); auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
@ -55,3 +77,28 @@ void MatriqueRoom::rejectInvitation() {
} }
void MatriqueRoom::forget() { connection()->forgetRoom(id()); } void MatriqueRoom::forget() { connection()->forgetRoom(id()); }
bool MatriqueRoom::hasUsersTyping() {
QList<User*> users = usersTyping();
if (users.isEmpty()) return false;
int count = users.length();
if (users.contains(localUser())) count--;
return count != 0;
}
QString MatriqueRoom::getUsersTyping() {
QString usersTypingStr;
QList<User*> users = usersTyping();
users.removeOne(localUser());
for (User* user : users) {
usersTypingStr += user->displayname() + " ";
}
usersTypingStr += users.count() == 1 ? "is" : "are";
usersTypingStr += " typing.";
return usersTypingStr;
}
void MatriqueRoom::sendTypingNotification(bool isTyping) {
connection()->callApi<SetTypingJob>(BackgroundRequest, localUser()->id(),
id(), isTyping, 10000);
}

View File

@ -4,17 +4,31 @@
#include "room.h" #include "room.h"
#include <QObject> #include <QObject>
#include <QTimer>
using namespace QMatrixClient; using namespace QMatrixClient;
class MatriqueRoom : public Room { class MatriqueRoom : public Room {
Q_OBJECT Q_OBJECT
Q_PROPERTY(
bool isTyping READ isTyping WRITE setIsTyping NOTIFY isTypingChanged)
Q_PROPERTY(bool hasUsersTyping READ hasUsersTyping NOTIFY typingChanged)
Q_PROPERTY(QString usersTyping READ getUsersTyping NOTIFY typingChanged)
Q_PROPERTY(QString cachedInput READ cachedInput WRITE setCachedInput NOTIFY Q_PROPERTY(QString cachedInput READ cachedInput WRITE setCachedInput NOTIFY
cachedInputChanged) cachedInputChanged)
public: public:
explicit MatriqueRoom(Connection* connection, QString roomId, explicit MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState = {}); JoinState joinState = {});
bool isTyping() { return m_isTyping; }
void setIsTyping(bool isTyping) {
if (isTyping) m_timeoutTimer->start();
if (isTyping != m_isTyping) {
m_isTyping = isTyping;
emit isTypingChanged();
}
}
const QString& cachedInput() const { return m_cachedInput; } const QString& cachedInput() const { return m_cachedInput; }
void setCachedInput(const QString& input) { void setCachedInput(const QString& input) {
if (input != m_cachedInput) { if (input != m_cachedInput) {
@ -23,13 +37,20 @@ class MatriqueRoom : public Room {
} }
} }
bool hasUsersTyping();
QString getUsersTyping();
private: private:
QString m_cachedInput; QString m_cachedInput;
bool m_isTyping;
QTimer* m_timeoutTimer = new QTimer();
QTimer* m_repeatTimer = new QTimer();
QString getMIME(const QUrl& fileUrl) const; QString getMIME(const QUrl& fileUrl) const;
void postFile(const QUrl& localFile, const QUrl& mxcUrl); void postFile(const QUrl& localFile, const QUrl& mxcUrl);
signals: signals:
void isTypingChanged();
void cachedInputChanged(); void cachedInputChanged();
public slots: public slots:
@ -38,6 +59,7 @@ class MatriqueRoom : public Room {
void acceptInvitation(); void acceptInvitation();
void rejectInvitation(); void rejectInvitation();
void forget(); void forget();
void sendTypingNotification(bool isTyping);
}; };
#endif // MATRIQUEROOM_H #endif // MATRIQUEROOM_H