From e92ae40ecb0f6a4a392a2c9fc3ccf2f23de3728e Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Mon, 13 Sep 2021 03:13:13 +0400 Subject: [PATCH] Implement close to taskbar option --- Telegram/Resources/langs/lang.strings | 1 + Telegram/SourceFiles/core/core_settings.cpp | 8 +++++- Telegram/SourceFiles/core/core_settings.h | 14 +++++++++++ Telegram/SourceFiles/core/launcher.cpp | 2 ++ Telegram/SourceFiles/core/sandbox.cpp | 8 ++++++ .../platform/linux/specific_linux.cpp | 14 ++++++++--- Telegram/SourceFiles/settings.cpp | 1 + Telegram/SourceFiles/settings.h | 1 + .../settings/settings_advanced.cpp | 25 +++++++++++++++++++ Telegram/SourceFiles/window/main_window.cpp | 9 +++++-- lib/xdg/telegramdesktop.desktop | 6 +++++ 11 files changed, 82 insertions(+), 7 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 91f1b15698..4ccb0fd247 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -357,6 +357,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_settings_update_fail" = "Update check failed :("; "lng_settings_workmode_tray" = "Show tray icon"; "lng_settings_workmode_window" = "Show taskbar icon"; +"lng_settings_close_to_taskbar" = "Close to taskbar"; "lng_settings_native_frame" = "Use system window frame"; "lng_settings_auto_start" = "Launch Telegram when system starts"; "lng_settings_start_min" = "Launch minimized"; diff --git a/Telegram/SourceFiles/core/core_settings.cpp b/Telegram/SourceFiles/core/core_settings.cpp index 236137f3fb..a0124b6a6a 100644 --- a/Telegram/SourceFiles/core/core_settings.cpp +++ b/Telegram/SourceFiles/core/core_settings.cpp @@ -220,7 +220,8 @@ QByteArray Settings::serialize() const { << qint32(_disableOpenGL ? 1 : 0) << _photoEditorBrush << qint32(_groupCallNoiseSuppression ? 1 : 0) - << qint32(_voicePlaybackSpeed * 100); + << qint32(_voicePlaybackSpeed * 100) + << qint32(_closeToTaskbar.current() ? 1 : 0); } return result; } @@ -303,6 +304,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) { QByteArray proxy; qint32 hiddenGroupCallTooltips = qint32(_hiddenGroupCallTooltips.value()); QByteArray photoEditorBrush = _photoEditorBrush; + qint32 closeToTaskbar = _closeToTaskbar.current() ? 1 : 0; stream >> themesAccentColors; if (!stream.atEnd()) { @@ -460,6 +462,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) { if (!stream.atEnd()) { stream >> voicePlaybackSpeed; } + if (!stream.atEnd()) { + stream >> closeToTaskbar; + } if (stream.status() != QDataStream::Ok) { LOG(("App Error: " "Bad data for Core::Settings::constructFromSerialized()")); @@ -600,6 +605,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) { : Tooltip(0)); }(); _photoEditorBrush = photoEditorBrush; + _closeToTaskbar = (closeToTaskbar == 1); } QString Settings::getSoundPath(const QString &key) const { diff --git a/Telegram/SourceFiles/core/core_settings.h b/Telegram/SourceFiles/core/core_settings.h index 86b2145e2f..ad3e3a89f9 100644 --- a/Telegram/SourceFiles/core/core_settings.h +++ b/Telegram/SourceFiles/core/core_settings.h @@ -601,6 +601,19 @@ public: _hiddenGroupCallTooltips |= value; } + void setCloseToTaskbar(bool value) { + _closeToTaskbar = value; + } + [[nodiscard]] bool closeToTaskbar() const { + return _closeToTaskbar.current(); + } + [[nodiscard]] rpl::producer closeToTaskbarValue() const { + return _closeToTaskbar.value(); + } + [[nodiscard]] rpl::producer closeToTaskbarChanges() const { + return _closeToTaskbar.changes(); + } + [[nodiscard]] static bool ThirdColumnByDefault(); [[nodiscard]] static float64 DefaultDialogsWidthRatio(); [[nodiscard]] static qint32 SerializePlaybackSpeed(float64 speed) { @@ -700,6 +713,7 @@ private: bool _disableOpenGL = false; rpl::variable _workMode = WorkMode::WindowAndTray; base::flags _hiddenGroupCallTooltips; + rpl::variable _closeToTaskbar = false; bool _tabbedReplacedWithInfo = false; // per-window rpl::event_stream _tabbedReplacedWithInfoValue; // per-window diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index cdeb89ac1b..dadc86aacf 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -464,6 +464,7 @@ void Launcher::processArguments() { { "-noupdate" , KeyFormat::NoValues }, { "-tosettings" , KeyFormat::NoValues }, { "-startintray" , KeyFormat::NoValues }, + { "-quit" , KeyFormat::NoValues }, { "-sendpath" , KeyFormat::AllLeftValues }, { "-workdir" , KeyFormat::OneValue }, { "--" , KeyFormat::OneValue }, @@ -504,6 +505,7 @@ void Launcher::processArguments() { gNoStartUpdate = parseResult.contains("-noupdate"); gStartToSettings = parseResult.contains("-tosettings"); gStartInTray = parseResult.contains("-startintray"); + gQuit = parseResult.contains("-quit"); gSendPaths = parseResult.value("-sendpath", {}); gWorkingDir = parseResult.value("-workdir", {}).join(QString()); if (!gWorkingDir.isEmpty()) { diff --git a/Telegram/SourceFiles/core/sandbox.cpp b/Telegram/SourceFiles/core/sandbox.cpp index cc329ebcf3..f26e07d6f3 100644 --- a/Telegram/SourceFiles/core/sandbox.cpp +++ b/Telegram/SourceFiles/core/sandbox.cpp @@ -237,6 +237,8 @@ void Sandbox::socketConnected() { } if (!cStartUrl().isEmpty()) { commands += qsl("OPEN:") + _escapeTo7bit(cStartUrl()) + ';'; + } else if (cQuit()) { + commands += qsl("CMD:quit;"); } else { commands += qsl("CMD:show;"); } @@ -305,6 +307,10 @@ void Sandbox::socketError(QLocalSocket::LocalSocketError e) { return App::quit(); } + if (cQuit()) { + return App::quit(); + } + singleInstanceChecked(); } @@ -605,6 +611,8 @@ void Sandbox::execExternal(const QString &cmd) { } else if (PreLaunchWindow::instance()) { PreLaunchWindow::instance()->activate(); } + } else if (cmd == "quit") { + App::quit(); } } diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 77a7a6a243..1782cfc3cf 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -315,12 +315,18 @@ bool GenerateDesktopFile( fileText = fileText.replace( QRegularExpression( - qsl("^Exec=.*$"), + qsl("^Exec=telegram-desktop(.*)$"), QRegularExpression::MultilineOption), - qsl("Exec=%1 -workdir %2").arg( + qsl("Exec=%1 -workdir %2\\1").arg( EscapeShellInLauncher(cExeDir() + cExeName()), - EscapeShellInLauncher(cWorkingDir())) - + (args.isEmpty() ? QString() : ' ' + args)); + EscapeShellInLauncher(cWorkingDir()))); + + fileText = fileText.replace( + QRegularExpression( + qsl("^Exec=(.*) -- %u$"), + QRegularExpression::MultilineOption), + qsl("Exec=\\1%1").arg( + args.isEmpty() ? QString() : ' ' + args)); target.write(fileText.toUtf8()); target.close(); diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp index bd6f3a56b7..5fff0f3217 100644 --- a/Telegram/SourceFiles/settings.cpp +++ b/Telegram/SourceFiles/settings.cpp @@ -35,6 +35,7 @@ bool gAutoUpdate = true; LaunchMode gLaunchMode = LaunchModeNormal; bool gSeenTrayTooltip = false; bool gRestartingUpdate = false, gRestarting = false, gRestartingToSettings = false, gWriteProtected = false; +bool gQuit = false; int32 gLastUpdateCheck = 0; bool gNoStartUpdate = false; bool gStartToSettings = false; diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index 3d00cc483f..bc91f19cbe 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -77,6 +77,7 @@ DeclareSetting(bool, NoStartUpdate); DeclareSetting(bool, StartToSettings); DeclareSetting(bool, DebugMode); DeclareReadSetting(bool, ManyInstance); +DeclareSetting(bool, Quit); DeclareSetting(QByteArray, LocalSalt); DeclareSetting(int, ScreenScale); diff --git a/Telegram/SourceFiles/settings/settings_advanced.cpp b/Telegram/SourceFiles/settings/settings_advanced.cpp index 7eabb47e65..915ebc5240 100644 --- a/Telegram/SourceFiles/settings/settings_advanced.cpp +++ b/Telegram/SourceFiles/settings/settings_advanced.cpp @@ -346,6 +346,7 @@ void SetupSystemIntegrationContent( checkbox(std::move(label), checked), st::settingsCheckboxPadding)); }; + if (Platform::TrayIconSupported()) { const auto trayEnabled = [] { const auto workMode = Core::App().settings().workMode(); @@ -406,6 +407,29 @@ void SetupSystemIntegrationContent( }, taskbar->lifetime()); } } + + if (!Platform::IsMac()) { + const auto closeToTaskbar = addSlidingCheckbox( + tr::lng_settings_close_to_taskbar(), + Core::App().settings().closeToTaskbar()); + + const auto closeToTaskbarShown = std::make_shared>(false); + Core::App().settings().workModeValue( + ) | rpl::start_with_next([=](WorkMode workMode) { + *closeToTaskbarShown = (workMode == WorkMode::WindowOnly) + || !Platform::TrayIconSupported(); + }, closeToTaskbar->lifetime()); + + closeToTaskbar->toggleOn(closeToTaskbarShown->value()); + closeToTaskbar->entity()->checkedChanges( + ) | rpl::filter([=](bool checked) { + return (checked != Core::App().settings().closeToTaskbar()); + }) | rpl::start_with_next([=](bool checked) { + Core::App().settings().setCloseToTaskbar(checked); + Local::writeSettings(); + }, closeToTaskbar->lifetime()); + } + if (Ui::Platform::NativeWindowFrameSupported()) { const auto nativeFrame = addCheckbox( tr::lng_settings_native_frame(), @@ -419,6 +443,7 @@ void SetupSystemIntegrationContent( Core::App().saveSettingsDelayed(); }, nativeFrame->lifetime()); } + if (Platform::AutostartSupported() && controller) { const auto minimizedToggled = [=] { return cStartMinimized() diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index e1ac04cf82..09fac0c225 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -213,8 +213,13 @@ bool MainWindow::hideNoQuit() { } return true; } - } else if (Platform::IsMac()) { - closeWithoutDestroy(); + } + if (Platform::IsMac() || Core::App().settings().closeToTaskbar()) { + if (Platform::IsMac()) { + closeWithoutDestroy(); + } else { + setWindowState(window()->windowState() | Qt::WindowMinimized); + } controller().updateIsActiveBlur(); updateGlobalMenu(); if (const auto controller = sessionController()) { diff --git a/lib/xdg/telegramdesktop.desktop b/lib/xdg/telegramdesktop.desktop index f4676ce692..c34d07c809 100644 --- a/lib/xdg/telegramdesktop.desktop +++ b/lib/xdg/telegramdesktop.desktop @@ -11,4 +11,10 @@ Type=Application Categories=Chat;Network;InstantMessaging;Qt; MimeType=x-scheme-handler/tg; Keywords=tg;chat;im;messaging;messenger;sms;tdesktop; +Actions=Quit; X-GNOME-UsesNotifications=true + +[Desktop Action Quit] +Exec=telegram-desktop -quit +Name=Quit Telegram +Icon=application-exit