|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Nodes; |
| 3 | +using SwitchifyPc.Core.Storage; |
| 4 | + |
| 5 | +namespace SwitchifyPc.Core.Settings; |
| 6 | + |
| 7 | +public sealed record CursorOverlayColorInfo(string Label, int[] Rgb, string Hex); |
| 8 | + |
| 9 | +public sealed record CursorOverlaySettings( |
| 10 | + bool Enabled, |
| 11 | + string Size, |
| 12 | + string Visibility, |
| 13 | + bool Crosshairs, |
| 14 | + string Color); |
| 15 | + |
| 16 | +public static class CursorOverlaySettingsModel |
| 17 | +{ |
| 18 | + public static readonly CursorOverlaySettings Default = new( |
| 19 | + Enabled: true, |
| 20 | + Size: "medium", |
| 21 | + Visibility: "onInput", |
| 22 | + Crosshairs: false, |
| 23 | + Color: "red"); |
| 24 | + |
| 25 | + public static readonly IReadOnlyDictionary<string, int> SizePixels = new Dictionary<string, int>(StringComparer.Ordinal) |
| 26 | + { |
| 27 | + ["small"] = 96, |
| 28 | + ["medium"] = 128, |
| 29 | + ["large"] = 176 |
| 30 | + }; |
| 31 | + |
| 32 | + public static readonly IReadOnlyDictionary<string, CursorOverlayColorInfo> Colors = |
| 33 | + new Dictionary<string, CursorOverlayColorInfo>(StringComparer.Ordinal) |
| 34 | + { |
| 35 | + ["red"] = new("Red", [211, 47, 47], "#d32f2f"), |
| 36 | + ["green"] = new("Green", [132, 255, 145], "#84ff91"), |
| 37 | + ["blue"] = new("Blue", [100, 166, 255], "#64a6ff"), |
| 38 | + ["yellow"] = new("Yellow", [255, 209, 102], "#ffd166"), |
| 39 | + ["white"] = new("White", [255, 255, 255], "#ffffff") |
| 40 | + }; |
| 41 | + |
| 42 | + private static readonly HashSet<string> VisibilityValues = new(StringComparer.Ordinal) |
| 43 | + { |
| 44 | + "onInput", |
| 45 | + "whileControlling" |
| 46 | + }; |
| 47 | + |
| 48 | + public static CursorOverlaySettings Normalize(JsonNode? value) |
| 49 | + { |
| 50 | + if (value is not JsonObject candidate) |
| 51 | + { |
| 52 | + return Default; |
| 53 | + } |
| 54 | + |
| 55 | + return new CursorOverlaySettings( |
| 56 | + Enabled: BooleanOrDefault(candidate, "enabled", Default.Enabled), |
| 57 | + Size: StringSetOrDefault(candidate, "size", SizePixels.Keys, Default.Size), |
| 58 | + Visibility: StringSetOrDefault(candidate, "visibility", VisibilityValues, Default.Visibility), |
| 59 | + Crosshairs: BooleanOrDefault(candidate, "crosshairs", Default.Crosshairs), |
| 60 | + Color: StringSetOrDefault(candidate, "color", Colors.Keys, Default.Color)); |
| 61 | + } |
| 62 | + |
| 63 | + public static CursorOverlaySettings Normalize(object? value) |
| 64 | + { |
| 65 | + return Normalize(JsonSerializer.SerializeToNode(value)); |
| 66 | + } |
| 67 | + |
| 68 | + public static CursorOverlaySettings Normalize(CursorOverlaySettings settings) |
| 69 | + { |
| 70 | + return new CursorOverlaySettings( |
| 71 | + Enabled: settings.Enabled, |
| 72 | + Size: SizePixels.ContainsKey(settings.Size) ? settings.Size : Default.Size, |
| 73 | + Visibility: VisibilityValues.Contains(settings.Visibility) ? settings.Visibility : Default.Visibility, |
| 74 | + Crosshairs: settings.Crosshairs, |
| 75 | + Color: Colors.ContainsKey(settings.Color) ? settings.Color : Default.Color); |
| 76 | + } |
| 77 | + |
| 78 | + public static int ResolveSizePixels(string size) |
| 79 | + { |
| 80 | + return SizePixels.TryGetValue(size, out int pixels) ? pixels : SizePixels[Default.Size]; |
| 81 | + } |
| 82 | + |
| 83 | + public static int[] ResolveColorRgb(string color) |
| 84 | + { |
| 85 | + return Colors.TryGetValue(color, out CursorOverlayColorInfo? value) |
| 86 | + ? value.Rgb.ToArray() |
| 87 | + : Colors[Default.Color].Rgb.ToArray(); |
| 88 | + } |
| 89 | + |
| 90 | + public static JsonObject ToJsonObject(CursorOverlaySettings settings) |
| 91 | + { |
| 92 | + CursorOverlaySettings normalized = Normalize(settings); |
| 93 | + return new JsonObject |
| 94 | + { |
| 95 | + ["enabled"] = normalized.Enabled, |
| 96 | + ["size"] = normalized.Size, |
| 97 | + ["visibility"] = normalized.Visibility, |
| 98 | + ["crosshairs"] = normalized.Crosshairs, |
| 99 | + ["color"] = normalized.Color |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + private static bool BooleanOrDefault(JsonObject value, string propertyName, bool fallback) |
| 104 | + { |
| 105 | + return value.TryGetPropertyValue(propertyName, out JsonNode? property) && |
| 106 | + property is not null && |
| 107 | + property.GetValueKind() is JsonValueKind.True or JsonValueKind.False |
| 108 | + ? property.GetValue<bool>() |
| 109 | + : fallback; |
| 110 | + } |
| 111 | + |
| 112 | + private static string StringSetOrDefault(JsonObject value, string propertyName, IEnumerable<string> allowed, string fallback) |
| 113 | + { |
| 114 | + return value.TryGetPropertyValue(propertyName, out JsonNode? property) && |
| 115 | + property?.GetValueKind() == JsonValueKind.String && |
| 116 | + allowed.Contains(property.GetValue<string>(), StringComparer.Ordinal) |
| 117 | + ? property.GetValue<string>() |
| 118 | + : fallback; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +public sealed class JsonCursorOverlaySettingsStore |
| 123 | +{ |
| 124 | + private readonly string filePath; |
| 125 | + private readonly Action<string> warn; |
| 126 | + |
| 127 | + public JsonCursorOverlaySettingsStore(string filePath, Action<string>? warn = null) |
| 128 | + { |
| 129 | + this.filePath = filePath; |
| 130 | + this.warn = warn ?? Console.WriteLine; |
| 131 | + } |
| 132 | + |
| 133 | + public CursorOverlaySettings Load() |
| 134 | + { |
| 135 | + try |
| 136 | + { |
| 137 | + return CursorOverlaySettingsModel.Normalize(JsonNode.Parse(File.ReadAllText(filePath))); |
| 138 | + } |
| 139 | + catch (Exception error) when (error is FileNotFoundException or DirectoryNotFoundException) |
| 140 | + { |
| 141 | + return CursorOverlaySettingsModel.Default; |
| 142 | + } |
| 143 | + catch |
| 144 | + { |
| 145 | + warn("Switchify cursor overlay settings could not be loaded. Defaults will be used."); |
| 146 | + return CursorOverlaySettingsModel.Default; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public CursorOverlaySettings Save(CursorOverlaySettings settings) |
| 151 | + { |
| 152 | + CursorOverlaySettings normalized = CursorOverlaySettingsModel.Normalize(settings); |
| 153 | + string content = JsonSerializer.Serialize(CursorOverlaySettingsModel.ToJsonObject(normalized), JsonOptions) + "\n"; |
| 154 | + JsonFileStore.WriteJsonFileAtomicSync(filePath, content); |
| 155 | + return normalized; |
| 156 | + } |
| 157 | + |
| 158 | + private static readonly JsonSerializerOptions JsonOptions = new() |
| 159 | + { |
| 160 | + WriteIndented = true |
| 161 | + }; |
| 162 | +} |
0 commit comments