From 9861370b757e2648244440d62145a06446a08756 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 8 Jun 2022 09:26:17 +0300 Subject: [PATCH] Added hotkeys to get next / previous item of search messages in dialogs. --- .../SourceFiles/history/history_widget.cpp | 2 +- .../controls/history_view_compose_search.cpp | 74 ++++++++++++++++++- Telegram/SourceFiles/mainwindow.cpp | 21 ++++-- 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index a41c3ad1d..2e4df9dad 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -4459,7 +4459,7 @@ void HistoryWidget::searchInChat() { } if (controller()->isPrimary()) { controller()->content()->searchInChat(_history); - } else { + } else if (!_composeSearch) { const auto search = [=] { const auto update = [=] { updateControlsVisibility(); diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp index bd764f558..c5c33fdf4 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp @@ -251,8 +251,13 @@ public: [[nodiscard]] rpl::producer<> queryChanges() const; [[nodiscard]] rpl::producer<> closeRequests() const; [[nodiscard]] rpl::producer<> cancelRequests() const; + [[nodiscard]] rpl::producer> keyEvents() const; void setFrom(PeerData *peer); + bool handleKeyPress(not_null e); + +protected: + void keyPressEvent(QKeyEvent *e) override; private: void clearItems(); @@ -271,6 +276,7 @@ private: rpl::event_stream _searchRequests; rpl::event_stream<> _queryChanges; rpl::event_stream<> _cancelRequests; + rpl::event_stream> _keyEvents; }; TopBar::TopBar(not_null parent) @@ -319,6 +325,18 @@ TopBar::TopBar(not_null parent) }); } +void TopBar::keyPressEvent(QKeyEvent *e) { + _keyEvents.fire_copy(e); +} + +bool TopBar::handleKeyPress(not_null e) { + return false; +} + +rpl::producer> TopBar::keyEvents() const { + return _keyEvents.events(); +} + void TopBar::setInnerFocus() { _select->setInnerFocus(); } @@ -410,13 +428,24 @@ public: void buttonFromToggleOn(rpl::producer &&visible); void buttonCalendarToggleOn(rpl::producer &&visible); + bool handleKeyPress(not_null e); + private: void updateText(int current); base::unique_qptr _showList; - base::unique_qptr _previous; - base::unique_qptr _next; + struct Navigation { + base::unique_qptr button; + bool enabled = false; + + Ui::IconButton *operator->() const { + return button.get(); + } + }; + + Navigation _previous; + Navigation _next; base::unique_qptr _jumpToDate; base::unique_qptr _chooseFromUser; @@ -433,8 +462,8 @@ BottomBar::BottomBar(not_null parent, bool fastShowChooseFrom) QString(), st::historyComposeButton)) // Icons are swaped. -, _previous(base::make_unique_q(this, st::calendarNext)) -, _next(base::make_unique_q(this, st::calendarPrevious)) +, _previous({ base::make_unique_q(this, st::calendarNext) }) +, _next({ base::make_unique_q(this, st::calendarPrevious) }) , _jumpToDate(base::make_unique_q(this, st::dialogCalendar)) , _chooseFromUser( base::make_unique_q(this, st::dialogSearchFrom)) @@ -490,6 +519,8 @@ BottomBar::BottomBar(not_null parent, bool fastShowChooseFrom) ) | rpl::start_with_next([=](int current) { const auto nextDisabled = (current <= 0) || (current >= _total); const auto prevDisabled = (current <= 1); + _next.enabled = !nextDisabled; + _previous.enabled = !prevDisabled; _next->setAttribute(Qt::WA_TransparentForMouseEvents, nextDisabled); _previous->setAttribute( Qt::WA_TransparentForMouseEvents, @@ -515,6 +546,34 @@ BottomBar::BottomBar(not_null parent, bool fastShowChooseFrom) }, lifetime()); } +bool BottomBar::handleKeyPress(not_null e) { + if (e->key() == Qt::Key_F3) { + const auto modifiers = e->modifiers(); + if (modifiers == Qt::NoModifier && _next.enabled) { + _next->clicked(Qt::KeyboardModifiers(), Qt::LeftButton); + return true; + } else if (modifiers == Qt::ShiftModifier && _previous.enabled) { + _previous->clicked(Qt::KeyboardModifiers(), Qt::LeftButton); + return true; + } + } +#ifdef Q_OS_MAC + if (e->key() == Qt::Key_G) { + const auto modifiers = e->modifiers(); + if (modifiers.testFlag(Qt::ControlModifier)) { + const auto &navigation = (modifiers.testFlag(Qt::ShiftModifier) + ? _previous + : _next); + if (navigation.enabled) { + navigation->clicked(Qt::KeyboardModifiers(), Qt::LeftButton); + return true; + } + } + } +#endif + return false; +} + void BottomBar::setTotal(int total) { _total = total; setCurrent(1); @@ -632,6 +691,13 @@ ComposeSearch::Inner::Inner( bottom.topLeft() + QPoint(bottom.width(), 0))); }, _list.container->lifetime()); + _topBar->keyEvents( + ) | rpl::start_with_next([=](not_null e) { + if (!_bottomBar->handleKeyPress(e)) { + _topBar->handleKeyPress(e); + } + }, _topBar->lifetime()); + _topBar->searchRequests( ) | rpl::start_with_next([=](const SearchRequest &search) { if (search.query.isEmpty() && !search.from) { diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 01671385e..f2a0cb2cf 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -577,13 +577,20 @@ bool MainWindow::eventFilter(QObject *object, QEvent *e) { FeedLangTestingKey(key); } #ifdef _DEBUG - switch (static_cast(e)->key()) { - case Qt::Key_F3: - anim::SetSlowMultiplier((anim::SlowMultiplier() == 10) ? 1 : 10); - return true; - case Qt::Key_F4: - anim::SetSlowMultiplier((anim::SlowMultiplier() == 50) ? 1 : 50); - return true; + if (static_cast(e)->modifiers().testFlag( + Qt::ControlModifier)) { + switch (static_cast(e)->key()) { + case Qt::Key_F11: + anim::SetSlowMultiplier((anim::SlowMultiplier() == 10) + ? 1 + : 10); + return true; + case Qt::Key_F12: + anim::SetSlowMultiplier((anim::SlowMultiplier() == 50) + ? 1 + : 50); + return true; + } } #endif } break;