From dd68c7e90d7afa72f969bc54236ac06b07a84155 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 6 Jun 2019 14:20:21 +0300 Subject: [PATCH] Use Window::Controller to manage MainWindow. --- Telegram/SourceFiles/app.cpp | 5 +- Telegram/SourceFiles/core/application.cpp | 44 ++++----- Telegram/SourceFiles/core/application.h | 6 +- Telegram/SourceFiles/core/shortcuts.cpp | 3 +- Telegram/SourceFiles/mainwindow.cpp | 3 +- Telegram/SourceFiles/mainwindow.h | 2 +- .../media/view/media_view_overlay_widget.cpp | 7 +- .../platform/linux/main_window_linux.cpp | 3 +- .../platform/linux/main_window_linux.h | 2 +- .../platform/mac/main_window_mac.h | 9 +- .../platform/mac/main_window_mac.mm | 5 +- .../platform/win/main_window_win.cpp | 5 +- .../platform/win/main_window_win.h | 2 +- Telegram/SourceFiles/window/main_window.cpp | 18 ++-- Telegram/SourceFiles/window/main_window.h | 15 +-- .../SourceFiles/window/window_controller.cpp | 95 +++++++++++++++++++ .../SourceFiles/window/window_controller.h | 64 +++++++++++++ Telegram/gyp/telegram_sources.txt | 2 + 18 files changed, 229 insertions(+), 61 deletions(-) create mode 100644 Telegram/SourceFiles/window/window_controller.cpp create mode 100644 Telegram/SourceFiles/window/window_controller.h diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 9365fd7d04..e1eb5de806 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -29,6 +29,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "window/themes/window_theme.h" #include "window/notifications_manager.h" +#include "window/window_controller.h" #include "platform/platform_notifications_manager.h" #include "storage/file_upload.h" #include "storage/localstorage.h" @@ -102,8 +103,8 @@ namespace App { } MainWindow *wnd() { - return Core::IsAppLaunched() - ? Core::App().getActiveWindow() + return (Core::IsAppLaunched() && Core::App().activeWindow()) + ? Core::App().activeWindow()->widget().get() : nullptr; } diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index be7376de96..e8ee13faed 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -53,6 +53,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/effects/animations.h" #include "storage/serialize_common.h" #include "window/window_session_controller.h" +#include "window/window_controller.h" #include "base/qthelp_regex.h" #include "base/qthelp_url.h" #include "boxes/connection_box.h" @@ -171,12 +172,11 @@ void Application::run() { // Create mime database, so it won't be slow later. QMimeDatabase().mimeTypeForName(qsl("text/plain")); - _window = std::make_unique(); - _window->init(); + _window = std::make_unique(&activeAccount()); - auto currentGeometry = _window->geometry(); + const auto currentGeometry = _window->widget()->geometry(); _mediaView = std::make_unique(); - _window->setGeometry(currentGeometry); + _window->widget()->setGeometry(currentGeometry); QCoreApplication::instance()->installEventFilter(this); connect( @@ -223,8 +223,8 @@ void Application::run() { bool Application::hideMediaView() { if (_mediaView && !_mediaView->isHidden()) { _mediaView->hide(); - if (auto activeWindow = getActiveWindow()) { - activeWindow->reActivateWindow(); + if (const auto window = activeWindow()) { + window->reActivate(); } return true; } @@ -966,7 +966,7 @@ rpl::producer Application::lockValue() const { _1 || _2); } -MainWindow *Application::getActiveWindow() const { +Window::Controller *Application::activeWindow() const { return _window.get(); } @@ -974,10 +974,8 @@ bool Application::closeActiveWindow() { if (hideMediaView()) { return true; } - if (auto activeWindow = getActiveWindow()) { - if (!activeWindow->hideNoQuit()) { - activeWindow->close(); - } + if (const auto window = activeWindow()) { + window->close(); return true; } return false; @@ -985,19 +983,19 @@ bool Application::closeActiveWindow() { bool Application::minimizeActiveWindow() { hideMediaView(); - if (auto activeWindow = getActiveWindow()) { - if (Global::WorkMode().value() == dbiwmTrayOnly) { - activeWindow->minimizeToTray(); - } else { - activeWindow->setWindowState(Qt::WindowMinimized); - } + if (const auto window = activeWindow()) { + window->minimize(); return true; } return false; } QWidget *Application::getFileDialogParent() { - return (_mediaView && _mediaView->isVisible()) ? (QWidget*)_mediaView.get() : (QWidget*)getActiveWindow(); + return (_mediaView && _mediaView->isVisible()) + ? (QWidget*)_mediaView.get() + : activeWindow() + ? (QWidget*)activeWindow()->widget() + : nullptr; } void Application::checkMediaViewActivation() { @@ -1032,7 +1030,7 @@ void Application::loggedOut() { clearPasscodeLock(); Media::Player::mixer()->stopAndClear(); Global::SetVoiceMsgPlaybackDoubled(false); - if (const auto window = getActiveWindow()) { + if (const auto window = activeWindow()) { window->tempDirDelete(Local::ClearManagerAll); window->setupIntro(); } @@ -1051,12 +1049,8 @@ void Application::loggedOut() { } QPoint Application::getPointForCallPanelCenter() const { - if (auto activeWindow = getActiveWindow()) { - Assert(activeWindow->windowHandle() != nullptr); - if (activeWindow->isActive()) { - return activeWindow->geometry().center(); - } - return activeWindow->windowHandle()->screen()->geometry().center(); + if (const auto window = activeWindow()) { + return window->getPointForCallPanelCenter(); } return QApplication::desktop()->screenGeometry().center(); } diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index ad846c6e18..dfa66803aa 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/timer.h" class AuthSessionSettings; +class MainWindow; class MainWidget; class FileUploader; class Translator; @@ -23,6 +24,7 @@ class Databases; namespace Window { struct TermsLock; +class Controller; } // namespace Window namespace ChatHelpers { @@ -89,7 +91,7 @@ public: } // Windows interface. - MainWindow *getActiveWindow() const; + Window::Controller *activeWindow() const; bool closeActiveWindow(); bool minimizeActiveWindow(); QWidget *getFileDialogParent(); @@ -272,7 +274,7 @@ private: const std::unique_ptr _databases; const std::unique_ptr _animationsManager; const std::unique_ptr _account; - std::unique_ptr _window; + std::unique_ptr _window; std::unique_ptr _mediaView; const std::unique_ptr _langpack; std::unique_ptr _langCloudManager; diff --git a/Telegram/SourceFiles/core/shortcuts.cpp b/Telegram/SourceFiles/core/shortcuts.cpp index 702cc30264..911926f74a 100644 --- a/Telegram/SourceFiles/core/shortcuts.cpp +++ b/Telegram/SourceFiles/core/shortcuts.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwindow.h" #include "mainwidget.h" +#include "window/window_controller.h" #include "core/application.h" #include "media/player/media_player_instance.h" #include "platform/platform_info.h" @@ -375,7 +376,7 @@ void Manager::set(const QString &keys, Command command) { } auto shortcut = base::make_unique_q( result, - Core::App().getActiveWindow(), + Core::App().activeWindow()->widget().get(), nullptr, nullptr, Qt::ApplicationShortcut); diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 2d7f079842..4d3de62084 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -66,7 +66,8 @@ void FeedLangTestingKey(int key) { } // namespace -MainWindow::MainWindow() { +MainWindow::MainWindow(not_null controller) +: Platform::MainWindow(controller) { auto logo = Core::App().logo(); icon16 = logo.scaledToWidth(16, Qt::SmoothTransformation); icon32 = logo.scaledToWidth(32, Qt::SmoothTransformation); diff --git a/Telegram/SourceFiles/mainwindow.h b/Telegram/SourceFiles/mainwindow.h index 89aa6b98f3..620de804b6 100644 --- a/Telegram/SourceFiles/mainwindow.h +++ b/Telegram/SourceFiles/mainwindow.h @@ -44,7 +44,7 @@ class MainWindow : public Platform::MainWindow { Q_OBJECT public: - MainWindow(); + explicit MainWindow(not_null controller); ~MainWindow(); void firstShow(); diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 4d577ee53d..285c50fee2 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -36,6 +36,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_user.h" #include "window/themes/window_theme_preview.h" #include "window/window_peer_menu.h" +#include "window/window_controller.h" #include "main/main_account.h" // Account::sessionValue. #include "observer_peer.h" #include "auth_session.h" @@ -326,8 +327,10 @@ void OverlayWidget::moveToScreen(bool force) { } return nullptr; }; - const auto activeWindow = Core::App().getActiveWindow(); - const auto activeWindowScreen = widgetScreen(activeWindow); + const auto window = Core::App().activeWindow() + ? Core::App().activeWindow()->widget().get() + : nullptr; + const auto activeWindowScreen = widgetScreen(window); const auto myScreen = widgetScreen(this); if (activeWindowScreen && myScreen && myScreen != activeWindowScreen) { windowHandle()->setScreen(activeWindowScreen); diff --git a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp index ac2be9cd4c..9580364888 100644 --- a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp @@ -193,7 +193,8 @@ quint32 djbStringHash(QString string) { } // namespace -MainWindow::MainWindow() { +MainWindow::MainWindow(not_null controller) +: Window::MainWindow(controller) { connect(&_psCheckStatusIconTimer, SIGNAL(timeout()), this, SLOT(psStatusIconCheck())); _psCheckStatusIconTimer.setSingleShot(false); diff --git a/Telegram/SourceFiles/platform/linux/main_window_linux.h b/Telegram/SourceFiles/platform/linux/main_window_linux.h index c53ee57bb5..8d92280856 100644 --- a/Telegram/SourceFiles/platform/linux/main_window_linux.h +++ b/Telegram/SourceFiles/platform/linux/main_window_linux.h @@ -15,7 +15,7 @@ class MainWindow : public Window::MainWindow { Q_OBJECT public: - MainWindow(); + explicit MainWindow(not_null controller); void psFirstShow(); void psInitSysMenu(); diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.h b/Telegram/SourceFiles/platform/mac/main_window_mac.h index 8910c810d1..98160f4f4c 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.h +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.h @@ -18,7 +18,7 @@ class MainWindow : public Window::MainWindow { Q_OBJECT public: - MainWindow(); + explicit MainWindow(not_null controller); void psFirstShow(); void psInitSysMenu(); @@ -37,7 +37,7 @@ public: ~MainWindow(); - class Private; + void updateWindowIcon() override; public slots: void psShowTrayMenu(); @@ -56,7 +56,6 @@ protected: void handleActiveChangedHook() override; void stateChangedHook(Qt::WindowState state) override; void initHook() override; - void updateWindowIcon() override; void titleVisibilityChangedHook() override; void unreadCounterChangedHook() override; @@ -83,13 +82,15 @@ protected: void closeWithoutDestroy() override; private: + class Private; + friend class Private; + void initTouchBar(); void hideAndDeactivate(); void createGlobalMenu(); void updateTitleCounter(); void updateIconCounters(); - friend class Private; std::unique_ptr _private; mutable bool psIdle; diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index cb32d6b72b..cd2cb69660 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -381,8 +381,9 @@ MainWindow::Private::~Private() { [_observer release]; } -MainWindow::MainWindow() -: _private(std::make_unique(this)) { +MainWindow::MainWindow(not_null controller) +: Window::MainWindow(controller) +, _private(std::make_unique(this)) { #ifndef OS_MAC_OLD auto forceOpenGL = std::make_unique(this); #endif // !OS_MAC_OLD diff --git a/Telegram/SourceFiles/platform/win/main_window_win.cpp b/Telegram/SourceFiles/platform/win/main_window_win.cpp index 737636de57..d4347c2aa2 100644 --- a/Telegram/SourceFiles/platform/win/main_window_win.cpp +++ b/Telegram/SourceFiles/platform/win/main_window_win.cpp @@ -598,8 +598,9 @@ bool handleSessionNotification = false; UINT MainWindow::_taskbarCreatedMsgId = 0; -MainWindow::MainWindow() -: ps_tbHider_hWnd(createTaskbarHider()) { +MainWindow::MainWindow(not_null controller) +: Window::MainWindow(controller) +, ps_tbHider_hWnd(createTaskbarHider()) { if (!_taskbarCreatedMsgId) { _taskbarCreatedMsgId = RegisterWindowMessage(L"TaskbarButtonCreated"); } diff --git a/Telegram/SourceFiles/platform/win/main_window_win.h b/Telegram/SourceFiles/platform/win/main_window_win.h index 9482d12c5b..c8bd437b8e 100644 --- a/Telegram/SourceFiles/platform/win/main_window_win.h +++ b/Telegram/SourceFiles/platform/win/main_window_win.h @@ -21,7 +21,7 @@ class MainWindow : public Window::MainWindow { Q_OBJECT public: - MainWindow(); + explicit MainWindow(not_null controller); HWND psHwnd() const; HMENU psMenu() const; diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index 8ce34415c5..4ce514474e 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -15,6 +15,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "window/window_lock_widgets.h" #include "window/window_outdated_bar.h" +#include "window/window_controller.h" #include "boxes/confirm_box.h" #include "main/main_account.h" // Account::authSessionValue. #include "core/click_handler_types.h" @@ -108,8 +109,9 @@ QIcon CreateIcon() { return result; } -MainWindow::MainWindow() -: _positionUpdatedTimer([=] { savePosition(); }) +MainWindow::MainWindow(not_null controller) +: _controller(controller) +, _positionUpdatedTimer([=] { savePosition(); }) , _outdated(CreateOutdatedBar(this)) , _body(this) , _icon(CreateIcon()) @@ -127,14 +129,6 @@ MainWindow::MainWindow() workmodeUpdated(mode); }); - Core::App().activeAccount().sessionValue( - ) | rpl::start_with_next([=](AuthSession *session) { - _controller = session - ? std::make_unique(session, this) - : nullptr; - updateWindowIcon(); - }, lifetime()); - Core::App().termsLockValue( ) | rpl::start_with_next([=] { checkLockByTerms(); @@ -156,6 +150,10 @@ MainWindow::MainWindow() _inactivePressTimer.setCallback([this] { setInactivePress(false); }); } +Window::SessionController *MainWindow::sessionController() const { + return _controller->sessionController(); +} + void MainWindow::checkLockByTerms() { const auto data = Core::App().termsLocked(); if (!data || !AuthSession::Exists()) { diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index 4fbe41e805..db18d996eb 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -15,6 +15,7 @@ class BoxContent; namespace Window { +class Controller; class SessionController; class TitleWidget; struct TermsLock; @@ -28,11 +29,12 @@ class MainWindow : public Ui::RpWidget, protected base::Subscriber { Q_OBJECT public: - MainWindow(); + explicit MainWindow(not_null controller); - Window::SessionController *sessionController() const { - return _controller.get(); + not_null controller() const { + return _controller; } + Window::SessionController *sessionController() const; void setInactivePress(bool inactive); bool wasInactivePress() const { return _wasInactivePress; @@ -83,6 +85,8 @@ public: rpl::producer<> leaveEvents() const; + virtual void updateWindowIcon(); + public slots: bool minimizeToTray(); void updateGlobalMenu() { @@ -110,8 +114,6 @@ protected: virtual void clearWidgetsHook() { } - virtual void updateWindowIcon(); - virtual void stateChangedHook(Qt::WindowState state) { } @@ -159,10 +161,11 @@ private: int computeMinHeight() const; + not_null _controller; + base::Timer _positionUpdatedTimer; bool _positionInited = false; - std::unique_ptr _controller; object_ptr _title = { nullptr }; object_ptr _outdated; object_ptr _body; diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp new file mode 100644 index 0000000000..d69b7a153a --- /dev/null +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -0,0 +1,95 @@ +/* +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 "window/window_controller.h" + +#include "core/application.h" +#include "main/main_account.h" +#include "window/window_session_controller.h" +#include "mainwindow.h" + +namespace Window { + +Controller::Controller(not_null account) +: _account(account) +, _widget(this) { + Core::App().activeAccount().sessionValue( + ) | rpl::start_with_next([=](AuthSession *session) { + _sessionController = session + ? std::make_unique(session, &_widget) + : nullptr; + _widget.updateWindowIcon(); + }, _lifetime); + + _widget.init(); +} + +Controller::~Controller() = default; + +void Controller::firstShow() { + _widget.firstShow(); +} + +void Controller::setupPasscodeLock() { + _widget.setupPasscodeLock(); +} + +void Controller::clearPasscodeLock() { + _widget.clearPasscodeLock(); +} + +void Controller::setupIntro() { + _widget.setupIntro(); +} + +void Controller::setupMain() { + _widget.setupMain(); +} + +void Controller::showSettings() { + _widget.showSettings(); +} + +void Controller::activate() { + _widget.activate(); +} + +void Controller::reActivate() { + _widget.reActivateWindow(); +} + +void Controller::updateIsActive(int timeout) { + _widget.updateIsActive(timeout); +} + +void Controller::minimize() { + if (Global::WorkMode().value() == dbiwmTrayOnly) { + _widget.minimizeToTray(); + } else { + _widget.setWindowState(Qt::WindowMinimized); + } +} + +void Controller::close() { + if (!_widget.hideNoQuit()) { + _widget.close(); + } +} + +QPoint Controller::getPointForCallPanelCenter() const { + Expects(_widget.windowHandle() != nullptr); + + return _widget.isActive() + ? _widget.geometry().center() + : _widget.windowHandle()->screen()->geometry().center(); +} + +void Controller::tempDirDelete(int task) { + _widget.tempDirDelete(task); +} + +} // namespace Window diff --git a/Telegram/SourceFiles/window/window_controller.h b/Telegram/SourceFiles/window/window_controller.h new file mode 100644 index 0000000000..cb1928cb39 --- /dev/null +++ b/Telegram/SourceFiles/window/window_controller.h @@ -0,0 +1,64 @@ +/* +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 +*/ +#pragma once + +#include "mainwindow.h" + +namespace Main { +class Account; +} // namespace Main + +namespace Window { + +class Controller final { +public: + explicit Controller(not_null account); + ~Controller(); + + Controller(const Controller &other) = delete; + Controller &operator=(const Controller &other) = delete; + + not_null account() const { + return _account; + } + not_null<::MainWindow*> widget() { + return &_widget; + } + SessionController *sessionController() const { + return _sessionController.get(); + } + + void firstShow(); + + void setupPasscodeLock(); + void clearPasscodeLock(); + void setupIntro(); + void setupMain(); + + void showSettings(); + + void activate(); + void reActivate(); + void updateIsActive(int timeout); + void minimize(); + void close(); + + QPoint getPointForCallPanelCenter() const; + + void tempDirDelete(int task); + +private: + not_null _account; + ::MainWindow _widget; + std::unique_ptr _sessionController; + + rpl::lifetime _lifetime; + +}; + +} // namespace Window diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index 76aae12d1e..08d6d5484b 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -827,6 +827,8 @@ <(src_loc)/window/section_widget.h <(src_loc)/window/window_connecting_widget.cpp <(src_loc)/window/window_connecting_widget.h +<(src_loc)/window/window_controller.cpp +<(src_loc)/window/window_controller.h <(src_loc)/window/window_history_hider.cpp <(src_loc)/window/window_history_hider.h <(src_loc)/window/window_lock_widgets.cpp