Replaced management of top bar for suggestion in dialogs with rpl.

This commit is contained in:
23rd 2025-04-05 20:11:49 +03:00 committed by John Preston
parent 640db8af7d
commit b280a26317
4 changed files with 153 additions and 120 deletions

View file

@ -3863,10 +3863,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_dialogs_skip_archive_in_search" = "Skip results from archive"; "lng_dialogs_skip_archive_in_search" = "Skip results from archive";
"lng_dialogs_show_archive_in_search" = "With results from archive"; "lng_dialogs_show_archive_in_search" = "With results from archive";
"lng_dialogs_top_bar_suggestions_birthday_title" = "Add your birthday! 🎂"; "lng_dialogs_suggestions_birthday_title" = "Add your birthday! 🎂";
"lng_dialogs_top_bar_suggestions_birthday_about" = "Let your contacts know when youre celebrating."; "lng_dialogs_suggestions_birthday_about" = "Let your contacts know when youre celebrating.";
"lng_dialogs_top_bar_suggestions_premium_annual_title" = "Telegram Premium with a {text} discount"; "lng_dialogs_suggestions_premium_annual_title" = "Telegram Premium with a {text} discount";
"lng_dialogs_top_bar_suggestions_premium_annual_about" = "Sign up for the annual payment plan for Telegram Premium now to get the discount."; "lng_dialogs_suggestions_premium_annual_about" = "Sign up for the annual payment plan for Telegram Premium now to get the discount.";
"lng_about_random" = "Send a {emoji} emoji to any chat to try your luck."; "lng_about_random" = "Send a {emoji} emoji to any chat to try your luck.";
"lng_about_random_send" = "Send"; "lng_about_random_send" = "Send";

View file

@ -39,25 +39,46 @@ constexpr auto kSugPremiumAnnual = "PREMIUM_ANNUAL"_cs;
} // namespace } // namespace
object_ptr<Ui::SlideWrap<Ui::RpWidget>> CreateTopBarSuggestion( rpl::producer<Ui::SlideWrap<Ui::RpWidget>*> TopBarSuggestionValue(
not_null<Ui::RpWidget*> parent, not_null<Ui::RpWidget*> parent,
not_null<Main::Session*> session) { not_null<Main::Session*> session) {
const auto content = Ui::CreateChild<TopBarSuggestionContent>(parent); return [=](auto consumer) {
auto result = object_ptr<Ui::SlideWrap<Ui::RpWidget>>( auto lifetime = rpl::lifetime();
parent,
object_ptr<Ui::RpWidget>::fromRaw(content));
const auto wrap = result.data();
wrap->toggle(false, anim::type::instant);
struct State { struct State {
TopBarSuggestionContent *content = nullptr;
Ui::SlideWrap<Ui::RpWidget> *wrap = nullptr;
rpl::lifetime birthdayLifetime; rpl::lifetime birthdayLifetime;
rpl::lifetime premiumLifetime; rpl::lifetime premiumLifetime;
}; };
const auto state = content->lifetime().make_state<State>(); const auto state = lifetime.make_state<State>();
const auto ensureWrap = [=] {
if (!state->content) {
state->content = Ui::CreateChild<TopBarSuggestionContent>(
parent);
rpl::combine(
parent->widthValue(),
state->content->desiredHeightValue()
) | rpl::start_with_next([=](int width, int height) {
state->content->resize(width, height);
}, state->content->lifetime());
}
if (!state->wrap) {
state->wrap = Ui::CreateChild<Ui::SlideWrap<Ui::RpWidget>>(
parent,
object_ptr<Ui::RpWidget>::fromRaw(state->content));
state->wrap->toggle(false, anim::type::instant);
}
};
const auto processCurrentSuggestion = [=](auto repeat) -> void { const auto processCurrentSuggestion = [=](auto repeat) -> void {
ensureWrap();
const auto content = state->content;
const auto wrap = state->wrap;
using RightIcon = TopBarSuggestionContent::RightIcon;
if (session->appConfig().suggestionCurrent(kSugSetBirthday.utf8()) if (session->appConfig().suggestionCurrent(kSugSetBirthday.utf8())
&& !Data::IsBirthdayToday(session->user()->birthday())) { && !Data::IsBirthdayToday(session->user()->birthday())) {
content->setRightIcon(TopBarSuggestionContent::RightIcon::Close); content->setRightIcon(RightIcon::Close);
content->setClickedCallback([=] { content->setClickedCallback([=] {
const auto controller = FindSessionController(parent); const auto controller = FindSessionController(parent);
if (!controller) { if (!controller) {
@ -85,10 +106,10 @@ object_ptr<Ui::SlideWrap<Ui::RpWidget>> CreateTopBarSuggestion(
repeat(repeat); repeat(repeat);
}); });
content->setContent( content->setContent(
tr::lng_dialogs_top_bar_suggestions_birthday_title( tr::lng_dialogs_suggestions_birthday_title(
tr::now, tr::now,
Ui::Text::Bold), Ui::Text::Bold),
tr::lng_dialogs_top_bar_suggestions_birthday_about( tr::lng_dialogs_suggestions_birthday_about(
tr::now, tr::now,
TextWithEntities::Simple)); TextWithEntities::Simple));
wrap->toggle(true, anim::type::normal); wrap->toggle(true, anim::type::normal);
@ -96,17 +117,17 @@ object_ptr<Ui::SlideWrap<Ui::RpWidget>> CreateTopBarSuggestion(
&& !session->premium() && !session->premium()
&& session->appConfig().suggestionCurrent( && session->appConfig().suggestionCurrent(
kSugPremiumAnnual.utf8())) { kSugPremiumAnnual.utf8())) {
content->setRightIcon(TopBarSuggestionContent::RightIcon::Arrow); content->setRightIcon(RightIcon::Arrow);
const auto api = &session->api().premium(); const auto api = &session->api().premium();
const auto set = [=](QString discount) { const auto set = [=](QString discount) {
constexpr auto kMinus = QChar(0x2212); constexpr auto kMinus = QChar(0x2212);
content->setContent( content->setContent(
tr::lng_dialogs_top_bar_suggestions_premium_annual_title( tr::lng_dialogs_suggestions_premium_annual_title(
tr::now, tr::now,
lt_text, lt_text,
{ discount.replace(kMinus, QChar()) }, { discount.replace(kMinus, QChar()) },
Ui::Text::Bold), Ui::Text::Bold),
tr::lng_dialogs_top_bar_suggestions_premium_annual_about( tr::lng_dialogs_suggestions_premium_annual_about(
tr::now, tr::now,
TextWithEntities::Simple)); TextWithEntities::Simple));
content->setClickedCallback([=] { content->setClickedCallback([=] {
@ -135,23 +156,23 @@ object_ptr<Ui::SlideWrap<Ui::RpWidget>> CreateTopBarSuggestion(
} else { } else {
wrap->toggle(false, anim::type::normal); wrap->toggle(false, anim::type::normal);
base::call_delayed(st::slideWrapDuration * 2, wrap, [=] { base::call_delayed(st::slideWrapDuration * 2, wrap, [=] {
delete wrap; state->content = nullptr;
state->wrap = nullptr;
consumer.put_next(nullptr);
}); });
} }
}; };
session->appConfig().refreshed() | rpl::start_with_next([=] { session->appConfig().value() | rpl::start_with_next([=] {
const auto was = state->wrap;
processCurrentSuggestion(processCurrentSuggestion); processCurrentSuggestion(processCurrentSuggestion);
}, content->lifetime()); if (was != state->wrap) {
consumer.put_next_copy(state->wrap);
}
}, lifetime);
rpl::combine( return lifetime;
parent->widthValue(), };
content->desiredHeightValue()
) | rpl::start_with_next([=](int width, int height) {
content->resize(width, height);
}, content->lifetime());
return result;
} }
} // namespace Dialogs } // namespace Dialogs

View file

@ -22,9 +22,10 @@ class SlideWrap;
namespace Dialogs { namespace Dialogs {
[[nodiscard]] object_ptr<Ui::SlideWrap<Ui::RpWidget>> CreateTopBarSuggestion( [[nodiscard]] auto TopBarSuggestionValue(
not_null<Ui::RpWidget*> parent, not_null<Ui::RpWidget*> parent,
not_null<Main::Session*>); not_null<Main::Session*>)
-> rpl::producer<Ui::SlideWrap<Ui::RpWidget>*>;
} // namespace Dialogs } // namespace Dialogs

View file

@ -380,16 +380,20 @@ Widget::Widget(
const auto innerList = _scroll->setOwnedWidget( const auto innerList = _scroll->setOwnedWidget(
object_ptr<Ui::VerticalLayout>(this)); object_ptr<Ui::VerticalLayout>(this));
if (_layout != Layout::Child) { if (_layout != Layout::Child) {
_topBarSuggestion = innerList->add(CreateTopBarSuggestion( TopBarSuggestionValue(
innerList, innerList,
&session())); &session()
_topBarSuggestion->lifetime().add([=] { ) | rpl::start_with_next([=](Ui::SlideWrap<Ui::RpWidget> *raw) {
_topBarSuggestion = nullptr; if (raw) {
}); _topBarSuggestion = innerList->insert(
0,
object_ptr<Ui::SlideWrap<Ui::RpWidget>>::fromRaw(raw));
rpl::combine( rpl::combine(
_topBarSuggestion->entity()->desiredHeightValue(), _topBarSuggestion->entity()->desiredHeightValue(),
_childListShown.value() _childListShown.value()
) | rpl::start_with_next([=](int desiredHeight, float64 shown) { ) | rpl::start_with_next([=](
int desiredHeight,
float64 shown) {
const auto newHeight = desiredHeight * (1. - shown); const auto newHeight = desiredHeight * (1. - shown);
_topBarSuggestion->entity()->setMaximumHeight(newHeight); _topBarSuggestion->entity()->setMaximumHeight(newHeight);
_topBarSuggestion->entity()->setMinimumWidth((shown > 0) _topBarSuggestion->entity()->setMinimumWidth((shown > 0)
@ -397,6 +401,13 @@ Widget::Widget(
: 0); : 0);
_topBarSuggestion->entity()->resize(width(), newHeight); _topBarSuggestion->entity()->resize(width(), newHeight);
}, _topBarSuggestion->lifetime()); }, _topBarSuggestion->lifetime());
} else {
if (_topBarSuggestion) {
delete _topBarSuggestion;
}
_topBarSuggestion = nullptr;
}
}, lifetime());
} }
_inner = innerList->add(object_ptr<InnerWidget>( _inner = innerList->add(object_ptr<InnerWidget>(
innerList, innerList,