From 2cb0651b046438cc444c9947a8cee95982e1e22f Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 7 May 2024 13:18:58 +0400 Subject: [PATCH] Redirect IME to search on Windows. --- .../SourceFiles/dialogs/dialogs_widget.cpp | 27 +++++++++++++++---- Telegram/SourceFiles/dialogs/dialogs_widget.h | 2 ++ .../platform/win/main_window_win.cpp | 15 +++++++++++ .../platform/win/main_window_win.h | 5 ++++ Telegram/SourceFiles/window/main_window.cpp | 8 ++++++ Telegram/SourceFiles/window/main_window.h | 5 +++- 6 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index 2f1763964..575516ffa 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -356,6 +356,14 @@ Widget::Widget( Ui::PostponeCall(this, [=] { listScrollUpdated(); }); }, lifetime()); + setAttribute(Qt::WA_InputMethodEnabled); + controller->widget()->imeCompositionStarts( + ) | rpl::filter([=] { + return redirectImeToSearch(); + }) | rpl::start_with_next([=] { + _search->setFocusFast(); + }, lifetime()); + _search->changes( ) | rpl::start_with_next([=] { applySearchUpdate(); @@ -3290,12 +3298,17 @@ void Widget::keyPressEvent(QKeyEvent *e) { } } +bool Widget::redirectToSearchPossible() const { + return !_openedFolder + && !_openedForum + && !_childList + && _search->isVisible() + && !_search->hasFocus() + && hasFocus(); +} + bool Widget::redirectKeyToSearch(QKeyEvent *e) const { - if (_openedFolder - || _openedForum - || _childList - || !_search->isVisible() - || _search->hasFocus()) { + if (!redirectToSearchPossible()) { return false; } const auto character = !(e->modifiers() & ~Qt::ShiftModifier) @@ -3316,6 +3329,10 @@ bool Widget::redirectKeyToSearch(QKeyEvent *e) const { return data && data->hasText(); } +bool Widget::redirectImeToSearch() const { + return redirectToSearchPossible(); +} + void Widget::paintEvent(QPaintEvent *e) { if (controller()->contentOverlapped(this, e)) { return; diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.h b/Telegram/SourceFiles/dialogs/dialogs_widget.h index ecd2f3608..5ff9828d5 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.h @@ -250,7 +250,9 @@ private: void updateSuggestions(anim::type animated); void processSearchFocusChange(); + [[nodiscard]] bool redirectToSearchPossible() const; [[nodiscard]] bool redirectKeyToSearch(QKeyEvent *e) const; + [[nodiscard]] bool redirectImeToSearch() const; MTP::Sender _api; diff --git a/Telegram/SourceFiles/platform/win/main_window_win.cpp b/Telegram/SourceFiles/platform/win/main_window_win.cpp index 8c852b837..dd2f3dafe 100644 --- a/Telegram/SourceFiles/platform/win/main_window_win.cpp +++ b/Telegram/SourceFiles/platform/win/main_window_win.cpp @@ -480,6 +480,21 @@ bool MainWindow::initGeometryFromSystem() { return true; } +bool MainWindow::nativeEvent( + const QByteArray &eventType, + void *message, + long *result) { + if (message) { + const auto msg = static_cast(message); + if (msg->message == WM_IME_STARTCOMPOSITION) { + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + imeCompositionStartReceived(); + }); + } + } + return false; +} + void MainWindow::updateWindowIcon() { updateTaskbarAndIconCounters(); } diff --git a/Telegram/SourceFiles/platform/win/main_window_win.h b/Telegram/SourceFiles/platform/win/main_window_win.h index 856bee773..63dc6ba17 100644 --- a/Telegram/SourceFiles/platform/win/main_window_win.h +++ b/Telegram/SourceFiles/platform/win/main_window_win.h @@ -48,6 +48,11 @@ protected: bool initGeometryFromSystem() override; + bool nativeEvent( + const QByteArray &eventType, + void *message, + long *result) override; + private: struct Private; diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index 3f8c9b5ba..1c5de5967 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -795,10 +795,18 @@ void MainWindow::setPositionInited() { _positionInited = true; } +void MainWindow::imeCompositionStartReceived() { + _imeCompositionStartReceived.fire({}); +} + rpl::producer<> MainWindow::leaveEvents() const { return _leaveEvents.events(); } +rpl::producer<> MainWindow::imeCompositionStarts() const { + return _imeCompositionStartReceived.events(); +} + void MainWindow::leaveEventHook(QEvent *e) { _leaveEvents.fire({}); } diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index 8feb329a9..79dc3c6fe 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -119,7 +119,8 @@ public: void launchDrag(std::unique_ptr data, Fn &&callback); - rpl::producer<> leaveEvents() const; + [[nodiscard]] rpl::producer<> leaveEvents() const; + [[nodiscard]] rpl::producer<> imeCompositionStarts() const; virtual void updateWindowIcon() = 0; void updateTitle(); @@ -185,6 +186,7 @@ protected: return false; } + void imeCompositionStartReceived(); void setPositionInited(); virtual QRect computeDesktopRect() const; @@ -214,6 +216,7 @@ private: bool _isActive = false; rpl::event_stream<> _leaveEvents; + rpl::event_stream<> _imeCompositionStartReceived; bool _maximizedBeforeHide = false;