From 2b9e7a6b2568c2265be17289d29f246c406f0c4c Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 29 May 2024 12:06:30 +0400 Subject: [PATCH] Show preview on Force-Click on macOS. --- .../dialogs/dialogs_inner_widget.cpp | 25 ++++++++++++++++--- .../dialogs/dialogs_inner_widget.h | 2 ++ .../platform/mac/main_window_mac.h | 6 +++++ .../platform/mac/main_window_mac.mm | 10 ++++++++ Telegram/SourceFiles/window/main_window.h | 4 +++ 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp index e21dcbcf4..3b996f385 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp @@ -332,6 +332,11 @@ InnerWidget::InnerWidget( switchToFilter(filterId); }, lifetime()); + _controller->window().widget()->globalForceClicks( + ) | rpl::start_with_next([=](QPoint globalPosition) { + processGlobalForceClick(globalPosition); + }, lifetime()); + session().data().stories().incrementPreloadingMainSources(); handleChatListEntryRefreshes(); @@ -1424,6 +1429,16 @@ void InnerWidget::selectByMouse(QPoint globalPosition) { } } +void InnerWidget::processGlobalForceClick(QPoint globalPosition) { + const auto parent = parentWidget(); + if (_pressButton == Qt::LeftButton + && parent->rect().contains(parent->mapFromGlobal(globalPosition)) + && pressShowsPreview(false)) { + _chatPreviewWillBeFor = computeChosenRow().key; + showChatPreview(false); + } +} + void InnerWidget::mousePressEvent(QMouseEvent *e) { selectByMouse(e->globalPos()); @@ -1761,6 +1776,7 @@ void InnerWidget::mousePressReleased( Qt::MouseButton button, Qt::KeyboardModifiers modifiers) { _chatPreviewTimer.cancel(); + _pressButton = Qt::NoButton; auto wasDragging = (_dragging != nullptr); if (wasDragging) { @@ -3598,8 +3614,11 @@ ChosenRow InnerWidget::computeChosenRow() const { bool InnerWidget::isUserpicPress() const { return (_lastRowLocalMouseX >= 0) - && (_lastRowLocalMouseX < _st->nameLeft) - && (width() > _narrowWidth); + && (_lastRowLocalMouseX < _st->nameLeft); +} + +bool InnerWidget::isUserpicPressOnWide() const { + return isUserpicPress() && (width() > _narrowWidth); } bool InnerWidget::pressShowsPreview(bool onlyUserpic) const { @@ -3625,7 +3644,7 @@ bool InnerWidget::chooseRow( ChosenRow row, Qt::KeyboardModifiers modifiers) { row.newWindow = (modifiers & Qt::ControlModifier); - row.userpicClick = isUserpicPress(); + row.userpicClick = isUserpicPressOnWide(); return row; }; auto chosen = modifyChosenRow(computeChosenRow(), modifiers); diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h index fe9a3d926..9194df36e 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h @@ -123,6 +123,7 @@ public: void resizeEmptyLabel(); [[nodiscard]] bool isUserpicPress() const; + [[nodiscard]] bool isUserpicPressOnWide() const; [[nodiscard]] bool pressShowsPreview(bool onlyUserpic) const; void cancelChatPreview(); void showChatPreview(bool onlyUserpic); @@ -258,6 +259,7 @@ private: QPoint globalPosition, Qt::MouseButton button, Qt::KeyboardModifiers modifiers); + void processGlobalForceClick(QPoint globalPosition); void clearIrrelevantState(); void selectByMouse(QPoint globalPosition); void preloadRowsData(); diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.h b/Telegram/SourceFiles/platform/mac/main_window_mac.h index 6c2d15d07..2a3a59b9c 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.h +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.h @@ -28,6 +28,10 @@ public: void updateWindowIcon() override; + rpl::producer globalForceClicks() override { + return _forceClicks.events(); + } + class Private; protected: @@ -85,7 +89,9 @@ private: QAction *psMonospace = nullptr; QAction *psClearFormat = nullptr; + rpl::event_stream _forceClicks; int _customTitleHeight = 0; + int _lastPressureStage = 0; }; diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index 0b2adb9aa..9b0dd14bb 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -310,6 +310,16 @@ bool MainWindow::nativeEvent( Core::Sandbox::Instance().customEnterFromEventLoop([&] { imeCompositionStartReceived(); }); + } else if ([event type] == NSEventTypePressure) { + const auto stage = [event stage]; + if (_lastPressureStage != stage) { + _lastPressureStage = stage; + if (stage == 2) { + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + _forceClicks.fire(QCursor::pos()); + }); + } + } } } return false; diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index 79dc3c6fe..67f38567c 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -144,6 +144,10 @@ public: Core::WindowPosition initial, QSize minSize) const; + [[nodiscard]] virtual rpl::producer globalForceClicks() { + return rpl::never(); + } + protected: void leaveEventHook(QEvent *e) override;