tdesktop/Telegram/SourceFiles/mainwidget.cpp

2759 lines
73 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "mainwidget.h"
#include "api/api_updates.h"
#include "api/api_views.h"
#include "data/data_photo.h"
#include "data/data_document.h"
#include "data/data_document_media.h"
#include "data/data_document_resolver.h"
2023-01-21 05:42:57 +00:00
#include "data/data_forum_topic.h"
#include "data/data_wall_paper.h"
#include "data/data_web_page.h"
#include "data/data_game.h"
#include "data/data_peer_values.h"
#include "data/data_drafts.h"
#include "data/data_session.h"
2020-06-12 14:09:04 +00:00
#include "data/data_changes.h"
#include "data/data_media_types.h"
2019-04-15 11:54:03 +00:00
#include "data/data_folder.h"
#include "data/data_forum_topic.h"
#include "data/data_channel.h"
#include "data/data_chat.h"
#include "data/data_user.h"
2020-02-07 16:07:21 +00:00
#include "data/data_chat_filters.h"
#include "data/data_scheduled_messages.h"
#include "data/data_file_origin.h"
2020-02-21 07:58:50 +00:00
#include "data/data_histories.h"
#include "data/stickers/data_stickers.h"
#include "ui/chat/chat_theme.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/shadow.h"
#include "ui/widgets/dropdown_menu.h"
#include "ui/image/image.h"
2017-09-25 16:06:53 +00:00
#include "ui/focus_persister.h"
#include "ui/resize_area.h"
2020-10-10 09:15:37 +00:00
#include "ui/text/text_options.h"
#include "ui/emoji_config.h"
2021-05-07 14:33:53 +00:00
#include "ui/ui_utility.h"
#include "window/section_memento.h"
#include "window/section_widget.h"
#include "window/window_connecting_widget.h"
#include "window/window_top_bar_wrap.h"
#include "window/notifications_manager.h"
#include "window/window_slide_animation.h"
#include "window/window_session_controller.h"
#include "window/window_history_hider.h"
#include "window/window_controller.h"
2022-11-01 04:46:31 +00:00
#include "window/window_peer_menu.h"
#include "window/themes/window_theme.h"
#include "chat_helpers/tabbed_selector.h" // TabbedSelector::refreshStickers
#include "chat_helpers/message_field.h"
#include "info/info_memento.h"
#include "info/info_controller.h"
#include "apiwrap.h"
#include "dialogs/dialogs_widget.h"
#include "dialogs/dialogs_key.h"
#include "history/history.h"
2017-06-29 10:27:09 +00:00
#include "history/history_widget.h"
2022-12-14 12:15:46 +00:00
#include "history/history_item.h"
#include "history/history_item_helpers.h" // GetErrorTextForSending.
#include "history/view/media/history_view_media.h"
2018-01-09 17:08:31 +00:00
#include "history/view/history_view_service_message.h"
#include "history/view/history_view_element.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "lang/lang_cloud_manager.h"
2017-04-06 14:38:10 +00:00
#include "boxes/add_contact_box.h"
#include "mainwindow.h"
#include "inline_bots/inline_bot_layout_item.h"
2021-10-18 21:36:55 +00:00
#include "ui/boxes/confirm_box.h"
2017-04-06 14:38:10 +00:00
#include "boxes/sticker_set_box.h"
#include "boxes/peer_list_controllers.h"
2017-04-06 14:38:10 +00:00
#include "boxes/download_path_box.h"
2018-05-07 17:44:33 +00:00
#include "boxes/connection_box.h"
#include "storage/storage_account.h"
2023-02-15 04:44:10 +00:00
#include "main/main_domain.h"
#include "media/audio/media_audio.h"
#include "media/player/media_player_panel.h"
2016-09-17 19:28:33 +00:00
#include "media/player/media_player_widget.h"
#include "media/player/media_player_dropdown.h"
#include "media/player/media_player_instance.h"
#include "media/player/media_player_float.h"
2017-04-06 14:38:10 +00:00
#include "base/qthelp_regex.h"
#include "base/qthelp_url.h"
#include "base/flat_set.h"
#include "mtproto/mtproto_dc_options.h"
#include "core/file_utilities.h"
#include "core/update_checker.h"
2018-11-16 12:15:14 +00:00
#include "core/shortcuts.h"
#include "core/application.h"
#include "core/changelogs.h"
2022-11-01 07:18:56 +00:00
#include "core/mime_type.h"
#include "base/unixtime.h"
#include "calls/calls_call.h"
#include "calls/calls_instance.h"
#include "calls/calls_top_bar.h"
#include "calls/group/calls_group_call.h"
2018-06-22 23:18:43 +00:00
#include "export/export_settings.h"
2020-06-25 07:14:05 +00:00
#include "export/export_manager.h"
#include "export/view/export_view_top_bar.h"
#include "export/view/export_view_panel_controller.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "main/main_session_settings.h"
2019-11-22 09:40:52 +00:00
#include "main/main_account.h"
#include "support/support_helper.h"
#include "storage/storage_facade.h"
#include "storage/storage_shared_media.h"
#include "storage/storage_user_photos.h"
#include "styles/style_dialogs.h"
2020-10-10 09:15:37 +00:00
#include "styles/style_chat.h"
#include "styles/style_boxes.h"
2022-12-19 11:48:24 +00:00
#include "styles/style_window.h"
2019-09-04 07:19:15 +00:00
#include <QtCore/QCoreApplication>
#include <QtCore/QMimeData>
enum StackItemType {
HistoryStackItem,
SectionStackItem,
};
class StackItem {
public:
explicit StackItem(PeerData *peer) : _peer(peer) {
}
[[nodiscard]] PeerData *peer() const {
return _peer;
}
void setThirdSectionMemento(
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> memento);
[[nodiscard]] auto takeThirdSectionMemento()
-> std::shared_ptr<Window::SectionMemento> {
return std::move(_thirdSectionMemento);
}
void setThirdSectionWeak(QPointer<Window::SectionWidget> section) {
_thirdSectionWeak = section;
}
[[nodiscard]] QPointer<Window::SectionWidget> thirdSectionWeak() const {
return _thirdSectionWeak;
}
[[nodiscard]] rpl::lifetime &lifetime() {
return _lifetime;
}
[[nodiscard]] virtual StackItemType type() const = 0;
2023-01-21 05:42:57 +00:00
[[nodiscard]] rpl::producer<> removeRequests() const {
return rpl::merge(
_thirdSectionRemoveRequests.events(),
sectionRemoveRequests());
}
virtual ~StackItem() = default;
private:
2023-01-21 05:42:57 +00:00
[[nodiscard]] virtual rpl::producer<> sectionRemoveRequests() const = 0;
PeerData *_peer = nullptr;
QPointer<Window::SectionWidget> _thirdSectionWeak;
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> _thirdSectionMemento;
2023-01-21 05:42:57 +00:00
rpl::event_stream<> _thirdSectionRemoveRequests;
rpl::lifetime _lifetime;
};
class StackItemHistory final : public StackItem {
public:
StackItemHistory(
not_null<History*> history,
MsgId msgId,
QVector<FullMsgId> replyReturns)
: StackItem(history->peer)
, history(history)
, msgId(msgId)
, replyReturns(replyReturns) {
}
StackItemType type() const override {
return HistoryStackItem;
}
not_null<History*> history;
MsgId msgId;
QVector<FullMsgId> replyReturns;
2023-01-21 05:42:57 +00:00
private:
rpl::producer<> sectionRemoveRequests() const override {
return rpl::never<>();
}
};
class StackItemSection : public StackItem {
public:
StackItemSection(
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> memento);
StackItemType type() const override {
return SectionStackItem;
}
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> takeMemento() {
return std::move(_memento);
}
private:
2023-01-21 05:42:57 +00:00
rpl::producer<> sectionRemoveRequests() const override;
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> _memento;
};
void StackItem::setThirdSectionMemento(
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> memento) {
_thirdSectionMemento = std::move(memento);
2023-01-21 05:42:57 +00:00
if (const auto memento = _thirdSectionMemento.get()) {
memento->removeRequests(
) | rpl::start_to_stream(_thirdSectionRemoveRequests, _lifetime);
}
}
StackItemSection::StackItemSection(
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> memento)
: StackItem(nullptr)
, _memento(std::move(memento)) {
}
2023-01-21 05:42:57 +00:00
rpl::producer<> StackItemSection::sectionRemoveRequests() const {
if (const auto topic = _memento->topicForRemoveRequests()) {
return rpl::merge(_memento->removeRequests(), topic->destroyed());
}
return _memento->removeRequests();
}
2019-01-16 12:39:35 +00:00
struct MainWidget::SettingBackground {
2019-01-28 13:59:49 +00:00
explicit SettingBackground(const Data::WallPaper &data);
2019-01-16 12:39:35 +00:00
Data::WallPaper data;
std::shared_ptr<Data::DocumentMedia> dataMedia;
2019-01-16 12:39:35 +00:00
base::binary_guard generating;
};
2019-01-28 13:59:49 +00:00
MainWidget::SettingBackground::SettingBackground(
const Data::WallPaper &data)
: data(data) {
}
2017-09-13 16:57:44 +00:00
MainWidget::MainWidget(
QWidget *parent,
not_null<Window::SessionController*> controller)
2017-09-13 16:57:44 +00:00
: RpWidget(parent)
, _controller(controller)
2017-11-12 16:50:58 +00:00
, _dialogsWidth(st::columnMinimalWidthLeft)
, _thirdColumnWidth(st::columnMinimalWidthThird)
, _sideShadow(isPrimary()
? base::make_unique_q<Ui::PlainShadow>(this)
: nullptr)
, _dialogs(isPrimary()
? base::make_unique_q<Dialogs::Widget>(
this,
_controller,
Dialogs::Widget::Layout::Main)
: nullptr)
, _history(std::in_place, this, _controller)
2019-02-28 21:03:25 +00:00
, _playerPlaylist(this, _controller)
, _changelogs(Core::Changelogs::Create(&controller->session())) {
if (isPrimary()) {
setupConnectingWidget();
}
2015-02-03 15:02:46 +00:00
2020-11-12 16:19:04 +00:00
_history->cancelRequests(
) | rpl::start_with_next([=] {
handleHistoryBack();
}, lifetime());
2020-06-25 17:57:36 +00:00
Core::App().calls().currentCallValue(
) | rpl::start_with_next([=](Calls::Call *call) {
2020-06-25 07:14:05 +00:00
setCurrentCall(call);
2020-06-25 17:57:36 +00:00
}, lifetime());
2020-11-24 14:40:10 +00:00
Core::App().calls().currentGroupCallValue(
) | rpl::start_with_next([=](Calls::GroupCall *call) {
setCurrentGroupCall(call);
}, lifetime());
2020-06-25 17:57:36 +00:00
if (_callTopBar) {
_callTopBar->finishAnimating();
}
controller->window().setDefaultFloatPlayerDelegate(
floatPlayerDelegate());
Core::App().floatPlayerClosed(
) | rpl::start_with_next([=](FullMsgId itemId) {
floatPlayerClosed(itemId);
2020-06-25 15:01:10 +00:00
}, lifetime());
2020-06-25 07:14:05 +00:00
Core::App().exportManager().currentView(
) | rpl::start_with_next([=](Export::View::PanelController *view) {
setCurrentExportView(view);
}, lifetime());
2020-06-25 07:14:05 +00:00
if (_exportTopBar) {
_exportTopBar->finishAnimating();
}
Media::Player::instance()->closePlayerRequests(
) | rpl::start_with_next([=] {
closeBothPlayers();
}, lifetime());
Media::Player::instance()->updatedNotifier(
) | rpl::start_with_next([=](const Media::Player::TrackState &state) {
handleAudioUpdate(state);
}, lifetime());
handleAudioUpdate(Media::Player::instance()->getState(AudioMsgId::Type::Song));
handleAudioUpdate(Media::Player::instance()->getState(AudioMsgId::Type::Voice));
if (_player) {
_player->finishAnimating();
}
rpl::merge(
_controller->dialogsListFocusedChanges(),
_controller->dialogsListDisplayForcedChanges()
) | rpl::start_with_next([=] {
updateDialogsWidthAnimated();
}, lifetime());
2017-11-12 16:50:58 +00:00
rpl::merge(
2020-06-25 12:16:09 +00:00
Core::App().settings().dialogsWidthRatioChanges() | rpl::to_empty,
Core::App().settings().thirdColumnWidthChanges() | rpl::to_empty
) | rpl::start_with_next([=] {
updateControlsGeometry();
}, lifetime());
2020-06-12 14:09:04 +00:00
session().changes().historyUpdates(
Data::HistoryUpdate::Flag::MessageSent
) | rpl::start_with_next([=](const Data::HistoryUpdate &update) {
const auto history = update.history;
2022-11-01 04:46:31 +00:00
history->forgetScrollState();
if (const auto from = history->peer->migrateFrom()) {
auto &owner = history->owner();
if (const auto migrated = owner.historyLoaded(from)) {
migrated->forgetScrollState();
2021-01-15 16:55:22 +00:00
}
}
}, lifetime());
2022-11-01 04:46:31 +00:00
session().changes().entryUpdates(
Data::EntryUpdate::Flag::LocalDraftSet
) | rpl::start_with_next([=](const Data::EntryUpdate &update) {
auto params = Window::SectionShow();
params.reapplyLocalDraft = true;
controller->showThread(
update.entry->asThread(),
ShowAtUnreadMsgId,
params);
2022-11-01 04:46:31 +00:00
controller->hideLayer();
}, lifetime());
2018-08-14 19:12:41 +00:00
// MSVC BUG + REGRESSION rpl::mappers::tuple :(
using namespace rpl::mappers;
_controller->activeChatValue(
) | rpl::map([](Dialogs::Key key) {
const auto peer = key.peer();
const auto topic = key.topic();
auto canWrite = topic
? Data::CanSendAnyOfValue(
topic,
Data::TabbedPanelSendRestrictions())
: peer
? Data::CanSendAnyOfValue(
peer, Data::TabbedPanelSendRestrictions())
: rpl::single(false);
2018-08-14 19:12:41 +00:00
return std::move(
canWrite
) | rpl::map([=](bool can) {
return std::make_tuple(key, can);
});
}) | rpl::flatten_latest(
) | rpl::start_with_next([this](Dialogs::Key key, bool canWrite) {
updateThirdColumnToCurrentChat(key, canWrite);
}, lifetime());
QCoreApplication::instance()->installEventFilter(this);
Media::Player::instance()->tracksFinished(
) | rpl::start_with_next([=](AudioMsgId::Type type) {
if (type == AudioMsgId::Type::Voice) {
const auto songState = Media::Player::instance()->getState(
AudioMsgId::Type::Song);
if (!songState.id || IsStoppedOrStopping(songState.state)) {
Media::Player::instance()->stopAndClose();
}
} else if (type == AudioMsgId::Type::Song) {
const auto songState = Media::Player::instance()->getState(
AudioMsgId::Type::Song);
if (!songState.id) {
Media::Player::instance()->stopAndClose();
}
}
}, lifetime());
_controller->adaptive().changes(
) | rpl::start_with_next([=] {
handleAdaptiveLayoutUpdate();
}, lifetime());
if (_dialogs) {
_dialogs->show();
}
if (_dialogs && isOneColumn()) {
_history->hide();
} else {
_history->show();
}
orderWidgets();
if (!Core::UpdaterDisabled()) {
Core::UpdateChecker checker;
checker.start();
}
2020-06-16 14:19:23 +00:00
cSetOtherOnline(0);
2020-06-16 14:19:23 +00:00
_history->start();
}
MainWidget::~MainWidget() = default;
2019-07-24 11:45:24 +00:00
Main::Session &MainWidget::session() const {
2019-01-18 12:27:37 +00:00
return _controller->session();
}
2020-06-10 18:08:17 +00:00
not_null<Window::SessionController*> MainWidget::controller() const {
return _controller;
}
2018-05-07 17:44:33 +00:00
void MainWidget::setupConnectingWidget() {
using namespace rpl::mappers;
2019-02-10 16:29:55 +00:00
_connecting = std::make_unique<Window::ConnectionState>(
2018-05-07 17:44:33 +00:00
this,
2020-06-10 14:52:44 +00:00
&session().account(),
_controller->adaptive().oneColumnValue() | rpl::map(!_1));
_controller->connectingBottomSkipValue(
) | rpl::start_with_next([=](int skip) {
_connecting->setBottomSkip(skip);
}, lifetime());
2018-05-07 17:44:33 +00:00
}
2018-11-05 11:16:09 +00:00
not_null<Media::Player::FloatDelegate*> MainWidget::floatPlayerDelegate() {
return static_cast<Media::Player::FloatDelegate*>(this);
}
2018-11-05 11:16:09 +00:00
not_null<Ui::RpWidget*> MainWidget::floatPlayerWidget() {
return this;
}
void MainWidget::floatPlayerToggleGifsPaused(bool paused) {
constexpr auto kReason = Window::GifPauseReason::RoundPlaying;
if (paused) {
_controller->enableGifPauseReason(kReason);
} else {
_controller->disableGifPauseReason(kReason);
}
}
auto MainWidget::floatPlayerGetSection(Window::Column column)
-> not_null<Media::Player::FloatSectionDelegate*> {
if (isThreeColumn()) {
if (_dialogs && column == Window::Column::First) {
return _dialogs;
} else if (column == Window::Column::Second
|| !_dialogs
|| !_thirdSection) {
if (_mainSection) {
return _mainSection;
}
return _history;
}
return _thirdSection;
} else if (isNormalColumn()) {
if (_dialogs && column == Window::Column::First) {
return _dialogs;
} else if (_mainSection) {
return _mainSection;
}
return _history;
} else if (_mainSection) {
return _mainSection;
} else if (!isOneColumn() || _history->peer()) {
return _history;
}
Assert(_dialogs != nullptr);
return _dialogs;
}
2018-11-05 11:16:09 +00:00
void MainWidget::floatPlayerEnumerateSections(Fn<void(
not_null<Media::Player::FloatSectionDelegate*> widget,
2018-11-05 11:16:09 +00:00
Window::Column widgetColumn)> callback) {
if (isThreeColumn()) {
if (_dialogs) {
callback(_dialogs, Window::Column::First);
}
if (_mainSection) {
2018-11-05 11:16:09 +00:00
callback(_mainSection, Window::Column::Second);
} else {
2018-11-05 11:16:09 +00:00
callback(_history, Window::Column::Second);
}
if (_thirdSection) {
2018-11-05 11:16:09 +00:00
callback(_thirdSection, Window::Column::Third);
}
} else if (isNormalColumn()) {
if (_dialogs) {
callback(_dialogs, Window::Column::First);
}
if (_mainSection) {
2018-11-05 11:16:09 +00:00
callback(_mainSection, Window::Column::Second);
} else {
2018-11-05 11:16:09 +00:00
callback(_history, Window::Column::Second);
}
} else {
if (_mainSection) {
2018-11-05 11:16:09 +00:00
callback(_mainSection, Window::Column::Second);
} else if (!isOneColumn() || _history->peer()) {
2018-11-05 11:16:09 +00:00
callback(_history, Window::Column::Second);
} else {
Assert(_dialogs != nullptr);
2018-11-05 11:16:09 +00:00
callback(_dialogs, Window::Column::First);
}
}
}
2018-11-05 13:18:54 +00:00
bool MainWidget::floatPlayerIsVisible(not_null<HistoryItem*> item) {
return session().data().queryItemVisibility(item);
2018-11-05 13:18:54 +00:00
}
void MainWidget::floatPlayerClosed(FullMsgId itemId) {
2018-11-05 11:16:09 +00:00
if (_player) {
const auto voiceData = Media::Player::instance()->current(
AudioMsgId::Type::Voice);
if (voiceData.contextId() == itemId) {
stopAndClosePlayer();
}
}
}
void MainWidget::floatPlayerDoubleClickEvent(
not_null<const HistoryItem*> item) {
_controller->showMessage(item);
}
2022-11-01 04:46:31 +00:00
bool MainWidget::setForwardDraft(
not_null<Data::Thread*> thread,
Data::ForwardDraft &&draft) {
const auto history = thread->owningHistory();
const auto items = session().data().idsToItems(draft.ids);
2022-11-01 04:46:31 +00:00
const auto topicRootId = thread->topicRootId();
2019-08-30 13:17:46 +00:00
const auto error = GetErrorTextForSending(
2022-11-01 04:46:31 +00:00
history->peer,
{
.topicRootId = topicRootId,
.forward = &items,
.ignoreSlowmodeCountdown = true,
});
if (!error.isEmpty()) {
_controller->show(Ui::MakeInformBox(error));
return false;
}
2022-11-01 04:46:31 +00:00
history->setForwardDraft(topicRootId, std::move(draft));
_controller->showThread(
thread,
ShowAtUnreadMsgId,
SectionShow::Way::Forward);
return true;
}
bool MainWidget::shareUrl(
2022-11-01 04:46:31 +00:00
not_null<Data::Thread*> thread,
const QString &url,
const QString &text) const {
if (!Data::CanSendTexts(thread)) {
_controller->show(Ui::MakeInformBox(tr::lng_share_cant()));
2015-10-18 12:49:34 +00:00
return false;
}
const auto textWithTags = TextWithTags{
url + '\n' + text,
TextWithTags::Tags()
};
const auto cursor = MessageCursor{
int(url.size()) + 1,
int(url.size()) + 1 + int(text.size()),
QFIXED_MAX
};
2022-11-01 04:46:31 +00:00
const auto history = thread->owningHistory();
const auto topicRootId = thread->topicRootId();
history->setLocalDraft(std::make_unique<Data::Draft>(
textWithTags,
0, // replyTo
topicRootId,
cursor,
Data::PreviewState::Allowed));
history->clearLocalEditDraft(topicRootId);
2022-11-01 04:46:31 +00:00
history->session().changes().entryUpdated(
thread,
Data::EntryUpdate::Flag::LocalDraftSet);
2015-10-18 12:49:34 +00:00
return true;
}
bool MainWidget::sendPaths(
not_null<Data::Thread*> thread,
const QStringList &paths) {
if (!Data::CanSendAnyOf(thread, Data::FilesSendRestrictions())) {
_controller->show(Ui::MakeInformBox(
tr::lng_forward_send_files_cant()));
return false;
} else if (const auto error = Data::AnyFileRestrictionError(
thread->peer())) {
_controller->show(Ui::MakeInformBox(*error));
return false;
2022-11-01 04:46:31 +00:00
} else {
_controller->showThread(
2022-11-01 04:46:31 +00:00
thread,
ShowAtTheEndMsgId,
Window::SectionShow::Way::ClearStack);
}
return (_controller->activeChatCurrent().thread() == thread)
2022-11-01 07:18:56 +00:00
&& (_mainSection
? _mainSection->confirmSendingFiles(paths)
: _history->confirmSendingFiles(paths));
}
bool MainWidget::filesOrForwardDrop(
2022-11-01 04:46:31 +00:00
not_null<Data::Thread*> thread,
2022-11-01 07:18:56 +00:00
not_null<const QMimeData*> data) {
if (const auto forum = thread->asForum()) {
2022-11-01 07:18:56 +00:00
Window::ShowDropMediaBox(
_controller,
Core::ShareMimeMediaData(data),
forum);
if (_hider) {
_hider->startHide();
clearHider(_hider);
}
return true;
}
2022-11-29 21:46:36 +00:00
if (data->hasFormat(u"application/x-td-forward"_q)) {
auto draft = Data::ForwardDraft{
.ids = session().data().takeMimeForwardIds(),
};
2022-11-01 07:18:56 +00:00
if (setForwardDraft(thread, std::move(draft))) {
return true;
2022-11-01 04:46:31 +00:00
}
// We've already released the mouse button,
// so the forwarding is cancelled.
if (_hider) {
_hider->startHide();
clearHider(_hider);
}
2022-11-01 07:18:56 +00:00
return false;
} else if (!Data::CanSendAnyOf(thread, Data::FilesSendRestrictions())) {
_controller->show(Ui::MakeInformBox(
tr::lng_forward_send_files_cant()));
return false;
} else if (const auto error = Data::AnyFileRestrictionError(
thread->peer())) {
_controller->show(Ui::MakeInformBox(*error));
2022-11-01 07:18:56 +00:00
return false;
} else {
_controller->showThread(
2022-11-01 04:46:31 +00:00
thread,
ShowAtTheEndMsgId,
Window::SectionShow::Way::ClearStack);
if (_controller->activeChatCurrent().thread() != thread) {
2022-11-01 07:18:56 +00:00
return false;
}
2022-11-01 07:18:56 +00:00
(_mainSection
? _mainSection->confirmSendingFiles(data)
: _history->confirmSendingFiles(data));
return true;
}
2015-06-26 09:07:59 +00:00
}
bool MainWidget::notify_switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo) {
return _history->notify_switchInlineBotButtonReceived(query, samePeerBot, samePeerReplyTo);
}
void MainWidget::clearHider(not_null<Window::HistoryHider*> instance) {
if (_hider != instance) {
return;
}
_hider.release();
}
void MainWidget::hiderLayer(base::unique_qptr<Window::HistoryHider> hider) {
if (!_dialogs || _controller->window().locked()) {
2015-04-04 20:01:34 +00:00
return;
}
_hider = std::move(hider);
_hider->setParent(this);
_hider->hidden(
) | rpl::start_with_next([=, instance = _hider.get()] {
clearHider(instance);
instance->hide();
instance->deleteLater();
}, _hider->lifetime());
_hider->show();
updateControlsGeometry();
_dialogs->setInnerFocus();
2018-11-05 13:18:54 +00:00
floatPlayerCheckVisibility();
}
void MainWidget::showDragForwardInfo() {
hiderLayer(base::make_unique_q<Window::HistoryHider>(
this,
tr::lng_forward_choose(tr::now)));
2015-10-18 12:49:34 +00:00
}
void MainWidget::hideDragForwardInfo() {
2022-11-01 04:46:31 +00:00
if (_hider) {
_hider->startHide();
_hider.release();
}
}
void MainWidget::sendBotCommand(Bot::SendCommandRequest request) {
2021-07-26 23:18:11 +00:00
const auto type = _mainSection
? _mainSection->sendBotCommand(request)
: Window::SectionActionResult::Fallback;
if (type == Window::SectionActionResult::Fallback) {
_controller->showPeerHistory(
request.peer,
SectionShow::Way::Forward,
2021-07-26 23:18:11 +00:00
ShowAtTheEndMsgId);
_history->sendBotCommand(request);
}
}
void MainWidget::hideSingleUseKeyboard(PeerData *peer, MsgId replyTo) {
_history->hideSingleUseKeyboard(peer, replyTo);
}
void MainWidget::searchMessages(const QString &query, Dialogs::Key inChat) {
2023-01-18 09:35:52 +00:00
if (controller()->isPrimary()) {
_dialogs->searchMessages(query, inChat);
if (isOneColumn()) {
_controller->clearSectionStack();
} else {
_dialogs->setInnerFocus();
}
2015-09-21 20:57:42 +00:00
} else {
2023-01-18 09:35:52 +00:00
const auto searchIn = [&](not_null<Window::Controller*> window) {
if (const auto controller = window->sessionController()) {
controller->content()->searchMessages(query, inChat);
controller->widget()->activate();
}
};
const auto account = &session().account();
if (const auto peer = inChat.peer()) {
if (peer == controller()->singlePeer()) {
if (_history->peer() != peer) {
controller()->showPeerHistory(peer);
}
_history->searchInChatEmbedded(query);
} else if (const auto window = Core::App().windowFor(peer)) {
searchIn(window);
}
} else if (const auto window = Core::App().windowFor(account)) {
searchIn(window);
}
2015-09-21 20:57:42 +00:00
}
}
void MainWidget::handleAudioUpdate(const Media::Player::TrackState &state) {
using State = Media::Player::State;
const auto document = state.id.audio();
if (!Media::Player::IsStoppedOrStopping(state.state)) {
createPlayer();
} else if (state.state == State::StoppedAtStart) {
Media::Player::instance()->stopAndClose();
2015-07-03 08:47:16 +00:00
}
2019-04-25 12:45:15 +00:00
if (const auto item = session().data().message(state.id.contextId())) {
2019-01-18 12:27:37 +00:00
session().data().requestItemRepaint(item);
}
2021-06-11 15:11:58 +00:00
if (document) {
if (const auto items = InlineBots::Layout::documentItems()) {
if (const auto i = items->find(document); i != items->end()) {
for (const auto &item : i->second) {
2021-06-11 15:11:58 +00:00
item->update();
}
}
}
}
2014-09-04 07:33:44 +00:00
}
void MainWidget::closeBothPlayers() {
2019-02-28 21:03:25 +00:00
if (_player) {
_player->hide(anim::type::normal);
}
_playerPlaylist->hideIgnoringEnterEvents();
}
void MainWidget::stopAndClosePlayer() {
if (_player) {
_player->entity()->stopAndClose();
}
}
void MainWidget::createPlayer() {
if (!_player) {
_player.create(
this,
object_ptr<Media::Player::Widget>(this, this, _controller),
_controller->adaptive().oneColumnValue());
rpl::merge(
_player->heightValue() | rpl::map_to(true),
_player->shownValue()
) | rpl::start_with_next(
[this] { playerHeightUpdated(); },
2017-12-27 07:18:09 +00:00
_player->lifetime());
_player->entity()->setCloseCallback([=] {
Media::Player::instance()->stopAndClose();
});
_player->entity()->setShowItemCallback([=](
not_null<const HistoryItem*> item) {
_controller->showMessage(item);
});
_player->entity()->togglePlaylistRequests(
) | rpl::start_with_next([=](bool shown) {
if (!shown) {
_playerPlaylist->hideFromOther();
return;
} else if (_playerPlaylist->isHidden()) {
auto position = mapFromGlobal(QCursor::pos()).x();
auto bestPosition = _playerPlaylist->bestPositionFor(position);
if (rtl()) bestPosition = position + 2 * (position - bestPosition) - _playerPlaylist->width();
updateMediaPlaylistPosition(bestPosition);
}
_playerPlaylist->showFromOther();
}, _player->lifetime());
orderWidgets();
if (_showAnimation) {
_player->show(anim::type::instant);
_player->setVisible(false);
Shortcuts::ToggleMediaShortcuts(true);
} else {
_player->hide(anim::type::instant);
}
}
if (_player && !_player->toggled()) {
if (!_showAnimation) {
_player->show(anim::type::normal);
_playerHeight = _contentScrollAddToY = _player->contentHeight();
updateControlsGeometry();
Shortcuts::ToggleMediaShortcuts(true);
}
}
}
void MainWidget::playerHeightUpdated() {
2017-12-27 07:18:09 +00:00
if (!_player) {
// Player could be already "destroyDelayed", but still handle events.
return;
}
auto playerHeight = _player->contentHeight();
if (playerHeight != _playerHeight) {
_contentScrollAddToY += playerHeight - _playerHeight;
_playerHeight = playerHeight;
updateControlsGeometry();
}
if (!_playerHeight && _player->isHidden()) {
2019-02-28 21:03:25 +00:00
const auto state = Media::Player::instance()->getState(Media::Player::instance()->getActiveType());
if (!state.id || Media::Player::IsStoppedOrStopping(state.state)) {
_player.destroyDelayed();
}
}
}
void MainWidget::setCurrentCall(Calls::Call *call) {
2020-11-24 14:40:10 +00:00
if (!call && _currentGroupCall) {
return;
}
2020-06-25 17:57:36 +00:00
_currentCallLifetime.destroy();
_currentCall = call;
if (_currentCall) {
2020-11-28 16:18:51 +00:00
_callTopBar.destroy();
2020-06-25 17:57:36 +00:00
_currentCall->stateValue(
) | rpl::start_with_next([=](Calls::Call::State state) {
using State = Calls::Call::State;
2020-11-28 16:18:51 +00:00
if (state != State::Established) {
destroyCallTopBar();
2020-11-28 16:18:51 +00:00
} else if (!_callTopBar) {
createCallTopBar();
}
2020-06-25 17:57:36 +00:00
}, _currentCallLifetime);
} else {
destroyCallTopBar();
}
}
2020-11-24 14:40:10 +00:00
void MainWidget::setCurrentGroupCall(Calls::GroupCall *call) {
if (!call && _currentCall) {
return;
}
_currentCallLifetime.destroy();
_currentGroupCall = call;
if (_currentGroupCall) {
2020-11-28 16:18:51 +00:00
_callTopBar.destroy();
2020-11-24 14:40:10 +00:00
_currentGroupCall->stateValue(
) | rpl::start_with_next([=](Calls::GroupCall::State state) {
using State = Calls::GroupCall::State;
if (state != State::Creating
&& state != State::Waiting
&& state != State::Joining
&& state != State::Joined
&& state != State::Connecting) {
2020-11-24 14:40:10 +00:00
destroyCallTopBar();
2020-11-28 16:18:51 +00:00
} else if (!_callTopBar) {
createCallTopBar();
2020-11-24 14:40:10 +00:00
}
}, _currentCallLifetime);
} else {
destroyCallTopBar();
}
}
void MainWidget::createCallTopBar() {
2020-11-24 14:40:10 +00:00
Expects(_currentCall != nullptr || _currentGroupCall != nullptr);
const auto show = controller()->uiShow();
2020-11-24 14:40:10 +00:00
_callTopBar.create(
this,
(_currentCall
2022-06-14 11:20:38 +00:00
? object_ptr<Calls::TopBar>(this, _currentCall, show)
: object_ptr<Calls::TopBar>(this, _currentGroupCall, show)));
2020-12-09 11:43:54 +00:00
_callTopBar->entity()->initBlobsUnder(this, _callTopBar->geometryValue());
_callTopBar->heightValue(
) | rpl::start_with_next([this](int value) {
callTopBarHeightUpdated(value);
}, lifetime());
orderWidgets();
if (_showAnimation) {
_callTopBar->show(anim::type::instant);
_callTopBar->setVisible(false);
} else {
_callTopBar->hide(anim::type::instant);
_callTopBar->show(anim::type::normal);
_callTopBarHeight = _contentScrollAddToY = _callTopBar->height();
updateControlsGeometry();
}
}
void MainWidget::destroyCallTopBar() {
if (_callTopBar) {
_callTopBar->hide(anim::type::normal);
}
}
2017-09-13 16:57:44 +00:00
void MainWidget::callTopBarHeightUpdated(int callTopBarHeight) {
2020-11-24 14:40:10 +00:00
if (!callTopBarHeight && !_currentCall && !_currentGroupCall) {
_callTopBar.destroyDelayed();
}
if (callTopBarHeight != _callTopBarHeight) {
_contentScrollAddToY += callTopBarHeight - _callTopBarHeight;
_callTopBarHeight = callTopBarHeight;
updateControlsGeometry();
}
}
void MainWidget::setCurrentExportView(Export::View::PanelController *view) {
_currentExportView = view;
if (_currentExportView) {
_currentExportView->progressState(
) | rpl::start_with_next([=](Export::View::Content &&data) {
if (!data.rows.empty()
&& data.rows[0].id == Export::View::Content::kDoneId) {
2018-06-24 02:15:12 +00:00
LOG(("Export Info: Destroy top bar by Done."));
destroyExportTopBar();
} else if (!_exportTopBar) {
2018-06-24 02:15:12 +00:00
LOG(("Export Info: Create top bar by State."));
createExportTopBar(std::move(data));
} else {
_exportTopBar->entity()->updateData(std::move(data));
}
2020-06-25 07:14:05 +00:00
}, _exportViewLifetime);
} else {
2020-06-25 07:14:05 +00:00
_exportViewLifetime.destroy();
2018-06-24 02:15:12 +00:00
LOG(("Export Info: Destroy top bar by controller removal."));
destroyExportTopBar();
}
}
void MainWidget::createExportTopBar(Export::View::Content &&data) {
_exportTopBar.create(
this,
object_ptr<Export::View::TopBar>(this, std::move(data)),
_controller->adaptive().oneColumnValue());
_exportTopBar->entity()->clicks(
) | rpl::start_with_next([=] {
if (_currentExportView) {
_currentExportView->activatePanel();
}
}, _exportTopBar->lifetime());
orderWidgets();
if (_showAnimation) {
_exportTopBar->show(anim::type::instant);
_exportTopBar->setVisible(false);
} else {
_exportTopBar->hide(anim::type::instant);
_exportTopBar->show(anim::type::normal);
_exportTopBarHeight = _contentScrollAddToY = _exportTopBar->contentHeight();
updateControlsGeometry();
}
rpl::merge(
_exportTopBar->heightValue() | rpl::map_to(true),
_exportTopBar->shownValue()
) | rpl::start_with_next([=] {
exportTopBarHeightUpdated();
}, _exportTopBar->lifetime());
}
void MainWidget::destroyExportTopBar() {
if (_exportTopBar) {
_exportTopBar->hide(anim::type::normal);
}
}
void MainWidget::exportTopBarHeightUpdated() {
if (!_exportTopBar) {
// Player could be already "destroyDelayed", but still handle events.
return;
}
const auto exportTopBarHeight = _exportTopBar->contentHeight();
if (exportTopBarHeight != _exportTopBarHeight) {
_contentScrollAddToY += exportTopBarHeight - _exportTopBarHeight;
_exportTopBarHeight = exportTopBarHeight;
updateControlsGeometry();
}
if (!_exportTopBarHeight && _exportTopBar->isHidden()) {
_exportTopBar.destroyDelayed();
}
}
SendMenu::Type MainWidget::sendMenuType() const {
return _history->sendMenuType();
}
2020-06-11 16:33:15 +00:00
bool MainWidget::sendExistingDocument(not_null<DocumentData*> document) {
2021-11-09 15:24:13 +00:00
return sendExistingDocument(document, {});
}
bool MainWidget::sendExistingDocument(
not_null<DocumentData*> document,
Api::SendOptions options) {
return _history->sendExistingDocument(document, options);
}
void MainWidget::dialogsCancelled() {
if (_hider) {
_hider->startHide();
clearHider(_hider);
}
_history->activate();
}
void MainWidget::setChatBackground(
const Data::WallPaper &background,
QImage &&image) {
using namespace Window::Theme;
if (isReadyChatBackground(background, image)) {
setReadyChatBackground(background, std::move(image));
return;
}
2019-01-28 13:59:49 +00:00
_background = std::make_unique<SettingBackground>(background);
if (const auto document = _background->data.document()) {
_background->dataMedia = document->createMediaView();
_background->dataMedia->thumbnailWanted(
_background->data.fileOrigin());
}
2019-01-28 13:59:49 +00:00
_background->data.loadDocument();
2015-02-03 15:02:46 +00:00
checkChatBackground();
2019-01-16 12:25:29 +00:00
2019-01-28 13:59:49 +00:00
const auto tile = Data::IsLegacy1DefaultWallPaper(background);
Window::Theme::Background()->downloadingStarted(tile);
2015-02-03 15:02:46 +00:00
}
bool MainWidget::isReadyChatBackground(
const Data::WallPaper &background,
const QImage &image) const {
2019-01-28 13:59:49 +00:00
return !image.isNull() || !background.document();
}
void MainWidget::setReadyChatBackground(
const Data::WallPaper &background,
QImage &&image) {
using namespace Window::Theme;
if (image.isNull()
2019-01-28 13:59:49 +00:00
&& !background.document()
2020-05-29 15:10:25 +00:00
&& background.localThumbnail()) {
image = background.localThumbnail()->original();
}
const auto resetToDefault = image.isNull()
2019-01-28 13:59:49 +00:00
&& !background.document()
2021-08-12 09:32:30 +00:00
&& background.backgroundColors().empty()
2019-01-28 13:59:49 +00:00
&& !Data::IsLegacy1DefaultWallPaper(background);
const auto ready = resetToDefault
2019-01-28 13:59:49 +00:00
? Data::DefaultWallPaper()
: background;
2019-01-29 10:57:29 +00:00
Background()->set(ready, std::move(image));
2019-01-28 13:59:49 +00:00
const auto tile = Data::IsLegacy1DefaultWallPaper(ready);
Background()->setTile(tile);
Ui::ForceFullRepaint(this);
}
2015-02-03 15:02:46 +00:00
bool MainWidget::chatBackgroundLoading() {
return (_background != nullptr);
}
float64 MainWidget::chatBackgroundProgress() const {
if (_background) {
if (_background->generating) {
2019-01-16 12:39:35 +00:00
return 1.;
2019-01-28 13:59:49 +00:00
} else if (const auto document = _background->data.document()) {
return _background->dataMedia->progress();
2019-01-16 12:25:29 +00:00
}
}
return 1.;
2015-02-03 15:02:46 +00:00
}
void MainWidget::checkChatBackground() {
if (!_background || _background->generating) {
2019-01-16 12:25:29 +00:00
return;
}
const auto &media = _background->dataMedia;
Assert(media != nullptr);
if (!media->loaded()) {
2019-01-16 12:25:29 +00:00
return;
}
const auto document = _background->data.document();
Assert(document != nullptr);
const auto generateCallback = [=](QImage &&image) {
const auto background = base::take(_background);
const auto ready = image.isNull()
2019-01-28 13:59:49 +00:00
? Data::DefaultWallPaper()
: background->data;
2019-01-28 13:59:49 +00:00
setReadyChatBackground(ready, std::move(image));
};
_background->generating = Data::ReadBackgroundImageAsync(
media.get(),
Ui::PreprocessBackgroundImage,
generateCallback);
2019-01-16 12:39:35 +00:00
}
Image *MainWidget::newBackgroundThumb() {
return !_background
? nullptr
: _background->data.localThumbnail()
? _background->data.localThumbnail()
: _background->dataMedia
? _background->dataMedia->thumbnail()
: nullptr;
2019-01-16 12:39:35 +00:00
}
void MainWidget::setInnerFocus() {
if (_hider || !_history->peer()) {
if (!_hider && _mainSection) {
_mainSection->setInnerFocus();
} else if (!_hider && _thirdSection) {
_thirdSection->setInnerFocus();
} else {
2022-02-25 13:11:49 +00:00
Assert(_dialogs != nullptr);
2019-04-24 14:12:35 +00:00
_dialogs->setInnerFocus();
}
} else if (_mainSection) {
_mainSection->setInnerFocus();
} else if (_history->peer() || !_thirdSection) {
_history->setInnerFocus();
} else {
_thirdSection->setInnerFocus();
}
}
void MainWidget::clearBotStartToken(PeerData *peer) {
2019-03-12 10:36:33 +00:00
if (peer && peer->isUser() && peer->asUser()->isBot()) {
peer->asUser()->botInfo->startToken = QString();
}
}
2015-09-16 13:04:08 +00:00
void MainWidget::ctrlEnterSubmitUpdated() {
_history->updateFieldSubmitSettings();
2015-09-16 13:04:08 +00:00
}
void MainWidget::showChooseReportMessages(
not_null<PeerData*> peer,
Ui::ReportReason reason,
Fn<void(MessageIdsList)> done) {
_history->setChooseReportMessagesDetails(reason, std::move(done));
_controller->showPeerHistory(
peer,
SectionShow::Way::Forward,
ShowForChooseMessagesMsgId);
controller()->showToast(tr::lng_report_please_select_messages(tr::now));
}
void MainWidget::clearChooseReportMessages() {
_history->setChooseReportMessagesDetails({}, nullptr);
}
void MainWidget::toggleChooseChatTheme(
not_null<PeerData*> peer,
std::optional<bool> show) {
_history->toggleChooseChatTheme(peer, show);
2021-09-24 15:10:25 +00:00
}
2022-01-04 16:36:33 +00:00
bool MainWidget::showHistoryInDifferentWindow(
PeerId peerId,
const SectionShow &params,
MsgId showAtMsgId) {
const auto peer = session().data().peer(peerId);
2023-02-15 04:44:10 +00:00
const auto account = &session().account();
auto primary = Core::App().separateWindowForAccount(account);
2022-01-04 16:36:33 +00:00
if (const auto separate = Core::App().separateWindowForPeer(peer)) {
if (separate == &_controller->window()) {
2022-01-04 16:36:33 +00:00
return false;
}
separate->sessionController()->showPeerHistory(
peerId,
params,
showAtMsgId);
separate->activate();
return true;
} else if (isPrimary()) {
if (primary && primary != &_controller->window()) {
primary->sessionController()->showPeerHistory(
peerId,
params,
showAtMsgId);
primary->activate();
return true;
}
return false;
2023-02-03 16:48:12 +00:00
} else if (!peerId) {
return true;
} else if (singlePeer()->id == peerId) {
2022-01-04 16:36:33 +00:00
return false;
2023-02-15 04:44:10 +00:00
} else if (!primary) {
Core::App().domain().activate(account);
primary = Core::App().separateWindowForAccount(account);
2022-01-04 16:36:33 +00:00
}
2023-02-15 04:44:10 +00:00
if (primary && &primary->account() == account) {
2022-01-04 16:36:33 +00:00
primary->sessionController()->showPeerHistory(
peerId,
params,
showAtMsgId);
2023-02-15 04:44:10 +00:00
primary->activate();
2022-01-04 16:36:33 +00:00
}
return true;
}
void MainWidget::showHistory(
PeerId peerId,
const SectionShow &params,
MsgId showAtMsgId) {
2022-01-04 16:36:33 +00:00
if (peerId && _controller->window().locked()) {
return;
} else if (auto peer = session().data().peerLoaded(peerId)) {
if (peer->migrateTo()) {
2016-03-23 13:21:26 +00:00
peer = peer->migrateTo();
peerId = peer->id;
if (showAtMsgId > 0) showAtMsgId = -showAtMsgId;
}
const auto unavailable = peer->computeUnavailableReason();
if (!unavailable.isEmpty()) {
Assert(isPrimary());
if (params.activation != anim::activation::background) {
_controller->show(Ui::MakeInformBox(unavailable));
_controller->window().activate();
}
2016-03-23 13:21:26 +00:00
return;
}
}
2022-05-13 14:54:35 +00:00
if ((IsServerMsgId(showAtMsgId) || Data::IsScheduledMsgId(showAtMsgId))
&& _mainSection
&& _mainSection->showMessage(peerId, params, showAtMsgId)) {
session().data().hideShownSpoilers();
return;
2022-01-04 16:36:33 +00:00
} else if (showHistoryInDifferentWindow(peerId, params, showAtMsgId)) {
return;
}
if (peerId && params.activation != anim::activation::background) {
_controller->window().activate();
}
const auto alreadyThatPeer = _history->peer()
&& (_history->peer()->id == peerId);
if (!alreadyThatPeer
&& preventsCloseSection(
[=] { showHistory(peerId, params, showAtMsgId); },
params)) {
return;
}
using OriginMessage = SectionShow::OriginMessage;
if (const auto origin = std::get_if<OriginMessage>(&params.origin)) {
if (const auto returnTo = session().data().message(origin->id)) {
if (returnTo->history()->peer->id == peerId) {
_history->pushReplyReturn(returnTo);
}
}
}
_controller->setDialogsListFocused(false);
2019-04-02 09:13:30 +00:00
_a_dialogsWidth.stop();
using Way = SectionShow::Way;
auto way = params.way;
bool back = (way == Way::Backward || !peerId);
bool foundInStack = !peerId;
if (foundInStack || (way == Way::ClearStack)) {
2019-04-24 14:12:35 +00:00
for (const auto &item : _stack) {
clearBotStartToken(item->peer());
}
_stack.clear();
} else {
for (auto i = 0, s = int(_stack.size()); i < s; ++i) {
if (_stack.at(i)->type() == HistoryStackItem && _stack.at(i)->peer()->id == peerId) {
foundInStack = true;
while (int(_stack.size()) > i + 1) {
clearBotStartToken(_stack.back()->peer());
_stack.pop_back();
}
_stack.pop_back();
if (!back) {
back = true;
}
break;
}
}
if (const auto activeChat = _controller->activeChatCurrent()) {
if (const auto peer = activeChat.peer()) {
if (way == Way::Forward && peer->id == peerId) {
way = _mainSection ? Way::Backward : Way::ClearStack;
}
}
}
}
const auto wasActivePeer = _controller->activeChatCurrent().peer();
if (params.activation != anim::activation::background) {
_controller->window().hideSettingsAndLayer();
}
auto animatedShow = [&] {
if (_showAnimation
|| Core::App().passcodeLocked()
|| (params.animated == anim::type::instant)) {
return false;
}
if (!peerId) {
if (isOneColumn()) {
return _dialogs && _dialogs->isHidden();
} else {
return false;
}
}
if (_history->isHidden()) {
if (!isOneColumn() && way == Way::ClearStack) {
return false;
}
return (_mainSection != nullptr)
|| (isOneColumn() && _dialogs && !_dialogs->isHidden());
}
if (back || way == Way::Forward) {
return true;
}
return false;
};
auto animationParams = animatedShow()
? prepareHistoryAnimation(peerId)
: Window::SectionSlideParams();
if (!back && (way != Way::ClearStack)) {
// This may modify the current section, for example remove its contents.
saveSectionInStack();
}
if (_history->peer()
&& _history->peer()->id != peerId
&& way != Way::Forward) {
clearBotStartToken(_history->peer());
}
_history->showHistory(peerId, showAtMsgId);
if (alreadyThatPeer && params.reapplyLocalDraft) {
_history->applyDraft(HistoryWidget::FieldHistoryAction::NewEntry);
}
auto noPeer = !_history->peer();
auto onlyDialogs = noPeer && isOneColumn();
_mainSection.destroy();
updateControlsGeometry();
if (noPeer) {
_controller->setActiveChatEntry(Dialogs::Key());
_controller->setChatStyleTheme(_controller->defaultChatTheme());
}
if (onlyDialogs) {
Assert(_dialogs != nullptr);
_history->hide();
if (!_showAnimation) {
if (animationParams) {
auto direction = back ? Window::SlideDirection::FromLeft : Window::SlideDirection::FromRight;
_dialogs->showAnimated(direction, animationParams);
} else {
_dialogs->showFast();
2014-08-15 11:19:32 +00:00
}
}
} else {
const auto nowActivePeer = _controller->activeChatCurrent().peer();
if (nowActivePeer && nowActivePeer != wasActivePeer) {
session().api().views().removeIncremented(nowActivePeer);
}
if (isOneColumn() && _dialogs && !_dialogs->isHidden()) {
_dialogs->hide();
}
if (!_showAnimation) {
if (!animationParams.oldContentCache.isNull()) {
_history->showAnimated(
back
? Window::SlideDirection::FromLeft
: Window::SlideDirection::FromRight,
animationParams);
} else {
_history->show();
2021-02-25 15:12:51 +00:00
crl::on_main(this, [=] {
_controller->widget()->setInnerFocus();
2019-01-03 12:36:01 +00:00
});
}
}
}
if (_dialogs && !_dialogs->isHidden()) {
if (!back) {
if (const auto history = _history->history()) {
2018-11-16 13:36:42 +00:00
_dialogs->scrollToEntry(Dialogs::RowDescriptor(
history,
FullMsgId(history->peer->id, showAtMsgId)));
}
}
_dialogs->update();
}
2018-11-05 13:18:54 +00:00
floatPlayerCheckVisibility();
}
2022-11-01 07:18:56 +00:00
void MainWidget::showMessage(
not_null<const HistoryItem*> item,
const SectionShow &params) {
const auto peerId = item->history()->peer->id;
const auto itemId = item->id;
if (!v::is_null(params.origin)) {
if (_mainSection) {
if (_mainSection->showMessage(peerId, params, itemId)) {
return;
}
} else if (_history->peer() == item->history()->peer) {
showHistory(peerId, params, itemId);
2022-11-01 07:18:56 +00:00
return;
}
}
if (const auto topic = item->topic()) {
_controller->showTopic(topic, item->id, params);
} else {
_controller->showPeerHistory(
item->history(),
params,
item->id);
}
}
void MainWidget::showForum(
not_null<Data::Forum*> forum,
const SectionShow &params) {
Expects(isPrimary() || (singlePeer() && singlePeer()->forum() == forum));
_dialogs->showForum(forum, params);
if (params.activation != anim::activation::background) {
_controller->hideLayer();
}
}
PeerData *MainWidget::peer() const {
return _history->peer();
}
Ui::ChatTheme *MainWidget::customChatTheme() const {
return _history->customChatTheme();
}
void MainWidget::saveSectionInStack() {
if (_mainSection) {
if (auto memento = _mainSection->createMemento()) {
_stack.push_back(std::make_unique<StackItemSection>(
std::move(memento)));
} else {
return;
}
} else if (const auto history = _history->history()) {
_stack.push_back(std::make_unique<StackItemHistory>(
history,
_history->msgId(),
_history->replyReturns()));
} else {
return;
}
const auto raw = _stack.back().get();
raw->setThirdSectionWeak(_thirdSection.data());
raw->removeRequests(
) | rpl::start_with_next([=] {
for (auto i = begin(_stack); i != end(_stack); ++i) {
if (i->get() == raw) {
_stack.erase(i);
return;
}
}
}, raw->lifetime());
}
void MainWidget::showSection(
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> memento,
const SectionShow &params) {
if (_mainSection && _mainSection->showInternal(
memento.get(),
params)) {
if (params.activation != anim::activation::background) {
_controller->window().hideSettingsAndLayer();
}
if (const auto entry = _mainSection->activeChat(); entry.key) {
_controller->setActiveChatEntry(entry);
}
return;
//
// Now third section handles only its own showSection() requests.
// General showSection() should show layer or main_section instead.
//
//} else if (_thirdSection && _thirdSection->showInternal(
// &memento,
// params)) {
// return;
}
if (preventsCloseSection(
2020-12-14 14:48:10 +00:00
[=] { showSection(memento, params); },
params)) {
return;
}
// If the window was not resized, but we've enabled
// tabbedSelectorSectionEnabled or thirdSectionInfoEnabled
// we need to update adaptive layout to Adaptive::ThirdColumn().
updateColumnLayout();
2020-12-14 14:48:10 +00:00
showNewSection(std::move(memento), params);
}
void MainWidget::updateColumnLayout() {
updateWindowAdaptiveLayout();
}
Window::SectionSlideParams MainWidget::prepareThirdSectionAnimation(Window::SectionWidget *section) {
Expects(_thirdSection != nullptr);
Window::SectionSlideParams result;
result.withTopBarShadow = section->hasTopBarShadow();
if (!_thirdSection->hasTopBarShadow()) {
result.withTopBarShadow = false;
}
2018-11-05 13:18:54 +00:00
floatPlayerHideAll();
result.oldContentCache = _thirdSection->grabForShowAnimation(result);
2018-11-05 13:18:54 +00:00
floatPlayerShowVisible();
return result;
}
Window::SectionSlideParams MainWidget::prepareShowAnimation(
bool willHaveTopBarShadow) {
Window::SectionSlideParams result;
result.withTopBarShadow = willHaveTopBarShadow;
if (_mainSection) {
if (!_mainSection->hasTopBarShadow()) {
result.withTopBarShadow = false;
}
} else if (!_history->peer()) {
result.withTopBarShadow = false;
}
2018-11-05 13:18:54 +00:00
floatPlayerHideAll();
if (_player) {
_player->entity()->hideShadowAndDropdowns();
}
const auto playerPlaylistVisible = !_playerPlaylist->isHidden();
if (playerPlaylistVisible) {
_playerPlaylist->hide();
}
const auto hiderVisible = (_hider && !_hider->isHidden());
if (hiderVisible) {
_hider->hide();
}
auto sectionTop = getMainSectionTop();
if (_mainSection) {
result.oldContentCache = _mainSection->grabForShowAnimation(result);
} else if (!isOneColumn() || !_history->isHidden()) {
2018-04-06 16:23:09 +00:00
result.oldContentCache = _history->grabForShowAnimation(result);
2018-07-24 18:37:22 +00:00
} else {
result.oldContentCache = Ui::GrabWidget(this, QRect(
0,
sectionTop,
_dialogsWidth,
height() - sectionTop));
}
if (_hider && hiderVisible) {
_hider->show();
}
if (playerPlaylistVisible) {
_playerPlaylist->show();
}
if (_player) {
_player->entity()->showShadowAndDropdowns();
}
2018-11-05 13:18:54 +00:00
floatPlayerShowVisible();
return result;
}
Window::SectionSlideParams MainWidget::prepareMainSectionAnimation(Window::SectionWidget *section) {
return prepareShowAnimation(section->hasTopBarShadow());
}
Window::SectionSlideParams MainWidget::prepareHistoryAnimation(PeerId historyPeerId) {
return prepareShowAnimation(historyPeerId != 0);
}
Window::SectionSlideParams MainWidget::prepareDialogsAnimation() {
return prepareShowAnimation(false);
}
void MainWidget::showNewSection(
2020-12-14 14:48:10 +00:00
std::shared_ptr<Window::SectionMemento> memento,
const SectionShow &params) {
using Column = Window::Column;
if (_controller->window().locked()) {
return;
}
auto saveInStack = (params.way == SectionShow::Way::Forward);
2020-03-24 12:44:15 +00:00
const auto thirdSectionTop = getThirdSectionTop();
const auto newThirdGeometry = QRect(
width() - st::columnMinimalWidthThird,
thirdSectionTop,
st::columnMinimalWidthThird,
height() - thirdSectionTop);
auto newThirdSection = (isThreeColumn() && params.thirdColumn)
? memento->createWidget(
this,
_controller,
Column::Third,
newThirdGeometry)
: nullptr;
2020-03-24 12:44:15 +00:00
const auto layerRect = parentWidget()->rect();
if (newThirdSection) {
saveInStack = false;
} else if (auto layer = memento->createLayer(_controller, layerRect)) {
if (params.activation != anim::activation::background) {
_controller->hideLayer(anim::type::instant);
}
_controller->showSpecialLayer(std::move(layer));
return;
}
if (params.activation != anim::activation::background) {
_controller->window().hideSettingsAndLayer();
}
_controller->setDialogsListFocused(false);
2019-04-02 09:13:30 +00:00
_a_dialogsWidth.stop();
auto mainSectionTop = getMainSectionTop();
auto newMainGeometry = QRect(
_history->x(),
mainSectionTop,
_history->width(),
height() - mainSectionTop);
auto newMainSection = newThirdSection
? nullptr
: memento->createWidget(
this,
_controller,
isOneColumn() ? Column::First : Column::Second,
newMainGeometry);
Assert(newMainSection || newThirdSection);
auto animatedShow = [&] {
if (_showAnimation
|| Core::App().passcodeLocked()
|| (params.animated == anim::type::instant)
|| memento->instant()) {
return false;
}
if (!isOneColumn() && params.way == SectionShow::Way::ClearStack) {
return false;
} else if (isOneColumn()
|| (newThirdSection && _thirdSection)
|| (newMainSection && isMainSectionShown())) {
return true;
}
return false;
}();
auto animationParams = animatedShow
? (newThirdSection
? prepareThirdSectionAnimation(newThirdSection)
: prepareMainSectionAnimation(newMainSection))
: Window::SectionSlideParams();
setFocus(); // otherwise dialogs widget could be focused.
if (saveInStack) {
// This may modify the current section, for example remove its contents.
saveSectionInStack();
}
auto &settingSection = newThirdSection
? _thirdSection
: _mainSection;
if (newThirdSection) {
_thirdSection = std::move(newThirdSection);
_thirdSection->removeRequests(
) | rpl::start_with_next([=] {
_thirdSection.destroy();
_thirdShadow.destroy();
updateControlsGeometry();
}, _thirdSection->lifetime());
if (!_thirdShadow) {
_thirdShadow.create(this);
_thirdShadow->show();
orderWidgets();
}
updateControlsGeometry();
} else {
_mainSection = std::move(newMainSection);
_history->finishAnimating();
_history->showHistory(0, 0);
if (const auto entry = _mainSection->activeChat(); entry.key) {
_controller->setActiveChatEntry(entry);
}
// Depends on SessionController::activeChatEntry
// for tabbed selector showing in the third column.
updateControlsGeometry();
_history->hide();
if (isOneColumn() && _dialogs) {
_dialogs->hide();
}
}
if (animationParams) {
auto back = (params.way == SectionShow::Way::Backward);
auto direction = (back || settingSection->forceAnimateBack())
? Window::SlideDirection::FromLeft
: Window::SlideDirection::FromRight;
if (isOneColumn()) {
_controller->removeLayerBlackout();
}
settingSection->showAnimated(direction, animationParams);
} else {
settingSection->showFast();
}
2018-11-05 13:18:54 +00:00
floatPlayerCheckVisibility();
orderWidgets();
}
void MainWidget::checkMainSectionToLayer() {
if (!_mainSection) {
return;
}
2017-09-25 16:06:53 +00:00
Ui::FocusPersister persister(this);
if (auto layer = _mainSection->moveContentToLayer(rect())) {
dropMainSection(_mainSection);
_controller->showSpecialLayer(
std::move(layer),
anim::type::instant);
}
}
void MainWidget::dropMainSection(Window::SectionWidget *widget) {
if (_mainSection != widget) {
return;
}
_mainSection.destroy();
_controller->showBackFromStack(
SectionShow(
anim::type::instant,
anim::activation::background));
}
PeerData *MainWidget::singlePeer() const {
return _controller->singlePeer();
}
bool MainWidget::isPrimary() const {
return _controller->isPrimary();
}
bool MainWidget::isMainSectionShown() const {
return _mainSection || _history->peer();
}
bool MainWidget::isThirdSectionShown() const {
return _thirdSection != nullptr;
}
2022-02-05 15:15:24 +00:00
Dialogs::RowDescriptor MainWidget::resolveChatNext(
Dialogs::RowDescriptor from) const {
return _dialogs ? _dialogs->resolveChatNext(from) : Dialogs::RowDescriptor();
}
Dialogs::RowDescriptor MainWidget::resolveChatPrevious(
Dialogs::RowDescriptor from) const {
return _dialogs ? _dialogs->resolveChatPrevious(from) : Dialogs::RowDescriptor();
}
bool MainWidget::stackIsEmpty() const {
return _stack.empty();
}
bool MainWidget::preventsCloseSection(Fn<void()> callback) const {
if (Core::App().passcodeLocked()) {
return false;
}
auto copy = callback;
return (_mainSection && _mainSection->preventsClose(std::move(copy)))
|| (_history && _history->preventsClose(std::move(callback)));
}
bool MainWidget::preventsCloseSection(
Fn<void()> callback,
const SectionShow &params) const {
return !params.thirdColumn
&& (params.activation != anim::activation::background)
&& preventsCloseSection(std::move(callback));
}
void MainWidget::showBackFromStack(
const SectionShow &params) {
if (preventsCloseSection([=] { showBackFromStack(params); }, params)) {
return;
}
if (_stack.empty()) {
if (isPrimary()) {
_controller->clearSectionStack(params);
}
2021-02-25 15:12:51 +00:00
crl::on_main(this, [=] {
_controller->widget()->setInnerFocus();
2019-01-03 12:36:01 +00:00
});
2015-07-03 08:47:16 +00:00
return;
}
auto item = std::move(_stack.back());
2014-08-15 11:19:32 +00:00
_stack.pop_back();
if (auto currentHistoryPeer = _history->peer()) {
clearBotStartToken(currentHistoryPeer);
}
_thirdSectionFromStack = item->takeThirdSectionMemento();
2014-08-15 11:19:32 +00:00
if (item->type() == HistoryStackItem) {
auto historyItem = static_cast<StackItemHistory*>(item.get());
_controller->showPeerHistory(
historyItem->peer()->id,
params.withWay(SectionShow::Way::Backward),
ShowAtUnreadMsgId);
_history->setReplyReturns(
historyItem->peer()->id,
std::move(historyItem->replyReturns));
} else if (item->type() == SectionStackItem) {
auto sectionItem = static_cast<StackItemSection*>(item.get());
showNewSection(
sectionItem->takeMemento(),
params.withWay(SectionShow::Way::Backward));
}
if (_thirdSectionFromStack && _thirdSection) {
_controller->showSection(
base::take(_thirdSectionFromStack),
SectionShow(
SectionShow::Way::ClearStack,
anim::type::instant,
anim::activation::background));
}
}
void MainWidget::orderWidgets() {
if (_dialogs) {
2023-07-28 08:10:01 +00:00
_dialogs->raiseWithTooltip();
}
if (_player) {
_player->raise();
}
if (_exportTopBar) {
_exportTopBar->raise();
}
if (_callTopBar) {
_callTopBar->raise();
}
if (_sideShadow) {
_sideShadow->raise();
}
if (_thirdShadow) {
_thirdShadow->raise();
}
if (_firstColumnResizeArea) {
_firstColumnResizeArea->raise();
}
if (_thirdColumnResizeArea) {
_thirdColumnResizeArea->raise();
}
if (_connecting) {
_connecting->raise();
}
2018-11-05 13:18:54 +00:00
floatPlayerRaiseAll();
_playerPlaylist->raise();
if (_player) {
_player->entity()->raiseDropdowns();
}
if (_hider) _hider->raise();
}
QPixmap MainWidget::grabForShowAnimation(const Window::SectionSlideParams &params) {
QPixmap result;
2018-11-05 13:18:54 +00:00
floatPlayerHideAll();
if (_player) {
_player->entity()->hideShadowAndDropdowns();
}
const auto playerPlaylistVisible = !_playerPlaylist->isHidden();
if (playerPlaylistVisible) {
_playerPlaylist->hide();
}
const auto hiderVisible = (_hider && !_hider->isHidden());
if (hiderVisible) {
_hider->hide();
}
auto sectionTop = getMainSectionTop();
if (isOneColumn()) {
result = Ui::GrabWidget(this, QRect(
2017-11-12 16:50:58 +00:00
0,
sectionTop,
width(),
2017-11-12 16:50:58 +00:00
height() - sectionTop));
} else {
if (_sideShadow) {
_sideShadow->hide();
}
if (_thirdShadow) {
_thirdShadow->hide();
}
result = Ui::GrabWidget(this, QRect(
2017-11-12 16:50:58 +00:00
_dialogsWidth,
sectionTop,
width() - _dialogsWidth,
height() - sectionTop));
if (_sideShadow) {
_sideShadow->show();
}
if (_thirdShadow) {
_thirdShadow->show();
}
}
if (_hider && hiderVisible) {
_hider->show();
}
if (playerPlaylistVisible) {
_playerPlaylist->show();
}
if (_player) {
_player->entity()->showShadowAndDropdowns();
}
2018-11-05 13:18:54 +00:00
floatPlayerShowVisible();
return result;
}
void MainWidget::windowShown() {
_history->windowShown();
}
void MainWidget::dialogsToUp() {
if (_dialogs) {
_dialogs->jumpToTop();
}
}
void MainWidget::checkActivation() {
_history->checkActivation();
if (_mainSection) {
_mainSection->checkActivation();
}
}
void MainWidget::showAnimated(QPixmap oldContentCache, bool back) {
_showAnimation = nullptr;
showAll();
floatPlayerHideAll();
auto newContentCache = Ui::GrabWidget(this);
2015-10-17 14:52:26 +00:00
hideAll();
floatPlayerShowVisible();
_showAnimation = std::make_unique<Window::SlideAnimation>();
_showAnimation->setDirection(back
? Window::SlideDirection::FromLeft
: Window::SlideDirection::FromRight);
_showAnimation->setRepaintCallback([=] { update(); });
_showAnimation->setFinishedCallback([=] { showFinished(); });
_showAnimation->setPixmaps(oldContentCache, newContentCache);
_showAnimation->start();
show();
}
void MainWidget::showFinished() {
_showAnimation = nullptr;
showAll();
activate();
2015-10-17 14:52:26 +00:00
}
void MainWidget::paintEvent(QPaintEvent *e) {
2019-01-16 12:25:29 +00:00
if (_background) {
checkChatBackground();
}
if (_showAnimation) {
auto p = QPainter(this);
_showAnimation->paintContents(p);
}
}
int MainWidget::getMainSectionTop() const {
return _callTopBarHeight + _exportTopBarHeight + _playerHeight;
}
int MainWidget::getThirdSectionTop() const {
return 0;
}
void MainWidget::hideAll() {
if (_dialogs) {
_dialogs->hide();
}
_history->hide();
if (_mainSection) {
_mainSection->hide();
}
if (_thirdSection) {
_thirdSection->hide();
}
if (_sideShadow) {
_sideShadow->hide();
}
if (_thirdShadow) {
_thirdShadow->hide();
}
if (_player) {
_player->setVisible(false);
_playerHeight = 0;
}
2020-06-25 17:57:36 +00:00
if (_callTopBar) {
_callTopBar->setVisible(false);
_callTopBarHeight = 0;
}
}
void MainWidget::showAll() {
if (cPasswordRecovered()) {
cSetPasswordRecovered(false);
_controller->show(Ui::MakeInformBox(
tr::lng_cloud_password_updated()));
}
if (isOneColumn()) {
if (_sideShadow) {
_sideShadow->hide();
}
if (_hider) {
_hider->hide();
}
if (_mainSection) {
_mainSection->show();
} else if (_history->peer()) {
_history->show();
_history->updateControlsGeometry();
} else {
Assert(_dialogs != nullptr);
_dialogs->showFast();
_history->hide();
}
if (_dialogs && isMainSectionShown()) {
_dialogs->hide();
}
} else {
if (_sideShadow) {
_sideShadow->show();
}
if (_hider) {
_hider->show();
}
if (_dialogs) {
_dialogs->showFast();
}
if (_mainSection) {
_mainSection->show();
} else {
_history->show();
_history->updateControlsGeometry();
}
if (_thirdSection) {
_thirdSection->show();
}
if (_thirdShadow) {
_thirdShadow->show();
}
}
if (_player) {
_player->setVisible(true);
_playerHeight = _player->contentHeight();
}
2020-06-25 17:57:36 +00:00
if (_callTopBar) {
_callTopBar->setVisible(true);
// show() could've send pending resize event that would update
// the height value and destroy the top bar if it was hiding.
if (_callTopBar) {
_callTopBarHeight = _callTopBar->height();
}
2020-06-25 17:57:36 +00:00
}
updateControlsGeometry();
2018-11-05 13:18:54 +00:00
floatPlayerCheckVisibility();
_controller->widget()->checkActivation();
}
void MainWidget::resizeEvent(QResizeEvent *e) {
updateControlsGeometry();
}
void MainWidget::updateControlsGeometry() {
if (!width()) {
return;
}
updateWindowAdaptiveLayout();
if (_dialogs) {
if (Core::App().settings().dialogsWidthRatio() > 0) {
_a_dialogsWidth.stop();
}
if (!_a_dialogsWidth.animating()) {
_dialogs->stopWidthAnimation();
}
}
if (isThreeColumn()) {
2017-09-19 10:32:34 +00:00
if (!_thirdSection
&& !_controller->takeThirdSectionFromLayer()) {
auto params = Window::SectionShow(
Window::SectionShow::Way::ClearStack,
anim::type::instant,
anim::activation::background);
const auto active = _controller->activeChatCurrent();
2022-10-25 08:07:47 +00:00
if (const auto thread = active.thread()) {
2020-06-25 12:16:09 +00:00
if (Core::App().settings().tabbedSelectorSectionEnabled()) {
if (_mainSection) {
2022-10-25 08:07:47 +00:00
_mainSection->pushTabbedSelectorToThirdSection(
thread,
params);
} else {
2022-10-25 08:07:47 +00:00
_history->pushTabbedSelectorToThirdSection(
thread,
params);
}
2020-06-25 12:16:09 +00:00
} else if (Core::App().settings().thirdSectionInfoEnabled()) {
2018-01-22 19:51:38 +00:00
_controller->showSection(
2022-10-25 08:07:47 +00:00
(thread->asTopic()
? std::make_shared<Info::Memento>(
thread->asTopic())
: Info::Memento::Default(
thread->asHistory()->peer)),
2018-01-22 19:51:38 +00:00
params.withThirdColumn());
}
}
}
} else {
_thirdSection.destroy();
_thirdShadow.destroy();
}
const auto mainSectionTop = getMainSectionTop();
auto dialogsWidth = _dialogs
? qRound(_a_dialogsWidth.value(_dialogsWidth))
: isOneColumn()
? width()
: 0;
if (isOneColumn()) {
if (_callTopBar) {
_callTopBar->resizeToWidth(dialogsWidth);
_callTopBar->moveToLeft(0, 0);
}
if (_exportTopBar) {
_exportTopBar->resizeToWidth(dialogsWidth);
_exportTopBar->moveToLeft(0, _callTopBarHeight);
}
if (_player) {
_player->resizeToWidth(dialogsWidth);
_player->moveToLeft(0, _callTopBarHeight + _exportTopBarHeight);
}
const auto mainSectionGeometry = QRect(
0,
mainSectionTop,
dialogsWidth,
height() - mainSectionTop);
if (_dialogs) {
_dialogs->setGeometryWithTopMoved(
mainSectionGeometry,
_contentScrollAddToY);
}
_history->setGeometryWithTopMoved(
mainSectionGeometry,
_contentScrollAddToY);
if (_hider) _hider->setGeometry(0, 0, dialogsWidth, height());
} else {
2017-11-12 16:50:58 +00:00
auto thirdSectionWidth = _thirdSection ? _thirdColumnWidth : 0;
if (_thirdSection) {
auto thirdSectionTop = getThirdSectionTop();
_thirdSection->setGeometry(
width() - thirdSectionWidth,
thirdSectionTop,
thirdSectionWidth,
height() - thirdSectionTop);
}
const auto shadowTop = _controller->window().verticalShadowTop();
const auto shadowHeight = height() - shadowTop;
if (_dialogs) {
accumulate_min(
dialogsWidth,
width() - st::columnMinimalWidthMain);
_dialogs->setGeometryToLeft(0, 0, dialogsWidth, height());
}
if (_sideShadow) {
_sideShadow->setGeometryToLeft(
dialogsWidth,
shadowTop,
st::lineWidth,
shadowHeight);
}
if (_thirdShadow) {
_thirdShadow->setGeometryToLeft(
width() - thirdSectionWidth - st::lineWidth,
shadowTop,
st::lineWidth,
shadowHeight);
}
const auto mainSectionWidth = width()
- dialogsWidth
- thirdSectionWidth;
if (_callTopBar) {
_callTopBar->resizeToWidth(mainSectionWidth);
_callTopBar->moveToLeft(dialogsWidth, 0);
}
if (_exportTopBar) {
_exportTopBar->resizeToWidth(mainSectionWidth);
_exportTopBar->moveToLeft(dialogsWidth, _callTopBarHeight);
}
if (_player) {
_player->resizeToWidth(mainSectionWidth);
_player->moveToLeft(
dialogsWidth,
_callTopBarHeight + _exportTopBarHeight);
}
_history->setGeometryWithTopMoved(QRect(
dialogsWidth,
mainSectionTop,
mainSectionWidth,
height() - mainSectionTop
), _contentScrollAddToY);
if (_hider) {
_hider->setGeometryToLeft(
dialogsWidth,
0,
mainSectionWidth,
height());
}
}
if (_mainSection) {
const auto mainSectionGeometry = QRect(
_history->x(),
mainSectionTop,
_history->width(),
height() - mainSectionTop);
_mainSection->setGeometryWithTopMoved(
mainSectionGeometry,
_contentScrollAddToY);
}
refreshResizeAreas();
if (_player) {
_player->entity()->updateDropdownsGeometry();
}
updateMediaPlaylistPosition(_playerPlaylist->x());
2015-07-03 08:47:16 +00:00
_contentScrollAddToY = 0;
2018-11-05 11:16:09 +00:00
2018-11-05 13:18:54 +00:00
floatPlayerUpdatePositions();
2015-07-03 08:47:16 +00:00
}
void MainWidget::refreshResizeAreas() {
if (!isOneColumn() && _dialogs) {
ensureFirstColumnResizeAreaCreated();
_firstColumnResizeArea->setGeometryToLeft(
_history->x(),
0,
st::historyResizeWidth,
height());
} else if (_firstColumnResizeArea) {
_firstColumnResizeArea.destroy();
}
if (isThreeColumn() && _thirdSection) {
ensureThirdColumnResizeAreaCreated();
_thirdColumnResizeArea->setGeometryToLeft(
_thirdSection->x(),
0,
st::historyResizeWidth,
height());
} else if (_thirdColumnResizeArea) {
_thirdColumnResizeArea.destroy();
}
}
template <typename MoveCallback, typename FinishCallback>
void MainWidget::createResizeArea(
object_ptr<Ui::ResizeArea> &area,
MoveCallback &&moveCallback,
FinishCallback &&finishCallback) {
area.create(this);
area->show();
area->addMoveLeftCallback(
std::forward<MoveCallback>(moveCallback));
area->addMoveFinishedCallback(
std::forward<FinishCallback>(finishCallback));
orderWidgets();
}
void MainWidget::ensureFirstColumnResizeAreaCreated() {
Expects(_dialogs != nullptr);
if (_firstColumnResizeArea) {
return;
}
auto moveLeftCallback = [=](int globalLeft) {
auto newWidth = globalLeft - mapToGlobal(QPoint(0, 0)).x();
2017-11-12 16:50:58 +00:00
auto newRatio = (newWidth < st::columnMinimalWidthLeft / 2)
? 0.
: float64(newWidth) / width();
2020-06-25 12:16:09 +00:00
Core::App().settings().setDialogsWidthRatio(newRatio);
};
auto moveFinishedCallback = [=] {
if (isOneColumn()) {
2017-11-12 16:50:58 +00:00
return;
}
2020-06-25 12:16:09 +00:00
if (Core::App().settings().dialogsWidthRatio() > 0) {
Core::App().settings().setDialogsWidthRatio(
float64(_dialogsWidth) / width());
}
2020-06-25 12:16:09 +00:00
Core::App().saveSettingsDelayed();
};
createResizeArea(
_firstColumnResizeArea,
std::move(moveLeftCallback),
std::move(moveFinishedCallback));
}
void MainWidget::ensureThirdColumnResizeAreaCreated() {
if (_thirdColumnResizeArea) {
return;
}
auto moveLeftCallback = [=](int globalLeft) {
2017-11-12 16:50:58 +00:00
auto newWidth = mapToGlobal(QPoint(width(), 0)).x() - globalLeft;
2020-06-25 12:16:09 +00:00
Core::App().settings().setThirdColumnWidth(newWidth);
};
auto moveFinishedCallback = [=] {
if (!isThreeColumn() || !_thirdSection) {
2017-11-12 16:50:58 +00:00
return;
}
Core::App().settings().setThirdColumnWidth(std::clamp(
2020-06-25 12:16:09 +00:00
Core::App().settings().thirdColumnWidth(),
2017-11-12 16:50:58 +00:00
st::columnMinimalWidthThird,
st::columnMaximalWidthThird));
2020-06-25 12:16:09 +00:00
Core::App().saveSettingsDelayed();
};
createResizeArea(
_thirdColumnResizeArea,
std::move(moveLeftCallback),
std::move(moveFinishedCallback));
}
void MainWidget::updateDialogsWidthAnimated() {
if (!_dialogs || Core::App().settings().dialogsWidthRatio() > 0) {
return;
}
auto dialogsWidth = _dialogsWidth;
updateWindowAdaptiveLayout();
2020-06-25 12:16:09 +00:00
if (!Core::App().settings().dialogsWidthRatio()
&& (_dialogsWidth != dialogsWidth
|| _a_dialogsWidth.animating())) {
_dialogs->startWidthAnimation();
_a_dialogsWidth.start(
[this] { updateControlsGeometry(); },
dialogsWidth,
_dialogsWidth,
st::dialogsWidthDuration,
anim::easeOutCirc);
updateControlsGeometry();
}
}
bool MainWidget::saveThirdSectionToStackBack() const {
return !_stack.empty()
&& _thirdSection != nullptr
&& _stack.back()->thirdSectionWeak() == _thirdSection.data();
}
auto MainWidget::thirdSectionForCurrentMainSection(
Dialogs::Key key)
2020-12-14 14:48:10 +00:00
-> std::shared_ptr<Window::SectionMemento> {
if (_thirdSectionFromStack) {
return std::move(_thirdSectionFromStack);
2022-09-27 12:05:47 +00:00
} else if (const auto topic = key.topic()) {
return std::make_shared<Info::Memento>(topic);
} else if (const auto peer = key.peer()) {
2020-12-14 14:48:10 +00:00
return std::make_shared<Info::Memento>(
2020-06-08 09:06:50 +00:00
peer,
2019-04-15 11:54:03 +00:00
Info::Memento::DefaultSection(peer));
}
2018-01-22 19:51:38 +00:00
Unexpected("Key in MainWidget::thirdSectionForCurrentMainSection().");
}
void MainWidget::updateThirdColumnToCurrentChat(
Dialogs::Key key,
bool canWrite) {
auto saveOldThirdSection = [&] {
if (saveThirdSectionToStackBack()) {
_stack.back()->setThirdSectionMemento(
_thirdSection->createMemento());
_thirdSection.destroy();
}
};
2020-06-25 12:16:09 +00:00
auto &settings = Core::App().settings();
auto params = Window::SectionShow(
Window::SectionShow::Way::ClearStack,
anim::type::instant,
anim::activation::background);
auto switchInfoFast = [&] {
saveOldThirdSection();
//
// Like in _controller->showPeerInfo()
//
if (isThreeColumn()
2020-06-25 12:16:09 +00:00
&& !settings.thirdSectionInfoEnabled()) {
settings.setThirdSectionInfoEnabled(true);
Core::App().saveSettingsDelayed();
}
_controller->showSection(
thirdSectionForCurrentMainSection(key),
params.withThirdColumn());
};
2022-10-25 08:07:47 +00:00
auto switchTabbedFast = [&](not_null<Data::Thread*> thread) {
saveOldThirdSection();
return _mainSection
2022-10-25 08:07:47 +00:00
? _mainSection->pushTabbedSelectorToThirdSection(thread, params)
: _history->pushTabbedSelectorToThirdSection(thread, params);
};
if (isThreeColumn()
2020-06-25 12:16:09 +00:00
&& settings.tabbedSelectorSectionEnabled()
&& key) {
if (!canWrite) {
switchInfoFast();
2020-06-25 12:16:09 +00:00
settings.setTabbedSelectorSectionEnabled(true);
settings.setTabbedReplacedWithInfo(true);
} else if (settings.tabbedReplacedWithInfo()
2022-10-25 08:07:47 +00:00
&& key.thread()
&& switchTabbedFast(key.thread())) {
2020-06-25 12:16:09 +00:00
settings.setTabbedReplacedWithInfo(false);
2017-09-19 19:44:34 +00:00
}
} else {
2020-06-25 12:16:09 +00:00
settings.setTabbedReplacedWithInfo(false);
if (!key) {
2017-11-08 16:45:30 +00:00
if (_thirdSection) {
_thirdSection.destroy();
_thirdShadow.destroy();
updateControlsGeometry();
}
} else if (isThreeColumn()
2020-06-25 12:16:09 +00:00
&& settings.thirdSectionInfoEnabled()) {
switchInfoFast();
2017-09-19 19:44:34 +00:00
}
}
}
void MainWidget::updateMediaPlaylistPosition(int x) {
if (_player) {
auto playlistLeft = x;
auto playlistWidth = _playerPlaylist->width();
auto playlistTop = _player->y() + _player->height();
auto rightEdge = width();
if (playlistLeft + playlistWidth > rightEdge) {
playlistLeft = rightEdge - playlistWidth;
} else if (playlistLeft < 0) {
playlistLeft = 0;
}
_playerPlaylist->move(playlistLeft, playlistTop);
}
}
void MainWidget::returnTabbedSelector() {
if (!_mainSection || !_mainSection->returnTabbedSelector()) {
_history->returnTabbedSelector();
}
}
bool MainWidget::eventFilter(QObject *o, QEvent *e) {
const auto widget = o->isWidgetType()
? static_cast<QWidget*>(o)
: nullptr;
if (e->type() == QEvent::FocusIn) {
if (widget && (widget->window() == window())) {
if (_history == widget || _history->isAncestorOf(widget)
|| (_mainSection
&& (_mainSection == widget
|| _mainSection->isAncestorOf(widget)))
|| (_thirdSection
&& (_thirdSection == widget
|| _thirdSection->isAncestorOf(widget)))) {
_controller->setDialogsListFocused(false);
} else if (_dialogs
&& (_dialogs == widget
|| _dialogs->isAncestorOf(widget))) {
_controller->setDialogsListFocused(true);
}
}
} else if (e->type() == QEvent::MouseButtonPress) {
if (widget && (widget->window() == window())) {
const auto event = static_cast<QMouseEvent*>(e);
if (event->button() == Qt::BackButton) {
if (!Core::App().hideMediaView()) {
handleHistoryBack();
}
return true;
}
}
2018-11-05 11:16:09 +00:00
} else if (e->type() == QEvent::Wheel) {
if (widget && (widget->window() == window())) {
if (const auto result = floatPlayerFilterWheelEvent(o, e)) {
return *result;
}
}
}
2018-11-05 11:16:09 +00:00
return RpWidget::eventFilter(o, e);
}
void MainWidget::handleAdaptiveLayoutUpdate() {
showAll();
if (_sideShadow) {
_sideShadow->setVisible(!isOneColumn());
}
if (_player) {
_player->updateAdaptiveLayout();
}
}
void MainWidget::handleHistoryBack() {
const auto openedFolder = _controller->openedFolder().current();
const auto openedForum = _controller->shownForum().current();
const auto rootPeer = !_stack.empty()
? _stack.front()->peer()
: _history->peer()
? _history->peer()
: _mainSection
? _mainSection->activeChat().key.peer()
: nullptr;
const auto rootHistory = rootPeer
? rootPeer->owner().historyLoaded(rootPeer)
: nullptr;
const auto rootFolder = rootHistory ? rootHistory->folder() : nullptr;
2022-12-02 09:48:30 +00:00
if (openedForum && (!rootPeer || rootPeer->forum() != openedForum)) {
_controller->closeForum();
} else if (!openedFolder
|| (rootFolder == openedFolder)
|| (!_dialogs || _dialogs->isHidden())) {
_controller->showBackFromStack();
if (_dialogs) {
_dialogs->setInnerFocus();
}
} else {
_controller->closeFolder();
}
}
void MainWidget::updateWindowAdaptiveLayout() {
auto layout = _controller->computeColumnLayout();
2020-06-25 12:16:09 +00:00
auto dialogsWidthRatio = Core::App().settings().dialogsWidthRatio();
// Check if we are in a single-column layout in a wide enough window
// for the normal layout. If so, switch to the normal layout.
if (layout.windowLayout == Window::Adaptive::WindowLayout::OneColumn) {
auto chatWidth = layout.chatWidth;
2019-01-18 12:27:37 +00:00
//if (session().settings().tabbedSelectorSectionEnabled()
// && chatWidth >= _history->minimalWidthForTabbedSelectorSection()) {
// chatWidth -= _history->tabbedSelectorSectionWidth();
//}
auto minimalNormalWidth = st::columnMinimalWidthLeft
+ st::columnMinimalWidthMain;
if (chatWidth >= minimalNormalWidth) {
// Switch layout back to normal in a wide enough window.
layout.windowLayout = Window::Adaptive::WindowLayout::Normal;
layout.dialogsWidth = st::columnMinimalWidthLeft;
layout.chatWidth = layout.bodyWidth - layout.dialogsWidth;
dialogsWidthRatio = float64(layout.dialogsWidth) / layout.bodyWidth;
}
}
// Check if we are going to create the third column and shrink the
// dialogs widget to provide a wide enough chat history column.
// Don't shrink the column on the first call, when window is inited.
if (layout.windowLayout == Window::Adaptive::WindowLayout::ThreeColumn
2020-06-16 14:19:23 +00:00
&& _controller->widget()->positionInited()) {
//auto chatWidth = layout.chatWidth;
//if (_history->willSwitchToTabbedSelectorWithWidth(chatWidth)) {
// auto thirdColumnWidth = _history->tabbedSelectorSectionWidth();
// auto twoColumnsWidth = (layout.bodyWidth - thirdColumnWidth);
// auto sameRatioChatWidth = twoColumnsWidth - qRound(dialogsWidthRatio * twoColumnsWidth);
2018-01-09 17:08:31 +00:00
// auto desiredChatWidth = qMax(sameRatioChatWidth, HistoryView::WideChatWidth());
// chatWidth -= thirdColumnWidth;
// auto extendChatBy = desiredChatWidth - chatWidth;
// accumulate_min(extendChatBy, layout.dialogsWidth - st::columnMinimalWidthLeft);
// if (extendChatBy > 0) {
// layout.dialogsWidth -= extendChatBy;
// layout.chatWidth += extendChatBy;
// dialogsWidthRatio = float64(layout.dialogsWidth) / layout.bodyWidth;
// }
//}
}
2020-06-25 12:16:09 +00:00
Core::App().settings().setDialogsWidthRatio(dialogsWidthRatio);
auto useSmallColumnWidth = !isOneColumn()
&& !dialogsWidthRatio
&& !_controller->forceWideDialogs();
_dialogsWidth = !_dialogs
? 0
: useSmallColumnWidth
? _controller->dialogsSmallColumnWidth()
: layout.dialogsWidth;
2017-11-12 16:50:58 +00:00
_thirdColumnWidth = layout.thirdWidth;
_controller->adaptive().setWindowLayout(layout.windowLayout);
}
int MainWidget::backgroundFromY() const {
return -getMainSectionTop();
}
2018-01-22 17:39:20 +00:00
void MainWidget::searchInChat(Dialogs::Key chat) {
2023-01-18 11:17:32 +00:00
searchMessages(QString(), chat);
}
bool MainWidget::contentOverlapped(const QRect &globalRect) {
return _history->contentOverlapped(globalRect)
|| _playerPlaylist->overlaps(globalRect);
}
void MainWidget::activate() {
if (_showAnimation) {
return;
} else if (const auto paths = cSendPaths(); !paths.isEmpty()) {
2022-11-26 21:20:17 +00:00
const auto interpret = u"interpret://"_q;
cSetSendPaths(QStringList());
if (paths[0].startsWith(interpret)) {
2022-11-01 07:18:56 +00:00
const auto error = Support::InterpretSendPath(
_controller,
paths[0].mid(interpret.size()));
2022-11-01 07:18:56 +00:00
if (!error.isEmpty()) {
_controller->show(Ui::MakeInformBox(error));
2022-11-01 07:18:56 +00:00
}
} else {
const auto chosen = [=](not_null<Data::Thread*> thread) {
return sendPaths(thread, paths);
};
Window::ShowChooseRecipientBox(_controller, chosen);
2022-11-01 07:18:56 +00:00
}
} else if (_mainSection) {
_mainSection->setInnerFocus();
} else if (_hider) {
Assert(_dialogs != nullptr);
_dialogs->setInnerFocus();
2022-11-30 13:28:09 +00:00
} else if (!_controller->isLayerShown()) {
2022-11-01 07:18:56 +00:00
if (_history->peer()) {
_history->activate();
} else {
Assert(_dialogs != nullptr);
2019-04-24 14:12:35 +00:00
_dialogs->setInnerFocus();
}
}
2021-02-25 15:12:51 +00:00
_controller->widget()->fixOrder();
}
bool MainWidget::animatingShow() const {
return _showAnimation != nullptr;
}
bool MainWidget::isOneColumn() const {
return _controller->adaptive().isOneColumn();
}
bool MainWidget::isNormalColumn() const {
return _controller->adaptive().isNormal();
}
bool MainWidget::isThreeColumn() const {
return _controller->adaptive().isThreeColumn();
}