mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
Runner: fix startup task state setting for MSIX (#1181)
This commit is contained in:
@@ -62,6 +62,7 @@
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
<AdditionalIncludeDirectories>inc;telemetry;..\..\deps\cpprestsdk\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@@ -82,6 +83,7 @@
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<AdditionalIncludeDirectories>inc;telemetry;..\..\deps\cpprestsdk\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
|
||||
@@ -3,9 +3,70 @@
|
||||
|
||||
#include <appmodel.h>
|
||||
|
||||
bool running_as_packaged()
|
||||
#include <winrt/Windows.ApplicationModel.h>
|
||||
|
||||
using winrt::Windows::ApplicationModel::StartupTask;
|
||||
|
||||
namespace
|
||||
{
|
||||
UINT32 length = 0;
|
||||
const auto rc = GetPackageFamilyName(GetCurrentProcess(), &length, nullptr);
|
||||
return rc != APPMODEL_ERROR_NO_PACKAGE;
|
||||
const wchar_t* STARTUP_TASKID = L"PowerToysStartupTaskID";
|
||||
}
|
||||
|
||||
namespace winstore
|
||||
{
|
||||
bool running_as_packaged()
|
||||
{
|
||||
UINT32 length = 0;
|
||||
const auto rc = GetPackageFamilyName(GetCurrentProcess(), &length, nullptr);
|
||||
return rc != APPMODEL_ERROR_NO_PACKAGE;
|
||||
}
|
||||
|
||||
std::future<StartupTaskState> get_startup_task_status_async()
|
||||
{
|
||||
const auto startupTask = co_await StartupTask::GetAsync(STARTUP_TASKID);
|
||||
co_return startupTask.State();
|
||||
}
|
||||
|
||||
std::future<void> switch_startup_task_state_async(const bool enabled)
|
||||
{
|
||||
const auto startupTask = co_await StartupTask::GetAsync(STARTUP_TASKID);
|
||||
enum class action
|
||||
{
|
||||
none,
|
||||
enable,
|
||||
disable,
|
||||
} action_to_try = action::none;
|
||||
switch (startupTask.State())
|
||||
{
|
||||
case StartupTaskState::Disabled:
|
||||
if (enabled)
|
||||
{
|
||||
action_to_try = action::enable;
|
||||
}
|
||||
break;
|
||||
case StartupTaskState::Enabled:
|
||||
if (!enabled)
|
||||
{
|
||||
action_to_try = action::disable;
|
||||
}
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
switch (action_to_try)
|
||||
{
|
||||
case action::enable:
|
||||
co_await startupTask.RequestEnableAsync();
|
||||
break;
|
||||
case action::disable:
|
||||
startupTask.Disable();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// We can't handle the error, in case we don't have a permission to change startup task state
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
bool running_as_packaged();
|
||||
#include <future>
|
||||
|
||||
#include <winrt/Windows.ApplicationModel.h>
|
||||
|
||||
|
||||
namespace winstore
|
||||
{
|
||||
using winrt::Windows::ApplicationModel::StartupTaskState;
|
||||
|
||||
bool running_as_packaged();
|
||||
std::future<void> switch_startup_task_state_async(const bool enabled);
|
||||
std::future<StartupTaskState> get_startup_task_status_async();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user