From 8c824edaa58ab0347866ea636fbffd34211f3008 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 9 Nov 2021 21:04:42 +0400 Subject: [PATCH] Choose send as in scheduled / discussions. --- .../SourceFiles/history/history_widget.cpp | 5 +-- .../history_view_compose_controls.cpp | 27 +++++++++++++++- .../controls/history_view_compose_controls.h | 3 ++ .../view/history_view_replies_section.cpp | 1 + .../view/history_view_scheduled_section.cpp | 1 + .../main/session/send_as_peers.cpp | 5 +++ .../SourceFiles/main/session/send_as_peers.h | 1 + .../SourceFiles/ui/chat/choose_send_as.cpp | 32 +++++++++++-------- Telegram/SourceFiles/ui/chat/choose_send_as.h | 5 +++ 9 files changed, 62 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 9caa842c2..2cbbf128c 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -2387,10 +2387,7 @@ void HistoryWidget::setupSendAsToggle() { void HistoryWidget::refreshSendAsToggle() { Expects(_peer != nullptr); - session().sendAsPeers().refresh(_peer); - const auto &list = session().sendAsPeers().list(_peer); - const auto has = _peer->canWrite() && (list.size() > 1); - if (!has) { + if (!session().sendAsPeers().shouldChoose(_peer)) { _sendAs.destroy(); return; } else if (_sendAs) { 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 c86ba7b1f..7068e2b8c 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -41,6 +41,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "inline_bots/inline_bot_result.h" #include "lang/lang_keys.h" #include "main/main_session.h" +#include "main/session/send_as_peers.h" #include "media/audio/media_audio_capture.h" #include "media/audio/media_audio.h" #include "styles/style_chat.h" @@ -50,6 +51,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/text/format_values.h" #include "ui/controls/emoji_button.h" #include "ui/controls/send_button.h" +#include "ui/controls/send_as_button.h" +#include "ui/chat/choose_send_as.h" #include "ui/special_buttons.h" #include "window/window_adaptive.h" #include "window/window_session_controller.h" @@ -675,6 +678,17 @@ void ComposeControls::setHistory(SetHistoryArgs &&args) { _wrap.get(), peer->asChannel()); } + if (!session().sendAsPeers().shouldChoose(peer)) { + _sendAs = nullptr; + } else if (!_sendAs) { + _sendAs = std::make_unique( + _wrap.get(), + st::sendAsButton); + Ui::SetupSendAsButton( + _sendAs.get(), + rpl::single(peer.get()), + _window); + } session().local().readDraftsWithCursors(_history); applyDraft(); } @@ -686,6 +700,12 @@ void ComposeControls::setCurrentDialogsEntryState(Dialogs::EntryState state) { } } +PeerData *ComposeControls::sendAsPeer() const { + return (_sendAs && _history) + ? session().sendAsPeers().resolveChosen(_history->peer).get() + : nullptr; +} + void ComposeControls::move(int x, int y) { _wrap->move(x, y); _writeRestricted->move(x, y); @@ -1802,11 +1822,12 @@ void ComposeControls::finishAnimating() { } void ComposeControls::updateControlsGeometry(QSize size) { - // _attachToggle -- _inlineResults ------ _tabbedPanel -- _fieldBarCancel + // _attachToggle (_sendAs) -- _inlineResults ------ _tabbedPanel -- _fieldBarCancel // (_attachDocument|_attachPhoto) _field (_ttlInfo) (_silent|_botCommandStart) _tabbedSelectorToggle _send const auto fieldWidth = size.width() - _attachToggle->width() + - (_sendAs ? _sendAs->width() : 0) - st::historySendRight - _send->width() - _tabbedSelectorToggle->width() @@ -1828,6 +1849,10 @@ void ComposeControls::updateControlsGeometry(QSize size) { auto left = st::historySendRight; _attachToggle->moveToLeft(left, buttonsTop); left += _attachToggle->width(); + if (_sendAs) { + _sendAs->moveToLeft(left, buttonsTop); + left += _sendAs->width(); + } _field->moveToLeft( left, size.height() - _field->height() - st::historySendPadding); diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h index 8cd5282e0..8cc73c8bd 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h @@ -49,6 +49,7 @@ namespace Ui { class SendButton; class IconButton; class EmojiButton; +class SendAsButton; class SilentToggle; } // namespace Ui @@ -102,6 +103,7 @@ public: [[nodiscard]] Main::Session &session() const; void setHistory(SetHistoryArgs &&args); void setCurrentDialogsEntryState(Dialogs::EntryState state); + [[nodiscard]] PeerData *sendAsPeer() const; void finishAnimating(); @@ -290,6 +292,7 @@ private: const not_null _tabbedSelectorToggle; const not_null _field; const not_null _botCommandStart; + std::unique_ptr _sendAs; std::unique_ptr _silent; std::unique_ptr _ttlInfo; diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp index 02474893a..e993acd31 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp @@ -916,6 +916,7 @@ Api::SendAction RepliesWidget::prepareSendAction( Api::SendOptions options) const { auto result = Api::SendAction(_history, options); result.replyTo = replyToId(); + result.options.sendAs = _composeControls->sendAsPeer(); return result; } diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp index 6230bd041..3aa1bc2c9 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp @@ -519,6 +519,7 @@ bool ScheduledWidget::showSendingFilesError( Api::SendAction ScheduledWidget::prepareSendAction( Api::SendOptions options) const { auto result = Api::SendAction(_history, options); + result.options.sendAs = _composeControls->sendAsPeer(); return result; } diff --git a/Telegram/SourceFiles/main/session/send_as_peers.cpp b/Telegram/SourceFiles/main/session/send_as_peers.cpp index 41ebdd2f3..35f75c8e6 100644 --- a/Telegram/SourceFiles/main/session/send_as_peers.cpp +++ b/Telegram/SourceFiles/main/session/send_as_peers.cpp @@ -24,6 +24,11 @@ SendAsPeers::SendAsPeers(not_null session) , _onlyMe({ session->user() }) { } +bool SendAsPeers::shouldChoose(not_null peer) { + refresh(peer); + return peer->canWrite() && (list(peer).size() > 1); +} + void SendAsPeers::refresh(not_null peer) { if (!peer->isMegagroup()) { return; diff --git a/Telegram/SourceFiles/main/session/send_as_peers.h b/Telegram/SourceFiles/main/session/send_as_peers.h index 46d2ad764..35a1db64e 100644 --- a/Telegram/SourceFiles/main/session/send_as_peers.h +++ b/Telegram/SourceFiles/main/session/send_as_peers.h @@ -17,6 +17,7 @@ class SendAsPeers final { public: explicit SendAsPeers(not_null session); + bool shouldChoose(not_null peer); void refresh(not_null peer); [[nodiscard]] const std::vector> &list( not_null peer) const; diff --git a/Telegram/SourceFiles/ui/chat/choose_send_as.cpp b/Telegram/SourceFiles/ui/chat/choose_send_as.cpp index 62b7af1f3..4b88a7a3f 100644 --- a/Telegram/SourceFiles/ui/chat/choose_send_as.cpp +++ b/Telegram/SourceFiles/ui/chat/choose_send_as.cpp @@ -148,14 +148,17 @@ void ChooseSendAsBox( void SetupSendAsButton( not_null button, + rpl::producer active, not_null window) { using namespace rpl::mappers; + const auto current = button->lifetime().make_state< + rpl::variable + >(std::move(active)); button->setClickedCallback([=] { - const auto history = window->activeChatCurrent().history(); - if (!history) { + const auto peer = current->current(); + if (!peer) { return; } - const auto peer = history->peer; const auto session = &peer->session(); const auto &list = session->sendAsPeers().list(peer); if (list.size() < 2) { @@ -171,16 +174,10 @@ void SetupSendAsButton( done)); }); - auto userpic = window->activeChatValue( - ) | rpl::map([=](const Dialogs::Key &key) -> PeerData* { - if (const auto history = key.history()) { - return history->peer->isMegagroup() - ? history->peer.get() - : nullptr; - } - return nullptr; - }) | rpl::filter_nullptr( - ) | rpl::map([=](not_null peer) { + auto userpic = current->value( + ) | rpl::filter([=](PeerData *peer) { + return peer && peer->isMegagroup(); + }) | rpl::map([=](not_null peer) { const auto channel = peer->asMegagroup(); auto updates = rpl::single( @@ -208,7 +205,16 @@ void SetupSendAsButton( ) | rpl::start_with_next([=](QImage &&userpic) { button->setUserpic(std::move(userpic)); }, button->lifetime()); +} +void SetupSendAsButton( + not_null button, + not_null window) { + auto active = window->activeChatValue( + ) | rpl::map([=](const Dialogs::Key &key) { + return key.history() ? key.history()->peer.get() : nullptr; + }); + SetupSendAsButton(button, std::move(active), window); } } // namespace Ui diff --git a/Telegram/SourceFiles/ui/chat/choose_send_as.h b/Telegram/SourceFiles/ui/chat/choose_send_as.h index eca82cc5b..1af733e1a 100644 --- a/Telegram/SourceFiles/ui/chat/choose_send_as.h +++ b/Telegram/SourceFiles/ui/chat/choose_send_as.h @@ -26,6 +26,11 @@ void ChooseSendAsBox( not_null chosen, Fn)> done); +void SetupSendAsButton( + not_null button, + rpl::producer active, + not_null window); + void SetupSendAsButton( not_null button, not_null window);