-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
351 lines (305 loc) · 10.3 KB
/
MainWindow.xaml.cs
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using WindowsDesktop;
namespace DesktopIconHiderWPF;
partial class MainWindow
{
private const int _delay = 2000;
private IDisposable? _applicationViewChangedListener;
public ObservableCollection<VirtualDesktopViewModel> Desktops { get; } = new();
private Guid currentId = Guid.Empty;
public MainWindow()
{
ConfigHelper.Load();
this.InitializeComponent();
this.InitializeComObjects();
}
private void InitializeComObjects()
{
VirtualDesktop.Configure();
VirtualDesktop.Created += (_, desktop) =>
{
this.Dispatcher.Invoke(() =>
{
this.Desktops.Add(new VirtualDesktopViewModel(desktop));
Debug.WriteLine($"Created: {desktop.Name}");
});
};
VirtualDesktop.CurrentChanged += (_, args) =>
{
var currentHidden = ConfigHelper.Get(currentId);
this.currentId = args.NewDesktop.Id;
var hidden = ConfigHelper.Get(currentId);
if (currentHidden != hidden)
{
SetIconsToggleText(hidden);
FileHiderHelper.SetHiddenFiles(hidden);
}
foreach (var desktop in this.Desktops)
{
desktop.IsCurrent = desktop.Id == this.currentId;
}
Debug.WriteLine($"Switched to {currentId}");
};
VirtualDesktop.Moved += (_, args) =>
{
this.Dispatcher.Invoke(() =>
{
this.Desktops.Move(args.OldIndex, args.NewIndex);
Debug.WriteLine($"Moved: {args.OldIndex} -> {args.NewIndex}, {args.Desktop}");
});
};
VirtualDesktop.Destroyed += (_, args) =>
{
this.Dispatcher.Invoke(() =>
{
var target = this.Desktops.FirstOrDefault(x => x.Id == args.Destroyed.Id);
if (target != null)
{
this.Desktops.Remove(target);
ConfigHelper.Remove(target.Id);
}
});
};
VirtualDesktop.Renamed += (_, args) =>
{
var desktop = this.Desktops.FirstOrDefault(x => x.Id == args.Desktop.Id);
if (desktop != null) desktop.Name = args.Name;
Debug.WriteLine($"Renamed: {args.Desktop}");
};
VirtualDesktop.WallpaperChanged += (_, args) =>
{
var desktop = this.Desktops.FirstOrDefault(x => x.Id == args.Desktop.Id);
if (desktop != null) desktop.WallpaperPath = new Uri(args.Path);
Debug.WriteLine($"Wallpaper changed: {args.Desktop}, {args.Path}");
};
currentId = VirtualDesktop.Current.Id;
foreach (var desktop in VirtualDesktop.GetDesktops())
{
var vm = new VirtualDesktopViewModel(desktop);
bool isHidden = ConfigHelper.Get(desktop.Id);
vm.ShowcaseMessage = isHidden ? "Icons hidden" : "";
if (desktop.Id == currentId)
{
vm.IsCurrent = true;
IconsToggle.Content = isHidden ? "Show Icons" : "Hide Icons";
FileHiderHelper.SetHiddenFiles(isHidden);
}
this.Desktops.Add(vm);
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
this._applicationViewChangedListener = VirtualDesktop.RegisterViewChanged(this.GetHandle(), handle =>
{
this.Dispatcher.Invoke(() =>
{
var parent = VirtualDesktop.FromHwnd(handle);
foreach (var desktop in this.Desktops)
{
// Do something on initialization
}
});
});
}
protected override void OnClosed(EventArgs e)
{
this._applicationViewChangedListener?.Dispose();
base.OnClosed(e);
}
private void CreateNew(object sender, RoutedEventArgs e)
=> VirtualDesktop.Create();
private void Remove(object sender, RoutedEventArgs e)
{
VirtualDesktop.Current.Remove();
}
private void SwitchDesktop(object sender, RoutedEventArgs e)
{
if (sender is Button { DataContext: VirtualDesktopViewModel vm })
{
VirtualDesktop.FromId(vm.Id)?.SwitchAndMove(this);
}
}
private void ChangeWallpaper(object sender, RoutedEventArgs e)
{
if (sender is Button { DataContext: VirtualDesktopViewModel vm })
{
var dialog = new OpenFileDialog()
{
Title = "Select wallpaper",
Filter = "Desktop wallpaper (*.jpg, *.png, *.bmp)|*.jpg;*.png;*.bmp",
};
if ((dialog.ShowDialog(this) ?? false)
&& File.Exists(dialog.FileName))
{
var desktop = VirtualDesktop.FromId(vm.Id);
if (desktop != null) desktop.WallpaperPath = dialog.FileName;
}
}
e.Handled = true;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
private void IconsToggle_Click(object sender, RoutedEventArgs e)
{
bool newHiddenState = !ConfigHelper.Get(currentId);
var model = Desktops.FirstOrDefault((model) => model.Id == currentId);
if (model == null) return;
model.ShowcaseMessage = newHiddenState ? "Icons hidden" : "";
SetIconsToggleText(newHiddenState);
FileHiderHelper.SetHiddenFiles(newHiddenState);
ConfigHelper.Set(currentId, newHiddenState);
}
private void SetIconsToggleText(bool state)
{
Dispatcher.Invoke(() =>
{
IconsToggle.Content = state ? "Show Icons" : "Hide Icons";
});
}
}
static class ConfigHelper
{
private const string configFilename = "user.config";
private static readonly HashSet<Guid> hiddenIconMap = new();
public static void Load()
{
foreach (var line in ReadConfigLines())
{
if (Guid.TryParse(line, out var id))
{
hiddenIconMap.Add(id);
}
}
}
public static bool Get(Guid id)
{
return hiddenIconMap.Contains(id);
}
public static void Set(Guid id, bool hidden)
{
if (hidden)
{
AddToConfig(id);
hiddenIconMap.Add(id);
}
else
{
RemoveFromConfig(id);
hiddenIconMap.Remove(id);
}
}
public static void Remove(Guid id)
{
RemoveFromConfig(id);
hiddenIconMap.Remove(id);
}
private static void AddToConfig(Guid id)
{
var input = ReadConfigLines().ToList();
bool alreadyExists = input.Any(line => line.Trim() == id.ToString());
if (!alreadyExists)
{
input.Add(id.ToString());
File.WriteAllLines(configFilename, input);
Debug.WriteLine($"Added {id} to config");
return;
}
Debug.WriteLine($"Add skipped, {id} already in config");
}
private static void RemoveFromConfig(Guid id)
{
var input = ReadConfigLines();
var result = input
.Where(line => line.Trim() != id.ToString())
.ToList();
if (result.Count < input.Length)
{
File.WriteAllLines(configFilename, result);
Debug.WriteLine($"Removed {id} from config");
return;
}
Debug.WriteLine($"Remove failed, didn't find {id} in config");
}
private static string[] ReadConfigLines()
{
if (!File.Exists(configFilename))
{
File.Create(configFilename).Close();
return Array.Empty<string>();
}
return File.ReadAllLines(configFilename);
}
}
static class FileHiderHelper
{
private static bool? lastState = null;
public static void SetHiddenFiles(bool hidden)
{
if (lastState == hidden) return;
lastState = hidden;
var userName = Environment.GetEnvironmentVariable("USERNAME") ?? throw new Exception("Couldn't get %USERNAME% environment variable");
SetDirectoryFilesHidden($"C:\\Users\\{userName}\\Desktop", hidden);
SetDirectoryFilesHidden($"C:\\Users\\Public\\Desktop", hidden);
RefreshDesktop();
}
private static void SetDirectoryFilesHidden(string path, bool hidden)
{
var parentInfo = new DirectoryInfo(path);
if (!parentInfo.Exists)
{
Debug.WriteLine($"Still couldn't find {path}");
return;
}
try
{
foreach (var filePath in Directory.EnumerateFiles(path))
{
if (!File.Exists(filePath)) throw new Exception($"Couldn't find file {filePath}");
if (filePath.EndsWith("desktop.ini")) continue; // Don't modify desktop.ini
var attributes = File.GetAttributes(filePath);
File.SetAttributes(filePath, SetHiddenAttribute(attributes, hidden));
}
foreach (var dirPath in Directory.EnumerateDirectories(path))
{
var info = new DirectoryInfo(dirPath);
if (!info.Exists) throw new Exception($"Couldn't find file {dirPath}");
info.Attributes = SetHiddenAttribute(info.Attributes, hidden);
}
}
catch (UnauthorizedAccessException e)
{
Debug.WriteLine(e);
return;
}
}
private static FileAttributes SetHiddenAttribute(FileAttributes attributes, bool hidden)
{
if (hidden)
{
attributes |= FileAttributes.Hidden;
}
else
{
attributes &= ~FileAttributes.Hidden;
}
return attributes;
}
private static void RefreshDesktop()
{
SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
}
[DllImport("Shell32.dll")]
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
}