Repository navigation
Expand file tree
/
Copy pathWidgetService.cs
More file actions
300 lines (242 loc) · 6.71 KB
/
Copy pathWidgetService.cs
File metadata and controls
300 lines (242 loc) · 6.71 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System.IO;
namespace MINWallPaper.Widgets;
/// <summary>管理桌面组件的实例:增删、启停、配置更新和落盘。</summary>
internal sealed class WidgetService
{
public static WidgetService Instance { get; } = new();
private readonly List<WidgetConfig> _configs = new();
private readonly Dictionary<string, ConfigWidget> _windows = new(StringComparer.OrdinalIgnoreCase);
private RainWidget? _rain;
private WidgetService()
{
}
public IReadOnlyList<WidgetConfig> Configs => _configs;
public bool RainEnabled { get; private set; }
/// <summary>拖动改了位置后通知界面刷新(位置描述会变)。</summary>
public event Action? Changed;
public void Load()
{
WidgetStoreData data = WidgetStore.Load();
_configs.Clear();
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (WidgetConfig config in data.Items)
{
// 启动器已经下线,旧配置直接跳过。
if (string.Equals(config.Type, "launcher", StringComparison.OrdinalIgnoreCase))
{
continue;
}
config.Sanitize();
if (!seen.Add(config.Id))
{
config.Id = WidgetConfig.NewId();
seen.Add(config.Id);
}
_configs.Add(config);
}
RainEnabled = data.Rain;
foreach (WidgetConfig config in _configs.Where(c => c.Enabled))
{
EnsureWindow(config);
}
if (RainEnabled)
{
CreateRain();
}
Save();
}
public WidgetConfig Add(string type)
{
type = type is "wave" or "text" or "cpu" or "memory" or "gpu" or "disk" or "image" or "date"
? type
: "clock";
int sameType = _configs.Count(c => c.Type == type);
WidgetConfig config = WidgetConfig.Create(type, sameType);
// 新建的稍微错开一点,避免和已有组件完全重叠。
config.OffsetX += (sameType % 3 - 1) * 28;
config.OffsetY += (sameType % 3) * 46;
_configs.Add(config);
EnsureWindow(config);
Save();
return config;
}
public void Update(WidgetConfig incoming)
{
incoming.Sanitize();
int index = _configs.FindIndex(c => c.Id == incoming.Id);
if (index < 0)
{
return;
}
// 类型不允许在编辑里改,保持原类型。
incoming.Type = _configs[index].Type;
_configs[index] = incoming;
if (incoming.Enabled)
{
EnsureWindow(incoming);
}
else
{
DisposeWindow(incoming.Id);
}
Save();
}
public void Remove(string id)
{
int index = _configs.FindIndex(c => c.Id == id);
if (index < 0)
{
return;
}
_configs.RemoveAt(index);
DisposeWindow(id);
Save();
}
public void SetEnabled(string id, bool enabled)
{
WidgetConfig? config = _configs.FirstOrDefault(c => c.Id == id);
if (config == null || config.Enabled == enabled)
{
return;
}
config.Enabled = enabled;
if (enabled)
{
EnsureWindow(config);
}
else
{
DisposeWindow(id);
}
Save();
}
/// <summary>编辑时只让当前组件接收鼠标,其余保持穿透。</summary>
public void SetDragMode(string? id)
{
foreach ((string key, ConfigWidget window) in _windows)
{
window.SetDragMode(string.Equals(key, id, StringComparison.OrdinalIgnoreCase));
}
}
/// <summary>拖动结束后把窗口位置记成自由坐标。</summary>
public void MoveWidget(string id, int x, int y)
{
WidgetConfig? config = _configs.FirstOrDefault(c => c.Id == id);
if (config == null)
{
return;
}
config.Anchor = "free";
config.PosX = x;
config.PosY = y;
Save();
Changed?.Invoke();
}
/// <summary>拖拽缩放结束后保存位置和大小。</summary>
public void SaveGeometry(string id, int x, int y, int width, int height)
{
WidgetConfig? config = _configs.FirstOrDefault(c => c.Id == id);
if (config == null)
{
return;
}
config.Anchor = "free";
config.PosX = x;
config.PosY = y;
config.Width = width;
config.Height = height;
Save();
Changed?.Invoke();
}
public void SetImages(string id, IEnumerable<string> paths)
{
Mutate(id, config =>
{
config.Images = paths
.Where(File.Exists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Take(9)
.ToList();
});
}
private void Mutate(string id, Action<WidgetConfig> change)
{
WidgetConfig? config = _configs.FirstOrDefault(c => c.Id == id);
if (config == null)
{
return;
}
change(config);
if (config.Enabled)
{
EnsureWindow(config);
}
Save();
Changed?.Invoke();
}
public void SetRainEnabled(bool enabled)
{
RainEnabled = enabled;
if (enabled)
{
CreateRain();
}
else if (_rain != null)
{
_rain.Stop();
_rain.Dispose();
_rain = null;
}
Save();
}
public void Shutdown()
{
foreach (ConfigWidget window in _windows.Values)
{
window.Stop();
window.Dispose();
}
_windows.Clear();
_rain?.Stop();
_rain?.Dispose();
_rain = null;
}
private void CreateRain()
{
if (_rain != null)
{
return;
}
_rain = new RainWidget();
_rain.Start();
}
private ConfigWidget EnsureWindow(WidgetConfig config)
{
if (_windows.TryGetValue(config.Id, out ConfigWidget? existing))
{
existing.Apply(config);
return existing;
}
var window = new ConfigWidget(config);
_windows[config.Id] = window;
window.Start();
return window;
}
private void DisposeWindow(string id)
{
if (!_windows.Remove(id, out ConfigWidget? window))
{
return;
}
window.Stop();
window.Dispose();
}
private void Save()
{
WidgetStore.Save(new WidgetStoreData
{
Rain = RainEnabled,
Items = _configs
});
}
}