-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowFilterConfig.cs
More file actions
83 lines (70 loc) · 2.27 KB
/
Copy pathWindowFilterConfig.cs
File metadata and controls
83 lines (70 loc) · 2.27 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
using System.Text.Json;
using System.Text.RegularExpressions;
namespace LittleSwitcher;
public class WindowFilterConfig
{
public List<string> IncludeTitleRegex { get; set; } = [];
public List<string> IncludeClassRegex { get; set; } = [];
public List<string> IncludeProcessRegex { get; set; } = [];
public List<string> ExcludeTitleRegex { get; set; } =
[
"^Program Manager$"
];
public List<string> ExcludeClassRegex { get; set; } =
[
"^Shell_TrayWnd$",
"^Shell_SecondaryTrayWnd$",
"^Progman$",
"^WorkerW$",
"^NotifyIconOverflowWindow$",
"^Windows.UI.Core.CoreWindow$"
];
public List<string> ExcludeProcessRegex { get; set; } =
[
"^LittleSwitcher$"
];
private static string ConfigPath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"LittleSwitcher", "window_filters.json");
public static string GetConfigPath() => ConfigPath;
public static WindowFilterConfig Load()
{
try
{
if (File.Exists(ConfigPath))
{
var json = File.ReadAllText(ConfigPath);
return JsonSerializer.Deserialize<WindowFilterConfig>(json) ?? new WindowFilterConfig();
}
}
catch { }
var config = new WindowFilterConfig();
config.Save();
return config;
}
public void Save()
{
var dir = Path.GetDirectoryName(ConfigPath)!;
Directory.CreateDirectory(dir);
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(ConfigPath, json);
}
public static bool MatchesAny(IEnumerable<string> patterns, string value)
{
if (string.IsNullOrEmpty(value))
return false;
foreach (var pattern in patterns)
{
try
{
if (Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
return true;
}
catch (ArgumentException ex)
{
System.Diagnostics.Debug.WriteLine($"Invalid window filter regex [{pattern}]: {ex.Message}");
}
}
return false;
}
}