2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Remove app module. Support delayed quit by Cmd+Q on macOS.

This commit is contained in:
John Preston
2022-01-27 15:48:51 +03:00
parent b8f1cebeb6
commit 28f2c213f7
38 changed files with 319 additions and 350 deletions

View File

@@ -83,7 +83,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/connection_box.h"
#include "ui/boxes/confirm_box.h"
#include "boxes/share_box.h"
#include "app.h"
#include <QtCore/QMimeDatabase>
#include <QtGui/QGuiApplication>
@@ -96,6 +95,8 @@ constexpr auto kQuitPreventTimeoutMs = crl::time(1500);
constexpr auto kAutoLockTimeoutLateMs = crl::time(3000);
constexpr auto kClearEmojiImageSourceTimeout = 10 * crl::time(1000);
LaunchState GlobalLaunchState/* = LaunchState::Running*/;
void SetCrashAnnotationsGL() {
#ifdef Q_OS_WIN
CrashReports::SetAnnotation("OpenGL ANGLE", [] {
@@ -235,7 +236,7 @@ void Application::run() {
if (cLaunchMode() == LaunchModeAutoStart && Platform::AutostartSkip()) {
Platform::AutostartToggle(false);
App::quit();
Quit();
return;
}
@@ -340,7 +341,7 @@ void Application::showOpenGLCrashNotification() {
Ui::GL::CrashCheckFinish();
Core::App().settings().setDisableOpenGL(false);
Local::writeSettings();
App::restart();
Restart();
};
const auto keepDisabled = [=] {
Ui::GL::ForceDisable(true);
@@ -721,7 +722,7 @@ void Application::switchDebugMode() {
if (Logs::DebugEnabled()) {
Logs::SetDebugEnabled(false);
_launcher->writeDebugModeSetting();
App::restart();
Restart();
} else {
Logs::SetDebugEnabled(true);
_launcher->writeDebugModeSetting();
@@ -742,7 +743,7 @@ void Application::switchFreeType() {
}
cSetUseFreeType(true);
}
App::restart();
Restart();
}
void Application::writeInstallBetaVersionsSetting() {
@@ -760,7 +761,7 @@ Main::Session *Application::maybeActiveSession() const {
bool Application::exportPreventsQuit() {
if (_exportManager->inProgress()) {
_exportManager->stopWithConfirmation([] {
App::quit();
Quit();
});
return true;
}
@@ -782,7 +783,7 @@ bool Application::uploadPreventsQuit() {
account->session().uploadsStop();
}
}
App::quit();
Quit();
});
return true;
}
@@ -790,8 +791,15 @@ bool Application::uploadPreventsQuit() {
return false;
}
bool Application::preventsQuit() {
return exportPreventsQuit() || uploadPreventsQuit();
bool Application::preventsQuit(QuitReason reason) {
if (exportPreventsQuit() || uploadPreventsQuit()) {
return true;
} else if (const auto window = activeWindow()) {
if (window->widget()->isActive()) {
return window->widget()->preventsQuit(reason);
}
}
return false;
}
int Application::unreadBadge() const {
@@ -999,7 +1007,7 @@ void Application::localPasscodeChanged() {
}
bool Application::hasActiveWindow(not_null<Main::Session*> session) const {
if (App::quitting() || !_primaryWindow) {
if (Quitting() || !_primaryWindow) {
return false;
} else if (_calls->hasActivePanel(session)) {
return true;
@@ -1173,9 +1181,10 @@ void Application::refreshGlobalProxy() {
Sandbox::Instance().refreshGlobalProxy();
}
void Application::QuitAttempt() {
void QuitAttempt() {
const auto savingSession = Sandbox::Instance().isSavingSession();
if (!IsAppLaunched()
|| Sandbox::Instance().isSavingSession()
|| savingSession
|| App().readyToQuit()) {
Sandbox::QuitWhenStarted();
}
@@ -1206,12 +1215,18 @@ bool Application::readyToQuit() {
}
void Application::quitPreventFinished() {
if (App::quitting()) {
if (Quitting()) {
QuitAttempt();
}
}
void Application::quitDelayed() {
if (_primaryWindow) {
_primaryWindow->widget()->hide();
}
for (const auto &[history, window] : _secondaryWindows) {
window->widget()->hide();
}
if (!_private->quitTimer.isActive()) {
_private->quitTimer.setCallback([] { Sandbox::QuitWhenStarted(); });
_private->quitTimer.callOnce(kQuitPreventTimeoutMs);
@@ -1234,7 +1249,7 @@ void Application::startShortcuts() {
) | rpl::start_with_next([=](not_null<Shortcuts::Request*> request) {
using Command = Shortcuts::Command;
request->check(Command::Quit) && request->handle([] {
App::quit();
Quit();
return true;
});
request->check(Command::Lock) && request->handle([=] {
@@ -1276,4 +1291,39 @@ Application &App() {
return *Application::Instance;
}
void Quit(QuitReason reason) {
if (Quitting()) {
return;
} else if (IsAppLaunched() && App().preventsQuit(reason)) {
return;
}
SetLaunchState(LaunchState::QuitRequested);
QuitAttempt();
}
bool Quitting() {
return GlobalLaunchState != LaunchState::Running;
}
LaunchState CurrentLaunchState() {
return GlobalLaunchState;
}
void SetLaunchState(LaunchState state) {
GlobalLaunchState = state;
}
void Restart() {
const auto updateReady = !UpdaterDisabled()
&& (UpdateChecker().state() == UpdateChecker::State::Ready);
if (updateReady) {
cSetRestartingUpdate(true);
} else {
cSetRestarting(true);
cSetRestartingToSettings(true);
}
Quit();
}
} // namespace Core

View File

@@ -39,10 +39,6 @@ namespace ChatHelpers {
class EmojiKeywords;
} // namespace ChatHelpers
namespace App {
void quit();
} // namespace App
namespace Main {
class Domain;
class Account;
@@ -107,6 +103,17 @@ namespace Core {
class Launcher;
struct LocalUrlHandler;
enum class LaunchState {
Running,
QuitRequested,
QuitProcessed,
};
enum class QuitReason {
Default,
QtQuitEvent,
};
class Application final : public QObject {
public:
struct ProxyChange {
@@ -255,7 +262,7 @@ public:
void checkAutoLockIn(crl::time time);
void localPasscodeChanged();
[[nodiscard]] bool preventsQuit();
[[nodiscard]] bool preventsQuit(QuitReason reason);
[[nodiscard]] crl::time lastNonIdleTime() const;
void updateNonIdle();
@@ -305,8 +312,7 @@ private:
void startEmojiImageLoader();
void startSystemDarkModeViewer();
friend void App::quit();
static void QuitAttempt();
friend void QuitAttempt();
void quitDelayed();
[[nodiscard]] bool readyToQuit();
@@ -391,4 +397,12 @@ private:
[[nodiscard]] bool IsAppLaunched();
[[nodiscard]] Application &App();
[[nodiscard]] LaunchState CurrentLaunchState();
void SetLaunchState(LaunchState state);
void Quit(QuitReason reason = QuitReason::Default);
[[nodiscard]] bool Quitting();
void Restart();
} // namespace Core

View File

@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/crash_report_window.h"
#include "core/crash_reports.h"
#include "core/application.h"
#include "core/launcher.h"
#include "core/sandbox.h"
#include "core/update_checker.h"
@@ -15,7 +16,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/main_window.h"
#include "platform/platform_specific.h"
#include "base/zlib_help.h"
#include "app.h"
#include <QtWidgets/QFileDialog>
#include <QtGui/QScreen>
@@ -208,7 +208,7 @@ void NotStartedWindow::updateControls() {
void NotStartedWindow::closeEvent(QCloseEvent *e) {
deleteLater();
App::quit();
Core::Quit();
}
void NotStartedWindow::resizeEvent(QResizeEvent *e) {
@@ -906,7 +906,7 @@ void LastCrashedWindow::setUpdatingState(UpdatingState state, bool force) {
case UpdatingReady:
if (Core::checkReadyUpdate()) {
cSetRestartingUpdate(true);
App::quit();
Core::Quit();
return;
} else {
setUpdatingState(UpdatingFail);

View File

@@ -27,7 +27,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/qt/qt_common_adapters.h"
#include "ui/ui_utility.h"
#include "ui/effects/animations.h"
#include "app.h"
#include <QtCore/QLockFile>
#include <QtGui/QSessionManager>
@@ -196,7 +195,7 @@ void Sandbox::QuitWhenStarted() {
void Sandbox::launchApplication() {
InvokeQueued(this, [=] {
if (App::quitting()) {
if (Quitting()) {
quit();
} else if (_application) {
return;
@@ -261,12 +260,12 @@ void Sandbox::setupScreenScale() {
Sandbox::~Sandbox() = default;
bool Sandbox::event(QEvent *e) {
if (e->type() == QEvent::Quit && !App::quitting()) {
App::quit();
if (e->type() == QEvent::Quit && !Quitting()) {
Quit(QuitReason::QtQuitEvent);
e->ignore();
return false;
} else if (e->type() == QEvent::Close) {
App::quit();
Quit();
}
return QApplication::event(e);
}
@@ -318,16 +317,16 @@ void Sandbox::socketReading() {
psActivateProcess(pid);
}
LOG(("Show command response received, pid = %1, activating and quitting...").arg(pid));
return App::quit();
return Quit();
}
}
void Sandbox::socketError(QLocalSocket::LocalSocketError e) {
if (App::quitting()) return;
if (Quitting()) return;
if (_secondInstance) {
LOG(("Could not write show command, error %1, quitting...").arg(e));
return App::quit();
return Quit();
}
if (e == QLocalSocket::ServerNotFoundError) {
@@ -343,7 +342,7 @@ void Sandbox::socketError(QLocalSocket::LocalSocketError e) {
if (!_localServer.listen(_localServerName)) {
LOG(("Failed to start listening to %1 server: %2").arg(_localServerName, _localServer.errorString()));
return App::quit();
return Quit();
}
#endif // !Q_OS_WINRT
@@ -352,11 +351,11 @@ void Sandbox::socketError(QLocalSocket::LocalSocketError e) {
&& Core::checkReadyUpdate()) {
cSetRestartingUpdate(true);
DEBUG_LOG(("Sandbox Info: installing update instead of starting app..."));
return App::quit();
return Quit();
}
if (cQuit()) {
return App::quit();
return Quit();
}
singleInstanceChecked();
@@ -407,7 +406,7 @@ void Sandbox::singleInstanceChecked() {
void Sandbox::socketDisconnected() {
if (_secondInstance) {
DEBUG_LOG(("Sandbox Error: socket disconnected before command response received, quitting..."));
return App::quit();
return Quit();
}
}
@@ -499,7 +498,7 @@ void Sandbox::removeClients() {
}
void Sandbox::checkForQuit() {
if (App::quitting()) {
if (Quitting()) {
quit();
}
}
@@ -633,10 +632,10 @@ MTP::ProxyData Sandbox::sandboxProxy() const {
}
void Sandbox::closeApplication() {
if (App::launchState() == App::QuitProcessed) {
if (CurrentLaunchState() == LaunchState::QuitProcessed) {
return;
}
App::setLaunchState(App::QuitProcessed);
SetLaunchState(LaunchState::QuitProcessed);
_application = nullptr;
@@ -660,7 +659,7 @@ void Sandbox::execExternal(const QString &cmd) {
PreLaunchWindow::instance()->activate();
}
} else if (cmd == "quit") {
App::quit();
Quit();
}
}

View File

@@ -27,7 +27,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_session_controller.h"
#include "settings/settings_intro.h"
#include "ui/layers/box_content.h"
#include "app.h"
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
@@ -1175,7 +1174,7 @@ void Updater::check() {
void Updater::handleReady() {
stop();
_action = Action::Ready;
if (!App::quitting()) {
if (!Quitting()) {
cSetLastUpdateCheck(base::unixtime::now());
Local::writeSettings();
}
@@ -1203,7 +1202,7 @@ void Updater::handleProgress() {
void Updater::scheduleNext() {
stop();
if (!App::quitting()) {
if (!Quitting()) {
cSetLastUpdateCheck(base::unixtime::now());
Local::writeSettings();
start(true);