Skip to content

A Swiss Army knife for developers.

Added IUISetting + Refactoring

Etienne Baudouxe7b4cbcaee49
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIButton.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIButton.cs
index 58ba08388..ed0caa9c7 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIButton.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIButton.cs
@@ -8,7 +8,7 @@ public interface IUIButton : IUIElement
/// <summary>
/// Gets the text to display in the button.
/// </summary>
- string? DisplayText { get; }
+ string? Text { get; }
/// <summary>
/// Gets the action to run when the user clicks the button.
@@ -16,13 +16,13 @@ public interface IUIButton : IUIElement
Func<ValueTask>? OnClickAction { get; }
/// <summary>
- /// Raised when <see cref="DisplayTextChanged"/> is changed.
+ /// Raised when <see cref="Text"/> is changed.
/// </summary>
- public event EventHandler? DisplayTextChanged;
+ public event EventHandler? TextChanged;
}
-[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(DisplayText)}}}")]
-internal class UIButton : UIElement, IUIButton
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(Text)}}}")]
+internal sealed class UIButton : UIElement, IUIButton
{
private string? _text;
@@ -31,19 +31,19 @@ internal class UIButton : UIElement, IUIButton
{
}
- public string? DisplayText
+ public string? Text
{
get => _text;
internal set
{
_text = value;
- DisplayTextChanged?.Invoke(this, EventArgs.Empty);
+ TextChanged?.Invoke(this, EventArgs.Empty);
}
}
public Func<ValueTask>? OnClickAction { get; internal set; }
- public event EventHandler? DisplayTextChanged;
+ public event EventHandler? TextChanged;
}
public static partial class GUI
@@ -66,11 +66,11 @@ public static partial class GUI
}
/// <summary>
- /// Sets the <see cref="IUIButton.DisplayText"/> of the button.
+ /// Sets the <see cref="IUIButton.Text"/> of the button.
/// </summary>
public static IUIButton Text(this IUIButton element, string? text)
{
- ((UIButton)element).DisplayText = text;
+ ((UIButton)element).Text = text;
return element;
}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIIcon.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIIcon.cs
new file mode 100644
index 000000000..eb80d0129
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIIcon.cs
@@ -0,0 +1,141 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents an icon.
+/// </summary>
+public interface IUIIcon : IUIElement
+{
+ /// <summary>
+ /// Gets the name of the font containing the icon.
+ /// </summary>
+ string FontName { get; }
+
+ /// <summary>
+ /// Gets the glyph corresponding to the icon in the <see cref="FontName"/>.
+ /// </summary>
+ string Glyph { get; }
+
+ /// <summary>
+ /// Gets the size of the icon.
+ /// </summary>
+ int Size { get; }
+
+ /// <summary>
+ /// Raised when <see cref="FontName"/> is changed.
+ /// </summary>
+ public event EventHandler? FontNameChanged;
+
+ /// <summary>
+ /// Raised when <see cref="Glyph"/> is changed.
+ /// </summary>
+ public event EventHandler? GlyphChanged;
+
+ /// <summary>
+ /// Raised when <see cref="Size"/> is changed.
+ /// </summary>
+ public event EventHandler? SizeChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Glyph = {{{nameof(Glyph)}}}")]
+internal sealed class UIIcon : UIElement, IUIIcon
+{
+ private string _fontName;
+ private string _glyph;
+ private int _size = 16;
+
+ internal UIIcon(string? id, string fontName, string glyph)
+ : base(id)
+ {
+ _fontName = fontName;
+ _glyph = glyph;
+ Guard.IsNotNullOrWhiteSpace(FontName);
+ Guard.IsNotNullOrWhiteSpace(Glyph);
+ }
+
+ public string FontName
+ {
+ get => _fontName;
+ internal set
+ {
+ _fontName = value;
+ Guard.IsNotNullOrWhiteSpace(FontName);
+ FontNameChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public string Glyph
+ {
+ get => _glyph;
+ internal set
+ {
+ _glyph = value;
+ Guard.IsNotNullOrWhiteSpace(Glyph);
+ GlyphChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public int Size
+ {
+ get => _size;
+ internal set
+ {
+ _size = value;
+ SizeChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public event EventHandler? FontNameChanged;
+ public event EventHandler? GlyphChanged;
+ public event EventHandler? SizeChanged;
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that represents an icon.
+ /// </summary>
+ /// <param name="fontName">The name of the font containing the icon.</param>
+ /// <param name="glyph">The glyph corresponding to the icon</param>
+ public static IUIIcon Icon(string fontName, string glyph)
+ {
+ return Icon(id: null, fontName, glyph);
+ }
+
+ /// <summary>
+ /// A component that represents an icon.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ /// <param name="fontName">The name of the font containing the icon.</param>
+ /// <param name="glyph">The glyph corresponding to the icon</param>
+ public static IUIIcon Icon(string? id, string fontName, string glyph)
+ {
+ return new UIIcon(id, fontName, glyph);
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUIIcon.FontName"/> of the icon.
+ /// </summary>
+ public static IUIIcon FontName(this IUIIcon element, string fontName)
+ {
+ ((UIIcon)element).FontName = fontName;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUIIcon.FontName"/> of the icon.
+ /// </summary>
+ public static IUIIcon Glyph(this IUIIcon element, string glyph)
+ {
+ ((UIIcon)element).Glyph = glyph;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUIIcon.Size"/> of the icon.
+ /// </summary>
+ public static IUIIcon Size(this IUIIcon element, int size)
+ {
+ ((UIIcon)element).Size = size;
+ return element;
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs
new file mode 100644
index 000000000..c0d3bea24
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs
@@ -0,0 +1,131 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents a setting, with a title, description, icon and <see cref="IUIElement"/> for the option value.
+/// </summary>
+public interface IUISetting : IUITitledElement
+{
+ /// <summary>
+ /// Gets the description of the setting.
+ /// </summary>
+ string? Description { get; }
+
+ /// <summary>
+ /// Gets the icon of the setting.
+ /// </summary>
+ IUIIcon? Icon { get; }
+
+ /// <summary>
+ /// Gets the <see cref="IUIElement"/> that represents the interactive part of the setting.
+ /// </summary>
+ IUIElement? InteractiveElement { get; }
+
+ /// <summary>
+ /// Raised when <see cref="Description"/> is changed.
+ /// </summary>
+ public event EventHandler? DescriptionChanged;
+
+ /// <summary>
+ /// Raised when <see cref="Icon"/> is changed.
+ /// </summary>
+ public event EventHandler? IconChanged;
+
+ /// <summary>
+ /// Raised when <see cref="InteractiveElement"/> is changed.
+ /// </summary>
+ public event EventHandler? InteractiveElementChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Title = {{{nameof(Title)}}}")]
+internal class UISetting : UITitledElement, IUISetting
+{
+ private string? _description;
+ private IUIIcon? _icon;
+ private IUIElement? _interactiveElement;
+
+ internal UISetting(string? id)
+ : base(id)
+ {
+ }
+
+ public string? Description
+ {
+ get => _description;
+ internal set
+ {
+ _description = value;
+ DescriptionChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public IUIIcon? Icon
+ {
+ get => _icon;
+ internal set
+ {
+ _icon = value;
+ IconChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public IUIElement? InteractiveElement
+ {
+ get => _interactiveElement;
+ internal set
+ {
+ _interactiveElement = value;
+ InteractiveElementChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public event EventHandler? DescriptionChanged;
+ public event EventHandler? IconChanged;
+ public event EventHandler? InteractiveElementChanged;
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that represents a setting, with a title, description, icon and <see cref="IUIElement"/> for the option value.
+ /// </summary>
+ public static IUISetting Setting()
+ {
+ return Setting(null);
+ }
+
+ /// <summary>
+ /// A component that represents a setting, with a title, description, icon and <see cref="IUIElement"/> for the option value.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ public static IUISetting Setting(string? id)
+ {
+ return new UISetting(id);
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISetting.Description"/> of the setting.
+ /// </summary>
+ public static IUISetting Description(this IUISetting element, string? text)
+ {
+ ((UISetting)element).Description = text;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISetting.Icon"/> of the setting.
+ /// </summary>
+ public static IUISetting Icon(this IUISetting element, string fontName, string glyph)
+ {
+ ((UISetting)element).Icon = Icon(fontName, glyph);
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISetting.InteractiveElement"/> of the setting.
+ /// </summary>
+ public static IUISetting InteractiveElement(this IUISetting element, IUIElement? uiElement)
+ {
+ ((UISetting)element).InteractiveElement = uiElement;
+ 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
new file mode 100644
index 000000000..21bc83859
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs
@@ -0,0 +1,116 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents a group of settings, with a title, description and an icon.
+/// </summary>
+public interface IUISettingGroup : IUISetting
+{
+ /// <summary>
+ /// Gets the list of child elements.
+ /// </summary>
+ IUIElement[]? Children { get; }
+
+ /// <summary>
+ /// Gets whether element in <see cref="Children"/> are all of type <see cref="IUISetting"/>.
+ /// </summary>
+ bool ChildrenAreAllSettings { get; }
+
+ /// <summary>
+ /// Raised when <see cref="Children"/> is changed.
+ /// </summary>
+ public event EventHandler? ChildrenChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Title = {{{nameof(Title)}}}")]
+internal sealed class UISettingGroup : UISetting, IUISettingGroup
+{
+ private IUIElement[]? _children;
+
+ internal UISettingGroup(string? id)
+ : base(id)
+ {
+ }
+
+ public IUIElement[]? Children
+ {
+ get => _children;
+ internal set
+ {
+ _children = value;
+ ChildrenChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public bool ChildrenAreAllSettings { get; internal set; }
+
+ public event EventHandler? ChildrenChanged;
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that represents a group of settings, with a title, description and an icon.
+ /// </summary>
+ public static IUISettingGroup SettingGroup()
+ {
+ return SettingGroup(null);
+ }
+
+ /// <summary>
+ /// A component that represents a group of settings, with a title, description and an icon.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ public static IUISettingGroup SettingGroup(string? id)
+ {
+ return new UISettingGroup(id);
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISettingGroup.Description"/> of the setting.
+ /// </summary>
+ public static IUISettingGroup Description(this IUISettingGroup element, string? text)
+ {
+ ((UISettingGroup)element).Description = text;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISettingGroup.Icon"/> of the setting.
+ /// </summary>
+ public static IUISettingGroup Icon(this IUISettingGroup element, string fontName, string glyph)
+ {
+ ((UISettingGroup)element).Icon = Icon(fontName, glyph);
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUISettingGroup.InteractiveElement"/> of the setting.
+ /// </summary>
+ public static IUISettingGroup InteractiveElement(this IUISettingGroup element, IUIElement? uiElement)
+ {
+ ((UISettingGroup)element).InteractiveElement = uiElement;
+ return element;
+ }
+
+ /// <summary>
+ /// Set the children to be displayed in the group.
+ /// </summary>
+ public static IUISettingGroup WithChildren(this IUISettingGroup element, params IUIElement[] children)
+ {
+ var settingGroup = (UISettingGroup)element;
+ settingGroup.ChildrenAreAllSettings = children.All(c => c is IUISetting);
+ settingGroup.Children = children;
+ return element;
+ }
+
+ /// <summary>
+ /// Set the children to be displayed in the group.
+ /// </summary>
+ public static IUISettingGroup WithSettings(this IUISettingGroup element, params IUISetting[] settings)
+ {
+ var settingGroup = (UISettingGroup)element;
+ settingGroup.ChildrenAreAllSettings = true;
+ settingGroup.Children = settings;
+ return element;
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs
index 8151a1875..c73272230 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs
@@ -28,7 +28,7 @@ public interface IUIStack : IUIElement
}
[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Orientation = {{{nameof(Orientation)}}}")]
-internal class UIStack : UIElement, IUIStack
+internal sealed class UIStack : UIElement, IUIStack
{
private UIOrientation _orientation;
private IUIElement[]? _children;
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElement.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElement.cs
index e0f37cd07..50e641ee4 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElement.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElement.cs
@@ -8,15 +8,15 @@ public interface IUITitledElement : IUIElement
/// <summary>
/// Gets a title to display for this element.
/// </summary>
- string? DisplayTitle { get; }
+ string? Title { get; }
/// <summary>
- /// Raised when <see cref="DisplayTitle"/> is changed.
+ /// Raised when <see cref="Title"/> is changed.
/// </summary>
- event EventHandler? DisplayTitleChanged;
+ event EventHandler? TitleChanged;
}
-[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Title = {{{nameof(DisplayTitle)}}}")]
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Title = {{{nameof(Title)}}}")]
internal abstract class UITitledElement : UIElement, IUITitledElement
{
private string? _title;
@@ -26,17 +26,17 @@ internal abstract class UITitledElement : UIElement, IUITitledElement
{
}
- public string? DisplayTitle
+ public string? Title
{
get => _title;
internal set
{
_title = value;
- DisplayTitleChanged?.Invoke(this, EventArgs.Empty);
+ TitleChanged?.Invoke(this, EventArgs.Empty);
}
}
- public event EventHandler? DisplayTitleChanged;
+ public event EventHandler? TitleChanged;
}
public static partial class GUI
@@ -48,7 +48,7 @@ public static partial class GUI
{
if (element is UITitledElement strongElement)
{
- strongElement.DisplayTitle = title;
+ strongElement.Title = title;
}
return element;
}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIToolView.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIToolView.cs
new file mode 100644
index 000000000..b5c23d72e
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIToolView.cs
@@ -0,0 +1,6 @@
+namespace DevToys.Api.Tool.GUI.Components;
+
+public interface IUIToolView : IUIElement
+{
+
+}
diff --git a/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs b/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs
index add184146..8a466f408 100644
--- a/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Settings/SettingsGuiTool.cs
@@ -1,4 +1,6 @@
-namespace DevToys.Tools.Settings;
+using DevToys.Api;
+
+namespace DevToys.Tools.Settings;
[Export(typeof(IGuiTool))]
[Name("Settings")]
@@ -33,19 +35,46 @@ internal sealed class SettingsGuiTool : IGuiTool
=> Stack()
.Vertical()
.WithChildren(
+ SettingGroup()
+ .Icon("FluentSystemIcons", "\uF6A9")
+ .Title("Title")
+ .Description("Description")
+ .InteractiveElement(
+ Button().Text("My option"))
+ .WithSettings(
+ Setting()
+ .Title("Title")
+ .Description("Description")
+ .InteractiveElement(
+ Button().Text("My option")),
+ Setting()
+ .Title("Title")
+ .Description("Description")
+ .InteractiveElement(
+ Button().Text("My option")),
+ Setting()
+ .Title("Title")
+ .Description("Description")
+ .InteractiveElement(
+ Button().Text("My option")),
+ Setting()
+ .Title("Title")
+ .Description("Description")
+ .InteractiveElement(
+ Button().Text("My option"))),
Stack()
- .Horizontal()
- .WithChildren(
- _topLeftButton,
- Button().Text("Top Center button"),
- Button().Text("Top Right button")),
+ .Horizontal()
+ .WithChildren(
+ _topLeftButton,
+ Button().Text("Top Center button"),
+ Button().Text("Top Right button")),
Stack()
- .Horizontal()
- .Disable()
- .WithChildren(
- Button().Text("Bottom Left button"),
- Button().Text("Bottom Center button").OnClick(OnBottomCenterButtonClickAsync),
- Button().Text("Bottom Right button")));
+ .Horizontal()
+ .Disable()
+ .WithChildren(
+ Button().Text("Bottom Left button"),
+ Button().Text("Bottom Center button").OnClick(OnBottomCenterButtonClickAsync),
+ Button().Text("Bottom Right button")));
private ValueTask OnMyButtonClickAsync()
{
@@ -59,8 +88,3 @@ internal sealed class SettingsGuiTool : IGuiTool
return ValueTask.CompletedTask;
}
}
-
-
-
-
-
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIButtonPresenter.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIButtonPresenter.xaml.cs
index 153eca18c..59f708eb5 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIButtonPresenter.xaml.cs
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIButtonPresenter.xaml.cs
@@ -19,19 +19,20 @@ public sealed partial class UIButtonPresenter : Button
{
UIButton.IsEnabledChanged += UIButton_IsEnabledChanged;
UIButton.IsVisibleChanged += UIButton_IsVisibleChanged;
- UIButton.DisplayTextChanged += UIButton_DisplayTextChanged;
+ UIButton.TextChanged += UIButton_TextChanged;
IsEnabled = UIButton.IsEnabled;
Visibility = UIButton.IsVisible ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
- Content = UIButton.DisplayText;
+ Content = UIButton.Text;
}
private void UIButtonPresenter_Unloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
UIButton.IsEnabledChanged -= UIButton_IsEnabledChanged;
UIButton.IsVisibleChanged -= UIButton_IsVisibleChanged;
- UIButton.DisplayTextChanged -= UIButton_DisplayTextChanged;
+ UIButton.TextChanged -= UIButton_TextChanged;
Loaded -= UIButtonPresenter_Loaded;
+ Unloaded -= UIButtonPresenter_Unloaded;
}
private void UIButton_IsEnabledChanged(object? sender, EventArgs e)
@@ -44,9 +45,9 @@ public sealed partial class UIButtonPresenter : Button
Visibility = UIButton.IsVisible ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
}
- private void UIButton_DisplayTextChanged(object? sender, EventArgs e)
+ private void UIButton_TextChanged(object? sender, EventArgs e)
{
- Content = UIButton.DisplayText;
+ Content = UIButton.Text;
}
private void OnButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
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 a1d147fc3..7dc7f9032 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementPresenter.xaml
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementPresenter.xaml
@@ -6,10 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
- AllowFocusOnInteraction="False"
- AllowFocusWhenDisabled="False"
- TabFocusNavigation="Local"
- TabNavigation="Local">
+ IsTabStop="False">
<Control.Resources>
<local:UIElementTemplateSelector x:Key="UIElementTemplateSelector">
@@ -27,6 +24,27 @@
</DataTemplate>
</local:UIElementTemplateSelector.UIStackTemplate>
+ <!-- UIIcon -->
+ <local:UIElementTemplateSelector.UIIconTemplate>
+ <DataTemplate>
+ <local:UIIconPresenter/>
+ </DataTemplate>
+ </local:UIElementTemplateSelector.UIIconTemplate>
+
+ <!-- UISetting -->
+ <local:UIElementTemplateSelector.UISettingTemplate>
+ <DataTemplate>
+ <local:UISettingPresenter/>
+ </DataTemplate>
+ </local:UIElementTemplateSelector.UISettingTemplate>
+
+ <!-- UISettingGroup -->
+ <local:UIElementTemplateSelector.UISettingGroupTemplate>
+ <DataTemplate>
+ <local:UISettingGroupPresenter/>
+ </DataTemplate>
+ </local:UIElementTemplateSelector.UISettingGroupTemplate>
+
</local:UIElementTemplateSelector>
</Control.Resources>
@@ -34,13 +52,10 @@
<ControlTemplate TargetType="local:UIElementPresenter">
<ContentControl
ContentTemplateSelector="{StaticResource UIElementTemplateSelector}"
- Content="{x:Bind UIElement}"
+ Content="{x:Bind UIElement, Mode=OneWay}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
- AllowFocusOnInteraction="False"
- AllowFocusWhenDisabled="False"
- TabFocusNavigation="Local"
- TabNavigation="Local"/>
+ IsTabStop="False"/>
</ControlTemplate>
</Control.Template>
</Control>
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 ebf9c5942..deec5a24f 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementTemplateSelector.cs
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIElementTemplateSelector.cs
@@ -19,17 +19,35 @@ internal sealed class UIElementTemplateSelector : DataTemplateSelector
/// </summary>
public DataTemplate UIStackTemplate { get; set; } = null!;
+ /// <summary>
+ /// The data template used to display a <see cref="IUIIcon"/>.
+ /// </summary>
+ public DataTemplate UIIconTemplate { get; set; } = null!;
+
+ /// <summary>
+ /// The data template used to display a <see cref="IUISetting"/>.
+ /// </summary>
+ public DataTemplate UISettingTemplate { get; set; } = null!;
+
+ /// <summary>
+ /// The data template used to display a <see cref="IUISettingGroup"/>.
+ /// </summary>
+ public DataTemplate UISettingGroupTemplate { get; set; } = null!;
+
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item is null)
{
- return null;
+ return null!;
}
return item switch
{
IUIButton => UIButtonTemplate,
IUIStack => UIStackTemplate,
+ IUIIcon => UIIconTemplate,
+ IUISettingGroup => UISettingGroupTemplate,
+ IUISetting => UISettingTemplate,
_ => 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/UIIconPresenter.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIIconPresenter.xaml
new file mode 100644
index 000000000..5f62c74c8
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIIconPresenter.xaml
@@ -0,0 +1,14 @@
+<ContentControl
+ x:Class="DevToys.UI.Framework.Controls.GuiTool.UIIconPresenter"
+ 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">
+ <FontIcon
+ x:Name="FontIcon"
+ HorizontalAlignment="Stretch"
+ VerticalAlignment="Stretch"/>
+</ContentControl>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIIconPresenter.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIIconPresenter.xaml.cs
new file mode 100644
index 000000000..1ce04d888
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIIconPresenter.xaml.cs
@@ -0,0 +1,86 @@
+using DevToys.Api;
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Controls;
+using Microsoft.UI.Xaml.Media;
+
+namespace DevToys.UI.Framework.Controls.GuiTool;
+
+public sealed partial class UIIconPresenter : ContentControl
+{
+ public UIIconPresenter()
+ {
+ this.InitializeComponent();
+
+ Loaded += UIIconPresenter_Loaded;
+ Unloaded += UIIconPresenter_Unloaded;
+ }
+
+ internal IUIIcon UIIcon => (IUIIcon)DataContext;
+
+ private void UIIconPresenter_Loaded(object sender, RoutedEventArgs e)
+ {
+ UIIcon.IsEnabledChanged += UIIcon_IsEnabledChanged;
+ UIIcon.IsVisibleChanged += UIIcon_IsVisibleChanged;
+ UIIcon.FontNameChanged += UIIcon_FontNameChanged;
+ UIIcon.GlyphChanged += UIIcon_GlyphChanged;
+ UIIcon.SizeChanged += UIIcon_SizeChanged;
+
+ IsEnabled = UIIcon.IsEnabled;
+ Visibility = UIIcon.IsVisible ? Visibility.Visible : Visibility.Collapsed;
+ SetFontFamily(UIIcon.FontName);
+ FontIcon.Glyph = UIIcon.Glyph;
+ FontIcon.FontSize = UIIcon.Size;
+ }
+
+ private void UIIconPresenter_Unloaded(object sender, RoutedEventArgs e)
+ {
+ UIIcon.IsEnabledChanged -= UIIcon_IsEnabledChanged;
+ UIIcon.IsVisibleChanged -= UIIcon_IsVisibleChanged;
+ UIIcon.FontNameChanged -= UIIcon_FontNameChanged;
+ UIIcon.GlyphChanged -= UIIcon_GlyphChanged;
+ UIIcon.SizeChanged -= UIIcon_SizeChanged;
+ Loaded -= UIIconPresenter_Loaded;
+ Unloaded -= UIIconPresenter_Unloaded;
+ }
+
+ private void UIIcon_IsEnabledChanged(object? sender, EventArgs e)
+ {
+ IsEnabled = UIIcon.IsEnabled;
+ }
+
+ private void UIIcon_IsVisibleChanged(object? sender, EventArgs e)
+ {
+ Visibility = UIIcon.IsVisible ? Visibility.Visible : Visibility.Collapsed;
+ }
+
+ private void UIIcon_FontNameChanged(object? sender, EventArgs e)
+ {
+ SetFontFamily(UIIcon.FontName);
+ }
+
+ private void UIIcon_GlyphChanged(object? sender, EventArgs e)
+ {
+ FontIcon.Glyph = UIIcon.Glyph;
+ }
+
+ private void UIIcon_SizeChanged(object? sender, EventArgs e)
+ {
+ FontIcon.FontSize = UIIcon.Size;
+ }
+
+ private void SetFontFamily(string fontName)
+ {
+ if (!string.IsNullOrEmpty(fontName))
+ {
+ if (Application.Current.Resources.TryGetValue(fontName, out object? result) && result is FontFamily fontFamily)
+ {
+ FontIcon.FontFamily = fontFamily;
+ return;
+ }
+
+ ThrowHelper.ThrowInvalidDataException($"Unable to font the font named '{fontName}' in the app resources.");
+ }
+
+ FontIcon.FontFamily = null;
+ }
+}
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingGroupPresenter.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingGroupPresenter.xaml
new file mode 100644
index 000000000..208ccc00a
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingGroupPresenter.xaml
@@ -0,0 +1,25 @@
+<Expander
+ x:Class="DevToys.UI.Framework.Controls.GuiTool.UISettingGroupPresenter"
+ 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"
+ HorizontalAlignment="Stretch"
+ HorizontalContentAlignment="Stretch"
+ VerticalAlignment="Stretch"
+ VerticalContentAlignment="Stretch"
+ Padding="0"
+ CornerRadius="{x:Bind CornerRadius}">
+ <Expander.Header>
+ <local:UISettingHeaderControl
+ x:Name="UISettingHeaderControl"
+ Margin="0,16"/>
+ </Expander.Header>
+
+ <StackPanel
+ x:Name="ContentStackPanel"
+ HorizontalAlignment="Stretch"
+ Orientation="Vertical"/>
+</Expander>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingGroupPresenter.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingGroupPresenter.xaml.cs
new file mode 100644
index 000000000..2f37629f7
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingGroupPresenter.xaml.cs
@@ -0,0 +1,152 @@
+using DevToys.Api;
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Automation;
+using Microsoft.UI.Xaml.Controls;
+
+namespace DevToys.UI.Framework.Controls.GuiTool;
+
+public sealed partial class UISettingGroupPresenter : Expander
+{
+ public UISettingGroupPresenter()
+ {
+ this.InitializeComponent();
+
+ CornerRadius = new CornerRadius(4); // Default corner radius.
+
+ Loaded += UISettingGroupPresenter_Loaded;
+ Unloaded += UISettingGroupPresenter_Unloaded;
+ }
+
+ internal IUISettingGroup UISettingGroup => (IUISettingGroup)DataContext;
+
+ private void UISettingGroupPresenter_Loaded(object sender, RoutedEventArgs e)
+ {
+ UISettingGroup.IsEnabledChanged += UISettingGroup_IsEnabledChanged;
+ UISettingGroup.IsVisibleChanged += UISettingGroup_IsVisibleChanged;
+ UISettingGroup.TitleChanged += UISettingGroup_TitleChanged;
+ UISettingGroup.DescriptionChanged += UISettingGroup_DescriptionChanged;
+ UISettingGroup.IconChanged += UISettingGroup_IconChanged;
+ UISettingGroup.InteractiveElementChanged += UISettingGroup_InteractiveElementChanged;
+ UISettingGroup.ChildrenChanged += UISettingGroup_ChildrenChanged;
+
+ IsEnabled = UISettingGroup.IsEnabled;
+ Visibility = UISettingGroup.IsVisible ? Visibility.Visible : Visibility.Collapsed;
+ UISettingHeaderControl.Title = UISettingGroup.Title;
+ UISettingHeaderControl.Description = UISettingGroup.Description;
+ UISettingHeaderControl.SettingActionableElement = UISettingGroup.InteractiveElement;
+ UISettingHeaderControl.Icon = UISettingGroup.Icon;
+ if (UISettingGroup.Icon != null)
+ {
+ UISettingGroup.Icon.Size(20);
+ }
+
+ SetChildren();
+
+ AutomationProperties.SetName(this, UISettingGroup.Title);
+ AutomationProperties.SetHelpText(this, UISettingGroup.Description);
+ }
+
+ private void UISettingGroupPresenter_Unloaded(object sender, RoutedEventArgs e)
+ {
+ UISettingGroup.IsEnabledChanged -= UISettingGroup_IsEnabledChanged;
+ UISettingGroup.IsVisibleChanged -= UISettingGroup_IsVisibleChanged;
+ UISettingGroup.TitleChanged -= UISettingGroup_TitleChanged;
+ UISettingGroup.DescriptionChanged -= UISettingGroup_DescriptionChanged;
+ UISettingGroup.IconChanged -= UISettingGroup_IconChanged;
+ UISettingGroup.InteractiveElementChanged -= UISettingGroup_InteractiveElementChanged;
+ UISettingGroup.ChildrenChanged -= UISettingGroup_ChildrenChanged;
+ Loaded -= UISettingGroupPresenter_Loaded;
+ Unloaded -= UISettingGroupPresenter_Unloaded;
+ }
+
+ private void UISettingGroup_TitleChanged(object? sender, EventArgs e)
+ {
+ UISettingHeaderControl.Title = UISettingGroup.Title;
+ }
+
+ private void UISettingGroup_DescriptionChanged(object? sender, EventArgs e)
+ {
+ UISettingHeaderControl.Description = UISettingGroup.Description;
+ }
+
+ private void UISettingGroup_IconChanged(object? sender, EventArgs e)
+ {
+ if (UISettingGroup.Icon != null)
+ {
+ UISettingGroup.Icon.Size(20);
+ }
+ UISettingHeaderControl.Icon = UISettingGroup.Icon;
+ }
+
+ private void UISettingGroup_InteractiveElementChanged(object? sender, EventArgs e)
+ {
+ UISettingHeaderControl.SettingActionableElement = UISettingGroup.InteractiveElement;
+ }
+
+ private void UISettingGroup_ChildrenChanged(object? sender, EventArgs e)
+ {
+ SetChildren();
+ }
+
+ private void UISettingGroup_IsEnabledChanged(object? sender, EventArgs e)
+ {
+ IsEnabled = UISettingGroup.IsEnabled;
+ }
+
+ private void UISettingGroup_IsVisibleChanged(object? sender, EventArgs e)
+ {
+ Visibility = UISettingGroup.IsVisible ? Visibility.Visible : Visibility.Collapsed;
+ }
+
+ private void SetChildren()
+ {
+ ContentStackPanel.Children.Clear();
+ if (UISettingGroup.Children is not null)
+ {
+ bool allChildrenAreUISetting = true;
+ for (int i = 0; i < UISettingGroup.Children.Length; i++)
+ {
+ IUIElement element = UISettingGroup.Children[i];
+ if (UISettingGroup.ChildrenAreAllSettings)
+ {
+ CornerRadius cornerRadius;
+ if (i == UISettingGroup.Children.Length - 1)
+ {
+ cornerRadius = new CornerRadius(0, 0, 4, 4);
+ }
+ else
+ {
+ cornerRadius = new CornerRadius(0);
+ }
+
+ var settingPresenter
+ = new UISettingPresenter()
+ {
+ DataContext = element,
+ CornerRadius = cornerRadius
+ };
+
+ ContentStackPanel.Children.Add(settingPresenter);
+ }
+ else
+ {
+ ContentStackPanel.Children.Add(UIElementPresenter.Create(element));
+ }
+ }
+
+ // Adjust the spacing inside of the group.
+ if (allChildrenAreUISetting)
+ {
+ ContentStackPanel.Spacing = 0;
+ ContentStackPanel.Padding = default;
+ ContentStackPanel.Margin = default;
+ }
+ else
+ {
+ ContentStackPanel.Spacing = 4;
+ ContentStackPanel.Padding = new Thickness(12);
+ ContentStackPanel.Margin = new Thickness(42, 0, 6, 0);
+ }
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingHeaderControl.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingHeaderControl.xaml
new file mode 100644
index 000000000..3f03c6365
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingHeaderControl.xaml
@@ -0,0 +1,96 @@
+<UserControl
+ x:Class="DevToys.UI.Framework.Controls.GuiTool.UISettingHeaderControl"
+ 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"
+ xmlns:converters="using:DevToys.UI.Framework.Converters"
+ mc:Ignorable="d">
+ <UserControl.Resources>
+ <converters:NullToBooleanConverter x:Key="NullToTrueConverter"/>
+ <converters:NullToBooleanConverter x:Key="NullToFalseConverter" IsInverted="True"/>
+ <converters:NullToBooleanConverter x:Key="StringNullOrWhiteSpaceToFalseConverter" IsInverted="True" EnforceNonWhiteSpaceString="True"/>
+
+ <Style x:Key="TextBlockSettingDescriptionStyle" TargetType="TextBlock">
+ <Setter Property="TextWrapping" Value="Wrap" />
+ <Setter Property="FontSize" Value="12" />
+ <Setter Property="Opacity" Value="0.75" />
+ </Style>
+ </UserControl.Resources>
+
+ <Grid
+ x:Name="MainPanel"
+ HorizontalAlignment="Stretch"
+ VerticalAlignment="Center"
+ SizeChanged="MainPanel_SizeChanged">
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition Width="Auto" />
+ <ColumnDefinition Width="*" />
+ <ColumnDefinition Width="Auto" />
+ </Grid.ColumnDefinitions>
+
+ <Grid.RowDefinitions>
+ <RowDefinition Height="*" />
+ <RowDefinition Height="Auto" />
+ </Grid.RowDefinitions>
+
+ <local:UIElementPresenter
+ x:Name="SettingIconElement"
+ Margin="2,0,20,0"
+ Width="20"
+ VerticalAlignment="Center"
+ UIElement="{x:Bind Icon, Mode=OneWay}"/>
+
+ <Grid
+ x:Name="DescriptionPanel"
+ Grid.Column="1"
+ HorizontalAlignment="Left"
+ VerticalAlignment="Center">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto" />
+ <RowDefinition Height="Auto" />
+ </Grid.RowDefinitions>
+
+ <TextBlock
+ x:Name="TitleBlock"
+ x:Load="{x:Bind Title, Mode=OneWay, Converter={StaticResource StringNullOrWhiteSpaceToFalseConverter}}"
+ Text="{x:Bind Title, Mode=OneWay}"
+ TextWrapping="Wrap" />
+
+ <TextBlock
+ x:Name="DescriptionBlock"
+ Grid.Row="1"
+ x:Load="{x:Bind Description, Mode=OneWay, Converter={StaticResource StringNullOrWhiteSpaceToFalseConverter}}"
+ Style="{StaticResource TextBlockSettingDescriptionStyle}"
+ Text="{x:Bind Description, Mode=OneWay}" />
+ </Grid>
+
+ <local:UIElementPresenter
+ x:Name="ActionableElement"
+ Grid.Column="2"
+ Margin="24,0,0,0"
+ HorizontalAlignment="Right"
+ VerticalAlignment="Center"
+ x:Load="{x:Bind SettingActionableElement, Mode=OneWay, Converter={StaticResource NullToFalseConverter}}"
+ UIElement="{x:Bind SettingActionableElement, Mode=OneWay}" />
+ <VisualStateManager.VisualStateGroups>
+ <VisualStateGroup>
+ <VisualState x:Name="NormalState">
+ <VisualState.Setters>
+ <Setter Target="ActionableElement.(Grid.Row)" Value="0" />
+ <Setter Target="ActionableElement.(Grid.Column)" Value="2" />
+ <Setter Target="ActionableElement.Margin" Value="24,0,0,0" />
+ </VisualState.Setters>
+ </VisualState>
+ <VisualState x:Name="CompactState">
+ <VisualState.Setters>
+ <Setter Target="ActionableElement.(Grid.Row)" Value="1" />
+ <Setter Target="ActionableElement.(Grid.Column)" Value="1" />
+ <Setter Target="ActionableElement.Margin" Value="0,4,0,0" />
+ </VisualState.Setters>
+ </VisualState>
+ </VisualStateGroup>
+ </VisualStateManager.VisualStateGroups>
+ </Grid>
+</UserControl>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingHeaderControl.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingHeaderControl.xaml.cs
new file mode 100644
index 000000000..fda2c7b7a
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingHeaderControl.xaml.cs
@@ -0,0 +1,96 @@
+using DevToys.Api;
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Automation;
+using Microsoft.UI.Xaml.Controls;
+using Microsoft.UI.Xaml.Markup;
+
+namespace DevToys.UI.Framework.Controls.GuiTool;
+
+[ContentProperty(Name = nameof(SettingActionableElement))]
+public sealed partial class UISettingHeaderControl : UserControl
+{
+ public static readonly DependencyProperty SettingActionableElementProperty
+ = DependencyProperty.Register(
+ nameof(SettingActionableElement),
+ typeof(IUIElement),
+ typeof(UISettingHeaderControl),
+ new PropertyMetadata(null));
+
+ public IUIElement? SettingActionableElement
+ {
+ get => (IUIElement?)GetValue(SettingActionableElementProperty);
+ set => SetValue(SettingActionableElementProperty, value);
+ }
+
+ public static readonly DependencyProperty TitleProperty
+ = DependencyProperty.Register(
+ nameof(Title),
+ typeof(string),
+ typeof(UISettingHeaderControl),
+ new PropertyMetadata(
+ string.Empty,
+ (d, e) =>
+ {
+ AutomationProperties.SetName(d, (string)e.NewValue);
+ }));
+
+ public string? Title
+ {
+ get => (string?)GetValue(TitleProperty);
+ set => SetValue(TitleProperty, value);
+ }
+
+ public static readonly DependencyProperty DescriptionProperty
+ = DependencyProperty.Register(
+ nameof(Description),
+ typeof(string),
+ typeof(UISettingHeaderControl),
+ new PropertyMetadata(
+ string.Empty,
+ (d, e) =>
+ {
+ AutomationProperties.SetHelpText(d, (string)e.NewValue);
+ }));
+
+ public string? Description
+ {
+ get => (string?)GetValue(DescriptionProperty);
+ set => SetValue(DescriptionProperty, value);
+ }
+
+ public static readonly DependencyProperty IconProperty
+ = DependencyProperty.Register(
+ nameof(Icon),
+ typeof(IUIIcon),
+ typeof(UISettingHeaderControl),
+ new PropertyMetadata(null));
+
+ public IUIIcon? Icon
+ {
+ get => (IUIIcon?)GetValue(IconProperty);
+ set => SetValue(IconProperty, value);
+ }
+
+ public UISettingHeaderControl()
+ {
+ InitializeComponent();
+ VisualStateManager.GoToState(this, "NormalState", false);
+ }
+
+ private void MainPanel_SizeChanged(object sender, SizeChangedEventArgs e)
+ {
+ if (e.NewSize.Width == e.PreviousSize.Width || ActionableElement == null)
+ {
+ return;
+ }
+
+ if (ActionableElement.ActualWidth > e.NewSize.Width / 3)
+ {
+ VisualStateManager.GoToState(this, "CompactState", false);
+ }
+ else
+ {
+ VisualStateManager.GoToState(this, "NormalState", false);
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingPresenter.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingPresenter.xaml
new file mode 100644
index 000000000..b33f78812
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingPresenter.xaml
@@ -0,0 +1,30 @@
+<UserControl
+ x:Class="DevToys.UI.Framework.Controls.GuiTool.UISettingPresenter"
+ 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"
+ xmlns:converters="using:DevToys.UI.Framework.Converters"
+ mc:Ignorable="d">
+ <UserControl.Resources>
+ <converters:NullToBooleanConverter x:Key="NullToTrueConverter"/>
+ <converters:NullToBooleanConverter x:Key="NullToFalseConverter" IsInverted="True"/>
+
+ <Style x:Key="GridSettingBlockStyle" TargetType="Grid">
+ <Setter Property="BorderBrush" Value="{ThemeResource ExpanderHeaderBorderBrush}" />
+ <Setter Property="BorderThickness" Value="{ThemeResource ExpanderHeaderBorderThickness}" />
+ <Setter Property="HorizontalAlignment" Value="Stretch" />
+ <Setter Property="MinHeight" Value="{StaticResource ExpanderMinHeight}" />
+ <Setter Property="Padding" Value="{StaticResource ExpanderContentPadding}" />
+ </Style>
+ </UserControl.Resources>
+
+ <Grid
+ x:Name="NonExpanderGrid"
+ Style="{StaticResource GridSettingBlockStyle}"
+ Background="{ThemeResource ExpanderHeaderBackground}"
+ CornerRadius="{x:Bind CornerRadius}">
+ <local:UISettingHeaderControl x:Name="UISettingHeaderControl"/>
+ </Grid>
+</UserControl>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingPresenter.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingPresenter.xaml.cs
new file mode 100644
index 000000000..b3d2402ad
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UISettingPresenter.xaml.cs
@@ -0,0 +1,87 @@
+using DevToys.Api;
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Controls;
+
+namespace DevToys.UI.Framework.Controls.GuiTool;
+
+public sealed partial class UISettingPresenter : UserControl
+{
+ public UISettingPresenter()
+ {
+ this.InitializeComponent();
+
+ CornerRadius = new CornerRadius(4); // Default corner radius.
+
+ Loaded += UISettingPresenter_Loaded;
+ Unloaded += UISettingPresenter_Unloaded;
+ }
+
+ internal IUISetting UISetting => (IUISetting)DataContext;
+
+ private void UISettingPresenter_Loaded(object sender, RoutedEventArgs e)
+ {
+ UISetting.IsEnabledChanged += UISetting_IsEnabledChanged;
+ UISetting.IsVisibleChanged += UISetting_IsVisibleChanged;
+ UISetting.TitleChanged += UISetting_TitleChanged;
+ UISetting.DescriptionChanged += UISetting_DescriptionChanged;
+ UISetting.IconChanged += UISetting_IconChanged;
+ UISetting.InteractiveElementChanged += UISetting_InteractiveElementChanged;
+
+ IsEnabled = UISetting.IsEnabled;
+ Visibility = UISetting.IsVisible ? Visibility.Visible : Visibility.Collapsed;
+ UISettingHeaderControl.Title = UISetting.Title;
+ UISettingHeaderControl.Description = UISetting.Description;
+ UISettingHeaderControl.SettingActionableElement = UISetting.InteractiveElement;
+ UISettingHeaderControl.Icon = UISetting.Icon;
+ if (UISetting.Icon != null)
+ {
+ UISetting.Icon.Size(20);
+ }
+ }
+
+ private void UISettingPresenter_Unloaded(object sender, RoutedEventArgs e)
+ {
+ UISetting.IsEnabledChanged -= UISetting_IsEnabledChanged;
+ UISetting.IsVisibleChanged -= UISetting_IsVisibleChanged;
+ UISetting.TitleChanged -= UISetting_TitleChanged;
+ UISetting.DescriptionChanged -= UISetting_DescriptionChanged;
+ UISetting.IconChanged -= UISetting_IconChanged;
+ UISetting.InteractiveElementChanged -= UISetting_InteractiveElementChanged;
+ Loaded -= UISettingPresenter_Loaded;
+ Unloaded -= UISettingPresenter_Unloaded;
+ }
+
+ private void UISetting_TitleChanged(object? sender, EventArgs e)
+ {
+ UISettingHeaderControl.Title = UISetting.Title;
+ }
+
+ private void UISetting_DescriptionChanged(object? sender, EventArgs e)
+ {
+ UISettingHeaderControl.Description = UISetting.Description;
+ }
+
+ private void UISetting_IconChanged(object? sender, EventArgs e)
+ {
+ if (UISetting.Icon != null)
+ {
+ UISetting.Icon.Size(20);
+ }
+ UISettingHeaderControl.Icon = UISetting.Icon;
+ }
+
+ private void UISetting_InteractiveElementChanged(object? sender, EventArgs e)
+ {
+ UISettingHeaderControl.SettingActionableElement = UISetting.InteractiveElement;
+ }
+
+ private void UISetting_IsEnabledChanged(object? sender, EventArgs e)
+ {
+ IsEnabled = UISetting.IsEnabled;
+ }
+
+ private void UISetting_IsVisibleChanged(object? sender, EventArgs e)
+ {
+ Visibility = UISetting.IsVisible ? Visibility.Visible : Visibility.Collapsed;
+ }
+}
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml
index c157f2124..223823b0a 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml
@@ -6,6 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
+ IsTabStop="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml.cs
index 149d9f699..1b18d0b96 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml.cs
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UIStackPresenter.xaml.cs
@@ -17,9 +17,9 @@ public sealed partial class UIStackPresenter : ContentControl
private void UIStackPresenter_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
- UIStack.IsEnabledChanged += UIButton_IsEnabledChanged;
- UIStack.IsVisibleChanged += UIButton_IsVisibleChanged;
- UIStack.OrientationChanged += UIButton_OrientationChanged;
+ UIStack.IsEnabledChanged += UIStack_IsEnabledChanged;
+ UIStack.IsVisibleChanged += UIStack_IsVisibleChanged;
+ UIStack.OrientationChanged += UIStack_OrientationChanged;
UIStack.ChildrenChanged += UIStack_ChildrenChanged;
IsEnabled = UIStack.IsEnabled;
@@ -30,23 +30,25 @@ public sealed partial class UIStackPresenter : ContentControl
private void UIStackPresenter_Unloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
- UIStack.IsEnabledChanged -= UIButton_IsEnabledChanged;
- UIStack.IsVisibleChanged -= UIButton_IsVisibleChanged;
- UIStack.OrientationChanged -= UIButton_OrientationChanged;
+ UIStack.IsEnabledChanged -= UIStack_IsEnabledChanged;
+ UIStack.IsVisibleChanged -= UIStack_IsVisibleChanged;
+ UIStack.OrientationChanged -= UIStack_OrientationChanged;
+ UIStack.ChildrenChanged -= UIStack_ChildrenChanged;
Loaded -= UIStackPresenter_Loaded;
+ Unloaded -= UIStackPresenter_Unloaded;
}
- private void UIButton_IsEnabledChanged(object? sender, EventArgs e)
+ private void UIStack_IsEnabledChanged(object? sender, EventArgs e)
{
IsEnabled = UIStack.IsEnabled;
}
- private void UIButton_IsVisibleChanged(object? sender, EventArgs e)
+ private void UIStack_IsVisibleChanged(object? sender, EventArgs e)
{
Visibility = UIStack.IsVisible ? Microsoft.UI.Xaml.Visibility.Visible : Microsoft.UI.Xaml.Visibility.Collapsed;
}
- private void UIButton_OrientationChanged(object? sender, EventArgs e)
+ private void UIStack_OrientationChanged(object? sender, EventArgs e)
{
StackPanel.Orientation = UIStack.Orientation == UIOrientation.Horizontal ? Orientation.Horizontal : Orientation.Vertical;
}
diff --git a/src/app/dev/DevToys.UI.Framework/Converters/NullToBooleanConverter.cs b/src/app/dev/DevToys.UI.Framework/Converters/NullToBooleanConverter.cs
new file mode 100644
index 000000000..1304054b7
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Converters/NullToBooleanConverter.cs
@@ -0,0 +1,45 @@
+using Microsoft.UI.Xaml.Data;
+
+namespace DevToys.UI.Framework.Converters;
+
+/// <summary>
+/// Converts a null value to a <see cref="bool"/> value.
+/// </summary>
+public sealed class NullToBooleanConverter : IValueConverter
+{
+ public bool IsInverted { get; set; }
+
+ public bool EnforceNonWhiteSpaceString { get; set; }
+
+ public object Convert(object value, Type targetType, object parameter, string language)
+ {
+ if (value?.GetType() == typeof(string))
+ {
+ if (IsInverted)
+ {
+ if (EnforceNonWhiteSpaceString)
+ {
+ return !string.IsNullOrWhiteSpace((string)value);
+ }
+ return !string.IsNullOrEmpty((string)value);
+ }
+
+ if (EnforceNonWhiteSpaceString)
+ {
+ return !string.IsNullOrWhiteSpace((string)value);
+ }
+ return string.IsNullOrEmpty((string)value);
+ }
+
+ if (IsInverted)
+ {
+ return value != null;
+ }
+ return value == null;
+ }
+
+ public object ConvertBack(object value, Type targetType, object parameter, string language)
+ {
+ throw new NotImplementedException();
+ }
+}
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 30ea3d1ba..9c0d31e6a 100644
--- a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
+++ b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
@@ -22,10 +22,6 @@
</Page>
<PRIResource Include="**\*.resw" />
<Content Include="Assets\**" />
- <None Remove="Controls\GuiTool\UIButtonPresenter.xaml" />
- <None Remove="Controls\GuiTool\UIStackPresenter.xaml" />
- <UpToDateCheckInput Remove="Controls\GuiTool\UIButtonPresenter.xaml" />
- <UpToDateCheckInput Remove="Controls\GuiTool\UIStackPresenter.xaml" />
<Compile Update="**\*.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
@@ -101,10 +97,4 @@
<ProjectReference Include="..\DevToys.Api\DevToys.Api.csproj" />
</ItemGroup>
- <ItemGroup>
- <Compile Update="Controls\GuiTool\UIStackPresenter.xaml.cs">
- <DependentUpon>UIStackPresenter.xaml</DependentUpon>
- </Compile>
- </ItemGroup>
-
</Project>