Repository navigation
Expand file tree
/
Copy pathAppSettings.cs
More file actions
100 lines (85 loc) · 3.8 KB
/
Copy pathAppSettings.cs
File metadata and controls
100 lines (85 loc) · 3.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.IO;
using HttpTraceAnalyser.Model;
namespace HttpTraceAnalyser
{
/// <summary>
/// Persisted application-level preferences (appearance, layout, MCP bridge pipe name). Whether
/// the MCP bridge is currently enabled is intentionally excluded: it always starts stopped and
/// must be explicitly re-enabled each session.
/// </summary>
public static class AppSettings
{
private const string DocumentType = "app-settings";
private static readonly string SettingsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"HttpTraceAnalyser",
"app-settings.json");
public static ThemePreference ThemePreference { get; set; } = ThemePreference.Auto;
/// <summary>The theme to actually apply: the system theme when <see cref="ThemePreference"/> is Auto.</summary>
public static AppTheme EffectiveTheme => ThemePreference switch
{
ThemePreference.Light => AppTheme.Light,
ThemePreference.Dark => AppTheme.Dark,
_ => ThemeManager.GetSystemTheme(),
};
public static bool UseSplitView { get; set; }
public static bool IsViewerPoppedOut { get; set; }
/// <summary>
/// Optional suffix distinguishing this instance's MCP named-pipe bridge from other running
/// copies of the app (e.g. "1", "2"). Null/empty selects the default, well-known pipe
/// name. Persisted under the legacy "McpPort" field name for settings-file compatibility.
/// </summary>
public static string? McpPipeNameSuffix { get; set; }
static AppSettings() => Load();
private static void Load()
{
if (!File.Exists(SettingsPath))
return;
try
{
var document = JsonConfigurationPersistence.Load<AppSettingsDocument>(SettingsPath, "app settings file");
JsonConfigurationPersistence.ValidateDocument(
document.DocumentType, DocumentType, document.Version, "app settings file");
ThemePreference = JsonConfigurationPersistence.ValidateEnum(
document.ThemePreference, nameof(document.ThemePreference));
UseSplitView = document.UseSplitView;
IsViewerPoppedOut = document.IsViewerPoppedOut;
McpPipeNameSuffix = string.IsNullOrWhiteSpace(document.McpPipeNameSuffix) ? null : document.McpPipeNameSuffix;
}
catch (InvalidDataException)
{
// A corrupt settings file must not prevent application startup.
}
}
public static void Save()
{
var document = new AppSettingsDocument
{
DocumentType = DocumentType,
Version = JsonConfigurationPersistence.CurrentVersion,
ThemePreference = ThemePreference,
UseSplitView = UseSplitView,
IsViewerPoppedOut = IsViewerPoppedOut,
McpPipeNameSuffix = McpPipeNameSuffix,
};
try
{
JsonConfigurationPersistence.Save(SettingsPath, document);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
}
}
private sealed class AppSettingsDocument
{
public AppSettingsDocument() { }
public required string DocumentType { get; set; }
public required int Version { get; set; }
public ThemePreference ThemePreference { get; set; }
public bool UseSplitView { get; set; }
public bool IsViewerPoppedOut { get; set; }
public string? McpPipeNameSuffix { get; set; }
}
}
}