Repository navigation
Expand file tree
/
Copy pathModels.cs
More file actions
79 lines (67 loc) · 2.31 KB
/
Copy pathModels.cs
File metadata and controls
79 lines (67 loc) · 2.31 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
78
79
using System.Text.Json;
using System.Text.Json.Serialization;
namespace KeyboardLayoutSwitcher;
internal sealed class AppConfig
{
public bool IsMonitoringEnabled { get; set; } = true;
public bool StartMinimized { get; set; }
public Dictionary<string, DeviceMapping> Mappings { get; set; } = new(StringComparer.OrdinalIgnoreCase);
}
internal sealed class DeviceMapping
{
public string LayoutId { get; set; } = "";
}
internal sealed class KeyboardDevice
{
public IntPtr Handle { get; init; }
public string DevicePath { get; init; } = "";
public string DisplayName { get; init; } = "";
public string Detail { get; init; } = "";
public override string ToString() => DisplayName;
}
internal sealed class KeyboardLayoutInfo
{
// Keyboard layout identifier (KLID) used by LoadKeyboardLayout, e.g. 00000409.
public string Id { get; init; } = "";
// The HKL that Windows currently assigned while loading this KLID.
public string ActiveHklId { get; init; } = "";
public string DisplayName { get; init; } = "";
public IntPtr Handle { get; init; }
public override string ToString() => DisplayName;
}
internal static class ConfigStore
{
private static readonly JsonSerializerOptions Options = new()
{
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
};
public static string ConfigPath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"KeyboardLayoutSwitcher",
"config.json");
public static AppConfig Load()
{
try
{
if (File.Exists(ConfigPath))
{
var json = File.ReadAllText(ConfigPath);
return JsonSerializer.Deserialize<AppConfig>(json, Options) ?? new AppConfig();
}
}
catch
{
// A damaged or partially written configuration should not prevent startup.
}
return new AppConfig();
}
public static void Save(AppConfig config)
{
var directory = Path.GetDirectoryName(ConfigPath)!;
Directory.CreateDirectory(directory);
var temporaryPath = ConfigPath + ".tmp";
File.WriteAllText(temporaryPath, JsonSerializer.Serialize(config, Options));
File.Move(temporaryPath, ConfigPath, true);
}
}