overhauled switch component

This commit is contained in:
krut_ni 2026-03-10 11:45:15 +01:00
parent b221d083d2
commit 25eb0cfbe2
No known key found for this signature in database
GPG Key ID: A5C0151B4DDB172C
4 changed files with 67 additions and 6 deletions

View File

@ -103,9 +103,23 @@
} }
break; break;
case AssistantComponentType.SWITCH: case AssistantComponentType.SWITCH:
if (component is AssistantSwitch assistantSwitch) if (component is AssistantSwitch switchComponent)
{ {
<MudTextSwitch Label="@assistantSwitch.Label" @bind-Value="@this.switchFields[assistantSwitch.Name]" LabelOn="@assistantSwitch.LabelOn" LabelOff="@assistantSwitch.LabelOff" /> var assistantSwitch = switchComponent;
var currentValue = this.switchFields[assistantSwitch.Name];
<MudField Label="@assistantSwitch.Label" Variant="Variant.Outlined" Class="mb-3" Disabled="@assistantSwitch.Disabled">
<MudSwitch T="bool"
Value="@currentValue"
ValueChanged="@((bool value) => this.switchFields[assistantSwitch.Name] = value)"
LabelPlacement="@assistantSwitch.GetLabelPlacement()"
Color="@assistantSwitch.GetColor(assistantSwitch.CheckedColor)"
UncheckedColor="@assistantSwitch.GetColor(assistantSwitch.UncheckedColor)"
ThumbIcon="@assistantSwitch.GetIconSvg()"
ThumbIconColor="@assistantSwitch.GetColor(assistantSwitch.IconColor)"
Disabled="@assistantSwitch.Disabled">
@(currentValue ? assistantSwitch.LabelOn : assistantSwitch.LabelOff)
</MudSwitch>
</MudField>
} }
break; break;
case AssistantComponentType.HEADING: case AssistantComponentType.HEADING:

View File

@ -107,9 +107,15 @@ ASSISTANT = {
["Name"] = "<unique identifier of this component>", -- required ["Name"] = "<unique identifier of this component>", -- required
["Label"] = "<heading of your component>", -- required ["Label"] = "<heading of your component>", -- required
["Value"] = true, -- initial switch state ["Value"] = true, -- initial switch state
["Disabled"] = false, -- if true, disables user interaction but the value can still be used in the user prompt (use for presentation purposes)
["UserPrompt"] = "<direct input of instructions, questions, or tasks by a user>", ["UserPrompt"] = "<direct input of instructions, questions, or tasks by a user>",
["LabelOn"] = "<text if state is true>", -- required ["LabelOn"] = "<text if state is true>",
["LabelOff"] = "<text if state is false>" -- required ["LabelOff"] = "<text if state is false>",
["LabelPlacement"] = "<Bottom|End|Left|Right|Start|Top>", -- Defaults to End (right of the switch)
["Icon"] = "Icons.Material.Filled.Bolt", -- places a thumb icon inside the switch
["IconColor"] = "<Dark|Error|Info|Inherit|Primary|Secondary|Success|Surface|Tertiary|Transparent|Warning>", -- color of the thumb icon. Defaults to `Inherit`
["CheckedColor"] = "<Dark|Error|Info|Inherit|Primary|Secondary|Success|Surface|Tertiary|Transparent|Warning>", -- color of the switch if state is true. Defaults to `Inherit`
["UncheckedColor"] = "<Dark|Error|Info|Inherit|Primary|Secondary|Success|Surface|Tertiary|Transparent|Warning>", -- color of the switch if state is false. Defaults to `Inherit`
} }
}, },
{ {

View File

@ -1,3 +1,5 @@
using AIStudio.Tools.PluginSystem.Assistants.Icons;
namespace AIStudio.Tools.PluginSystem.Assistants.DataModel; namespace AIStudio.Tools.PluginSystem.Assistants.DataModel;
internal sealed class AssistantSwitch : AssistantComponentBase internal sealed class AssistantSwitch : AssistantComponentBase
@ -24,6 +26,12 @@ internal sealed class AssistantSwitch : AssistantComponentBase
set => AssistantComponentPropHelper.WriteBool(this.Props, nameof(this.Value), value); set => AssistantComponentPropHelper.WriteBool(this.Props, nameof(this.Value), value);
} }
public bool Disabled
{
get => AssistantComponentPropHelper.ReadBool(this.Props, nameof(this.Disabled), false);
set => AssistantComponentPropHelper.WriteBool(this.Props, nameof(this.Disabled), value);
}
public string UserPrompt public string UserPrompt
{ {
get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.UserPrompt)); get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.UserPrompt));
@ -42,4 +50,37 @@ internal sealed class AssistantSwitch : AssistantComponentBase
set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.LabelOff), value); set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.LabelOff), value);
} }
public string LabelPlacement
{
get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.LabelPlacement));
set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.LabelPlacement), value);
}
public string CheckedColor
{
get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.CheckedColor));
set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.CheckedColor), value);
}
public string UncheckedColor
{
get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.UncheckedColor));
set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.UncheckedColor), value);
}
public string Icon
{
get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.Icon));
set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.Icon), value);
}
public string IconColor
{
get => AssistantComponentPropHelper.ReadString(this.Props, nameof(this.IconColor));
set => AssistantComponentPropHelper.WriteString(this.Props, nameof(this.IconColor), value);
}
public MudBlazor.Color GetColor(string colorString) => Enum.TryParse<Color>(colorString, out var color) ? color : MudBlazor.Color.Inherit;
public Placement GetLabelPlacement() => Enum.TryParse<Placement>(this.LabelPlacement, out var placement) ? placement : Placement.Right;
public string GetIconSvg() => MudBlazorIconRegistry.TryGetSvg(this.Icon, out var svg) ? svg : string.Empty;
} }

View File

@ -34,8 +34,8 @@ public static class ComponentPropSpecs
optional: ["ValidationMessage", "Class", "Style"] optional: ["ValidationMessage", "Class", "Style"]
), ),
[AssistantComponentType.SWITCH] = new( [AssistantComponentType.SWITCH] = new(
required: ["Name", "Label", "LabelOn", "LabelOff", "Value"], required: ["Name", "Label", "Value"],
optional: ["UserPrompt"] optional: [ "LabelOn", "LabelOff", "LabelPlacement", "Icon", "IconColor", "UserPrompt", "CheckedColor", "UncheckedColor", "Disabled"]
), ),
[AssistantComponentType.HEADING] = new( [AssistantComponentType.HEADING] = new(
required: ["Text", "Level"], required: ["Text", "Level"],