Choose send as in scheduled / discussions.

This commit is contained in:
John Preston 2021-11-09 21:04:42 +04:00
parent ad52c0cb16
commit 8c824edaa5
9 changed files with 62 additions and 18 deletions

View file

@ -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) {

View file

@ -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<Ui::SendAsButton>(
_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);

View file

@ -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<Ui::EmojiButton*> _tabbedSelectorToggle;
const not_null<Ui::InputField*> _field;
const not_null<Ui::IconButton*> _botCommandStart;
std::unique_ptr<Ui::SendAsButton> _sendAs;
std::unique_ptr<Ui::SilentToggle> _silent;
std::unique_ptr<Controls::TTLButton> _ttlInfo;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -24,6 +24,11 @@ SendAsPeers::SendAsPeers(not_null<Session*> session)
, _onlyMe({ session->user() }) {
}
bool SendAsPeers::shouldChoose(not_null<PeerData*> peer) {
refresh(peer);
return peer->canWrite() && (list(peer).size() > 1);
}
void SendAsPeers::refresh(not_null<PeerData*> peer) {
if (!peer->isMegagroup()) {
return;

View file

@ -17,6 +17,7 @@ class SendAsPeers final {
public:
explicit SendAsPeers(not_null<Session*> session);
bool shouldChoose(not_null<PeerData*> peer);
void refresh(not_null<PeerData*> peer);
[[nodiscard]] const std::vector<not_null<PeerData*>> &list(
not_null<PeerData*> peer) const;

View file

@ -148,14 +148,17 @@ void ChooseSendAsBox(
void SetupSendAsButton(
not_null<SendAsButton*> button,
rpl::producer<PeerData*> active,
not_null<Window::SessionController*> window) {
using namespace rpl::mappers;
const auto current = button->lifetime().make_state<
rpl::variable<PeerData*>
>(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<PeerData*> peer) {
auto userpic = current->value(
) | rpl::filter([=](PeerData *peer) {
return peer && peer->isMegagroup();
}) | rpl::map([=](not_null<PeerData*> 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<SendAsButton*> button,
not_null<Window::SessionController*> 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

View file

@ -26,6 +26,11 @@ void ChooseSendAsBox(
not_null<PeerData*> chosen,
Fn<void(not_null<PeerData*>)> done);
void SetupSendAsButton(
not_null<SendAsButton*> button,
rpl::producer<PeerData*> active,
not_null<Window::SessionController*> window);
void SetupSendAsButton(
not_null<SendAsButton*> button,
not_null<Window::SessionController*> window);