From 5433f95eda3f55248cfcf998b72ff2ad2bfacb5f Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 16 Apr 2024 02:26:37 +0300 Subject: [PATCH] Simplified creation of label with custom emoji. --- Telegram/CMakeLists.txt | 2 + .../SourceFiles/boxes/gift_premium_box.cpp | 43 +++++----- .../earn/info_earn_inner_widget.cpp | 79 +++++++++---------- Telegram/SourceFiles/menu/menu_sponsored.cpp | 42 ++++------ .../ui/widgets/label_with_custom_emoji.cpp | 32 ++++++++ .../ui/widgets/label_with_custom_emoji.h | 35 ++++++++ 6 files changed, 143 insertions(+), 90 deletions(-) create mode 100644 Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.cpp create mode 100644 Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 0a6ed8663..c99cd34b5 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1474,6 +1474,8 @@ PRIVATE ui/search_field_controller.h ui/text/format_song_document_name.cpp ui/text/format_song_document_name.h + ui/widgets/label_with_custom_emoji.cpp + ui/widgets/label_with_custom_emoji.h ui/unread_badge.cpp ui/unread_badge.h window/main_window.cpp diff --git a/Telegram/SourceFiles/boxes/gift_premium_box.cpp b/Telegram/SourceFiles/boxes/gift_premium_box.cpp index b107af7c9..10ad9409d 100644 --- a/Telegram/SourceFiles/boxes/gift_premium_box.cpp +++ b/Telegram/SourceFiles/boxes/gift_premium_box.cpp @@ -16,7 +16,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peers/prepare_short_info_box.h" #include "boxes/peers/replace_boost_box.h" // BoostsForGift. #include "boxes/premium_preview_box.h" // ShowPremiumPreviewBox. -#include "core/ui_integration.h" // Core::MarkedTextContext. #include "data/data_boosts.h" #include "data/data_changes.h" #include "data/data_channel.h" @@ -48,6 +47,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/toast/toast.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/gradient_round_button.h" +#include "ui/widgets/label_with_custom_emoji.h" #include "ui/wrap/padding_wrap.h" #include "ui/wrap/slide_wrap.h" #include "ui/wrap/table_layout.h" @@ -319,21 +319,20 @@ void GiftBox( std::move(titleLabel)), st::premiumGiftTitlePadding); - auto textLabel = object_ptr(box, st::premiumPreviewAbout); - tr::lng_premium_gift_about( - lt_user, - user->session().changes().peerFlagsValue( - user, - Data::PeerUpdate::Flag::Name - ) | rpl::map([=] { return TextWithEntities{ user->firstName }; }), - Ui::Text::RichLangValue - ) | rpl::map( - BoostsForGiftText({ user }) - ) | rpl::start_with_next([ - raw = textLabel.data(), - session = &user->session()](const TextWithEntities &t) { - raw->setMarkedText(t, Core::MarkedTextContext{ .session = session }); - }, textLabel->lifetime()); + auto textLabel = Ui::CreateLabelWithCustomEmoji( + box, + tr::lng_premium_gift_about( + lt_user, + user->session().changes().peerFlagsValue( + user, + Data::PeerUpdate::Flag::Name + ) | rpl::map([=] { return TextWithEntities{ user->firstName }; }), + Ui::Text::RichLangValue + ) | rpl::map( + BoostsForGiftText({ user }) + ), + { .session = &user->session() }, + st::premiumPreviewAbout); textLabel->setTextColorOverride(stTitle.textFg->c); textLabel->resizeToWidth(available); box->addRow( @@ -536,14 +535,12 @@ void GiftsBox( const auto label = box->addRow( object_ptr>( box, - object_ptr(box, st::premiumPreviewAbout)), + Ui::CreateLabelWithCustomEmoji( + box, + std::move(text), + { .session = session }, + st::premiumPreviewAbout)), padding)->entity(); - std::move( - text - ) | rpl::start_with_next([=](const TextWithEntities &t) { - using namespace Core; - label->setMarkedText(t, MarkedTextContext{ .session = session }); - }, label->lifetime()); label->setTextColorOverride(stTitle.textFg->c); label->resizeToWidth(available); } diff --git a/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp b/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp index 80e377e1e..ad1155b89 100644 --- a/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp +++ b/Telegram/SourceFiles/info/channel_statistics/earn/info_earn_inner_widget.cpp @@ -13,7 +13,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peers/edit_peer_color_box.h" // AddLevelBadge. #include "chat_helpers/stickers_emoji_pack.h" #include "core/application.h" -#include "core/ui_integration.h" // Core::MarkedTextContext. #include "data/data_channel.h" #include "data/data_premium_limits.h" #include "data/data_session.h" @@ -31,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "statistics/chart_widget.h" #include "ui/basic_click_handlers.h" +#include "ui/widgets/label_with_custom_emoji.h" #include "ui/boxes/boost_box.h" #include "ui/controls/userpic_button.h" #include "ui/effects/animation_value_f.h" @@ -334,23 +334,21 @@ void InnerWidget::fill() { st::channelEarnLearnArrowMargins, false)); const auto addAboutWithLearn = [&](const tr::phrase &text) { - auto label = object_ptr( + auto label = Ui::CreateLabelWithCustomEmoji( container, - st::boxDividerLabel); - const auto raw = label.data(); - text( - lt_link, - tr::lng_channel_earn_about_link( - lt_emoji, - rpl::single(arrow), + text( + lt_link, + tr::lng_channel_earn_about_link( + lt_emoji, + rpl::single(arrow), + Ui::Text::RichLangValue + ) | rpl::map([](TextWithEntities text) { + return Ui::Text::Link(std::move(text), 1); + }), Ui::Text::RichLangValue - ) | rpl::map([](TextWithEntities text) { - return Ui::Text::Link(std::move(text), 1); - }), - Ui::Text::RichLangValue - ) | rpl::start_with_next([=](const TextWithEntities &text) { - raw->setMarkedText(text, makeContext(raw)); - }, label->lifetime()); + ), + { .session = session }, + st::boxDividerLabel); label->setLink(1, std::make_shared([=] { _show->showBox(Box([=](not_null box) { box->setNoContentMargin(true); @@ -454,17 +452,16 @@ void InnerWidget::fill() { const auto l = box->addRow( object_ptr>( content, - object_ptr( + Ui::CreateLabelWithCustomEmoji( content, + tr::lng_channel_earn_learn_coin_title( + lt_emoji, + rpl::single( + Ui::Text::Link(bigCurrencyIcon, 1)), + Ui::Text::RichLangValue + ), + { .session = session }, st::boxTitle)))->entity(); - tr::lng_channel_earn_learn_coin_title( - lt_emoji, - rpl::single( - Ui::Text::Link(bigCurrencyIcon, 1)), - Ui::Text::RichLangValue - ) | rpl::start_with_next([=](TextWithEntities t) { - l->setMarkedText(std::move(t), makeContext(l)); - }, l->lifetime()); const auto diamonds = l->lifetime().make_state(0); l->setLink(1, std::make_shared([=] { const auto count = (*diamonds); @@ -480,25 +477,23 @@ void InnerWidget::fill() { Ui::AddSkip(content); { const auto label = box->addRow( - object_ptr( + Ui::CreateLabelWithCustomEmoji( content, + tr::lng_channel_earn_learn_coin_about( + lt_link, + tr::lng_channel_earn_about_link( + lt_emoji, + rpl::single(arrow), + Ui::Text::RichLangValue + ) | rpl::map([](TextWithEntities text) { + return Ui::Text::Link(std::move(text), 1); + }), + Ui::Text::RichLangValue + ), + { .session = session }, st::channelEarnLearnDescription)); - tr::lng_channel_earn_learn_coin_about( - lt_link, - tr::lng_channel_earn_about_link( - lt_emoji, - rpl::single(arrow), - Ui::Text::RichLangValue - ) | rpl::map([](TextWithEntities text) { - return Ui::Text::Link(std::move(text), 1); - }), - Ui::Text::RichLangValue - ) | rpl::start_with_next([=, l = label]( - TextWithEntities t) { - l->setMarkedText(std::move(t), makeContext(l)); - l->resizeToWidth(box->width() - - rect::m::sum::h(st::boxRowPadding)); - }, label->lifetime()); + label->resizeToWidth(box->width() + - rect::m::sum::h(st::boxRowPadding)); label->setLink( 1, LearnMoreCurrencyLink( diff --git a/Telegram/SourceFiles/menu/menu_sponsored.cpp b/Telegram/SourceFiles/menu/menu_sponsored.cpp index 153bb46bd..e917c3f07 100644 --- a/Telegram/SourceFiles/menu/menu_sponsored.cpp +++ b/Telegram/SourceFiles/menu/menu_sponsored.cpp @@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/premium_preview_box.h" #include "chat_helpers/compose/compose_show.h" -#include "core/ui_integration.h" // Core::MarkedTextContext. #include "data/components/sponsored_messages.h" #include "data/data_premium_limits.h" #include "data/data_session.h" @@ -25,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/vertical_list.h" #include "ui/widgets/buttons.h" #include "ui/widgets/popup_menu.h" +#include "ui/widgets/label_with_custom_emoji.h" #include "styles/style_channel_earn.h" #include "styles/style_chat.h" #include "styles/style_layers.h" @@ -169,31 +169,23 @@ void AboutBox( st::topicButtonArrow, st::channelEarnLearnArrowMargins, false)); - const auto label = box->addRow( - object_ptr( + const auto available = box->width() + - rect::m::sum::h(st::boxRowPadding); + box->addRow( + Ui::CreateLabelWithCustomEmoji( content, - st::channelEarnLearnDescription)); - tr::lng_sponsored_revenued_footer_description( - lt_link, - tr::lng_channel_earn_about_link( - lt_emoji, - rpl::single(arrow), - Ui::Text::RichLangValue - ) | rpl::map([=](TextWithEntities text) { - return Ui::Text::Link(std::move(text), kUrl.utf16()); - }), - Ui::Text::RichLangValue - ) | rpl::start_with_next([=, l = label]( - TextWithEntities t) { - l->setMarkedText( - std::move(t), - Core::MarkedTextContext{ - .session = session, - .customEmojiRepaint = [=] { l->update(); }, - }); - l->resizeToWidth(box->width() - - rect::m::sum::h(st::boxRowPadding)); - }, label->lifetime()); + tr::lng_sponsored_revenued_footer_description( + lt_link, + tr::lng_channel_earn_about_link( + lt_emoji, + rpl::single(arrow), + Ui::Text::RichLangValue + ) | rpl::map([=](TextWithEntities text) { + return Ui::Text::Link(std::move(text), kUrl.utf16()); + }), + Ui::Text::RichLangValue), + { .session = session }, + st::channelEarnLearnDescription))->resizeToWidth(available); } Ui::AddSkip(content); Ui::AddSkip(content); diff --git a/Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.cpp b/Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.cpp new file mode 100644 index 000000000..4502d6553 --- /dev/null +++ b/Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.cpp @@ -0,0 +1,32 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "ui/widgets/label_with_custom_emoji.h" + +#include "core/ui_integration.h" +#include "ui/widgets/labels.h" +#include "styles/style_widgets.h" + +namespace Ui { + +object_ptr CreateLabelWithCustomEmoji( + QWidget *parent, + rpl::producer &&text, + Core::MarkedTextContext context, + const style::FlatLabel &st) { + auto label = object_ptr(parent, st); + const auto raw = label.data(); + if (!context.customEmojiRepaint) { + context.customEmojiRepaint = [=] { raw->update(); }; + } + std::move(text) | rpl::start_with_next([=](const TextWithEntities &text) { + raw->setMarkedText(text, context); + }, label->lifetime()); + return label; +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.h b/Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.h new file mode 100644 index 000000000..f22e4e801 --- /dev/null +++ b/Telegram/SourceFiles/ui/widgets/label_with_custom_emoji.h @@ -0,0 +1,35 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +#include "core/ui_integration.h" // Core::MarkedTextContext. + +template +class object_ptr; + +namespace Main { +class Session; +} // namespace Main + +namespace style { +struct FlatLabel; +} // namespace style + +namespace Ui { +class FlatLabel; +} // namespace Ui + +namespace Ui { + +[[nodiscard]] object_ptr CreateLabelWithCustomEmoji( + QWidget *parent, + rpl::producer &&text, + Core::MarkedTextContext context, + const style::FlatLabel &st); + +} // namespace Ui