Support autostart in Windows Store version.

Fixes #3234.
This commit is contained in:
John Preston 2021-11-04 12:35:34 +04:00
parent 81327ede7b
commit f10ed4b9bc
17 changed files with 343 additions and 53 deletions

View File

@ -982,6 +982,8 @@ PRIVATE
platform/win/windows_dlls.h
platform/win/windows_event_filter.cpp
platform/win/windows_event_filter.h
platform/win/windows_autostart_task.cpp
platform/win/windows_autostart_task.h
platform/win/windows_toast_activator.cpp
platform/win/windows_toast_activator.h
platform/platform_audio.h
@ -1192,6 +1194,13 @@ PRIVATE
stdafx.h
)
if (NOT build_winstore)
remove_target_sources(Telegram ${src_loc}
platform/win/windows_start_task.cpp
platform/win/windows_start_task.h
)
endif()
if (DESKTOP_APP_DISABLE_DBUS_INTEGRATION)
remove_target_sources(Telegram ${src_loc}
platform/linux/linux_xdp_file_dialog.cpp
@ -1490,6 +1499,20 @@ if ((NOT DESKTOP_APP_DISABLE_AUTOUPDATE OR APPLE) AND NOT build_macstore AND NOT
set_target_properties(Packer PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${output_folder})
endif()
elseif (build_winstore)
add_executable(StartupTask WIN32)
init_non_host_target(StartupTask)
add_dependencies(Telegram StartupTask)
nice_target_sources(StartupTask ${src_loc}
PRIVATE
_other/startup_task_win.cpp
)
set_target_properties(StartupTask PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${output_folder}
)
endif()
if (LINUX AND DESKTOP_APP_USE_PACKAGED)

View File

@ -361,6 +361,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_settings_native_frame" = "Use system window frame";
"lng_settings_auto_start" = "Launch Telegram when system starts";
"lng_settings_start_min" = "Launch minimized";
"lng_settings_auto_start_disabled_uwp" = "Starting with the system was disabled in Windows Settings.\n\nPlease enable Telegram Desktop in the Startup Apps Settings.";
"lng_settings_open_system_settings" = "Open Settings";
"lng_settings_add_sendto" = "Place Telegram in \"Send to\" menu";
"lng_settings_section_scale" = "Interface Scale";
"lng_settings_scale_auto" = "Auto ({cur})";

View File

@ -4,8 +4,9 @@
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2"
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap uap2 uap3 rescap">
IgnorableNamespaces="uap uap2 uap3 desktop rescap">
<Identity Name="TelegramMessengerLLP.TelegramDesktop"
ProcessorArchitecture="ARCHITECTURE"
Publisher="CN=536BC709-8EE1-4478-AF22-F0F0F26FF64A"
@ -37,6 +38,15 @@
<uap3:Extension Category="windows.protocol">
<uap3:Protocol Name="tg" Parameters="-- &quot;%1&quot;" />
</uap3:Extension>
<desktop:Extension
Category="windows.startupTask"
Executable="StartupTask.exe"
EntryPoint="Windows.FullTrustApplication">
<desktop:StartupTask
TaskId="TelegramStartupTask"
Enabled="false"
DisplayName="Telegram Desktop" />
</desktop:Extension>
</Extensions>
</Application>
</Applications>

View File

@ -0,0 +1,54 @@
/*
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 <windows.h>
#include <array>
#include <string>
using namespace std;
constexpr auto kMaxPathLong = 32767;
[[nodiscard]] std::wstring ExecutableDirectory() {
auto exePath = std::array<WCHAR, kMaxPathLong + 1>{ 0 };
const auto exeLength = GetModuleFileName(
nullptr,
exePath.data(),
kMaxPathLong + 1);
if (!exeLength || exeLength >= kMaxPathLong + 1) {
return {};
}
const auto exe = std::wstring(exePath.data());
const auto last1 = exe.find_last_of('\\');
const auto last2 = exe.find_last_of('/');
const auto last = std::max(
(last1 == std::wstring::npos) ? -1 : int(last1),
(last2 == std::wstring::npos) ? -1 : int(last2));
if (last < 0) {
return {};
}
return exe.substr(0, last);
}
int APIENTRY wWinMain(
HINSTANCE instance,
HINSTANCE prevInstance,
LPWSTR cmdParamarg,
int cmdShow) {
const auto directory = ExecutableDirectory();
if (!directory.empty()) {
ShellExecute(
nullptr,
nullptr,
(directory + L"\\Telegram.exe").c_str(),
L"-autostart",
directory.data(),
SW_SHOWNORMAL);
}
return 0;
}

View File

@ -226,8 +226,8 @@ void Application::run() {
cSetAutoStart(false);
}
if (cLaunchMode() == LaunchModeAutoStart && !cAutoStart()) {
psAutoStart(false, true);
if (cLaunchMode() == LaunchModeAutoStart && Platform::AutostartSkip()) {
Platform::AutostartToggle(false);
App::quit();
return;
}

View File

@ -383,11 +383,7 @@ void start(not_null<Core::Launcher*> launcher) {
#elif defined OS_WIN_STORE // Q_OS_UNIX || Q_OS_WINRT
#ifdef _DEBUG
cForceWorkingDir(cExeDir());
#else // _DEBUG
cForceWorkingDir(psAppDataPath());
#endif // !_DEBUG
workingDirChosen = true;
#elif defined Q_OS_WIN

View File

@ -560,6 +560,41 @@ bool AutostartSupported() {
return !InSnap();
}
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
const auto guard = gsl::finally([&] {
done(enabled);
});
#ifdef __HAIKU__
HaikuAutostart(enabled);
#else // __HAIKU__
const auto silent = !done;
if (InFlatpak()) {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
PortalAutostart(enabled, silent);
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
} else {
const auto autostart = QStandardPaths::writableLocation(
QStandardPaths::GenericConfigLocation)
+ qsl("/autostart/");
if (enabled) {
GenerateDesktopFile(autostart, qsl("-autostart"), silent);
} else {
QFile::remove(autostart + QGuiApplication::desktopFileName());
}
}
#endif // __HAIKU__
}
bool AutostartSkip() {
return !cAutoStart();
}
bool TrayIconSupported() {
return App::wnd()
? App::wnd()->trayAvailable()
@ -637,7 +672,7 @@ QString psAppDataPath() {
void psDoCleanup() {
try {
psAutoStart(false, true);
Platform::AutostartToggle(false);
psSendToMenu(false, true);
} catch (...) {
}
@ -837,29 +872,6 @@ void psNewVersion() {
#endif // __HAIKU__
}
void psAutoStart(bool start, bool silent) {
#ifdef __HAIKU__
HaikuAutostart(start);
return;
#endif // __HAIKU__
if (InFlatpak()) {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
PortalAutostart(start, silent);
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
} else {
const auto autostart = QStandardPaths::writableLocation(
QStandardPaths::GenericConfigLocation)
+ qsl("/autostart/");
if (start) {
GenerateDesktopFile(autostart, qsl("-autostart"), silent);
} else {
QFile::remove(autostart + QGuiApplication::desktopFileName());
}
}
}
void psSendToMenu(bool send, bool silent) {
}

View File

@ -29,6 +29,9 @@ inline void IgnoreApplicationActivationRightNow() {
inline void WriteCrashDumpDetails() {
}
inline void AutostartRequestStateFromSystem(Fn<void(bool)> callback) {
}
} // namespace Platform
inline void psCheckLocalSocket(const QString &serverName) {
@ -40,7 +43,6 @@ inline void psCheckLocalSocket(const QString &serverName) {
void psActivateProcess(uint64 pid = 0);
QString psAppDataPath();
void psAutoStart(bool start, bool silent = false);
void psSendToMenu(bool send, bool silent = false);
int psCleanup();

View File

@ -22,6 +22,9 @@ inline bool AutostartSupported() {
return false;
}
inline void AutostartRequestStateFromSystem(Fn<void(bool)> callback) {
}
inline bool TrayIconSupported() {
return true;
}
@ -50,7 +53,6 @@ inline void psCheckLocalSocket(const QString &serverName) {
void psActivateProcess(uint64 pid = 0);
QString psAppDataPath();
void psAutoStart(bool start, bool silent = false);
void psSendToMenu(bool send, bool silent = false);
int psCleanup();

View File

@ -48,7 +48,7 @@ QString psAppDataPath() {
void psDoCleanup() {
try {
psAutoStart(false, true);
Platform::AutostartToggle(false);
psSendToMenu(false, true);
} catch (...) {
}
@ -181,14 +181,19 @@ void IgnoreApplicationActivationRightNow() {
objc_ignoreApplicationActivationRightNow();
}
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
done(false);
}
bool AutostartSkip() {
return !cAutoStart();
}
} // namespace Platform
void psNewVersion() {
}
void psAutoStart(bool start, bool silent) {
}
void psSendToMenu(bool send, bool silent) {
}

View File

@ -34,7 +34,10 @@ void RequestPermission(PermissionType type, Fn<void(PermissionStatus)> resultCal
void OpenSystemSettingsForPermission(PermissionType type);
bool OpenSystemSettings(SystemSettingsType type);
void IgnoreApplicationActivationRightNow();
bool AutostartSupported();
[[nodiscard]] bool AutostartSupported();
void AutostartRequestStateFromSystem(Fn<void(bool)> callback);
void AutostartToggle(bool enabled, Fn<void(bool)> done = nullptr);
[[nodiscard]] bool AutostartSkip();
bool TrayIconSupported();
bool SkipTaskbarSupported();
void WriteCrashDumpDetails();

View File

@ -11,10 +11,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "platform/win/notifications_manager_win.h"
#include "platform/win/windows_app_user_model_id.h"
#include "platform/win/windows_dlls.h"
#include "platform/win/windows_autostart_task.h"
#include "base/platform/base_platform_info.h"
#include "base/platform/win/base_windows_co_task_mem.h"
#include "base/platform/win/base_windows_winrt.h"
#include "base/call_delayed.h"
#include "ui/boxes/confirm_box.h"
#include "lang/lang_keys.h"
#include "mainwindow.h"
#include "mainwidget.h"
@ -175,7 +177,7 @@ QString psAppDataPathOld() {
void psDoCleanup() {
try {
psAutoStart(false, true);
Platform::AutostartToggle(false);
psSendToMenu(false, true);
AppUserModelId::cleanupShortcut();
DeleteMyModules();
@ -328,7 +330,51 @@ std::optional<bool> IsDarkMode() {
}
bool AutostartSupported() {
return !IsWindowsStoreBuild();
return true;
}
void AutostartRequestStateFromSystem(Fn<void(bool)> callback) {
#ifdef OS_WIN_STORE
AutostartTask::RequestState([=](bool enabled) {
crl::on_main([=] {
callback(enabled);
});
});
#endif // OS_WIN_STORE
}
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
#ifdef OS_WIN_STORE
const auto requested = enabled;
const auto callback = [=](bool enabled) { crl::on_main([=] {
if (!Core::IsAppLaunched()) {
return;
}
done(enabled);
if (!requested || enabled) {
return;
} else if (const auto window = Core::App().activeWindow()) {
window->show(Box<Ui::ConfirmBox>(
tr::lng_settings_auto_start_disabled_uwp(tr::now),
tr::lng_settings_open_system_settings(tr::now),
[] { AutostartTask::OpenSettings(); Ui::hideLayer(); }));
}
}); };
AutostartTask::Toggle(
enabled,
done ? Fn<void(bool)>(callback) : nullptr);
#else // OS_WIN_STORE
_manageAppLnk(start, silent, CSIDL_STARTUP, L"-autostart", L"Telegram autorun link.\nYou can disable autorun in Telegram settings.");
done(enabled);
#endif // OS_WIN_STORE
}
bool AutostartSkip() {
#ifdef OS_WIN_STORE
return false;
#else // OS_WIN_STORE
return !cAutoStart();
#endif // OS_WIN_STORE
}
void WriteCrashDumpDetails() {
@ -534,10 +580,6 @@ void _manageAppLnk(bool create, bool silent, int path_csidl, const wchar_t *args
}
}
void psAutoStart(bool start, bool silent) {
_manageAppLnk(start, silent, CSIDL_STARTUP, L"-autostart", L"Telegram autorun link.\nYou can disable autorun in Telegram settings.");
}
void psSendToMenu(bool send, bool silent) {
_manageAppLnk(send, silent, CSIDL_SENDTO, L"-sendpath", L"Telegram send to link.\nYou can disable send to menu item in Telegram settings.");
}

View File

@ -43,7 +43,6 @@ inline void psCheckLocalSocket(const QString &) {
void psActivateProcess(uint64 pid = 0);
QString psAppDataPath();
QString psAppDataPathOld();
void psAutoStart(bool start, bool silent = false);
void psSendToMenu(bool send, bool silent = false);
int psCleanup();

View File

@ -0,0 +1,111 @@
/*
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 "platform/win/windows_autostart_task.h"
#include "base/platform/win/base_windows_winrt.h"
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.System.h>
namespace Platform::AutostartTask {
namespace {
using namespace winrt::Windows::ApplicationModel;
using namespace winrt::Windows::System;
using namespace winrt::Windows::Foundation;
[[nodiscard]] bool IsEnabled(StartupTaskState state) {
switch (state) {
case StartupTaskState::Enabled:
case StartupTaskState::EnabledByPolicy:
return true;
case StartupTaskState::Disabled:
case StartupTaskState::DisabledByPolicy:
case StartupTaskState::DisabledByUser:
default:
return false;
}
}
} // namespace
void Toggle(bool enabled, Fn<void(bool)> done) {
if (!base::WinRT::Supported()) {
return;
}
const auto processEnableResult = [=](StartupTaskState state) {
LOG(("Startup Task: Enable finished, state: %1").arg(int(state)));
done(IsEnabled(state));
};
const auto processTask = [=](StartupTask task) {
LOG(("Startup Task: Got it, state: %1, requested: %2"
).arg(int(task.State())
).arg(Logs::b(enabled)));
if (IsEnabled(task.State()) == enabled) {
return;
}
if (!enabled) {
LOG(("Startup Task: Disabling."));
task.Disable();
return;
}
LOG(("Startup Task: Requesting enable."));
const auto asyncState = task.RequestEnableAsync();
if (!done) {
return;
}
asyncState.Completed([=](
IAsyncOperation<StartupTaskState> operation,
AsyncStatus status) {
base::WinRT::Try([&] {
processEnableResult(operation.GetResults());
});
});
};
base::WinRT::Try([&] {
StartupTask::GetAsync(L"TelegramStartupTask").Completed([=](
IAsyncOperation<StartupTask> operation,
AsyncStatus status) {
base::WinRT::Try([&] {
processTask(operation.GetResults());
});
});
});
}
void RequestState(Fn<void(bool)> callback) {
Expects(callback != nullptr);
if (!base::WinRT::Supported()) {
return;
}
const auto processTask = [=](StartupTask task) {
DEBUG_LOG(("Startup Task: Got value, state: %1"
).arg(int(task.State())));
callback(IsEnabled(task.State()));
};
base::WinRT::Try([&] {
StartupTask::GetAsync(L"TelegramStartupTask").Completed([=](
IAsyncOperation<StartupTask> operation,
AsyncStatus status) {
base::WinRT::Try([&] {
processTask(operation.GetResults());
});
});
});
}
void OpenSettings() {
Launcher::LaunchUriAsync(Uri(L"ms-settings:startupapps"));
}
} // namespace Platform::AutostartTask

View File

@ -0,0 +1,16 @@
/*
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
namespace Platform::AutostartTask {
void Toggle(bool enabled, Fn<void(bool)> done);
void RequestState(Fn<void(bool)> callback);
void OpenSettings();
} // namespace Platform::AutostartTask

View File

@ -462,16 +462,21 @@ void SetupSystemIntegrationContent(
return (checked != cAutoStart());
}) | rpl::start_with_next([=](bool checked) {
cSetAutoStart(checked);
psAutoStart(checked);
if (checked) {
Local::writeSettings();
} else if (minimized->entity()->checked()) {
minimized->entity()->setChecked(false);
} else {
Local::writeSettings();
}
Platform::AutostartToggle(checked, crl::guard(autostart, [=](
bool enabled) {
autostart->setChecked(enabled);
if (enabled || !minimized->entity()->checked()) {
Local::writeSettings();
} else {
minimized->entity()->setChecked(false);
}
}));
}, autostart->lifetime());
Platform::AutostartRequestStateFromSystem(crl::guard(
controller,
[=](bool enabled) { autostart->setChecked(enabled); }));
minimized->toggleOn(autostart->checkedValue());
minimized->entity()->checkedChanges(
) | rpl::filter([=](bool checked) {

View File

@ -223,6 +223,13 @@ if %BuildUWP% equ 0 (
set "UpdateFile=!UpdateFile!_!AlphaSignature!"
set "PortableFile=talpha!AlphaVersion!_!AlphaSignature!.zip"
)
) else (
:sign2
call "%SignPath%" "StartupTask.exe"
if %errorlevel% neq 0 (
timeout /t 3
goto sign2
)
)
for /f ^"usebackq^ eol^=^
@ -251,6 +258,7 @@ if %BuildUWP% neq 0 (
if %errorlevel% neq 0 goto error
xcopy "%ReleasePath%\%BinaryName%.exe" "%ReleasePath%\AppX\"
xcopy "%ReleasePath%\StartupTask.exe" "%ReleasePath%\AppX\"
xcopy "%ReleasePath%\modules\%Platform%\d3d\d3dcompiler_47.dll" "%ReleasePath%\AppX\modules\%Platform%\d3d\"
MakeAppx.exe pack /d "%ReleasePath%\AppX" /l /p ..\out\Release\%BinaryName%.%Platform%.appx