From 82d73e2396cb9d4f6d5be62ee8181755de017cce Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 17 Dec 2023 06:55:55 +0300 Subject: [PATCH] Moved out making of "new" badges to single place. --- .../SourceFiles/settings/settings_premium.cpp | 28 +------- Telegram/SourceFiles/ui/new_badges.cpp | 69 +++++++++++++++++++ Telegram/SourceFiles/ui/new_badges.h | 21 ++++++ .../SourceFiles/window/window_main_menu.cpp | 37 +--------- Telegram/cmake/td_ui.cmake | 2 + 5 files changed, 98 insertions(+), 59 deletions(-) create mode 100644 Telegram/SourceFiles/ui/new_badges.cpp create mode 100644 Telegram/SourceFiles/ui/new_badges.h diff --git a/Telegram/SourceFiles/settings/settings_premium.cpp b/Telegram/SourceFiles/settings/settings_premium.cpp index 319a0830a2..fd6c2322b6 100644 --- a/Telegram/SourceFiles/settings/settings_premium.cpp +++ b/Telegram/SourceFiles/settings/settings_premium.cpp @@ -47,6 +47,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/wrap/padding_wrap.h" #include "ui/wrap/slide_wrap.h" #include "ui/wrap/vertical_layout.h" +#include "ui/new_badges.h" #include "ui/painter.h" #include "ui/power_saving.h" #include "ui/vertical_list.h" @@ -951,31 +952,8 @@ void Premium::setupContent() { descriptionPadding); description->setAttribute(Qt::WA_TransparentForMouseEvents); - const auto badge = entry.newBadge - ? Ui::CreateChild>( - content, - object_ptr( - content, - tr::lng_premium_summary_new_badge(), - st::settingsPremiumNewBadge), - st::settingsPremiumNewBadgePadding) - : nullptr; - if (badge) { - badge->setAttribute(Qt::WA_TransparentForMouseEvents); - badge->paintRequest() | rpl::start_with_next([=] { - auto p = QPainter(badge); - auto hq = PainterHighQualityEnabler(p); - p.setPen(Qt::NoPen); - p.setBrush(st::windowBgActive); - const auto r = st::settingsPremiumNewBadgePadding.left(); - p.drawRoundedRect(badge->rect(), r, r); - }, badge->lifetime()); - - label->geometryValue( - ) | rpl::start_with_next([=](QRect geometry) { - badge->move(st::settingsPremiumNewBadgePosition - + QPoint(label->x() + label->width(), label->y())); - }, badge->lifetime()); + if (entry.newBadge) { + Ui::NewBadge::AddAfterLabel(content, label); } const auto dummy = Ui::CreateChild(content); dummy->setAttribute(Qt::WA_TransparentForMouseEvents); diff --git a/Telegram/SourceFiles/ui/new_badges.cpp b/Telegram/SourceFiles/ui/new_badges.cpp new file mode 100644 index 0000000000..aa1cadafa3 --- /dev/null +++ b/Telegram/SourceFiles/ui/new_badges.cpp @@ -0,0 +1,69 @@ +/* +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/new_badges.h" + +#include "lang/lang_keys.h" +#include "ui/painter.h" +#include "ui/widgets/labels.h" +#include "styles/style_window.h" +#include "styles/style_settings.h" + +namespace Ui::NewBadge { +namespace { + +[[nodiscard]] not_null CreateNewBadge( + not_null parent, + rpl::producer text) { + const auto badge = Ui::CreateChild>( + parent.get(), + object_ptr( + parent, + std::move(text), + st::settingsPremiumNewBadge), + st::settingsPremiumNewBadgePadding); + badge->setAttribute(Qt::WA_TransparentForMouseEvents); + badge->paintRequest() | rpl::start_with_next([=] { + auto p = QPainter(badge); + auto hq = PainterHighQualityEnabler(p); + p.setPen(Qt::NoPen); + p.setBrush(st::windowBgActive); + const auto r = st::settingsPremiumNewBadgePadding.left(); + p.drawRoundedRect(badge->rect(), r, r); + }, badge->lifetime()); + return badge; +} + +} // namespace + +void AddToRight(not_null parent) { + const auto badge = CreateNewBadge(parent, tr::lng_bot_side_menu_new()); + + parent->sizeValue( + ) | rpl::start_with_next([=](QSize size) { + badge->moveToRight( + st::mainMenuButton.padding.right(), + (size.height() - badge->height()) / 2, + size.width()); + }, badge->lifetime()); +} + +void AddAfterLabel( + not_null parent, + not_null label) { + const auto badge = CreateNewBadge( + parent, + tr::lng_premium_summary_new_badge()); + + label->geometryValue( + ) | rpl::start_with_next([=](QRect geometry) { + badge->move(st::settingsPremiumNewBadgePosition + + QPoint(label->x() + label->width(), label->y())); + }, badge->lifetime()); +} + +} // namespace Ui::NewBadge diff --git a/Telegram/SourceFiles/ui/new_badges.h b/Telegram/SourceFiles/ui/new_badges.h new file mode 100644 index 0000000000..0d2161251f --- /dev/null +++ b/Telegram/SourceFiles/ui/new_badges.h @@ -0,0 +1,21 @@ +/* +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 + +namespace Ui { +class RpWidget; +} // namespace Ui + +namespace Ui::NewBadge { + +void AddToRight(not_null parent); +void AddAfterLabel( + not_null parent, + not_null label); + +} // namespace Ui::NewBadge diff --git a/Telegram/SourceFiles/window/window_main_menu.cpp b/Telegram/SourceFiles/window/window_main_menu.cpp index 3b5b0928a6..051a99d8b8 100644 --- a/Telegram/SourceFiles/window/window_main_menu.cpp +++ b/Telegram/SourceFiles/window/window_main_menu.cpp @@ -15,22 +15,18 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/controls/userpic_button.h" #include "ui/effects/snowflakes.h" #include "ui/effects/toggle_arrow.h" -#include "ui/widgets/buttons.h" -#include "ui/widgets/labels.h" #include "ui/widgets/popup_menu.h" #include "ui/widgets/scroll_area.h" #include "ui/widgets/shadow.h" #include "ui/widgets/tooltip.h" #include "ui/wrap/slide_wrap.h" -#include "ui/wrap/vertical_layout.h" #include "ui/text/text_utilities.h" #include "ui/text/text_options.h" +#include "ui/new_badges.h" #include "ui/painter.h" -#include "ui/empty_userpic.h" #include "ui/vertical_list.h" #include "ui/unread_badge_paint.h" #include "inline_bots/bot_attach_web_view.h" -#include "mainwindow.h" #include "storage/localstorage.h" #include "storage/storage_account.h" #include "support/support_templates.h" @@ -50,7 +46,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "calls/calls_box_controller.h" #include "lang/lang_keys.h" #include "core/click_handler_types.h" -#include "core/core_settings.h" #include "core/application.h" #include "main/main_session.h" #include "main/main_session_settings.h" @@ -66,7 +61,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "styles/style_chat.h" // popupMenuExpandedSeparator #include "styles/style_window.h" -#include "styles/style_widgets.h" #include "styles/style_settings.h" #include "styles/style_info.h" // infoTopBarMenu #include "styles/style_layers.h" @@ -301,33 +295,8 @@ void SetupMenuBots( } }, button->lifetime()); - const auto badge = bots->showMainMenuNewBadge(bot) - ? Ui::CreateChild>( - button, - object_ptr( - button, - tr::lng_bot_side_menu_new(), - st::settingsPremiumNewBadge), - st::settingsPremiumNewBadgePadding) - : nullptr; - if (badge) { - badge->setAttribute(Qt::WA_TransparentForMouseEvents); - badge->paintRequest() | rpl::start_with_next([=] { - auto p = QPainter(badge); - auto hq = PainterHighQualityEnabler(p); - p.setPen(Qt::NoPen); - p.setBrush(st::windowBgActive); - const auto r = st::settingsPremiumNewBadgePadding.left(); - p.drawRoundedRect(badge->rect(), r, r); - }, badge->lifetime()); - - button->sizeValue( - ) | rpl::start_with_next([=](QSize size) { - badge->moveToRight( - st::mainMenuButton.padding.right(), - (size.height() - badge->height()) / 2, - size.width()); - }, badge->lifetime()); + if (bots->showMainMenuNewBadge(bot)) { + Ui::NewBadge::AddToRight(button); } } wrap->resizeToWidth(width); diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index bacd9fe150..4df7b5c6ef 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -377,6 +377,8 @@ PRIVATE ui/empty_userpic.h ui/grouped_layout.cpp ui/grouped_layout.h + ui/new_badges.cpp + ui/new_badges.h ui/power_saving.cpp ui/power_saving.h ui/vertical_list.cpp