Files
PowerToys/src/modules/cmdpal/Exts/SamplePagesExtension/Pages/SampleSettingsPage.cs
Mike Griese b96ab5e871 Add helper classes for very simple settings (#141)
This updates the spec to enable CmdPal to host settings for extensions. Extensions can provide us essentially, a FormPage, and we'll give that special treatment as _the settings_ for the extension. We're gonna use this in #100 in the future.

For now, I also added a helper set of classes for quickly constructing a `Settings` object, which is basically just a dictionary. Notably though, it lets us define simple control types for each of these settings, which we can then turn into the `IFormPage` on the developer's behalf.

See the [`SampleSettingsPage.cs`](https://github.com/zadjii-msft/PowerToys/compare/main...dev/migrie/s/settings-for-extensions#diff-ac06e39258579222e94539315ad59e0bf04f3b0f3e83a2f8a11aa5a42d569ebe) for an example.


Closes #123
2024-11-12 05:37:51 -08:00

45 lines
1.4 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 Microsoft.CmdPal.Extensions;
using Microsoft.CmdPal.Extensions.Helpers;
namespace SamplePagesExtension;
internal sealed partial class SampleSettingsPage : FormPage
{
private readonly Settings _settings = new();
public override IForm[] Forms()
{
var s = _settings.ToForms();
return s;
}
public SampleSettingsPage()
{
Name = "Sample Settings";
Icon = new(string.Empty);
_settings.Add(new ToggleSetting("onOff", true)
{
Label = "This is a toggle",
Description = "It produces a simple checkbox",
});
_settings.Add(new TextSetting("someText", "initial value")
{
Label = "This is a text box",
Description = "For some string of text",
});
_settings.SettingsChanged += SettingsChanged;
}
private void SettingsChanged(object sender, Settings args)
{
/* Do something with the new settings here */
var onOff = _settings.GetSetting<bool>("onOff");
ExtensionHost.LogMessage(new LogMessage() { Message = $"SampleSettingsPage: Changed the value of onOff to {onOff}" });
}
}