From edcc8acb199c5ca25fbbd2252022d79d55265eb6 Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Sat, 10 Sep 2022 20:30:57 +0300 Subject: [PATCH] [Improvement] Hiding and showing pinned messages --- Telegram/CMakeLists.txt | 2 + Telegram/Resources/langs/rewrites/en.json | 3 + Telegram/SourceFiles/data/data_changes.h | 4 +- Telegram/SourceFiles/history/history.cpp | 1 + .../SourceFiles/history/history_widget.cpp | 38 +++++--- Telegram/SourceFiles/history/history_widget.h | 2 +- .../view/history_view_replies_section.cpp | 8 ++ .../kotato/boxes/kotato_unpin_box.cpp | 84 +++++++++++++++++ .../kotato/boxes/kotato_unpin_box.h | 45 ++++++++++ .../SourceFiles/window/window_peer_menu.cpp | 89 +++++++++++++++---- .../SourceFiles/window/window_peer_menu.h | 3 +- 11 files changed, 246 insertions(+), 33 deletions(-) create mode 100644 Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.cpp create mode 100644 Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 5c071a83f..84526d572 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1027,6 +1027,8 @@ PRIVATE kotato/boxes/kotato_fonts_box.h kotato/boxes/kotato_radio_box.cpp kotato/boxes/kotato_radio_box.h + kotato/boxes/kotato_unpin_box.cpp + kotato/boxes/kotato_unpin_box.h kotato/kotato_lang.cpp kotato/kotato_lang.h kotato/kotato_settings.cpp diff --git a/Telegram/Resources/langs/rewrites/en.json b/Telegram/Resources/langs/rewrites/en.json index b9e646d65..15893bb1f 100644 --- a/Telegram/Resources/langs/rewrites/en.json +++ b/Telegram/Resources/langs/rewrites/en.json @@ -31,6 +31,8 @@ "ktg_settings_show_json_settings": "Show settings file", "ktg_settings_restart": "Restart Kotatogram", "ktg_copy_btn_callback": "Copy callback data", + "ktg_pinned_message_show": "Show pinned message", + "ktg_pinned_message_hide": "Hide pinned message", "ktg_settings_chats": "Chats", "ktg_settings_sticker_height": "Sticker height: {pixels}px", "ktg_settings_sticker_scale_both": "Apply to sticker width", @@ -77,6 +79,7 @@ "ktg_settings_top_bar_mute": "Mute in profile top bar", "ktg_settings_messages": "Messages", "ktg_settings_filters_hide_all": "Hide \"All chats\" folder", + "ktg_hide_pinned_message": "Hide", "ktg_settings_tray_icon": "Tray icon", "ktg_settings_tray_icon_default": "Default", "ktg_settings_tray_icon_blue": "Blue", diff --git a/Telegram/SourceFiles/data/data_changes.h b/Telegram/SourceFiles/data/data_changes.h index d33a41617..1a2b25fda 100644 --- a/Telegram/SourceFiles/data/data_changes.h +++ b/Telegram/SourceFiles/data/data_changes.h @@ -213,7 +213,9 @@ struct EntryUpdate { Height = (1U << 4), Destroyed = (1U << 5), - LastUsedBit = (1U << 5), + PinVisible = (1U << 6), + + LastUsedBit = (1U << 6), }; using Flags = base::flags; friend inline constexpr auto is_flag_type(Flag) { return true; } diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index b2a44e438..2eafcedeb 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -48,6 +48,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "mainwindow.h" #include "main/main_session.h" +#include "main/main_session_settings.h" #include "window/notifications_manager.h" #include "calls/calls_instance.h" #include "spellcheck/spellcheck_types.h" diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index f4e2cce5d..e329137a7 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -748,6 +748,14 @@ HistoryWidget::HistoryWidget( } }, lifetime()); + session().changes().entryUpdates( + EntryUpdateFlag::PinVisible + ) | rpl::start_with_next([=](const Data::EntryUpdate &update) { + if (_pinnedTracker) { + checkPinnedBarState(); + } + }, lifetime()); + using HistoryUpdateFlag = Data::HistoryUpdate::Flag; session().changes().historyUpdates( HistoryUpdateFlag::MessageSent @@ -7196,9 +7204,7 @@ void HistoryWidget::checkPinnedBarState() { Expects(_pinnedTracker != nullptr); Expects(_list != nullptr); - const auto hiddenId = _peer->canPinMessages() - ? MsgId(0) - : session().settings().hiddenPinnedMessageId(_peer->id); + const auto hiddenId = session().settings().hiddenPinnedMessageId(_peer->id); const auto currentPinnedId = Data::ResolveTopPinnedId( _peer, MsgId(0), // topicRootId @@ -7445,7 +7451,11 @@ void HistoryWidget::refreshPinnedBarButton(bool many, HistoryItem *item) { button->clicks( ) | rpl::start_with_next([=] { if (close) { - hidePinnedMessage(); + // if (button->clickModifiers() & Qt::ControlModifier) { + // hidePinnedMessage(true); + // } else { + hidePinnedMessage(); + // } } else { openSection(); } @@ -7918,21 +7928,25 @@ void HistoryWidget::editMessage( setInnerFocus(); } -void HistoryWidget::hidePinnedMessage() { +void HistoryWidget::hidePinnedMessage(bool force) { Expects(_pinnedBar != nullptr); const auto id = _pinnedTracker->currentMessageId(); if (!id.message) { return; } - if (_peer->canPinMessages()) { - Window::ToggleMessagePinned(controller(), id.message, false); + const auto callback = [=] { + if (_pinnedTracker) { + checkPinnedBarState(); + } + }; + if (_peer->canPinMessages() && !force) { + Window::ToggleMessagePinned( + controller(), + id.message, + false, + crl::guard(this, callback)); } else { - const auto callback = [=] { - if (_pinnedTracker) { - checkPinnedBarState(); - } - }; Window::HidePinnedBar( controller(), _peer, diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 945dd65e7..7ec356f7a 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -419,7 +419,7 @@ private: void showKeyboardHideButton(); void toggleKeyboard(bool manual = true); void startBotCommand(); - void hidePinnedMessage(); + void hidePinnedMessage(bool force = false); void cancelFieldAreaState(); void unblockUser(); void sendBotStartCommand(); diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp index b1dcb13db..9c1112e46 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp @@ -595,6 +595,14 @@ void RepliesWidget::subscribeToPinnedMessages() { } }, lifetime()); + session().changes().entryUpdates( + EntryUpdateFlag::PinVisible + ) | rpl::start_with_next([=](const Data::EntryUpdate &update) { + if (_pinnedTracker) { + checkPinnedBarState(); + } + }, lifetime()); + setupPinnedTracker(); } diff --git a/Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.cpp b/Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.cpp new file mode 100644 index 000000000..f3761e3e0 --- /dev/null +++ b/Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.cpp @@ -0,0 +1,84 @@ +/* +This file is part of Kotatogram Desktop, +the unofficial app based on Telegram Desktop. + +For license and copyright information please follow this link: +https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL +*/ +#include "kotato/boxes/kotato_unpin_box.h" + +#include "kotato/kotato_lang.h" +#include "lang/lang_keys.h" +#include "apiwrap.h" +#include "history/history_widget.h" +#include "ui/widgets/labels.h" +#include "data/data_channel.h" +#include "data/data_chat.h" +#include "data/data_changes.h" +#include "data/data_user.h" +#include "data/data_session.h" +#include "main/main_session.h" +#include "main/main_session_settings.h" +#include "styles/style_layers.h" +#include "styles/style_boxes.h" + +UnpinMessageBox::UnpinMessageBox( + QWidget*, + not_null peer, + MsgId topicRootId, + MsgId msgId, + Fn onHidden) +: _peer(peer) +, _api(&peer->session().mtp()) +, _topicRootId(topicRootId) +, _msgId(msgId) +, _onHidden(std::move(onHidden)) +, _text(this, tr::lng_pinned_unpin_sure(tr::now), st::boxLabel) { +} + +void UnpinMessageBox::prepare() { + if (_onHidden) { + addLeftButton(rktr("ktg_hide_pinned_message"), [this] { + _onHidden(); + Ui::hideLayer(); + }); + } + + addButton(tr::lng_pinned_unpin(), [this] { unpinMessage(); }); + addButton(tr::lng_cancel(), [this] { closeBox(); }); + + auto height = st::boxPadding.top() + _text->height() + st::boxPadding.bottom(); + setDimensions(st::boxWidth, height); +} + +void UnpinMessageBox::resizeEvent(QResizeEvent *e) { + BoxContent::resizeEvent(e); + _text->moveToLeft(st::boxPadding.left(), st::boxPadding.top()); +} + +void UnpinMessageBox::keyPressEvent(QKeyEvent *e) { + if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { + unpinMessage(); + } else if (e->key() == Qt::Key_Backspace) { + _onHidden(); + Ui::hideLayer(); + } else { + BoxContent::keyPressEvent(e); + } +} + +void UnpinMessageBox::unpinMessage() { + if (_requestId) return; + + //auto flags = MTPmessages_UpdatePinnedMessage::Flags(0); + _requestId = _api.request(MTPmessages_UpdatePinnedMessage( + MTP_flags(MTPmessages_UpdatePinnedMessage::Flag::f_unpin), + _peer->input, + MTP_int(_msgId) + )).done([=](const MTPUpdates &result) { + _peer->session().api().applyUpdates(result); + Ui::hideLayer(); + }).fail([=](const MTP::Error &error) { + Ui::hideLayer(); + }).send(); +} diff --git a/Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.h b/Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.h new file mode 100644 index 000000000..12e0c752c --- /dev/null +++ b/Telegram/SourceFiles/kotato/boxes/kotato_unpin_box.h @@ -0,0 +1,45 @@ +/* +This file is part of Kotatogram Desktop, +the unofficial app based on Telegram Desktop. + +For license and copyright information please follow this link: +https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL +*/ +#pragma once + +#include "boxes/abstract_box.h" +#include "mtproto/sender.h" + +namespace Ui { +class FlatLabel; +} // namespace Ui + +class UnpinMessageBox final : public Ui::BoxContent { +public: + UnpinMessageBox( + QWidget*, + not_null peer, + MsgId topicRootId, + MsgId msgId, + Fn onHidden); + +protected: + void prepare() override; + + void resizeEvent(QResizeEvent *e) override; + void keyPressEvent(QKeyEvent *e) override; + +private: + void unpinMessage(); + + const not_null _peer; + MTP::Sender _api; + MsgId _topicRootId = 0; + MsgId _msgId = 0; + Fn _onHidden; + + object_ptr _text; + + mtpRequestId _requestId = 0; + +}; diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 80e695c21..6bf191e4e 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "window/window_peer_menu.h" +#include "kotato/kotato_lang.h" #include "kotato/kotato_settings.h" #include "menu/menu_check_item.h" #include "boxes/share_box.h" @@ -35,6 +36,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peers/edit_contact_box.h" #include "boxes/share_box.h" #include "calls/calls_instance.h" +#include "kotato/boxes/kotato_unpin_box.h" +#include "ui/boxes/confirm_box.h" #include "inline_bots/bot_attach_web_view.h" // InlineBots::PeerType. #include "ui/boxes/report_box.h" #include "ui/toast/toast.h" @@ -303,6 +306,7 @@ private: void addBotToGroup(); void addNewMembers(); void addDeleteContact(); + void addHidePin(); void addTTLSubmenu(bool addSeparator); void addGiftPremium(); void addCreateTopic(); @@ -979,6 +983,64 @@ void Filler::addDeleteContact() { }); } +void Filler::addHidePin() { + if (!_peer || !_thread) { + return; + } + const auto topicRootId = _thread->topicRootId(); + const auto migrated = topicRootId ? nullptr : _peer->migrateFrom(); + const auto top = Data::ResolveTopPinnedId( + _peer, + topicRootId, + migrated); + const auto universal = !top + ? MsgId(0) + : (migrated && !peerIsChannel(top.peer)) + ? (top.msg - ServerMaxMsgId) + : top.msg; + if (!universal) { + return; + } + const auto hiddenId = _peer->session().settings().hiddenPinnedMessageId(_peer->id); + + if (hiddenId != 0) { + _addAction( + ktr("ktg_pinned_message_show"), + [=, peer = _peer, thread = _thread] { + auto &session = peer->session(); + session.settings().setHiddenPinnedMessageId( + peer->id, + topicRootId, + 0); + session.saveSettingsDelayed(); + session.changes().entryUpdated( + thread, + Data::EntryUpdate::Flag::PinVisible); + }, + &st::menuIconPin); + + } else { + _addAction( + ktr("ktg_pinned_message_hide"), + [=, peer = _peer, thread = _thread] { + auto &session = peer->session(); + if (universal) { + session.settings().setHiddenPinnedMessageId( + peer->id, + topicRootId, + universal); + session.saveSettingsDelayed(); + session.changes().entryUpdated( + thread, + Data::EntryUpdate::Flag::PinVisible); + } else { + session.api().requestFullPeer(peer); + } + }, + &st::menuIconPin); + } +} + void Filler::addDeleteTopic() { if (!_topic || !_topic->canDelete()) { return; @@ -1364,6 +1426,7 @@ void Filler::fillHistoryActions() { addViewAsTopics(); addStoryArchive(); addSupportInfo(); + addHidePin(); addManageChat(); addBoostChat(); addCreatePoll(); @@ -1404,6 +1467,7 @@ void Filler::fillProfileActions() { void Filler::fillRepliesActions() { if (_topic) { addInfo(); + addHidePin(); addManageTopic(); } addCreatePoll(); @@ -2567,7 +2631,8 @@ void PeerMenuAddChannelMembers( void ToggleMessagePinned( not_null navigation, FullMsgId itemId, - bool pin) { + bool pin, + Fn onHidden) { const auto item = navigation->session().data().message(itemId); if (!item || !item->canPin()) { return; @@ -2577,24 +2642,12 @@ void ToggleMessagePinned( Box(PinMessageBox, item), Ui::LayerOption::CloseOther); } else { - const auto peer = item->history()->peer; - const auto session = &peer->session(); - const auto callback = crl::guard(session, [=](Fn &&close) { - close(); - session->api().request(MTPmessages_UpdatePinnedMessage( - MTP_flags(MTPmessages_UpdatePinnedMessage::Flag::f_unpin), - peer->input, - MTP_int(itemId.msg) - )).done([=](const MTPUpdates &result) { - session->api().applyUpdates(result); - }).send(); - }); navigation->parentController()->show( - Ui::MakeConfirmBox({ - .text = tr::lng_pinned_unpin_sure(), - .confirmed = callback, - .confirmText = tr::lng_pinned_unpin(), - }), + Box( + item->history()->peer, + item->topicRootId(), + item->id, + std::move(onHidden)), Ui::LayerOption::CloseOther); } } diff --git a/Telegram/SourceFiles/window/window_peer_menu.h b/Telegram/SourceFiles/window/window_peer_menu.h index cc4f56178..1fd259d05 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.h +++ b/Telegram/SourceFiles/window/window_peer_menu.h @@ -172,7 +172,8 @@ QPointer ShowSendNowMessagesBox( void ToggleMessagePinned( not_null navigation, FullMsgId itemId, - bool pin); + bool pin, + Fn onHidden = Fn()); void HidePinnedBar( not_null navigation, not_null peer,