Skip to content

Commit ef6e7b8

Browse files
committed
Separate Rules.cs to individual classes
1 parent c537789 commit ef6e7b8

11 files changed

Lines changed: 537 additions & 519 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace PopGuard;
2+
3+
/// <summary>The whole config (rules + duration options).</summary>
4+
internal sealed class AppConfig
5+
{
6+
public IReadOnlyList<TargetRule> Rules { get; }
7+
public IReadOnlyList<DurationOption> Durations { get; }
8+
public bool AutoSuppressDuringFocus { get; }
9+
public bool AutoSuppressDuringMicrophone { get; }
10+
public bool ExcludeSystemWindows { get; }
11+
12+
public AppConfig(
13+
IReadOnlyList<TargetRule> rules,
14+
IReadOnlyList<DurationOption> durations,
15+
bool autoSuppressDuringFocus,
16+
bool autoSuppressDuringMicrophone,
17+
bool excludeSystemWindows)
18+
{
19+
Rules = rules;
20+
Durations = durations;
21+
AutoSuppressDuringFocus = autoSuppressDuringFocus;
22+
AutoSuppressDuringMicrophone = autoSuppressDuringMicrophone;
23+
ExcludeSystemWindows = excludeSystemWindows;
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace PopGuard;
2+
3+
/// <summary>One suppression-duration option (JSON). minutes &lt;= 0 means unlimited.</summary>
4+
internal sealed class DurationEntry
5+
{
6+
// Menu label. May be a plain string (common to all languages) or a language-keyed object
7+
// like { "ja": "...", "en": "..." }. Omit to auto-generate a localized label from the minutes.
8+
public LocalizedText? Label { get; set; }
9+
10+
public int Minutes { get; set; }
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace PopGuard;
2+
3+
/// <summary>A compiled suppression-duration option. A null <see cref="Duration"/> means unlimited.</summary>
4+
internal sealed class DurationOption
5+
{
6+
public string Label { get; }
7+
public TimeSpan? Duration { get; }
8+
9+
public DurationOption(string label, TimeSpan? duration)
10+
{
11+
Label = label;
12+
Duration = duration;
13+
}
14+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PopGuard;
5+
6+
/// <summary>
7+
/// A piece of UI text given either as a plain string (common to every language) or as a
8+
/// language-keyed object, e.g. <c>{ "ja": "2時間", "en": "2 hours" }</c>.
9+
/// </summary>
10+
[JsonConverter(typeof(LocalizedTextConverter))]
11+
internal sealed class LocalizedText
12+
{
13+
// Per-language text, keyed by two-letter language code (case-insensitive). Null when a common
14+
// string was given instead.
15+
private readonly Dictionary<string, string>? _byLang;
16+
private readonly string? _common;
17+
18+
public LocalizedText(string common) => _common = common;
19+
20+
public LocalizedText(Dictionary<string, string> byLang) => _byLang = byLang;
21+
22+
/// <summary>Build a Japanese/English pair (used for the sample config).</summary>
23+
public static LocalizedText Of(string ja, string en) =>
24+
new(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { ["ja"] = ja, ["en"] = en });
25+
26+
/// <summary>
27+
/// Resolve the text for the current display language. Returns null when nothing applies,
28+
/// so the caller can fall back (e.g. auto-generate from the minutes).
29+
/// A language-keyed object only yields text for the language actually present: if just one
30+
/// language was given, the others get null (and auto-generate) rather than borrowing it.
31+
/// A plain string is common to every language.
32+
/// </summary>
33+
public string? Resolve(bool japanese)
34+
{
35+
if (_byLang is { Count: > 0 })
36+
{
37+
return _byLang.TryGetValue(japanese ? "ja" : "en", out string? v) && !string.IsNullOrWhiteSpace(v)
38+
? v.Trim()
39+
: null;
40+
}
41+
return string.IsNullOrWhiteSpace(_common) ? null : _common!.Trim();
42+
}
43+
44+
/// <summary>Serialize back (only needed because the sample config is written out).</summary>
45+
internal void WriteTo(Utf8JsonWriter writer)
46+
{
47+
if (_byLang is { Count: > 0 })
48+
{
49+
writer.WriteStartObject();
50+
foreach (KeyValuePair<string, string> kv in _byLang)
51+
{
52+
writer.WriteString(kv.Key, kv.Value);
53+
}
54+
writer.WriteEndObject();
55+
}
56+
else
57+
{
58+
writer.WriteStringValue(_common ?? string.Empty);
59+
}
60+
}
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PopGuard;
5+
6+
/// <summary>Reads a <see cref="LocalizedText"/> from either a JSON string or a language-keyed object.</summary>
7+
internal sealed class LocalizedTextConverter : JsonConverter<LocalizedText>
8+
{
9+
public override LocalizedText? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
switch (reader.TokenType)
12+
{
13+
case JsonTokenType.Null:
14+
return null;
15+
16+
case JsonTokenType.String:
17+
return new LocalizedText(reader.GetString() ?? string.Empty);
18+
19+
case JsonTokenType.StartObject:
20+
var map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
21+
while (reader.Read())
22+
{
23+
if (reader.TokenType == JsonTokenType.EndObject)
24+
{
25+
return new LocalizedText(map);
26+
}
27+
if (reader.TokenType != JsonTokenType.PropertyName)
28+
{
29+
throw new JsonException("Unexpected token in localized label object.");
30+
}
31+
string key = reader.GetString() ?? string.Empty;
32+
reader.Read();
33+
if (reader.TokenType == JsonTokenType.String)
34+
{
35+
map[key] = reader.GetString() ?? string.Empty;
36+
}
37+
else
38+
{
39+
reader.Skip(); // ignore non-string values defensively
40+
}
41+
}
42+
throw new JsonException("Unterminated localized label object.");
43+
44+
default:
45+
throw new JsonException($"Unexpected token for label: {reader.TokenType}.");
46+
}
47+
}
48+
49+
public override void Write(Utf8JsonWriter writer, LocalizedText value, JsonSerializerOptions options)
50+
=> value.WriteTo(writer);
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace PopGuard;
2+
3+
/// <summary>Shape of one rule in JSON (raw strings).</summary>
4+
internal sealed class RuleEntry
5+
{
6+
public bool Enabled { get; set; } = true;
7+
public string? Process { get; set; }
8+
public string? Title { get; set; }
9+
public string? Class { get; set; }
10+
public string? Hide { get; set; }
11+
12+
// When true, windows matching this rule are NEVER suppressed ("allow"/exclusion rule).
13+
// Because the first matching rule wins, place an exclude rule above broader rules to carve out
14+
// exceptions (e.g. exclude a specific title, then suppress everything else with process "*").
15+
public bool Exclude { get; set; }
16+
17+
// Whether to target only topmost (TOPMOST) windows.
18+
// [Currently not implemented] accepted as config, but internally always TOPMOST-only.
19+
// Kept to leave room for implementing false (also target non-TOPMOST) later.
20+
public bool TopMostOnly { get; set; } = true;
21+
}

‎PopGuard/Configuration/RuleFile.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace PopGuard;
2+
3+
/// <summary>Shape of one config JSON file.</summary>
4+
internal sealed class RuleFile
5+
{
6+
public List<RuleEntry>? Rules { get; set; }
7+
8+
// Suppression-duration options shown in the tray menu. Defaults are used when omitted.
9+
public List<DurationEntry>? Durations { get; set; }
10+
11+
// Whether to auto-suppress during a Windows 11 focus session (default true).
12+
public bool? AutoSuppressDuringFocus { get; set; }
13+
14+
// Whether to auto-suppress while the microphone is in use, e.g. during a call/meeting (default true).
15+
public bool? AutoSuppressDuringMicrophone { get; set; }
16+
17+
// Whether to always exclude Windows shell windows (taskbar, desktop, …) from suppression (default true).
18+
public bool? ExcludeSystemWindows { get; set; }
19+
20+
// Display language: "auto" (default, follow the OS UI language) / "ja" / "en".
21+
public string? Language { get; set; }
22+
}

0 commit comments

Comments
 (0)