-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathIntSetting.cs
More file actions
77 lines (65 loc) · 2.11 KB
/
IntSetting.cs
File metadata and controls
77 lines (65 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace Microsoft.CommandPalette.Extensions.Toolkit;
public sealed class IntSetting : Setting<int>
{
public int? Min { get; set; }
public int? Max { get; set; }
public string Placeholder { get; set; } = string.Empty;
private IntSetting()
: base()
{
Value = 0;
}
public IntSetting(string key, int defaultValue, int? min = null, int? max = null)
: base(key, defaultValue)
{
Min = min;
Max = max;
}
public IntSetting(string key, string label, string description, int defaultValue,
int? min = null, int? max = null)
: base(key, label, description, defaultValue)
{
Min = min;
Max = max;
}
public override Dictionary<string, object> ToDictionary()
{
var dict = new Dictionary<string, object>
{
{ "id", Key },
{ "type", "Input.Number" },
{ "title", Label },
{ "label", Description },
{ "value", Value },
{ "isRequired", IsRequired },
{ "errorMessage", ErrorMessage },
{ "placeholder", Placeholder },
};
if (Min.HasValue) dict["min"] = Min.Value;
if (Max.HasValue) dict["max"] = Max.Value;
return dict;
}
public static IntSetting LoadFromJson(JsonObject jsonObject) => new() { Value = jsonObject["value"]?.GetValue<int>() ?? 0 };
public override void Update(JsonObject payload)
{
if (payload.TryGetPropertyValue(Key, out JsonNode? node) && node is not null)
{
if (node is JsonValue jsonValue && jsonValue.TryGetValue<int>(out var value))
{
Value = value;
}
else if (int.TryParse(node.ToString(), out value))
{
Value = value;
}
}
if (Min.HasValue && Value < Min.Value) Value = Min.Value;
if (Max.HasValue && Value > Max.Value) Value = Max.Value;
}
public override string ToState()
{
return $"\"{Key}\": {Value}";
}
}