Attempt to fix a couple of crashes.

This commit is contained in:
John Preston 2024-04-24 15:34:07 +04:00
parent 493f1d69e2
commit f675a8dcf7
6 changed files with 51 additions and 42 deletions

View file

@ -1527,14 +1527,14 @@ void Application::closeChatFromWindows(not_null<PeerData*> peer) {
} }
} }
if (const auto window = windowFor(&peer->account())) { if (const auto window = windowFor(&peer->account())) {
const auto primary = window->sessionController(); if (const auto primary = window->sessionController()) {
if ((primary->activeChatCurrent().peer() == peer) if (primary->activeChatCurrent().peer() == peer) {
&& (&primary->session() == &peer->session())) { primary->clearSectionStack();
primary->clearSectionStack(); }
} if (const auto forum = primary->shownForum().current()) {
if (const auto forum = primary->shownForum().current()) { if (peer->forum() == forum) {
if (peer->forum() == forum) { primary->closeForum();
primary->closeForum(); }
} }
} }
} }

View file

@ -810,7 +810,7 @@ HistoryWidget::HistoryWidget(
const auto account = &_peer->account(); const auto account = &_peer->account();
closeCurrent(); closeCurrent();
if (const auto primary = Core::App().windowFor(account)) { if (const auto primary = Core::App().windowFor(account)) {
controller->showToast(unavailable); primary->showToast(unavailable);
} }
return; return;
} }
@ -3316,7 +3316,7 @@ void HistoryWidget::messagesFailed(const MTP::Error &error, int requestId) {
auto was = _peer; auto was = _peer;
closeCurrent(); closeCurrent();
if (const auto primary = Core::App().windowFor(&was->account())) { if (const auto primary = Core::App().windowFor(&was->account())) {
controller()->showToast((was && was->isMegagroup()) primary->showToast((was && was->isMegagroup())
? tr::lng_group_not_accessible(tr::now) ? tr::lng_group_not_accessible(tr::now)
: tr::lng_channel_not_accessible(tr::now)); : tr::lng_channel_not_accessible(tr::now));
} }

View file

@ -980,20 +980,16 @@ void Instance::processOpenChannel(const QString &context) {
} else if (const auto channelId = ChannelId(context.toLongLong())) { } else if (const auto channelId = ChannelId(context.toLongLong())) {
const auto channel = _shownSession->data().channel(channelId); const auto channel = _shownSession->data().channel(channelId);
if (channel->isLoaded()) { if (channel->isLoaded()) {
if (const auto window = Core::App().windowFor(channel)) { if (const auto controller = _shownSession->tryResolveWindow(channel)) {
if (const auto controller = window->sessionController()) { controller->showPeerHistory(channel);
controller->showPeerHistory(channel); _shown = nullptr;
_shown = nullptr;
}
} }
} else if (!channel->username().isEmpty()) { } else if (!channel->username().isEmpty()) {
if (const auto window = Core::App().windowFor(channel)) { if (const auto controller = _shownSession->tryResolveWindow(channel)) {
if (const auto controller = window->sessionController()) { controller->showPeerByLink({
controller->showPeerByLink({ .usernameOrId = channel->username(),
.usernameOrId = channel->username(), });
}); _shown = nullptr;
_shown = nullptr;
}
} }
} }
} }
@ -1008,13 +1004,11 @@ void Instance::processJoinChannel(const QString &context) {
if (channel->isLoaded()) { if (channel->isLoaded()) {
_shownSession->api().joinChannel(channel); _shownSession->api().joinChannel(channel);
} else if (!channel->username().isEmpty()) { } else if (!channel->username().isEmpty()) {
if (const auto window = Core::App().windowFor(channel)) { if (const auto controller = _shownSession->tryResolveWindow(channel)) {
if (const auto controller = window->sessionController()) { controller->showPeerByLink({
controller->showPeerByLink({ .usernameOrId = channel->username(),
.usernameOrId = channel->username(), .joinChannel = true,
.joinChannel = true, });
});
}
} }
} }
} }

View file

@ -434,6 +434,10 @@ void Session::uploadsStopWithConfirmation(Fn<void()> done) {
const auto window = message const auto window = message
? Core::App().windowFor(message->history()->peer) ? Core::App().windowFor(message->history()->peer)
: Core::App().activePrimaryWindow(); : Core::App().activePrimaryWindow();
if (!window) {
done();
return;
}
auto box = Box([=](not_null<Ui::GenericBox*> box) { auto box = Box([=](not_null<Ui::GenericBox*> box) {
box->addRow( box->addRow(
object_ptr<Ui::FlatLabel>( object_ptr<Ui::FlatLabel>(
@ -476,8 +480,22 @@ auto Session::windows() const
return _windows; return _windows;
} }
Window::SessionController *Session::tryResolveWindow() const { Window::SessionController *Session::tryResolveWindow(
if (_windows.empty()) { PeerData *forPeer) const {
if (forPeer) {
auto primary = (Window::SessionController*)nullptr;
for (const auto &window : _windows) {
if (window->singlePeer() == forPeer) {
return window;
} else if (window->isPrimary()) {
primary = window;
}
}
if (primary) {
return primary;
}
}
if (_windows.empty() || forPeer) {
domain().activate(_account); domain().activate(_account);
if (_windows.empty()) { if (_windows.empty()) {
return nullptr; return nullptr;

View file

@ -164,7 +164,8 @@ public:
void addWindow(not_null<Window::SessionController*> controller); void addWindow(not_null<Window::SessionController*> controller);
[[nodiscard]] auto windows() const [[nodiscard]] auto windows() const
-> const base::flat_set<not_null<Window::SessionController*>> &; -> const base::flat_set<not_null<Window::SessionController*>> &;
[[nodiscard]] Window::SessionController *tryResolveWindow() const; [[nodiscard]] Window::SessionController *tryResolveWindow(
PeerData *forPeer = nullptr) const;
// Shortcuts. // Shortcuts.
void notifyDownloaderTaskFinished(); void notifyDownloaderTaskFinished();

View file

@ -1891,16 +1891,12 @@ ClickHandlerPtr MakeChannelPostHandler(
FullMsgId item) { FullMsgId item) {
return std::make_shared<LambdaClickHandler>(crl::guard(session, [=] { return std::make_shared<LambdaClickHandler>(crl::guard(session, [=] {
const auto peer = session->data().peer(item.peer); const auto peer = session->data().peer(item.peer);
if (const auto window = Core::App().windowFor(peer)) { if (const auto controller = session->tryResolveWindow(peer)) {
if (const auto controller = window->sessionController()) { Core::App().hideMediaView();
if (&controller->session() == &peer->session()) { controller->showPeerHistory(
Core::App().hideMediaView(); peer,
controller->showPeerHistory( Window::SectionShow::Way::ClearStack,
item.peer, item.msg);
Window::SectionShow::Way::ClearStack,
item.msg);
}
}
} }
})); }));
} }