generalized getters and setters for ints and bools

This commit is contained in:
krut_ni 2026-03-09 18:25:30 +01:00
parent 96d0b9e959
commit 867890a46f
No known key found for this signature in database
GPG Key ID: A5C0151B4DDB172C
3 changed files with 41 additions and 11 deletions

View File

@ -25,20 +25,20 @@ internal sealed class AssistantColorPicker : AssistantComponentBase
public bool ShowAlpha public bool ShowAlpha
{ {
get => !this.Props.TryGetValue(nameof(this.ShowAlpha), out var val) || val is true; get => AssistantComponentPropHelper.ReadBool(this.Props, nameof(this.ShowAlpha), true);
set => this.Props[nameof(this.ShowAlpha)] = value; set => AssistantComponentPropHelper.WriteBool(this.Props, nameof(this.ShowAlpha), value);
} }
public bool ShowToolbar public bool ShowToolbar
{ {
get => !this.Props.TryGetValue(nameof(this.ShowToolbar), out var val) || val is true; get => AssistantComponentPropHelper.ReadBool(this.Props, nameof(this.ShowToolbar), true);
set => this.Props[nameof(this.ShowToolbar)] = value; set => AssistantComponentPropHelper.WriteBool(this.Props, nameof(this.ShowToolbar), value);
} }
public bool ShowModeSwitch public bool ShowModeSwitch
{ {
get => !this.Props.TryGetValue(nameof(this.ShowModeSwitch), out var val) || val is true; get => AssistantComponentPropHelper.ReadBool(this.Props, nameof(this.ShowModeSwitch), true);
set => this.Props[nameof(this.ShowModeSwitch)] = value; set => AssistantComponentPropHelper.WriteBool(this.Props, nameof(this.ShowModeSwitch), value);
} }
public string PickerVariant public string PickerVariant

View File

@ -16,4 +16,37 @@ internal static class AssistantComponentPropHelper
{ {
props[key] = value ?? string.Empty; props[key] = value ?? string.Empty;
} }
public static int ReadInt(Dictionary<string, object> props, string key, int fallback = 0)
{
return props.TryGetValue(key, out var value) && int.TryParse(value?.ToString(), out var i) ? i : fallback;
}
public static void WriteInt(Dictionary<string, object> props, string key, int value)
{
props[key] = value;
}
public static int? ReadNullableInt(Dictionary<string, object> props, string key)
{
return props.TryGetValue(key, out var value) && int.TryParse(value?.ToString(), out var i) ? i : null;
}
public static void WriteNullableInt(Dictionary<string, object> props, string key, int? value)
{
if (value.HasValue)
props[key] = value.Value;
else
props.Remove(key);
}
public static bool ReadBool(Dictionary<string, object> props, string key, bool fallback = false)
{
return props.TryGetValue(key, out var value) && bool.TryParse(value.ToString(), out var b) ? b : fallback;
}
public static void WriteBool(Dictionary<string, object> props, string key, bool value)
{
props[key] = value;
}
} }

View File

@ -14,11 +14,8 @@ internal sealed class AssistantHeading : AssistantComponentBase
public int Level public int Level
{ {
get => this.Props.TryGetValue(nameof(this.Level), out var v) get => AssistantComponentPropHelper.ReadInt(this.Props, nameof(this.Level), 2);
&& int.TryParse(v.ToString(), out var i) set => AssistantComponentPropHelper.WriteInt(this.Props, nameof(this.Level), value);
? i
: 2;
set => this.Props[nameof(this.Level)] = value;
} }
public string Class public string Class