From d4fe5f7a83bc75786f6b6886203cd5d09ff419cf Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 18 Oct 2021 23:37:25 +0300 Subject: [PATCH] Extracted PinMessageBox to separated file. --- Telegram/CMakeLists.txt | 2 + Telegram/SourceFiles/boxes/confirm_box.cpp | 101 -------------- Telegram/SourceFiles/boxes/confirm_box.h | 27 ---- .../SourceFiles/boxes/pin_messages_box.cpp | 129 ++++++++++++++++++ Telegram/SourceFiles/boxes/pin_messages_box.h | 43 ++++++ .../SourceFiles/window/window_peer_menu.cpp | 1 + 6 files changed, 175 insertions(+), 128 deletions(-) create mode 100644 Telegram/SourceFiles/boxes/pin_messages_box.cpp create mode 100644 Telegram/SourceFiles/boxes/pin_messages_box.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 55f6326e04..5ac0480057 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -227,6 +227,8 @@ PRIVATE boxes/passcode_box.h boxes/phone_banned_box.cpp boxes/phone_banned_box.h + boxes/pin_messages_box.cpp + boxes/pin_messages_box.h boxes/rate_call_box.cpp boxes/rate_call_box.h boxes/self_destruction_box.cpp diff --git a/Telegram/SourceFiles/boxes/confirm_box.cpp b/Telegram/SourceFiles/boxes/confirm_box.cpp index 97b6498208..b945d5d239 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_box.cpp @@ -71,21 +71,6 @@ TextParseOptions kMarkedTextBoxOptions = { Qt::LayoutDirectionAuto, // dir }; -[[nodiscard]] bool IsOldForPin(MsgId id, not_null peer) { - const auto normal = peer->migrateToOrMe(); - const auto migrated = normal->migrateFrom(); - const auto top = Data::ResolveTopPinnedId(normal, migrated); - if (!top) { - return false; - } else if (peer == migrated) { - return top.channel || (id < top.msg); - } else if (migrated) { - return top.channel && (id < top.msg); - } else { - return (id < top.msg); - } -} - } // namespace ConfirmBox::ConfirmBox( @@ -456,92 +441,6 @@ void MaxInviteBox::resizeEvent(QResizeEvent *e) { _invitationLink = myrtlrect(st::boxPadding.left(), st::boxPadding.top() + _textHeight + st::boxTextFont->height, width() - st::boxPadding.left() - st::boxPadding.right(), 2 * st::boxTextFont->height); } -PinMessageBox::PinMessageBox( - QWidget*, - not_null peer, - MsgId msgId) -: _peer(peer) -, _api(&peer->session().mtp()) -, _msgId(msgId) -, _pinningOld(IsOldForPin(msgId, peer)) -, _text( - this, - (_pinningOld - ? tr::lng_pinned_pin_old_sure(tr::now) - : (peer->isChat() || peer->isMegagroup()) - ? tr::lng_pinned_pin_sure_group(tr::now) - : tr::lng_pinned_pin_sure(tr::now)), - st::boxLabel) { -} - -void PinMessageBox::prepare() { - addButton(tr::lng_pinned_pin(), [this] { pinMessage(); }); - addButton(tr::lng_cancel(), [this] { closeBox(); }); - - if (_peer->isUser() && !_peer->isSelf()) { - _pinForPeer.create( - this, - tr::lng_pinned_also_for_other( - tr::now, - lt_user, - _peer->shortName()), - false, - st::defaultBoxCheckbox); - _checkbox = _pinForPeer; - } else if (!_pinningOld && (_peer->isChat() || _peer->isMegagroup())) { - _notify.create( - this, - tr::lng_pinned_notify(tr::now), - true, - st::defaultBoxCheckbox); - _checkbox = _notify; - } - - auto height = st::boxPadding.top() + _text->height() + st::boxPadding.bottom(); - if (_checkbox) { - height += st::boxMediumSkip + _checkbox->heightNoMargins(); - } - setDimensions(st::boxWidth, height); -} - -void PinMessageBox::resizeEvent(QResizeEvent *e) { - BoxContent::resizeEvent(e); - _text->moveToLeft(st::boxPadding.left(), st::boxPadding.top()); - if (_checkbox) { - _checkbox->moveToLeft(st::boxPadding.left(), _text->y() + _text->height() + st::boxMediumSkip); - } -} - -void PinMessageBox::keyPressEvent(QKeyEvent *e) { - if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { - pinMessage(); - } else { - BoxContent::keyPressEvent(e); - } -} - -void PinMessageBox::pinMessage() { - if (_requestId) return; - - auto flags = MTPmessages_UpdatePinnedMessage::Flags(0); - if (_notify && !_notify->checked()) { - flags |= MTPmessages_UpdatePinnedMessage::Flag::f_silent; - } - if (_pinForPeer && !_pinForPeer->checked()) { - flags |= MTPmessages_UpdatePinnedMessage::Flag::f_pm_oneside; - } - _requestId = _api.request(MTPmessages_UpdatePinnedMessage( - MTP_flags(flags), - _peer->input, - MTP_int(_msgId) - )).done([=](const MTPUpdates &result) { - _peer->session().api().applyUpdates(result); - Ui::hideLayer(); - }).fail([=](const MTP::Error &error) { - Ui::hideLayer(); - }).send(); -} - DeleteMessagesBox::DeleteMessagesBox( QWidget*, not_null item, diff --git a/Telegram/SourceFiles/boxes/confirm_box.h b/Telegram/SourceFiles/boxes/confirm_box.h index ca0a2cbe22..dbf53901f7 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.h +++ b/Telegram/SourceFiles/boxes/confirm_box.h @@ -169,33 +169,6 @@ private: }; -class PinMessageBox final : public Ui::BoxContent { -public: - PinMessageBox(QWidget*, not_null peer, MsgId msgId); - -protected: - void prepare() override; - - void resizeEvent(QResizeEvent *e) override; - void keyPressEvent(QKeyEvent *e) override; - -private: - void pinMessage(); - - const not_null _peer; - MTP::Sender _api; - MsgId _msgId = 0; - bool _pinningOld = false; - - object_ptr _text; - object_ptr _notify = { nullptr }; - object_ptr _pinForPeer = { nullptr }; - QPointer _checkbox; - - mtpRequestId _requestId = 0; - -}; - class DeleteMessagesBox final : public Ui::BoxContent { public: DeleteMessagesBox( diff --git a/Telegram/SourceFiles/boxes/pin_messages_box.cpp b/Telegram/SourceFiles/boxes/pin_messages_box.cpp new file mode 100644 index 0000000000..3b9718cebe --- /dev/null +++ b/Telegram/SourceFiles/boxes/pin_messages_box.cpp @@ -0,0 +1,129 @@ +/* +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 "boxes/pin_messages_box.h" + +#include "apiwrap.h" +#include "data/data_chat.h" +#include "data/data_user.h" +#include "lang/lang_keys.h" +#include "main/main_session.h" +#include "ui/widgets/checkbox.h" +#include "ui/widgets/labels.h" +#include "styles/style_layers.h" +#include "styles/style_boxes.h" + +namespace { + +[[nodiscard]] bool IsOldForPin(MsgId id, not_null peer) { + const auto normal = peer->migrateToOrMe(); + const auto migrated = normal->migrateFrom(); + const auto top = Data::ResolveTopPinnedId(normal, migrated); + if (!top) { + return false; + } else if (peer == migrated) { + return top.channel || (id < top.msg); + } else if (migrated) { + return top.channel && (id < top.msg); + } else { + return (id < top.msg); + } +} + +} // namespace + +PinMessageBox::PinMessageBox( + QWidget*, + not_null peer, + MsgId msgId) +: _peer(peer) +, _api(&peer->session().mtp()) +, _msgId(msgId) +, _pinningOld(IsOldForPin(msgId, peer)) +, _text( + this, + (_pinningOld + ? tr::lng_pinned_pin_old_sure(tr::now) + : (peer->isChat() || peer->isMegagroup()) + ? tr::lng_pinned_pin_sure_group(tr::now) + : tr::lng_pinned_pin_sure(tr::now)), + st::boxLabel) { +} + +void PinMessageBox::prepare() { + addButton(tr::lng_pinned_pin(), [this] { pinMessage(); }); + addButton(tr::lng_cancel(), [this] { closeBox(); }); + + if (_peer->isUser() && !_peer->isSelf()) { + _pinForPeer.create( + this, + tr::lng_pinned_also_for_other( + tr::now, + lt_user, + _peer->shortName()), + false, + st::defaultBoxCheckbox); + _checkbox = _pinForPeer; + } else if (!_pinningOld && (_peer->isChat() || _peer->isMegagroup())) { + _notify.create( + this, + tr::lng_pinned_notify(tr::now), + true, + st::defaultBoxCheckbox); + _checkbox = _notify; + } + + auto height = st::boxPadding.top() + + _text->height() + + st::boxPadding.bottom(); + if (_checkbox) { + height += st::boxMediumSkip + _checkbox->heightNoMargins(); + } + setDimensions(st::boxWidth, height); +} + +void PinMessageBox::resizeEvent(QResizeEvent *e) { + BoxContent::resizeEvent(e); + _text->moveToLeft(st::boxPadding.left(), st::boxPadding.top()); + if (_checkbox) { + _checkbox->moveToLeft( + st::boxPadding.left(), + _text->y() + _text->height() + st::boxMediumSkip); + } +} + +void PinMessageBox::keyPressEvent(QKeyEvent *e) { + if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) { + pinMessage(); + } else { + BoxContent::keyPressEvent(e); + } +} + +void PinMessageBox::pinMessage() { + if (_requestId) { + return; + } + + auto flags = MTPmessages_UpdatePinnedMessage::Flags(0); + if (_notify && !_notify->checked()) { + flags |= MTPmessages_UpdatePinnedMessage::Flag::f_silent; + } + if (_pinForPeer && !_pinForPeer->checked()) { + flags |= MTPmessages_UpdatePinnedMessage::Flag::f_pm_oneside; + } + _requestId = _api.request(MTPmessages_UpdatePinnedMessage( + MTP_flags(flags), + _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/boxes/pin_messages_box.h b/Telegram/SourceFiles/boxes/pin_messages_box.h new file mode 100644 index 0000000000..dc99431909 --- /dev/null +++ b/Telegram/SourceFiles/boxes/pin_messages_box.h @@ -0,0 +1,43 @@ +/* +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 + +#include "boxes/abstract_box.h" +#include "mtproto/sender.h" + +namespace Ui { +class Checkbox; +class FlatLabel; +} // namespace Ui + +class PinMessageBox final : public Ui::BoxContent { +public: + PinMessageBox(QWidget*, not_null peer, MsgId msgId); + +protected: + void prepare() override; + + void resizeEvent(QResizeEvent *e) override; + void keyPressEvent(QKeyEvent *e) override; + +private: + void pinMessage(); + + const not_null _peer; + MTP::Sender _api; + MsgId _msgId = 0; + bool _pinningOld = false; + + object_ptr _text; + object_ptr _notify = { nullptr }; + object_ptr _pinForPeer = { nullptr }; + QPointer _checkbox; + + mtpRequestId _requestId = 0; + +}; diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 054023f80e..236d2179b3 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/mute_settings_box.h" #include "boxes/add_contact_box.h" #include "boxes/create_poll_box.h" +#include "boxes/pin_messages_box.h" #include "boxes/peers/add_participants_box.h" #include "boxes/peers/edit_contact_box.h" #include "ui/boxes/report_box.h"