From 558fd10f7be4c9ea99e53d442acef181c857f6df Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Fri, 31 Jan 2020 01:39:51 +0100 Subject: [PATCH] Add next/prev room shortcuts --- src/ChatPage.cpp | 12 +++++++++ src/RoomList.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ src/RoomList.h | 2 ++ 3 files changed, 80 insertions(+) diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 9574751..2191c6d 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "AvatarProvider.h" @@ -148,6 +149,17 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) trySync(); }); + connect( + new QShortcut(QKeySequence("Ctrl+Down"), this), &QShortcut::activated, this, [this]() { + if (isVisible()) + room_list_->nextRoom(); + }); + connect( + new QShortcut(QKeySequence("Ctrl+Up"), this), &QShortcut::activated, this, [this]() { + if (isVisible()) + room_list_->previousRoom(); + }); + connect(top_bar_, &TopRoomBar::mentionsClicked, this, [this](const QPoint &mentionsPos) { if (user_mentions_popup_->isVisible()) { user_mentions_popup_->hide(); diff --git a/src/RoomList.cpp b/src/RoomList.cpp index dfd4eff..6434489 100644 --- a/src/RoomList.cpp +++ b/src/RoomList.cpp @@ -223,6 +223,72 @@ RoomList::highlightSelectedRoom(const QString &room_id) selectedRoom_ = room_id; } +void +RoomList::nextRoom() +{ + for (int ii = 0; ii < contentsLayout_->count() - 1; ++ii) { + auto room = qobject_cast(contentsLayout_->itemAt(ii)->widget()); + + if (!room) + continue; + + if (room->roomId() == selectedRoom_) { + auto nextRoom = qobject_cast( + contentsLayout_->itemAt(ii + 1)->widget()); + + // Not a room message. + if (!nextRoom || nextRoom->isInvite()) + return; + + emit roomChanged(nextRoom->roomId()); + if (!roomExists(nextRoom->roomId())) { + nhlog::ui()->warn("roomlist: clicked unknown room_id"); + return; + } + + room->setPressedState(false); + nextRoom->setPressedState(true); + + scrollArea_->ensureWidgetVisible(nextRoom); + selectedRoom_ = nextRoom->roomId(); + return; + } + } +} + +void +RoomList::previousRoom() +{ + for (int ii = 1; ii < contentsLayout_->count(); ++ii) { + auto room = qobject_cast(contentsLayout_->itemAt(ii)->widget()); + + if (!room) + continue; + + if (room->roomId() == selectedRoom_) { + auto nextRoom = qobject_cast( + contentsLayout_->itemAt(ii - 1)->widget()); + + // Not a room message. + if (!nextRoom || nextRoom->isInvite()) + return; + + emit roomChanged(nextRoom->roomId()); + if (!roomExists(nextRoom->roomId())) { + nhlog::ui()->warn("roomlist: clicked unknown room_id"); + return; + } + + room->setPressedState(false); + nextRoom->setPressedState(true); + + scrollArea_->ensureWidgetVisible(nextRoom); + selectedRoom_ = nextRoom->roomId(); + return; + } + } +} + void RoomList::updateRoomAvatar(const QString &roomid, const QString &img) { diff --git a/src/RoomList.h b/src/RoomList.h index 95fc0d9..d921990 100644 --- a/src/RoomList.h +++ b/src/RoomList.h @@ -72,6 +72,8 @@ public slots: void updateRoomDescription(const QString &roomid, const DescInfo &info); void closeJoinRoomDialog(bool isJoining, QString roomAlias); void updateReadStatus(const std::map &status); + void nextRoom(); + void previousRoom(); protected: void paintEvent(QPaintEvent *event) override;