mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [ ] Closes: #xxx <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Installer built, and every command works as expected, Now use sparse app deployment, so we don't need an extra msix --------- Co-authored-by: kaitao-ms <kaitao1105@gmail.com>
152 lines
4.9 KiB
C#
152 lines
4.9 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Diagnostics;
|
|
using System.Text.Json;
|
|
using Common.UI;
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
using PowerToys.ModuleContracts;
|
|
|
|
namespace Awake.ModuleServices;
|
|
|
|
/// <summary>
|
|
/// Provides CLI-based Awake control for reuse across hosts.
|
|
/// </summary>
|
|
public sealed class AwakeService : ModuleServiceBase, IAwakeService
|
|
{
|
|
public static AwakeService Instance { get; } = new();
|
|
|
|
public override string Key => SettingsDeepLink.SettingsWindow.Awake.ToString();
|
|
|
|
protected override SettingsDeepLink.SettingsWindow SettingsWindow => SettingsDeepLink.SettingsWindow.Awake;
|
|
|
|
public override Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
// Default launch -> indefinite, honoring Awake's own settings for display behavior.
|
|
return SetIndefiniteAsync(cancellationToken);
|
|
}
|
|
|
|
public AwakeState GetCurrentState()
|
|
{
|
|
var isRunning = IsAwakeProcessRunning();
|
|
var settings = ReadSettings();
|
|
|
|
if (settings is null)
|
|
{
|
|
return new AwakeState(isRunning, AwakeStateMode.Passive, false, null, null);
|
|
}
|
|
|
|
var mode = settings.Properties.Mode switch
|
|
{
|
|
AwakeMode.PASSIVE => AwakeStateMode.Passive,
|
|
AwakeMode.INDEFINITE => AwakeStateMode.Indefinite,
|
|
AwakeMode.TIMED => AwakeStateMode.Timed,
|
|
AwakeMode.EXPIRABLE => AwakeStateMode.Expirable,
|
|
_ => AwakeStateMode.Passive,
|
|
};
|
|
|
|
TimeSpan? duration = null;
|
|
DateTimeOffset? expiration = null;
|
|
|
|
switch (mode)
|
|
{
|
|
case AwakeStateMode.Timed:
|
|
duration = TimeSpan.FromHours(settings.Properties.IntervalHours) + TimeSpan.FromMinutes(settings.Properties.IntervalMinutes);
|
|
break;
|
|
case AwakeStateMode.Expirable:
|
|
expiration = settings.Properties.ExpirationDateTime;
|
|
break;
|
|
}
|
|
|
|
return new AwakeState(isRunning, mode, settings.Properties.KeepDisplayOn, duration, expiration);
|
|
}
|
|
|
|
public Task<OperationResult> SetIndefiniteAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return UpdateSettingsAsync(
|
|
settings =>
|
|
{
|
|
settings.Properties.Mode = AwakeMode.INDEFINITE;
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<OperationResult> SetTimedAsync(int minutes, CancellationToken cancellationToken = default)
|
|
{
|
|
if (minutes <= 0)
|
|
{
|
|
return Task.FromResult(OperationResult.Fail("Minutes must be greater than zero."));
|
|
}
|
|
|
|
return UpdateSettingsAsync(
|
|
settings =>
|
|
{
|
|
var totalMinutes = Math.Min(minutes, int.MaxValue);
|
|
settings.Properties.Mode = AwakeMode.TIMED;
|
|
settings.Properties.IntervalHours = (uint)(totalMinutes / 60);
|
|
settings.Properties.IntervalMinutes = (uint)(totalMinutes % 60);
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<OperationResult> SetOffAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return UpdateSettingsAsync(
|
|
settings =>
|
|
{
|
|
settings.Properties.Mode = AwakeMode.PASSIVE;
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
private static Task<OperationResult> UpdateSettingsAsync(Action<AwakeSettings> mutateSettings, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var settingsUtils = SettingsUtils.Default;
|
|
var settings = settingsUtils.GetSettingsOrDefault<AwakeSettings>(AwakeSettings.ModuleName);
|
|
|
|
mutateSettings(settings);
|
|
|
|
settingsUtils.SaveSettings(JsonSerializer.Serialize(settings, AwakeServiceJsonContext.Default.AwakeSettings), AwakeSettings.ModuleName);
|
|
return Task.FromResult(OperationResult.Ok());
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
return Task.FromResult(OperationResult.Fail("Awake update was cancelled."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Task.FromResult(OperationResult.Fail($"Failed to update Awake settings: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
private static bool IsAwakeProcessRunning()
|
|
{
|
|
try
|
|
{
|
|
return Process.GetProcessesByName("PowerToys.Awake").Length > 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static AwakeSettings? ReadSettings()
|
|
{
|
|
try
|
|
{
|
|
var settingsUtils = SettingsUtils.Default;
|
|
return settingsUtils.GetSettingsOrDefault<AwakeSettings>(AwakeSettings.ModuleName);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|