mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Cmdpal extension: Powertoys extension for cmdpal (#44006)
<!-- 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>
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<!-- Look at Directory.Build.props in root for common stuff as well -->
|
||||
<Import Project="..\..\..\Common.Dotnet.CsWinRT.props" />
|
||||
<Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
|
||||
<ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" />
|
||||
<ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" />
|
||||
<ProjectReference Include="..\..\..\common\PowerToys.ModuleContracts\PowerToys.ModuleContracts.csproj" />
|
||||
<ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
151
src/modules/awake/Awake.ModuleServices/AwakeService.cs
Normal file
151
src/modules/awake/Awake.ModuleServices/AwakeService.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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.Text.Json.Serialization;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
namespace Awake.ModuleServices;
|
||||
|
||||
[JsonSerializable(typeof(AwakeSettings))]
|
||||
internal sealed partial class AwakeServiceJsonContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
26
src/modules/awake/Awake.ModuleServices/AwakeState.cs
Normal file
26
src/modules/awake/Awake.ModuleServices/AwakeState.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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.
|
||||
|
||||
namespace Awake.ModuleServices;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the current state of the Awake module.
|
||||
/// </summary>
|
||||
/// <param name="IsRunning">Whether the Awake process is currently running.</param>
|
||||
/// <param name="Mode">The current Awake mode (Passive, Indefinite, Timed, Expirable).</param>
|
||||
/// <param name="KeepDisplayOn">Whether the display is kept on.</param>
|
||||
/// <param name="Duration">For timed mode, the configured duration.</param>
|
||||
/// <param name="Expiration">For expirable mode, the expiration date/time.</param>
|
||||
public readonly record struct AwakeState(bool IsRunning, AwakeStateMode Mode, bool KeepDisplayOn, TimeSpan? Duration, DateTimeOffset? Expiration);
|
||||
|
||||
/// <summary>
|
||||
/// The mode of the Awake module.
|
||||
/// </summary>
|
||||
public enum AwakeStateMode
|
||||
{
|
||||
Passive = 0,
|
||||
Indefinite = 1,
|
||||
Timed = 2,
|
||||
Expirable = 3,
|
||||
}
|
||||
21
src/modules/awake/Awake.ModuleServices/IAwakeService.cs
Normal file
21
src/modules/awake/Awake.ModuleServices/IAwakeService.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
// 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 PowerToys.ModuleContracts;
|
||||
|
||||
namespace Awake.ModuleServices;
|
||||
|
||||
public interface IAwakeService : IModuleService
|
||||
{
|
||||
Task<OperationResult> SetIndefiniteAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
Task<OperationResult> SetTimedAsync(int minutes, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<OperationResult> SetOffAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current state of the Awake module.
|
||||
/// </summary>
|
||||
AwakeState GetCurrentState();
|
||||
}
|
||||
Reference in New Issue
Block a user