From 6de471db1779f05eba1ea3a6d38d474dd4d33700 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 15 Feb 2024 09:37:11 +0300 Subject: [PATCH] Added counter label of characters limit to send files box. --- Telegram/SourceFiles/boxes/send_files_box.cpp | 35 +++++++++++++++++++ Telegram/SourceFiles/boxes/send_files_box.h | 8 +++++ .../SourceFiles/history/history_widget.cpp | 3 +- .../history_view_characters_limit.cpp | 20 ++++++++--- .../controls/history_view_characters_limit.h | 3 +- .../history_view_compose_controls.cpp | 3 +- 6 files changed, 64 insertions(+), 8 deletions(-) diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 18f02439c..7cd37dbe8 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "chat_helpers/tabbed_selector.h" #include "editor/photo_editor_layer_widget.h" #include "history/history_drag_area.h" +#include "history/view/controls/history_view_characters_limit.h" #include "history/view/history_view_schedule_box.h" #include "core/file_utilities.h" #include "core/mime_type.h" @@ -48,6 +49,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lottie/lottie_single_player.h" #include "data/data_document.h" #include "data/data_user.h" +#include "data/data_peer_values.h" // Data::AmPremiumValue. #include "data/data_premium_limits.h" #include "data/stickers/data_stickers.h" #include "data/stickers/data_custom_emoji.h" @@ -1060,6 +1062,39 @@ void SendFilesBox::setupCaption() { updateCaptionPlaceholder(); setupEmojiPanel(); + + rpl::single(rpl::empty_value()) | rpl::then( + _caption->changes() + ) | rpl::start_with_next([=] { + checkCharsLimitation(); + }, _caption->lifetime()); +} + +void SendFilesBox::checkCharsLimitation() { + const auto limits = Data::PremiumLimits(&_show->session()); + const auto caption = (_caption && !_caption->isHidden()) + ? _caption->getTextWithAppliedMarkdown() + : TextWithTags(); + const auto remove = caption.text.size() - limits.captionLengthCurrent(); + if ((remove > 0) && _emojiToggle) { + if (!_charsLimitation) { + _charsLimitation = base::make_unique_q( + this, + _emojiToggle.data(), + style::al_top); + _charsLimitation->show(); + Data::AmPremiumValue( + &_show->session() + ) | rpl::start_with_next([=] { + checkCharsLimitation(); + }, _charsLimitation->lifetime()); + } + _charsLimitation->setLeft(remove); + } else { + if (_charsLimitation) { + _charsLimitation = nullptr; + } + } } void SendFilesBox::setupEmojiPanel() { diff --git a/Telegram/SourceFiles/boxes/send_files_box.h b/Telegram/SourceFiles/boxes/send_files_box.h index b1db58787..0acbb8f49 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.h +++ b/Telegram/SourceFiles/boxes/send_files_box.h @@ -50,6 +50,10 @@ namespace SendMenu { enum class Type; } // namespace SendMenu +namespace HistoryView::Controls { +class CharactersLimitLabel; +} // namespace HistoryView::Controls + enum class SendFilesAllow { OnlyOne = (1 << 0), Photos = (1 << 1), @@ -221,6 +225,8 @@ private: void enqueueNextPrepare(); void addPreparedAsyncFile(Ui::PreparedFile &&file); + void checkCharsLimitation(); + const std::shared_ptr _show; const style::ComposeControls &_st; const Api::SendType _sendType = Api::SendType(); @@ -244,6 +250,8 @@ private: object_ptr _emojiToggle = { nullptr }; base::unique_qptr _emojiPanel; base::unique_qptr _emojiFilter; + using CharactersLimitLabel = HistoryView::Controls::CharactersLimitLabel; + base::unique_qptr _charsLimitation; object_ptr _groupFiles = { nullptr }; object_ptr _sendImagesAsPhotos = { nullptr }; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index fbc4aa1e2..e003b6020 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -7319,7 +7319,8 @@ void HistoryWidget::checkCharsLimitation() { if (!_charsLimitation) { _charsLimitation = base::make_unique_q( this, - _send.get()); + _send.get(), + style::al_bottom); _charsLimitation->show(); Data::AmPremiumValue( &session() diff --git a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp index 8fa5621c6..c3f273674 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp @@ -7,20 +7,30 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "history/view/controls/history_view_characters_limit.h" +#include "ui/rect.h" #include "styles/style_chat_helpers.h" namespace HistoryView::Controls { CharactersLimitLabel::CharactersLimitLabel( not_null parent, - not_null widgetBelow) + not_null widgetToAlign, + style::align align) : Ui::FlatLabel(parent, st::historyCharsLimitationLabel) { + Expects((align == style::al_top) || align == style::al_bottom); + const auto w = st::historyCharsLimitationLabel.minWidth; + using F = Fn; + const auto position = (align == style::al_top) + ? F([=](int height, const QRect &g) { + move(g.x() + (g.width() - w) / 2, rect::bottom(g)); + }) + : F([=](int height, const QRect &g) { + move(g.x() + (g.width() - w) / 2, g.y() - height); + }); rpl::combine( Ui::RpWidget::heightValue(), - widgetBelow->positionValue() - ) | rpl::start_with_next([=](int height, const QPoint &p) { - move(p.x(), p.y() - height); - }, lifetime()); + widgetToAlign->geometryValue() + ) | rpl::start_with_next(position, lifetime()); } void CharactersLimitLabel::setLeft(int value) { diff --git a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h index b16b6bfa1..3b6054e19 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h @@ -15,7 +15,8 @@ class CharactersLimitLabel final : public Ui::FlatLabel { public: CharactersLimitLabel( not_null parent, - not_null widgetBelow); + not_null widgetToAlign, + style::align align); void setLeft(int value); diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp index 9122d0674..19ec21b33 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -3322,7 +3322,8 @@ void ComposeControls::checkCharsLimitation() { using namespace Controls; _charsLimitation = base::make_unique_q( _wrap.get(), - _send.get()); + _send.get(), + style::al_bottom); _charsLimitation->show(); Data::AmPremiumValue( &session()