From 5020aec6ecaf3a5699b7d12a9e0fd608cf08ca6d Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 22 Aug 2024 13:59:41 +0300 Subject: [PATCH] Moved out AddEmojiToggleToField to separate file. --- Telegram/CMakeLists.txt | 2 + .../SourceFiles/boxes/create_poll_box.cpp | 99 +-------------- .../ui/controls/emoji_button_factory.cpp | 120 ++++++++++++++++++ .../ui/controls/emoji_button_factory.h | 31 +++++ 4 files changed, 156 insertions(+), 96 deletions(-) create mode 100644 Telegram/SourceFiles/ui/controls/emoji_button_factory.cpp create mode 100644 Telegram/SourceFiles/ui/controls/emoji_button_factory.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 8d88fe7fd5..e67fd1520e 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1484,6 +1484,8 @@ PRIVATE ui/chat/choose_send_as.h ui/chat/choose_theme_controller.cpp ui/chat/choose_theme_controller.h + ui/controls/emoji_button_factory.cpp + ui/controls/emoji_button_factory.h ui/controls/location_picker.cpp ui/controls/location_picker.h ui/controls/silent_toggle.cpp diff --git a/Telegram/SourceFiles/boxes/create_poll_box.cpp b/Telegram/SourceFiles/boxes/create_poll_box.cpp index 9ff85e099a..6e7ba971db 100644 --- a/Telegram/SourceFiles/boxes/create_poll_box.cpp +++ b/Telegram/SourceFiles/boxes/create_poll_box.cpp @@ -25,6 +25,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "menu/menu_send.h" #include "ui/controls/emoji_button.h" +#include "ui/controls/emoji_button_factory.h" #include "ui/rect.h" #include "ui/text/text_utilities.h" #include "ui/toast/toast.h" @@ -54,100 +55,6 @@ constexpr auto kSolutionLimit = 200; constexpr auto kWarnSolutionLimit = 60; constexpr auto kErrorLimit = 99; -[[nodiscard]] not_null AddEmojiToggleToField( - not_null field, - not_null box, - not_null controller, - not_null emojiPanel, - QPoint shift) { - const auto emojiToggle = Ui::CreateChild( - field->parentWidget(), - st::defaultComposeFiles.emoji); - const auto fade = Ui::CreateChild( - emojiToggle, - emojiToggle, - 0.5); - { - const auto fadeTarget = Ui::CreateChild(emojiToggle); - fadeTarget->resize(emojiToggle->size()); - fadeTarget->paintRequest( - ) | rpl::start_with_next([=](const QRect &rect) { - auto p = QPainter(fadeTarget); - if (fade->animating()) { - p.fillRect(fadeTarget->rect(), st::boxBg); - } - fade->paint(p); - }, fadeTarget->lifetime()); - rpl::single(false) | rpl::then( - field->focusedChanges() - ) | rpl::start_with_next([=](bool shown) { - if (shown) { - fade->fadeIn(st::universalDuration); - } else { - fade->fadeOut(st::universalDuration); - } - }, emojiToggle->lifetime()); - fade->fadeOut(1); - fade->finish(); - } - - - const auto outer = box->getDelegate()->outerContainer(); - const auto allow = [](not_null) { return true; }; - InitMessageFieldHandlers( - controller, - field, - Window::GifPauseReason::Layer, - allow); - Ui::Emoji::SuggestionsController::Init( - outer, - field, - &controller->session(), - Ui::Emoji::SuggestionsController::Options{ - .suggestCustomEmoji = true, - .allowCustomWithoutPremium = allow, - }); - const auto updateEmojiPanelGeometry = [=] { - const auto parent = emojiPanel->parentWidget(); - const auto global = emojiToggle->mapToGlobal({ 0, 0 }); - const auto local = parent->mapFromGlobal(global); - const auto right = local.x() + emojiToggle->width() * 3; - const auto isDropDown = local.y() < parent->height() / 2; - emojiPanel->setDropDown(isDropDown); - if (isDropDown) { - emojiPanel->moveTopRight( - local.y() + emojiToggle->height(), - right); - } else { - emojiPanel->moveBottomRight(local.y(), right); - } - }; - rpl::combine( - box->sizeValue(), - field->geometryValue() - ) | rpl::start_with_next([=](QSize outer, QRect inner) { - emojiToggle->moveToLeft( - rect::right(inner) + shift.x(), - inner.y() + shift.y()); - emojiToggle->update(); - }, emojiToggle->lifetime()); - - emojiToggle->installEventFilter(emojiPanel); - emojiToggle->addClickHandler([=] { - updateEmojiPanelGeometry(); - emojiPanel->toggleAnimated(); - }); - const auto filterCallback = [=](not_null event) { - if (event->type() == QEvent::Enter) { - updateEmojiPanelGeometry(); - } - return base::EventFilterResult::Continue; - }; - base::install_event_filter(emojiToggle, filterCallback); - - return emojiToggle; -} - class Options { public: Options( @@ -770,7 +677,7 @@ void Options::addEmptyOption() { _chooseCorrectGroup)); const auto field = _list.back()->field(); if (const auto emojiPanel = _emojiPanel) { - const auto emojiToggle = AddEmojiToggleToField( + const auto emojiToggle = Ui::AddEmojiToggleToField( field, _box, _controller, @@ -972,7 +879,7 @@ not_null CreatePollBox::setupQuestion( emojiPanel->hide(); emojiPanel->selector()->setCurrentPeer(session->user()); - const auto emojiToggle = AddEmojiToggleToField( + const auto emojiToggle = Ui::AddEmojiToggleToField( question, this, _controller, diff --git a/Telegram/SourceFiles/ui/controls/emoji_button_factory.cpp b/Telegram/SourceFiles/ui/controls/emoji_button_factory.cpp new file mode 100644 index 0000000000..a62c2444cc --- /dev/null +++ b/Telegram/SourceFiles/ui/controls/emoji_button_factory.cpp @@ -0,0 +1,120 @@ +/* +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/controls/emoji_button_factory.h" + +#include "base/event_filter.h" +#include "chat_helpers/emoji_suggestions_widget.h" +#include "chat_helpers/message_field.h" +#include "chat_helpers/tabbed_panel.h" +#include "chat_helpers/tabbed_selector.h" +#include "ui/controls/emoji_button.h" +#include "ui/effects/fade_animation.h" +#include "ui/layers/box_content.h" +#include "ui/rect.h" +#include "ui/widgets/fields/input_field.h" +#include "window/window_session_controller.h" +#include "styles/style_chat_helpers.h" // defaultComposeFiles. +#include "styles/style_settings.h" + +namespace Ui { + +[[nodiscard]] not_null AddEmojiToggleToField( + not_null field, + not_null box, + not_null controller, + not_null emojiPanel, + QPoint shift) { + const auto emojiToggle = Ui::CreateChild( + field->parentWidget(), + st::defaultComposeFiles.emoji); + const auto fade = Ui::CreateChild( + emojiToggle, + emojiToggle, + 0.5); + { + const auto fadeTarget = Ui::CreateChild(emojiToggle); + fadeTarget->resize(emojiToggle->size()); + fadeTarget->paintRequest( + ) | rpl::start_with_next([=](const QRect &rect) { + auto p = QPainter(fadeTarget); + if (fade->animating()) { + p.fillRect(fadeTarget->rect(), st::boxBg); + } + fade->paint(p); + }, fadeTarget->lifetime()); + rpl::single(false) | rpl::then( + field->focusedChanges() + ) | rpl::start_with_next([=](bool shown) { + if (shown) { + fade->fadeIn(st::universalDuration); + } else { + fade->fadeOut(st::universalDuration); + } + }, emojiToggle->lifetime()); + fade->fadeOut(1); + fade->finish(); + } + + + const auto outer = box->getDelegate()->outerContainer(); + const auto allow = [](not_null) { return true; }; + InitMessageFieldHandlers( + controller, + field, + Window::GifPauseReason::Layer, + allow); + Ui::Emoji::SuggestionsController::Init( + outer, + field, + &controller->session(), + Ui::Emoji::SuggestionsController::Options{ + .suggestCustomEmoji = true, + .allowCustomWithoutPremium = allow, + }); + const auto updateEmojiPanelGeometry = [=] { + const auto parent = emojiPanel->parentWidget(); + const auto global = emojiToggle->mapToGlobal({ 0, 0 }); + const auto local = parent->mapFromGlobal(global); + const auto right = local.x() + emojiToggle->width() * 3; + const auto isDropDown = local.y() < parent->height() / 2; + emojiPanel->setDropDown(isDropDown); + if (isDropDown) { + emojiPanel->moveTopRight( + local.y() + emojiToggle->height(), + right); + } else { + emojiPanel->moveBottomRight(local.y(), right); + } + }; + rpl::combine( + box->sizeValue(), + field->geometryValue() + ) | rpl::start_with_next([=](QSize outer, QRect inner) { + emojiToggle->moveToLeft( + rect::right(inner) + shift.x(), + inner.y() + shift.y()); + emojiToggle->update(); + }, emojiToggle->lifetime()); + + emojiToggle->installEventFilter(emojiPanel); + emojiToggle->addClickHandler([=] { + updateEmojiPanelGeometry(); + emojiPanel->toggleAnimated(); + }); + const auto filterCallback = [=](not_null event) { + if (event->type() == QEvent::Enter) { + updateEmojiPanelGeometry(); + } + return base::EventFilterResult::Continue; + }; + base::install_event_filter(emojiToggle, filterCallback); + + return emojiToggle; +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/controls/emoji_button_factory.h b/Telegram/SourceFiles/ui/controls/emoji_button_factory.h new file mode 100644 index 0000000000..3fa3dc728c --- /dev/null +++ b/Telegram/SourceFiles/ui/controls/emoji_button_factory.h @@ -0,0 +1,31 @@ +/* +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 ChatHelpers { +class TabbedPanel; +} // namespace ChatHelpers + +namespace Window { +class SessionController; +} // namespace Window + +namespace Ui { + +class BoxContent; +class EmojiButton; +class InputField; + +[[nodiscard]] not_null AddEmojiToggleToField( + not_null field, + not_null box, + not_null controller, + not_null emojiPanel, + QPoint shift); + +} // namespace Ui