Repository navigation
Expand file tree
/
Copy pathAppStackConfig.cs
More file actions
178 lines (166 loc) · 6.92 KB
/
Copy pathAppStackConfig.cs
File metadata and controls
178 lines (166 loc) · 6.92 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System.Text.Json.Serialization;
sealed class AppStackConfig
{
public bool Enabled { get; set; }
public bool AutoCompact { get; set; }
public int CompactAtPercent { get; set; } = 75;
public List<AppStackGroup> Groups { get; set; } = [];
[JsonIgnore]
public int IdentityMask
{
get
{
if (!Enabled) return 0;
int mask = 0;
for (int i = 0; i < Math.Min(Groups?.Count ?? 0, 4); i++)
if (Groups![i].Enabled && Groups[i].Members is { Count: >= 2 }) mask |= 1 << i;
return mask;
}
}
[JsonIgnore]
public bool HasGroups => PackRanges() != 0 || IdentityMask != 0;
// Four 10-bit slots: 5-bit zero-based start, then 5-bit count.
// Invalid or overlapping ranges fail closed and are omitted.
[JsonIgnore]
public ulong PackedRanges => PackRanges();
public ulong PackRanges()
{
ulong packed = 0;
var occupied = new bool[32];
for (int slot = 0; slot < Math.Min(Groups?.Count ?? 0, 4); slot++)
{
var group = Groups![slot];
if (!group.Enabled || group.Members is { Count: > 0 }) continue;
int start = group.Start - 1;
int count = group.Count;
if (start < 0 || start >= 32 || count < 2 || count > 31 || start + count > 32 ||
Enumerable.Range(start, count).Any(index => occupied[index])) continue;
for (int index = start; index < start + count; index++) occupied[index] = true;
packed |= ((ulong)(uint)start | ((ulong)(uint)count << 5)) << (slot * 10);
}
return packed;
}
public IReadOnlyList<AppStackGroup> ValidPackedGroups()
{
if (!Enabled) return [];
var accepted = new List<AppStackGroup>(4);
var occupied = new bool[32];
foreach (var group in Groups ?? [])
{
if (accepted.Count >= 4) break;
if (!group.Enabled || group.Members is { Count: > 0 }) continue;
int start = group.Start - 1;
int count = group.Count;
if (start < 0 || start >= 32 || count < 2 || count > 31 || start + count > 32 ||
Enumerable.Range(start, count).Any(index => occupied[index])) continue;
for (int i = start; i < start + count; i++) occupied[i] = true;
accepted.Add(group);
}
return accepted;
}
public AppStackConfig Copy() => new()
{
Enabled = Enabled,
AutoCompact = AutoCompact,
CompactAtPercent = CompactAtPercent,
Groups = (Groups ?? []).Select(group => group.Copy()).ToList()
};
public bool IsValid(out string reason)
{
if (Groups == null || Groups.Count > 4)
{
reason = Localization.T("You can create up to four stacks.", "Du kannst höchstens vier Stapel anlegen.");
return false;
}
var occupied = new bool[32];
var appIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var shortcutKeys = new HashSet<int>();
foreach (var group in Groups)
{
if (!group.Enabled) continue;
if (group.ShortcutFunctionKey is < 0 or > 12 ||
(group.ShortcutFunctionKey != 0 && !shortcutKeys.Add(group.ShortcutFunctionKey)))
{
reason = Localization.T("Choose a different Ctrl+Alt+F key for each folder.",
"Wähle für jeden Ordner eine andere Strg+Alt+F-Taste.");
return false;
}
if (group.Members is { Count: > 0 })
{
if (group.Members.Count is < 2 or > 12 || group.Members.Any(member =>
string.IsNullOrWhiteSpace(member.AppId) || member.AppId.Length > 256))
{
reason = Localization.T($"{group.Name}: choose 2–12 taskbar apps.",
$"{group.Name}: Wähle 2–12 Taskbar-Apps.");
return false;
}
foreach (var member in group.Members)
{
if (appIds.Add(member.AppId)) continue;
reason = Localization.T($"{group.Name}: an app is already in another stack.",
$"{group.Name}: Eine App ist bereits in einem anderen Stapel.");
return false;
}
continue;
}
int start = group.Start - 1;
int count = group.Count;
if (start < 0 || start >= 32 || count < 2 || count > 31 || start + count > 32)
{
reason = Localization.T($"{group.Name}: position or count exceeds the first 32 apps.",
$"{group.Name}: Position oder Anzahl liegt außerhalb der ersten 32 Apps.");
return false;
}
if (Enumerable.Range(start, count).Any(index => occupied[index]))
{
reason = Localization.T($"{group.Name}: range overlaps another stack.",
$"{group.Name}: Der Bereich überschneidet sich mit einem anderen Stapel.");
return false;
}
for (int i = start; i < start + count; i++) occupied[i] = true;
}
reason = "";
return true;
}
}
sealed class AppStackGroup
{
public Guid Id { get; set; } = Guid.NewGuid();
public bool Enabled { get; set; } = true;
public string Name { get; set; } = Localization.T("Stack", "Stapel");
// Null follows the selected taskbar theme. Custom colors are opaque RGB.
public int? FolderColor { get; set; }
// 0: saved favorites; 1: selected apps only while running; 2: running first, then favorites.
public int ContentMode { get; set; }
// Hover is the default; click mode avoids opening a folder while merely passing over it.
public bool OpenOnClick { get; set; }
// 0 disables the optional Ctrl+Alt+F-key shortcut for this folder.
public int ShortcutFunctionKey { get; set; }
public string? LastUsedAppId { get; set; }
public int Start { get; set; } = 1; // 1-based position in Windows pin order
public int Count { get; set; } = 2;
public List<string> LaunchPaths { get; set; } = [];
public List<StackAppMember> Members { get; set; } = [];
public AppStackGroup Copy() => new()
{
Id = Id,
Enabled = Enabled,
Name = Name,
FolderColor = FolderColor,
ContentMode = ContentMode,
OpenOnClick = OpenOnClick,
ShortcutFunctionKey = ShortcutFunctionKey,
LastUsedAppId = LastUsedAppId,
Start = Start,
Count = Count,
LaunchPaths = [.. (LaunchPaths ?? [])],
Members = (Members ?? []).Select(member => member.Copy()).ToList()
};
}
sealed class StackAppMember
{
public string AppId { get; set; } = "";
public string Name { get; set; } = "";
public string? LaunchPath { get; set; }
public StackAppMember Copy() => new() { AppId = AppId, Name = Name, LaunchPath = LaunchPath };
}