From fc6aa288c2785570ffd6218bab4f638f14806b04 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 3 Jul 2017 16:57:46 +0300 Subject: [PATCH] Show all returned rows in RevokePublicLinkBox. Fixes #3617 --- .../SourceFiles/boxes/add_contact_box.cpp | 154 ++++++++++-------- Telegram/SourceFiles/boxes/add_contact_box.h | 34 +--- Telegram/SourceFiles/boxes/peer_list_box.h | 3 +- 3 files changed, 95 insertions(+), 96 deletions(-) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index a6fa8592ad..131288c383 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "styles/style_dialogs.h" #include "lang/lang_keys.h" #include "messenger.h" +#include "mtproto/sender.h" #include "boxes/contacts_box.h" #include "boxes/confirm_box.h" #include "boxes/photo_crop_box.h" @@ -40,6 +41,42 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "observer_peer.h" #include "auth_session.h" +class RevokePublicLinkBox::Inner : public TWidget, private MTP::Sender { +public: + Inner(QWidget *parent, base::lambda revokeCallback); + +protected: + void mouseMoveEvent(QMouseEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + void mouseReleaseEvent(QMouseEvent *e) override; + void paintEvent(QPaintEvent *e) override; + +private: + struct ChatRow { + ChatRow(gsl::not_null peer) : peer(peer) { + } + + gsl::not_null peer; + Text name, status; + }; + void paintChat(Painter &p, const ChatRow &row, bool selected) const; + void updateSelected(); + + PeerData *_selected = nullptr; + PeerData *_pressed = nullptr; + + std::vector _rows; + + int _rowsTop = 0; + int _rowHeight = 0; + int _revokeWidth = 0; + + base::lambda _revokeCallback; + mtpRequestId _revokeRequestId = 0; + QPointer _weakRevokeConfirmBox; + +}; + AddContactBox::AddContactBox(QWidget*, QString fname, QString lname, QString phone) : _first(this, st::defaultInputField, langFactory(lng_signup_firstname), fname) , _last(this, st::defaultInputField, langFactory(lng_signup_lastname), lname) @@ -1153,35 +1190,62 @@ void EditChannelBox::onSaveInvitesDone(const MTPUpdates &result) { closeBox(); } -RevokePublicLinkBox::RevokePublicLinkBox(QWidget*, base::lambda revokeCallback) -: _rowHeight(st::contactsPadding.top() + st::contactsPhotoSize + st::contactsPadding.bottom()) +RevokePublicLinkBox::Inner::Inner(QWidget *parent, base::lambda revokeCallback) : TWidget(parent) +, _rowHeight(st::contactsPadding.top() + st::contactsPhotoSize + st::contactsPadding.bottom()) , _revokeWidth(st::normalFont->width(lang(lng_channels_too_much_public_revoke))) -, _aboutRevoke(this, lang(lng_channels_too_much_public_about), Ui::FlatLabel::InitType::Simple, st::aboutRevokePublicLabel) +, _revokeCallback(std::move(revokeCallback)) { + setMouseTracking(true); + + resize(width(), 5 * _rowHeight); + + request(MTPchannels_GetAdminedPublicChannels()).done([this](const MTPmessages_Chats &result) { + if (auto chats = Api::getChatsFromMessagesChats(result)) { + for_const (auto &chat, chats->v) { + if (auto peer = App::feedChat(chat)) { + if (!peer->isChannel() || peer->userName().isEmpty()) { + continue; + } + + auto row = ChatRow(peer); + row.peer = peer; + row.name.setText(st::contactsNameStyle, peer->name, _textNameOptions); + row.status.setText(st::defaultTextStyle, Messenger::Instance().createInternalLink(textcmdLink(1, peer->userName())), _textDlgOptions); + _rows.push_back(std::move(row)); + } + } + } + resize(width(), _rows.size() * _rowHeight); + update(); + }).send(); +} + +RevokePublicLinkBox::RevokePublicLinkBox(QWidget*, base::lambda revokeCallback) +: _aboutRevoke(this, lang(lng_channels_too_much_public_about), Ui::FlatLabel::InitType::Simple, st::aboutRevokePublicLabel) , _revokeCallback(std::move(revokeCallback)) { } void RevokePublicLinkBox::prepare() { - setMouseTracking(true); - - MTP::send(MTPchannels_GetAdminedPublicChannels(), rpcDone(&RevokePublicLinkBox::getPublicDone), rpcFail(&RevokePublicLinkBox::getPublicFail)); + _innerTop = st::boxPadding.top() + _aboutRevoke->height() + st::boxPadding.top(); + _inner = setInnerWidget(object_ptr(this, [this] { + closeBox(); + if (_revokeCallback) { + _revokeCallback(); + } + }), st::boxLayerScroll, _innerTop); addButton(langFactory(lng_cancel), [this] { closeBox(); }); subscribe(AuthSession::CurrentDownloaderTaskFinished(), [this] { update(); }); - updateMaxHeight(); + _inner->resizeToWidth(st::boxWideWidth); + setDimensions(st::boxWideWidth, _innerTop + _inner->height()); } -void RevokePublicLinkBox::updateMaxHeight() { - _rowsTop = st::boxPadding.top() + _aboutRevoke->height() + st::boxPadding.top(); - setDimensions(st::boxWideWidth, _rowsTop + (5 * _rowHeight)); -} - -void RevokePublicLinkBox::mouseMoveEvent(QMouseEvent *e) { +void RevokePublicLinkBox::Inner::mouseMoveEvent(QMouseEvent *e) { updateSelected(); } -void RevokePublicLinkBox::updateSelected() { +void RevokePublicLinkBox::Inner::updateSelected() { auto point = mapFromGlobal(QCursor::pos()); PeerData *selected = nullptr; auto top = _rowsTop; @@ -1200,14 +1264,14 @@ void RevokePublicLinkBox::updateSelected() { } } -void RevokePublicLinkBox::mousePressEvent(QMouseEvent *e) { +void RevokePublicLinkBox::Inner::mousePressEvent(QMouseEvent *e) { if (_pressed != _selected) { _pressed = _selected; update(); } } -void RevokePublicLinkBox::mouseReleaseEvent(QMouseEvent *e) { +void RevokePublicLinkBox::Inner::mouseReleaseEvent(QMouseEvent *e) { auto pressed = base::take(_pressed); setCursor((_selected || _pressed) ? style::cur_pointer : style::cur_default); if (pressed && pressed == _selected) { @@ -1216,14 +1280,19 @@ void RevokePublicLinkBox::mouseReleaseEvent(QMouseEvent *e) { auto confirmText = lang(lng_channels_too_much_public_revoke); _weakRevokeConfirmBox = Ui::show(Box(text, confirmText, base::lambda_guarded(this, [this, pressed]() { if (_revokeRequestId) return; - _revokeRequestId = MTP::send(MTPchannels_UpdateUsername(pressed->asChannel()->inputChannel, MTP_string("")), rpcDone(&RevokePublicLinkBox::revokeLinkDone), rpcFail(&RevokePublicLinkBox::revokeLinkFail)); + _revokeRequestId = request(MTPchannels_UpdateUsername(pressed->asChannel()->inputChannel, MTP_string(""))).done([this](const MTPBool &result) { + if (_weakRevokeConfirmBox) { + _weakRevokeConfirmBox->closeBox(); + } + if (_revokeCallback) { + _revokeCallback(); + } + }).send(); })), KeepOtherLayers); } } -void RevokePublicLinkBox::paintEvent(QPaintEvent *e) { - BoxContent::paintEvent(e); - +void RevokePublicLinkBox::Inner::paintEvent(QPaintEvent *e) { Painter p(this); p.translate(0, _rowsTop); for_const (auto &row, _rows) { @@ -1238,7 +1307,7 @@ void RevokePublicLinkBox::resizeEvent(QResizeEvent *e) { _aboutRevoke->moveToLeft(st::boxPadding.left(), st::boxPadding.top()); } -void RevokePublicLinkBox::paintChat(Painter &p, const ChatRow &row, bool selected) const { +void RevokePublicLinkBox::Inner::paintChat(Painter &p, const ChatRow &row, bool selected) const { auto peer = row.peer; peer->paintUserpicLeft(p, st::contactsPadding.left(), st::contactsPadding.top(), width(), st::contactsPhotoSize); @@ -1262,46 +1331,3 @@ void RevokePublicLinkBox::paintChat(Painter &p, const ChatRow &row, bool selecte row.status.drawLeftElided(p, namex, st::contactsPadding.top() + st::contactsStatusTop, namew, width()); p.restoreTextPalette(); } - -void RevokePublicLinkBox::getPublicDone(const MTPmessages_Chats &result) { - if (auto chats = Api::getChatsFromMessagesChats(result)) { - for_const (auto &chat, chats->v) { - if (auto peer = App::feedChat(chat)) { - if (!peer->isChannel() || peer->userName().isEmpty()) continue; - - ChatRow row; - row.peer = peer; - row.name.setText(st::contactsNameStyle, peer->name, _textNameOptions); - row.status.setText(st::defaultTextStyle, Messenger::Instance().createInternalLink(textcmdLink(1, peer->userName())), _textDlgOptions); - _rows.push_back(std::move(row)); - } - } - } - update(); -} - -bool RevokePublicLinkBox::getPublicFail(const RPCError &error) { - if (MTP::isDefaultHandledError(error)) { - return false; - } - - return true; -} - -void RevokePublicLinkBox::revokeLinkDone(const MTPBool &result) { - if (_weakRevokeConfirmBox) { - _weakRevokeConfirmBox->closeBox(); - } - closeBox(); - if (_revokeCallback) { - _revokeCallback(); - } -} - -bool RevokePublicLinkBox::revokeLinkFail(const RPCError &error) { - if (MTP::isDefaultHandledError(error)) { - return false; - } - - return true; -} diff --git a/Telegram/SourceFiles/boxes/add_contact_box.h b/Telegram/SourceFiles/boxes/add_contact_box.h index 67665b0cb4..d11ecadaee 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.h +++ b/Telegram/SourceFiles/boxes/add_contact_box.h @@ -301,41 +301,15 @@ public: protected: void prepare() override; - void mouseMoveEvent(QMouseEvent *e) override; - void mousePressEvent(QMouseEvent *e) override; - void mouseReleaseEvent(QMouseEvent *e) override; - void paintEvent(QPaintEvent *e) override; void resizeEvent(QResizeEvent *e) override; private: - void updateMaxHeight(); - void updateSelected(); - - struct ChatRow { - PeerData *peer; - Text name, status; - }; - void paintChat(Painter &p, const ChatRow &row, bool selected) const; - - void getPublicDone(const MTPmessages_Chats &result); - bool getPublicFail(const RPCError &error); - - void revokeLinkDone(const MTPBool &result); - bool revokeLinkFail(const RPCError &error); - - PeerData *_selected = nullptr; - PeerData *_pressed = nullptr; - - QVector _rows; - - int _rowsTop = 0; - int _rowHeight = 0; - int _revokeWidth = 0; - object_ptr _aboutRevoke; + class Inner; + QPointer _inner; + + int _innerTop = 0; base::lambda _revokeCallback; - mtpRequestId _revokeRequestId = 0; - QPointer _weakRevokeConfirmBox; }; diff --git a/Telegram/SourceFiles/boxes/peer_list_box.h b/Telegram/SourceFiles/boxes/peer_list_box.h index 6a65023aea..a9e7153e7e 100644 --- a/Telegram/SourceFiles/boxes/peer_list_box.h +++ b/Telegram/SourceFiles/boxes/peer_list_box.h @@ -302,8 +302,6 @@ private: }; class PeerListBox : public BoxContent, public PeerListDelegate { - class Inner; - public: PeerListBox(QWidget*, std::unique_ptr controller, base::lambda init); @@ -353,6 +351,7 @@ private: object_ptr> _select = { nullptr }; + class Inner; QPointer _inner; std::unique_ptr _controller;