Skip to content

A Swiss Army knife for developers.

Added IUISwitch

Etienne Baudouxaf1abf692031
diff --git a/src/app/dev/DevToys.Api/DevToys.Api.csproj b/src/app/dev/DevToys.Api/DevToys.Api.csproj
index 598f02c1b..eaee65ced 100644
--- a/src/app/dev/DevToys.Api/DevToys.Api.csproj
+++ b/src/app/dev/DevToys.Api/DevToys.Api.csproj
@@ -10,4 +10,19 @@
<PackageReference Include="System.ComponentModel.Composition" />
<PackageReference Include="System.Threading.Tasks.Extensions" />
</ItemGroup>
+
+ <ItemGroup>
+ <Compile Update="Strings\ApiText\ApiText.Designer.cs">
+ <DesignTime>True</DesignTime>
+ <AutoGen>True</AutoGen>
+ <DependentUpon>ApiText.resx</DependentUpon>
+ </Compile>
+ </ItemGroup>
+
+ <ItemGroup>
+ <EmbeddedResource Update="Strings\ApiText\ApiText.resx">
+ <Generator>ResXFileCodeGenerator</Generator>
+ <LastGenOutput>ApiText.Designer.cs</LastGenOutput>
+ </EmbeddedResource>
+ </ItemGroup>
</Project>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Api/Strings/ApiText/ApiText.Designer.cs b/src/app/dev/DevToys.Api/Strings/ApiText/ApiText.Designer.cs
new file mode 100644
index 000000000..e62ae2124
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Strings/ApiText/ApiText.Designer.cs
@@ -0,0 +1,81 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace DevToys.Api.Strings.ApiText {
+ using System;
+
+
+ /// <summary>
+ /// A strongly-typed resource class, for looking up localized strings, etc.
+ /// </summary>
+ // This class was auto-generated by the StronglyTypedResourceBuilder
+ // class via a tool like ResGen or Visual Studio.
+ // To add or remove a member, edit your .ResX file then rerun ResGen
+ // with the /str option, or rebuild your VS project.
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class ApiText {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal ApiText() {
+ }
+
+ /// <summary>
+ /// Returns the cached ResourceManager instance used by this class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DevToys.Api.Strings.ApiText.ApiText", typeof(ApiText).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ /// <summary>
+ /// Overrides the current thread's CurrentUICulture property for all
+ /// resource lookups using this strongly typed resource class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
+ return resourceCulture;
+ }
+ set {
+ resourceCulture = value;
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Off.
+ /// </summary>
+ internal static string SwitchOffDefaultText {
+ get {
+ return ResourceManager.GetString("SwitchOffDefaultText", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to On.
+ /// </summary>
+ internal static string SwitchOnDefaultText {
+ get {
+ return ResourceManager.GetString("SwitchOnDefaultText", resourceCulture);
+ }
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Strings/ApiText/ApiText.resx b/src/app/dev/DevToys.Api/Strings/ApiText/ApiText.resx
new file mode 100644
index 000000000..8d2ca4c86
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Strings/ApiText/ApiText.resx
@@ -0,0 +1,126 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+ <!--
+ Microsoft ResX Schema
+
+ Version 2.0
+
+ The primary goals of this format is to allow a simple XML format
+ that is mostly human readable. The generation and parsing of the
+ various data types are done through the TypeConverter classes
+ associated with the data types.
+
+ Example:
+
+ ... ado.net/XML headers & schema ...
+ <resheader name="resmimetype">text/microsoft-resx</resheader>
+ <resheader name="version">2.0</resheader>
+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+ <value>[base64 mime encoded serialized .NET Framework object]</value>
+ </data>
+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+ <comment>This is a comment</comment>
+ </data>
+
+ There are any number of "resheader" rows that contain simple
+ name/value pairs.
+
+ Each data row contains a name, and value. The row also contains a
+ type or mimetype. Type corresponds to a .NET class that support
+ text/value conversion through the TypeConverter architecture.
+ Classes that don't support this are serialized and stored with the
+ mimetype set.
+
+ The mimetype is used for serialized objects, and tells the
+ ResXResourceReader how to depersist the object. This is currently not
+ extensible. For a given mimetype the value must be set accordingly:
+
+ Note - application/x-microsoft.net.object.binary.base64 is the format
+ that the ResXResourceWriter will generate, however the reader can
+ read any of the formats listed below.
+
+ mimetype: application/x-microsoft.net.object.binary.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.soap.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.bytearray.base64
+ value : The object must be serialized into a byte array
+ : using a System.ComponentModel.TypeConverter
+ : and then encoded with base64 encoding.
+ -->
+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+ <xsd:element name="root" msdata:IsDataSet="true">
+ <xsd:complexType>
+ <xsd:choice maxOccurs="unbounded">
+ <xsd:element name="metadata">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" />
+ </xsd:sequence>
+ <xsd:attribute name="name" use="required" type="xsd:string" />
+ <xsd:attribute name="type" type="xsd:string" />
+ <xsd:attribute name="mimetype" type="xsd:string" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="assembly">
+ <xsd:complexType>
+ <xsd:attribute name="alias" type="xsd:string" />
+ <xsd:attribute name="name" type="xsd:string" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="data">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="resheader">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" />
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:choice>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:schema>
+ <resheader name="resmimetype">
+ <value>text/microsoft-resx</value>
+ </resheader>
+ <resheader name="version">
+ <value>2.0</value>
+ </resheader>
+ <resheader name="reader">
+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <resheader name="writer">
+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <data name="SwitchOffDefaultText" xml:space="preserve">
+ <value>Off</value>
+ </data>
+ <data name="SwitchOnDefaultText" xml:space="preserve">
+ <value>On</value>
+ </data>
+</root>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs
index c0d3bea24..007de0cfc 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs
@@ -128,4 +128,36 @@ public static partial class GUI
((UISetting)element).InteractiveElement = uiElement;
return element;
}
+
+ /// <summary>
+ /// Sets a <see cref="IUISwitch"/> to <see cref="IUISetting.InteractiveElement"/> and automatically associate the
+ /// given <paramref name="settingDefinition"/> to the switch state.
+ /// </summary>
+ /// <param name="settingsProvider">The settings provider used for handling the given <paramref name="settingDefinition"/>.</param>
+ /// <param name="settingDefinition">The definition of the setting to associate to this <see cref="IUISetting"/>.</param>
+ /// <param name="onToggled">(optional) A method to invoke when the setting value changed.</param>
+ public static IUISetting Handle(this IUISetting element, ISettingsProvider settingsProvider, SettingDefinition<bool> settingDefinition, Func<bool, ValueTask>? onToggled = null)
+ {
+ var settingElement = (UISetting)element;
+
+ IUISwitch toggleSwitch = Switch();
+ if (settingsProvider.GetSetting(settingDefinition))
+ {
+ toggleSwitch.On();
+ }
+ else
+ {
+ toggleSwitch.Off();
+ }
+
+ toggleSwitch.OnToggle((bool state) =>
+ {
+ settingsProvider.SetSetting(settingDefinition, state);
+ onToggled?.Invoke(state);
+ return ValueTask.CompletedTask;
+ });
+
+ settingElement.InteractiveElement(toggleSwitch);
+ return element;
+ }
}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs
index 21bc83859..0977ccf9c 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs
@@ -92,6 +92,38 @@ public static partial class GUI
return element;
}
+ /// <summary>
+ /// Sets a <see cref="IUISwitch"/> to <see cref="IUISettingGroup.InteractiveElement"/> and automatically associate the
+ /// given <paramref name="settingDefinition"/> to the switch state.
+ /// </summary>
+ /// <param name="settingsProvider">The settings provider used for handling the given <paramref name="settingDefinition"/>.</param>
+ /// <param name="settingDefinition">The definition of the setting to associate to this <see cref="IUISettingGroup"/>.</param>
+ /// <param name="onToggled">(optional) A method to invoke when the setting value changed.</param>
+ public static IUISettingGroup Handle(this IUISettingGroup element, ISettingsProvider settingsProvider, SettingDefinition<bool> settingDefinition, Func<bool, ValueTask>? onToggled = null)
+ {
+ var settingElement = (UISetting)element;
+
+ IUISwitch toggleSwitch = Switch();
+ if (settingsProvider.GetSetting(settingDefinition))
+ {
+ toggleSwitch.On();
+ }
+ else
+ {
+ toggleSwitch.Off();
+ }
+
+ toggleSwitch.OnToggle((bool state) =>
+ {
+ settingsProvider.SetSetting(settingDefinition, state);
+ onToggled?.Invoke(state);
+ return ValueTask.CompletedTask;
+ });
+
+ settingElement.InteractiveElement(toggleSwitch);
+ return element;
+ }
+
/// <summary>
/// Set the children to be displayed in the group.
/// </summary>
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs
new file mode 100644
index 000000000..c4a09b651
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs
@@ -0,0 +1,156 @@
+using DevToys.Api.Strings.ApiText;
+
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents a changeable switch.
+/// </summary>
+public interface IUISwitch : IUIElement
+{
+ /// <summary>
+ /// Gets the current state of the switch.
+ /// </summary>
+ bool IsOn { get; }
+
+ /// <summary>
+ /// Gets the text to display when the <see cref="IsOn"/> is true. Default is "On".
+ /// </summary>
+ string? OnText { get; }
+
+ /// <summary>
+ /// Gets the text to display when the <see cref="IsOn"/> is false. Default is "Off".
+ /// </summary>
+ string? OffText { get; }
+
+ /// <summary>
+ /// Raised when <see cref="OnText"/> is changed.
+ /// </summary>
+ public event EventHandler? OnTextChanged;
+
+ /// <summary>
+ /// Raised when <see cref="OffText"/> is changed.
+ /// </summary>
+ public event EventHandler? OffTextChanged;
+
+ /// <summary>
+ /// Raised when <see cref="IsOn"/> is changed.
+ /// </summary>
+ public event EventHandler? IsOnChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, IsOn = {{{nameof(IsOn)}}}")]
+internal sealed class UISwitch : UITitledElement, IUISwitch
+{
+ private bool _state;
+ private string? _onText;
+ private string? _offText;
+
+ internal UISwitch(string? id)
+ : base(id)
+ {
+ }
+
+ public bool IsOn
+ {
+ get => _state;
+ internal set
+ {
+ _state = value;
+ IsOnChanged?.Invoke(this, EventArgs.Empty);
+ ActionOnToggle?.Invoke(_state);
+ }
+ }
+
+ public string? OnText
+ {
+ get => _onText;
+ internal set
+ {
+ _onText = value;
+ OnTextChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public string? OffText
+ {
+ get => _offText;
+ internal set
+ {
+ _offText = value;
+ OffTextChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ internal Func<bool, ValueTask>? ActionOnToggle { get; set; }
+
+ public event EventHandler? OnTextChanged;
+ public event EventHandler? OffTextChanged;
+ public event EventHandler? IsOnChanged;
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that represents a changeable boolean value.
+ /// </summary>
+ public static IUISwitch Switch()
+ {
+ return Switch(null);
+ }
+
+ /// <summary>
+ /// A component that represents a changeable boolean value.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ public static IUISwitch Switch(string? id)
+ {
+ return new UISwitch(id)
+ .OnText(ApiText.SwitchOnDefaultText)
+ .OffText(ApiText.SwitchOffDefaultText);
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISwitch.OnText"/> of the switch.
+ /// </summary>
+ public static IUISwitch OnText(this IUISwitch element, string? text)
+ {
+ ((UISwitch)element).OnText = text;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISwitch.OffText"/> of the switch.
+ /// </summary>
+ public static IUISwitch OffText(this IUISwitch element, string? text)
+ {
+ ((UISwitch)element).OffText = text;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISwitch.IsOn"/> of the switch to true.
+ /// </summary>
+ public static IUISwitch On(this IUISwitch element)
+ {
+ ((UISwitch)element).IsOn = true;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISwitch.IsOn"/> of the switch to off.
+ /// </summary>
+ public static IUISwitch Off(this IUISwitch element)
+ {
+ ((UISwitch)element).IsOn = false;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the action to run when toggling the switch.
+ /// </summary>
+ public static IUISwitch OnToggle(this IUISwitch element, Func<bool, ValueTask>? actionOnToggle)
+ {
+ ((UISwitch)element).ActionOnToggle = actionOnToggle;
+ return element;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs b/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs
index 8a466f408..05db78898 100644
--- a/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs
@@ -1,6 +1,4 @@
-using DevToys.Api;
-
-namespace DevToys.Tools.Settings;
+namespace DevToys.Tools.Settings;
[Export(typeof(IGuiTool))]
[Name("Settings")]
@@ -19,6 +17,16 @@ namespace DevToys.Tools.Settings;
[NoCompactOverlaySupport]
internal sealed class SettingsGuiTool : IGuiTool
{
+#pragma warning disable IDE0044 // Add readonly modifier
+ [Import]
+ private ISettingsProvider _settingsProvider = null!;
+#pragma warning restore IDE0044 // Add readonly modifier
+
+ /// <summary>
+ /// Dummy setting
+ /// </summary>
+ public static readonly SettingDefinition<bool> DummySetting = new(name: nameof(DummySetting), defaultValue: true);
+
private readonly IUIButton _topLeftButton;
private int _clickCount;
@@ -45,23 +53,22 @@ internal sealed class SettingsGuiTool : IGuiTool
Setting()
.Title("Title")
.Description("Description")
- .InteractiveElement(
- Button().Text("My option")),
+ .Handle(_settingsProvider, DummySetting, OnDummySettingChangedAsync),
Setting()
.Title("Title")
.Description("Description")
.InteractiveElement(
- Button().Text("My option")),
+ Switch()),
Setting()
.Title("Title")
.Description("Description")
.InteractiveElement(
- Button().Text("My option")),
+ Switch()),
Setting()
.Title("Title")
.Description("Description")
.InteractiveElement(
- Button().Text("My option"))),
+ Switch())),
Stack()
.Horizontal()
.WithChildren(
@@ -76,6 +83,13 @@ internal sealed class SettingsGuiTool : IGuiTool
Button().Text("Bottom Center button").OnClick(OnBottomCenterButtonClickAsync),
Button().Text("Bottom Right button")));
+ private ValueTask OnDummySettingChangedAsync(bool state)
+ {
+ _clickCount++;
+ _topLeftButton.Text($"Clicked {_clickCount} time !");
+ return ValueTask.CompletedTask;
+ }
+
private ValueTask OnMyButtonClickAsync()
{
_clickCount++;
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementPresenter.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementPresenter.xaml
index 7dc7f9032..d03ad8501 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementPresenter.xaml
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementPresenter.xaml
@@ -45,6 +45,13 @@
</DataTemplate>
</local:UIElementTemplateSelector.UISettingGroupTemplate>
+ <!-- UISwitch -->
+ <local:UIElementTemplateSelector.UISwitchTemplate>
+ <DataTemplate>
+ <local:UISwitchPresenter/>
+ </DataTemplate>
+ </local:UIElementTemplateSelector.UISwitchTemplate>
+
</local:UIElementTemplateSelector>
</Control.Resources>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementTemplateSelector.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementTemplateSelector.cs
index deec5a24f..9f6e8745e 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementTemplateSelector.cs
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementTemplateSelector.cs
@@ -34,6 +34,11 @@ internal sealed class UIElementTemplateSelector : DataTemplateSelector
/// </summary>
public DataTemplate UISettingGroupTemplate { get; set; } = null!;
+ /// <summary>
+ /// The data template used to display a <see cref="IUISwitch"/>.
+ /// </summary>
+ public DataTemplate UISwitchTemplate { get; set; } = null!;
+
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item is null)
@@ -48,6 +53,7 @@ internal sealed class UIElementTemplateSelector : DataTemplateSelector
IUIIcon => UIIconTemplate,
IUISettingGroup => UISettingGroupTemplate,
IUISetting => UISettingTemplate,
+ IUISwitch => UISwitchTemplate,
_ => throw new NotSupportedException($"Gui Tool component of type '{item.GetType().FullName}' isn't supported.")
};
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISwitchPresenter.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISwitchPresenter.xaml
new file mode 100644
index 000000000..2e92743e8
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISwitchPresenter.xaml
@@ -0,0 +1,14 @@
+<ContentControl
+ x:Class="DevToys.UI.Framework.Controls.GuiTool.UISwitchPresenter"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:local="using:DevToys.UI.Framework.Controls.GuiTool"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ mc:Ignorable="d"
+ IsTabStop="false">
+ <ToggleSwitch
+ x:Name="ToggleSwitch"
+ Style="{StaticResource RightAlignedToggleSwitchStyle}"
+ Toggled="ToggleSwitch_Toggled"/>
+</ContentControl>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISwitchPresenter.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISwitchPresenter.xaml.cs
new file mode 100644
index 000000000..f7449399a
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISwitchPresenter.xaml.cs
@@ -0,0 +1,90 @@
+using DevToys.Api;
+using Microsoft.UI.Xaml.Controls;
+
+namespace DevToys.UI.Framework.Controls.GuiTool;
+
+public sealed partial class UISwitchPresenter : ContentControl
+{
+ private bool _toggledProgrammatically;
+
+ public UISwitchPresenter()
+ {
+ this.InitializeComponent();
+
+ Loaded += UISwitchPresenter_Loaded;
+ Unloaded += UISwitchPresenter_Unloaded;
+ }
+
+ internal IUISwitch UISwitch => (IUISwitch)DataContext;
+
+ private void UISwitchPresenter_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
+ {
+ UISwitch.IsEnabledChanged += UISwitch_IsEnabledChanged;
+ UISwitch.IsVisibleChanged += UISwitch_IsVisibleChanged;
+ UISwitch.OnTextChanged += UISwitch_OnTextChanged;
+ UISwitch.OffTextChanged += UISwitch_OffTextChanged;
+ UISwitch.IsOnChanged += UISwitch_IsOnChanged;
+
+ IsEnabled = UISwitch.IsEnabled;
+ Visibility = UISwitch.IsVisible ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
+ ToggleSwitch.OnContent = UISwitch.OnText;
+ ToggleSwitch.OffContent = UISwitch.OffText;
+
+ _toggledProgrammatically = true;
+ ToggleSwitch.IsOn = UISwitch.IsOn;
+ _toggledProgrammatically = false;
+ }
+
+ private void UISwitchPresenter_Unloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
+ {
+ UISwitch.IsEnabledChanged -= UISwitch_IsEnabledChanged;
+ UISwitch.IsVisibleChanged -= UISwitch_IsVisibleChanged;
+ UISwitch.OnTextChanged -= UISwitch_OnTextChanged;
+ UISwitch.OffTextChanged -= UISwitch_OffTextChanged;
+ UISwitch.IsOnChanged -= UISwitch_IsOnChanged;
+ Loaded -= UISwitchPresenter_Loaded;
+ Unloaded -= UISwitchPresenter_Unloaded;
+ }
+
+ private void UISwitch_IsEnabledChanged(object? sender, EventArgs e)
+ {
+ IsEnabled = UISwitch.IsEnabled;
+ }
+
+ private void UISwitch_IsVisibleChanged(object? sender, EventArgs e)
+ {
+ Visibility = UISwitch.IsVisible ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
+ }
+
+ private void UISwitch_IsOnChanged(object? sender, EventArgs e)
+ {
+ _toggledProgrammatically = true;
+ ToggleSwitch.IsOn = UISwitch.IsOn;
+ _toggledProgrammatically = false;
+ }
+
+ private void UISwitch_OffTextChanged(object? sender, EventArgs e)
+ {
+ ToggleSwitch.OffContent = UISwitch.OffText;
+ }
+
+ private void UISwitch_OnTextChanged(object? sender, EventArgs e)
+ {
+ ToggleSwitch.OnContent = UISwitch.OnText;
+ }
+
+ private void ToggleSwitch_Toggled(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
+ {
+ if (!_toggledProgrammatically)
+ {
+ if (ToggleSwitch.IsOn)
+ {
+ UISwitch.On();
+ }
+ else
+ {
+ UISwitch.Off();
+ }
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
index 9c0d31e6a..5cde0fb59 100644
--- a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
+++ b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
@@ -22,6 +22,8 @@
</Page>
<PRIResource Include="**\*.resw" />
<Content Include="Assets\**" />
+ <None Remove="Controls\GuiTool\UISwitchPresenter.xaml" />
+ <UpToDateCheckInput Remove="Controls\GuiTool\UISwitchPresenter.xaml" />
<Compile Update="**\*.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
@@ -97,4 +99,10 @@
<ProjectReference Include="..\DevToys.Api\DevToys.Api.csproj" />
</ItemGroup>
+ <ItemGroup>
+ <Compile Update="Controls\GuiTool\UISwitchPresenter.xaml.cs">
+ <DependentUpon>UISwitchPresenter.xaml</DependentUpon>
+ </Compile>
+ </ItemGroup>
+
</Project>