Extracted common toast timer logic to recent self forwards tagger.

This commit is contained in:
23rd
2025-11-01 17:22:33 +03:00
parent ba363285a7
commit e62a4b065a
2 changed files with 42 additions and 52 deletions
@@ -188,35 +188,13 @@ void SelfForwardsTagger::showSelectorForMessages(
base::install_event_filter(selector, list, eventFilterCallback); base::install_event_filter(selector, list, eventFilterCallback);
} }
struct State { const auto state = selector->lifetime().make_state<ToastTimerState>();
rpl::lifetime timerLifetime;
bool expanded = false;
};
const auto state = selector->lifetime().make_state<State>();
const auto restartTimer = [=](crl::time ms) {
state->timerLifetime.destroy();
base::timer_once(ms) | rpl::start_with_next([=] {
hideAndDestroy();
}, state->timerLifetime);
};
selector->willExpand() | rpl::start_with_next([=] { selector->willExpand() | rpl::start_with_next([=] {
state->expanded = true; state->expanded = true;
}, selector->lifetime()); }, selector->lifetime());
base::install_event_filter(selector, [=](not_null<QEvent*> event) { setupToastTimer(selector, state, hideAndDestroy);
if (event->type() == QEvent::MouseButtonPress) {
state->timerLifetime.destroy();
return base::EventFilterResult::Continue;
} else if (!state->expanded && event->type() == QEvent::Enter) {
state->timerLifetime.destroy();
return base::EventFilterResult::Continue;
} else if (!state->expanded && event->type() == QEvent::Leave) {
restartTimer(kTimerOnLeave);
return base::EventFilterResult::Continue;
}
return base::EventFilterResult::Continue;
}, selector->lifetime());
QObject::connect( QObject::connect(
_toast->widget(), _toast->widget(),
@@ -237,7 +215,6 @@ void SelfForwardsTagger::showSelectorForMessages(
rect.x() + (rect.width() - selector->width()) / 2, rect.x() + (rect.width() - selector->width()) / 2,
rect::bottom(rect) - st::selfForwardsTaggerStripSkip); rect::bottom(rect) - st::selfForwardsTaggerStripSkip);
}, selector->lifetime()); }, selector->lifetime());
restartTimer(kInitTimer);
selector->show(); selector->show();
} }
@@ -372,20 +349,10 @@ void SelfForwardsTagger::showChannelFilterToast(not_null<PeerData*> peer) {
const auto rightButton = createRightButton(widget); const auto rightButton = createRightButton(widget);
const auto history = peer->owner().history(peer); const auto history = peer->owner().history(peer);
struct State { const auto state = widget->lifetime().make_state<ToastTimerState>();
rpl::lifetime timerLifetime;
bool menuExpanded = false;
};
const auto state = widget->lifetime().make_state<State>();
const auto restartTimer = [=](crl::time ms) {
state->timerLifetime.destroy();
base::timer_once(ms) | rpl::start_with_next([=] {
hideToast();
}, state->timerLifetime);
};
rightButton->setClickedCallback([=] { rightButton->setClickedCallback([=] {
state->menuExpanded = true; state->expanded = true;
state->timerLifetime.destroy(); state->timerLifetime.destroy();
const auto menu = Ui::CreateChild<Ui::PopupMenu>( const auto menu = Ui::CreateChild<Ui::PopupMenu>(
rightButton, rightButton,
@@ -406,21 +373,7 @@ void SelfForwardsTagger::showChannelFilterToast(not_null<PeerData*> peer) {
} }
}); });
base::install_event_filter(widget, [=](not_null<QEvent*> event) { setupToastTimer(widget, state, [=] { hideToast(); });
if (event->type() == QEvent::MouseButtonPress) {
state->timerLifetime.destroy();
return base::EventFilterResult::Continue;
} else if (!state->menuExpanded && event->type() == QEvent::Enter) {
state->timerLifetime.destroy();
return base::EventFilterResult::Continue;
} else if (!state->menuExpanded && event->type() == QEvent::Leave) {
restartTimer(kTimerOnLeave);
return base::EventFilterResult::Continue;
}
return base::EventFilterResult::Continue;
}, widget->lifetime());
restartTimer(kInitTimer);
} }
} }
@@ -439,6 +392,34 @@ not_null<Ui::AbstractButton*> SelfForwardsTagger::createRightButton(
return button; return button;
} }
void SelfForwardsTagger::setupToastTimer(
not_null<Ui::RpWidget*> widget,
not_null<ToastTimerState*> state,
Fn<void()> hideCallback) {
const auto restartTimer = [=](crl::time ms) {
state->timerLifetime.destroy();
base::timer_once(ms) | rpl::start_with_next([=] {
hideCallback();
}, state->timerLifetime);
};
base::install_event_filter(widget, [=](not_null<QEvent*> event) {
if (event->type() == QEvent::MouseButtonPress) {
state->timerLifetime.destroy();
return base::EventFilterResult::Continue;
} else if (!state->expanded && event->type() == QEvent::Enter) {
state->timerLifetime.destroy();
return base::EventFilterResult::Continue;
} else if (!state->expanded && event->type() == QEvent::Leave) {
restartTimer(kTimerOnLeave);
return base::EventFilterResult::Continue;
}
return base::EventFilterResult::Continue;
}, state->timerLifetime);
restartTimer(kInitTimer);
}
void SelfForwardsTagger::hideToast() { void SelfForwardsTagger::hideToast() {
if (const auto strong = _toast.get()) { if (const auto strong = _toast.get()) {
strong->hideAnimated(); strong->hideAnimated();
@@ -54,6 +54,11 @@ public:
~SelfForwardsTagger(); ~SelfForwardsTagger();
private: private:
struct ToastTimerState {
rpl::lifetime timerLifetime;
bool expanded = false;
};
void setup(); void setup();
void showSelectorForMessages(const MessageIdsList &ids); void showSelectorForMessages(const MessageIdsList &ids);
void showToast(const TextWithEntities &text, Fn<void()> callback); void showToast(const TextWithEntities &text, Fn<void()> callback);
@@ -62,6 +67,10 @@ private:
void createLottieIcon(not_null<QWidget*> widget, const QString &name); void createLottieIcon(not_null<QWidget*> widget, const QString &name);
not_null<Ui::AbstractButton*> createRightButton( not_null<Ui::AbstractButton*> createRightButton(
not_null<Ui::RpWidget*> widget); not_null<Ui::RpWidget*> widget);
void setupToastTimer(
not_null<Ui::RpWidget*> widget,
not_null<ToastTimerState*> state,
Fn<void()> hideCallback);
void hideToast(); void hideToast();
[[nodiscard]] QRect toastGeometry() const; [[nodiscard]] QRect toastGeometry() const;