Skip to content

Commit

Permalink
Show preview on Force-Click on macOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed May 30, 2024
1 parent d6e827e commit 2b9e7a6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
25 changes: 22 additions & 3 deletions Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -1761,6 +1776,7 @@ void InnerWidget::mousePressReleased(
Qt::MouseButton button,
Qt::KeyboardModifiers modifiers) {
_chatPreviewTimer.cancel();
_pressButton = Qt::NoButton;

auto wasDragging = (_dragging != nullptr);
if (wasDragging) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/dialogs/dialogs_inner_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class InnerWidget final : public Ui::RpWidget {
void resizeEmptyLabel();

[[nodiscard]] bool isUserpicPress() const;
[[nodiscard]] bool isUserpicPressOnWide() const;
[[nodiscard]] bool pressShowsPreview(bool onlyUserpic) const;
void cancelChatPreview();
void showChatPreview(bool onlyUserpic);
Expand Down Expand Up @@ -258,6 +259,7 @@ class InnerWidget final : public Ui::RpWidget {
QPoint globalPosition,
Qt::MouseButton button,
Qt::KeyboardModifiers modifiers);
void processGlobalForceClick(QPoint globalPosition);
void clearIrrelevantState();
void selectByMouse(QPoint globalPosition);
void preloadRowsData();
Expand Down
6 changes: 6 additions & 0 deletions Telegram/SourceFiles/platform/mac/main_window_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class MainWindow : public Window::MainWindow {

void updateWindowIcon() override;

rpl::producer<QPoint> globalForceClicks() override {
return _forceClicks.events();
}

class Private;

protected:
Expand Down Expand Up @@ -85,7 +89,9 @@ class MainWindow : public Window::MainWindow {
QAction *psMonospace = nullptr;
QAction *psClearFormat = nullptr;

rpl::event_stream<QPoint> _forceClicks;
int _customTitleHeight = 0;
int _lastPressureStage = 0;

};

Expand Down
10 changes: 10 additions & 0 deletions Telegram/SourceFiles/platform/mac/main_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ QString strNotificationAboutScreenUnlocked() {
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;
Expand Down
4 changes: 4 additions & 0 deletions Telegram/SourceFiles/window/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class MainWindow : public Ui::RpWindow {
Core::WindowPosition initial,
QSize minSize) const;

[[nodiscard]] virtual rpl::producer<QPoint> globalForceClicks() {
return rpl::never<QPoint>();
}

protected:
void leaveEventHook(QEvent *e) override;

Expand Down

0 comments on commit 2b9e7a6

Please sign in to comment.