Removed App::wnd() from HistoryView::Message.

This commit is contained in:
23rd 2021-07-26 10:45:35 +03:00
parent ca83b8a8c6
commit 65aecf16a6

View file

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/ */
#include "history/view/history_view_message.h" #include "history/view/history_view_message.h"
#include "core/click_handler_types.h" // ClickHandlerContext
#include "history/view/history_view_cursor_state.h" #include "history/view/history_view_cursor_state.h"
#include "history/history_item_components.h" #include "history/history_item_components.h"
#include "history/history_message.h" #include "history/history_message.h"
@ -25,12 +26,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_channel.h" #include "data/data_channel.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "mainwindow.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "window/window_session_controller.h" #include "window/window_session_controller.h"
#include "apiwrap.h" #include "apiwrap.h"
#include "layout/layout_selection.h" #include "layout/layout_selection.h"
#include "facades.h"
#include "styles/style_widgets.h" #include "styles/style_widgets.h"
#include "styles/style_chat.h" #include "styles/style_chat.h"
#include "styles/style_dialogs.h" #include "styles/style_dialogs.h"
@ -40,6 +40,15 @@ namespace {
const auto kPsaTooltipPrefix = "cloud_lng_tooltip_psa_"; const auto kPsaTooltipPrefix = "cloud_lng_tooltip_psa_";
std::optional<Window::SessionController*> ExtractController(
const ClickContext &context) {
const auto my = context.other.value<ClickHandlerContext>();
if (const auto controller = my.sessionWindow.get()) {
return controller;
}
return std::nullopt;
}
class KeyboardStyle : public ReplyKeyboard::Style { class KeyboardStyle : public ReplyKeyboard::Style {
public: public:
using ReplyKeyboard::Style::Style; using ReplyKeyboard::Style::Style;
@ -1380,9 +1389,15 @@ bool Message::getStateCommentsButton(
ClickHandlerPtr Message::createGoToCommentsLink() const { ClickHandlerPtr Message::createGoToCommentsLink() const {
const auto fullId = data()->fullId(); const auto fullId = data()->fullId();
return std::make_shared<LambdaClickHandler>([=] { const auto sessionId = data()->history()->session().uniqueId();
if (const auto window = App::wnd()) { return std::make_shared<LambdaClickHandler>([=](ClickContext context) {
if (const auto controller = window->sessionController()) { const auto controller = ExtractController(context).value_or(nullptr);
if (!controller) {
return;
}
if (controller->session().uniqueId() != sessionId) {
return;
}
if (const auto item = controller->session().data().message(fullId)) { if (const auto item = controller->session().data().message(fullId)) {
const auto history = item->history(); const auto history = item->history();
if (const auto channel = history->peer->asChannel()) { if (const auto channel = history->peer->asChannel()) {
@ -1394,8 +1409,6 @@ ClickHandlerPtr Message::createGoToCommentsLink() const {
} }
controller->showRepliesForMessage(history, item->id); controller->showRepliesForMessage(history, item->id);
} }
}
}
}); });
} }
@ -2318,7 +2331,9 @@ void Message::drawRightAction(
} }
ClickHandlerPtr Message::rightActionLink() const { ClickHandlerPtr Message::rightActionLink() const {
if (!_rightActionLink) { if (_rightActionLink) {
return _rightActionLink;
}
if (isPinnedContext()) { if (isPinnedContext()) {
_rightActionLink = goToMessageClickHandler(data()); _rightActionLink = goToMessageClickHandler(data());
return _rightActionLink; return _rightActionLink;
@ -2326,18 +2341,24 @@ ClickHandlerPtr Message::rightActionLink() const {
_rightActionLink = createGoToCommentsLink(); _rightActionLink = createGoToCommentsLink();
return _rightActionLink; return _rightActionLink;
} }
const auto sessionId = data()->history()->session().uniqueId();
const auto owner = &data()->history()->owner(); const auto owner = &data()->history()->owner();
const auto itemId = data()->fullId(); const auto itemId = data()->fullId();
const auto forwarded = data()->Get<HistoryMessageForwarded>(); const auto forwarded = data()->Get<HistoryMessageForwarded>();
const auto savedFromPeer = forwarded ? forwarded->savedFromPeer : nullptr; const auto savedFromPeer = forwarded ? forwarded->savedFromPeer : nullptr;
const auto savedFromMsgId = forwarded ? forwarded->savedFromMsgId : 0; const auto savedFromMsgId = forwarded ? forwarded->savedFromMsgId : 0;
const auto showByThread = std::make_shared<FnMut<void()>>();
const auto showByThreadWeak = std::weak_ptr<FnMut<void()>>(showByThread); using Callback = FnMut<void(not_null<Window::SessionController*>)>;
const auto showByThread = std::make_shared<Callback>();
const auto showByThreadWeak = std::weak_ptr<Callback>(showByThread);
if (data()->externalReply()) { if (data()->externalReply()) {
*showByThread = [=, requested = 0]() mutable { *showByThread = [=, requested = 0](
const auto original = savedFromPeer->owner().message(savedFromPeer->asChannel(), savedFromMsgId); not_null<Window::SessionController*> controller) mutable {
const auto original = savedFromPeer->owner().message(
savedFromPeer->asChannel(),
savedFromMsgId);
if (original && original->replyToTop()) { if (original && original->replyToTop()) {
App::wnd()->sessionController()->showRepliesForMessage( controller->showRepliesForMessage(
original->history(), original->history(),
original->replyToTop(), original->replyToTop(),
original->id, original->id,
@ -2346,26 +2367,41 @@ ClickHandlerPtr Message::rightActionLink() const {
const auto channel = savedFromPeer->asChannel(); const auto channel = savedFromPeer->asChannel();
const auto prequested = &requested; const auto prequested = &requested;
requested = 1; requested = 1;
channel->session().api().requestMessageData(channel, savedFromMsgId, [=](ChannelData *gotChannel, MsgId gotId) { channel->session().api().requestMessageData(
channel,
savedFromMsgId,
[=, weak = base::make_weak(controller.get())](
ChannelData *gotChannel, MsgId gotId) {
if (const auto strong = showByThreadWeak.lock()) { if (const auto strong = showByThreadWeak.lock()) {
if (const auto strongController = weak.get()) {
*prequested = 2; *prequested = 2;
(*strong)(); (*strong)(strongController);
}
} }
}); });
} else if (requested == 2) { } else if (requested == 2) {
App::wnd()->sessionController()->showPeerHistory( controller->showPeerHistory(
savedFromPeer, savedFromPeer,
Window::SectionShow::Way::Forward, Window::SectionShow::Way::Forward,
savedFromMsgId); savedFromMsgId);
} }
}; };
}; };
_rightActionLink = std::make_shared<LambdaClickHandler>([=] { _rightActionLink = std::make_shared<LambdaClickHandler>([=](
ClickContext context) {
const auto controller = ExtractController(context).value_or(nullptr);
if (!controller) {
return;
}
if (controller->session().uniqueId() != sessionId) {
return;
}
if (const auto item = owner->message(itemId)) { if (const auto item = owner->message(itemId)) {
if (*showByThread) { if (*showByThread) {
(*showByThread)(); (*showByThread)(controller);
} else if (savedFromPeer && savedFromMsgId) { } else if (savedFromPeer && savedFromMsgId) {
App::wnd()->sessionController()->showPeerHistory( controller->showPeerHistory(
savedFromPeer, savedFromPeer,
Window::SectionShow::Way::Forward, Window::SectionShow::Way::Forward,
savedFromMsgId); savedFromMsgId);
@ -2374,24 +2410,29 @@ ClickHandlerPtr Message::rightActionLink() const {
} }
} }
}); });
}
return _rightActionLink; return _rightActionLink;
} }
ClickHandlerPtr Message::fastReplyLink() const { ClickHandlerPtr Message::fastReplyLink() const {
if (!_fastReplyLink) { if (_fastReplyLink) {
return _fastReplyLink;
}
const auto owner = &data()->history()->owner(); const auto owner = &data()->history()->owner();
const auto itemId = data()->fullId(); const auto itemId = data()->fullId();
_fastReplyLink = std::make_shared<LambdaClickHandler>([=] { _fastReplyLink = std::make_shared<LambdaClickHandler>([=](
ClickContext context) {
const auto controller = ExtractController(context).value_or(nullptr);
if (!controller) {
return;
}
if (const auto item = owner->message(itemId)) { if (const auto item = owner->message(itemId)) {
if (const auto main = App::main()) { // multi good if (const auto main = controller->content()) {
if (&main->session() == &owner->session()) { if (&main->session() == &owner->session()) {
main->replyToItem(item); main->replyToItem(item);
} }
} }
} }
}); });
}
return _fastReplyLink; return _fastReplyLink;
} }