From 8fcd07863afbbd00f2d23157575b5a2fde7b4d32 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 29 Mar 2022 01:22:21 +0300 Subject: [PATCH] Replaced box for auto-delete settings with menu. --- .../SourceFiles/boxes/delete_messages_box.cpp | 4 +- .../SourceFiles/history/history_widget.cpp | 1 + .../history_view_compose_controls.cpp | 5 +- .../view/controls/history_view_ttl_button.cpp | 67 ++++++++++++------- .../view/controls/history_view_ttl_button.h | 15 +++-- 5 files changed, 59 insertions(+), 33 deletions(-) diff --git a/Telegram/SourceFiles/boxes/delete_messages_box.cpp b/Telegram/SourceFiles/boxes/delete_messages_box.cpp index e6c3b2de2..39c09647d 100644 --- a/Telegram/SourceFiles/boxes/delete_messages_box.cpp +++ b/Telegram/SourceFiles/boxes/delete_messages_box.cpp @@ -236,6 +236,7 @@ void DeleteMessagesBox::prepare() { || (_wipeHistoryPeer->isChannel() && _wipeHistoryPeer->asChannel()->canDeleteMessages()))) { _wipeHistoryPeer->updateFull(); +#if 0 _autoDeleteSettings.create( this, (_wipeHistoryPeer->messagesTTL() @@ -245,10 +246,11 @@ void DeleteMessagesBox::prepare() { _autoDeleteSettings->setClickedCallback([=] { getDelegate()->show( Box( - HistoryView::Controls::AutoDeleteSettingsBox, + HistoryView::Controls::AutoDeleteSettingsMenu, _wipeHistoryPeer), Ui::LayerOption(0)); }); +#endif } if (canDelete) { diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 9e745de8e..ee02ef7b8 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -6386,6 +6386,7 @@ void HistoryWidget::checkMessagesTTL() { } else if (!_ttlInfo || _ttlInfo->peer() != _peer) { _ttlInfo = std::make_unique( this, + std::make_shared(controller()), _peer); orderWidgets(); updateControlsGeometry(); 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 2f54d0d0d..a40a55a90 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -1949,7 +1949,10 @@ void ComposeControls::updateMessagesTTLShown() { updateControlsVisibility(); updateControlsGeometry(_wrap->size()); } else if (shown && !_ttlInfo) { - _ttlInfo = std::make_unique(_wrap.get(), peer); + _ttlInfo = std::make_unique( + _wrap.get(), + std::make_shared(_window), + peer); orderControls(); updateControlsVisibility(); updateControlsGeometry(_wrap->size()); diff --git a/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.cpp b/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.cpp index 61ec114ed..af3ae4947 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_channel.h" #include "data/data_changes.h" #include "main/main_session.h" +#include "menu/menu_ttl.h" #include "lang/lang_keys.h" #include "boxes/peers/edit_peer_info_box.h" #include "ui/boxes/auto_delete_settings.h" @@ -26,12 +27,12 @@ namespace { constexpr auto kToastDuration = crl::time(3500); -} // namespace - -void ShowAutoDeleteToast(not_null peer) { +void ShowAutoDeleteToast( + not_null parent, + not_null peer) { const auto period = peer->messagesTTL(); if (!period) { - Ui::Toast::Show(tr::lng_ttl_about_tooltip_off(tr::now)); + Ui::Toast::Show(parent, tr::lng_ttl_about_tooltip_off(tr::now)); return; } @@ -46,20 +47,27 @@ void ShowAutoDeleteToast(not_null peer) { ? tr::lng_ttl_about_tooltip_channel(tr::now, lt_duration, duration) : tr::lng_ttl_about_tooltip(tr::now, lt_duration, duration); Ui::ShowMultilineToast({ + .parentOverride = parent, .text = { text }, .duration = kToastDuration, }); } -void AutoDeleteSettingsBox( - not_null box, - not_null peer) { +} // namespace + +void AutoDeleteSettingsMenu( + not_null parent, + std::shared_ptr show, + not_null peer, + rpl::producer<> triggers) { struct State { TimeId savingPeriod = 0; mtpRequestId savingRequestId = 0; - QPointer weak; + QPointer weak; }; - const auto state = std::make_shared(State{ .weak = box.get() }); + const auto state = std::make_shared(State{ + .weak = Ui::MakeWeak(parent.get()), + }); auto callback = [=](TimeId period) { auto &api = peer->session().api(); if (state->savingRequestId) { @@ -74,42 +82,49 @@ void AutoDeleteSettingsBox( MTP_int(period) )).done([=](const MTPUpdates &result) { peer->session().api().applyUpdates(result); - ShowAutoDeleteToast(peer); + ShowAutoDeleteToast(show->toastParent(), peer); +#if 0 if (const auto strong = state->weak.data()) { strong->closeBox(); } +#endif }).fail([=] { state->savingRequestId = 0; }).send(); }; - Ui::AutoDeleteSettingsBox( - box, - peer->messagesTTL(), - (peer->isUser() - ? tr::lng_ttl_edit_about(lt_user, rpl::single(peer->shortName())) - : peer->isBroadcast() - ? tr::lng_ttl_edit_about_channel() - : tr::lng_ttl_edit_about_group()), - std::move(callback)); + auto about = peer->isUser() + ? tr::lng_ttl_edit_about(lt_user, rpl::single(peer->shortName())) + : peer->isBroadcast() + ? tr::lng_ttl_edit_about_channel() + : tr::lng_ttl_edit_about_group(); + const auto ttl = peer->messagesTTL(); + TTLMenu::SetupTTLMenu( + parent, + std::move(triggers), + { std::move(show), ttl, std::move(about), std::move(callback) }); } -TTLButton::TTLButton(not_null parent, not_null peer) +TTLButton::TTLButton( + not_null parent, + std::shared_ptr show, + not_null peer) : _peer(peer) , _button(parent, st::historyMessagesTTL) { - _button.setClickedCallback([=] { + auto triggers = _button.clicks( + ) | rpl::to_empty | rpl::filter([=] { const auto canEdit = peer->isUser() || (peer->isChat() && peer->asChat()->canDeleteMessages()) || (peer->isChannel() && peer->asChannel()->canDeleteMessages()); if (!canEdit) { - ShowAutoDeleteToast(peer); - return; + ShowAutoDeleteToast(show->toastParent(), peer); + return false; } - Ui::show( - Box(AutoDeleteSettingsBox, peer), - Ui::LayerOption(0)); + return true; }); + AutoDeleteSettingsMenu(parent, show, peer, std::move(triggers)); + peer->session().changes().peerFlagsValue( peer, Data::PeerUpdate::Flag::MessagesTTL diff --git a/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.h b/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.h index a0b38156b..bfc431765 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_ttl_button.h @@ -10,18 +10,23 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/buttons.h" namespace Ui { -class GenericBox; +class Show; } // namespace Ui namespace HistoryView::Controls { -void AutoDeleteSettingsBox( - not_null box, - not_null peer); +void AutoDeleteSettingsMenu( + not_null parent, + std::shared_ptr show, + not_null peer, + rpl::producer<> triggers); class TTLButton final { public: - TTLButton(not_null parent, not_null peer); + TTLButton( + not_null parent, + std::shared_ptr show, + not_null peer); [[nodiscard]] not_null peer() const { return _peer;