-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemMonitor.xaml.cs
More file actions
380 lines (335 loc) · 15.8 KB
/
Copy pathSystemMonitor.xaml.cs
File metadata and controls
380 lines (335 loc) · 15.8 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace CrystalDock
{
/// <summary>
/// Interaction logic for SystemMonitor.xaml
/// </summary>
public partial class SystemMonitor : Window
{
public const ulong BYTES = 1024;
public const ulong MEGABYTES = BYTES * BYTES;
public const ulong GIGABYTES = MEGABYTES * BYTES;
public const ulong TERABYTES = GIGABYTES * BYTES;
public static SystemMonitor? _instance;
private bool isResizing = false;
private bool EnteredResizing = false;
private Point startPoint;
private Grid driveEntryGrid = new Grid();
private Line bottomLine = new Line();
private static ConcurrentDictionary<string, DriveEntry> driveEntries = new ConcurrentDictionary<string, DriveEntry>();
private Timer updateTimer;
private bool isUpdating = false;
public class DriveEntry
{
public BitmapImage? DriveIcon { get; set; } = null;
public string DriveLetter { get; set; } = "";
public string DriveInfoText { get; set; } = "";
public double TotalSpace { get; set; }
public double UsedSpace { get; set; }
public bool NeedsUpdating { get; set; } = true;
}
public SystemMonitor()
{
InitializeComponent();
_instance = this;
LoadUIInformation();
updateTimer = new Timer(UpdateTimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
private void UpdateTimerCallback(object? state)
{
Dispatcher.Invoke(new Action(() =>
{
if (isUpdating) return;
isUpdating = true;
GatherDriveInfo();
UpdateUI();
isUpdating = false;
}));
}
private void LoadUIInformation()
{
if (!App.settings!.IsDockLocked)
{
mainStackPanel.Background = new SolidColorBrush(Color.FromArgb(50, 255, 255, 255));
resizeGripBottomRight.Visibility = Visibility.Visible;
}
if(App.settings!.IsDockLocked)
{
resizeGripBottomRight.Visibility = Visibility.Hidden;
}
mainStackPanel.VerticalAlignment = VerticalAlignment.Bottom;
GatherDriveInfo();
}
private async void GatherDriveInfo()
{
await Task.Run(() =>
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.IsReady)
{
double totalSize = drive.TotalSize;
double freeSpace = drive.AvailableFreeSpace;
double usedSpace = totalSize - freeSpace;
var driveLetter = drive.Name;
if (driveEntries.ContainsKey(driveLetter))
{
var existingDriveEntry = driveEntries[driveLetter];
double oldFreeSpace = existingDriveEntry.TotalSpace - existingDriveEntry.UsedSpace;
if (Math.Abs(freeSpace - oldFreeSpace) >= MEGABYTES * 512) // 1 GB in bytes
{
// Update the properties directly without reinitializing the DriveIcon
string driveText = $"{driveLetter}";
var driveTotalSize = GetDriveSizeStandard(totalSize);
var driveAvailableFreeSpace = GetDriveSizeStandard(freeSpace);
driveText += $" {driveAvailableFreeSpace.Key.ToString("F2")} {driveAvailableFreeSpace.Value}/{driveTotalSize.Key.ToString("F2")} {driveTotalSize.Value}";
existingDriveEntry.DriveInfoText = driveText;
existingDriveEntry.TotalSpace = totalSize;
existingDriveEntry.UsedSpace = usedSpace;
existingDriveEntry.NeedsUpdating = true;
}
}
else
{
// New drive entry
DriveEntry driveEntry = new DriveEntry
{
DriveIcon = Dispatcher.Invoke(() => new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "\\\\" + Properties.Resources.IconFolder + "DriveIcon.png"))),
DriveLetter = driveLetter,
TotalSpace = totalSize,
UsedSpace = usedSpace,
NeedsUpdating = true
};
string driveText = $"{driveLetter}";
var driveTotalSize = GetDriveSizeStandard(totalSize);
var driveAvailableFreeSpace = GetDriveSizeStandard(freeSpace);
driveText += $" {driveAvailableFreeSpace.Key.ToString("F2")} {driveAvailableFreeSpace.Value}/{driveTotalSize.Key.ToString("F2")} {driveTotalSize.Value}";
driveEntry.DriveInfoText = driveText;
driveEntries[driveLetter] = driveEntry;
}
}
}
});
}
private void UpdateUI()
{
foreach (DriveEntry driveEntry in driveEntries.Values)
{
if (driveEntry.NeedsUpdating)
{
// Find the existing grid for the drive letter
Grid? existingGrid = mainStackPanel.Children.OfType<Grid>().FirstOrDefault(g => g.Name == "drive_" + driveEntry.DriveLetter[0]);
if (existingGrid != null)
{
// Update the existing grid
var progressBar = existingGrid.Children.OfType<Grid>().First().Children.OfType<ProgressBar>().First();
progressBar.Value = driveEntry.UsedSpace;
var textBlock = existingGrid.Children.OfType<Grid>().First().Children.OfType<TextBlock>().First();
textBlock.Text = driveEntry.DriveInfoText;
}
else
{
// Create the main grid for the drive entry
driveEntryGrid = new Grid();
driveEntryGrid.Name = "drive_" + driveEntry.DriveLetter[0];
driveEntryGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(100) });
driveEntryGrid.ColumnDefinitions.Add(new ColumnDefinition());
// Set the drive icon image source from DriveEntry
Image driveImage = new Image
{
Width = 75,
Height = 75,
Margin = new Thickness(10),
Source = driveEntry.DriveIcon
};
// Create the grid for progress bar and drive info
Grid infoGrid = new Grid();
infoGrid.RowDefinitions.Add(new RowDefinition());
infoGrid.RowDefinitions.Add(new RowDefinition());
// Create progress bar
ProgressBar progressBar = new ProgressBar
{
Height = 20,
MaxWidth = this.Width - driveEntryGrid.ColumnDefinitions[0].Width.Value - 20,
Maximum = driveEntry.TotalSpace,
Value = driveEntry.UsedSpace,
VerticalAlignment = VerticalAlignment.Bottom
};
// Create text block for drive info
TextBlock textBlock = new TextBlock
{
Text = driveEntry.DriveInfoText,
Foreground = new SolidColorBrush(Colors.White),
Margin = new Thickness(1),
TextWrapping = TextWrapping.Wrap
};
// Add progress bar and text block to info grid
Grid.SetRow(progressBar, 0);
infoGrid.Children.Add(progressBar);
Grid.SetRow(textBlock, 1);
infoGrid.Children.Add(textBlock);
// Add drive image and info grid to drive entry grid
driveEntryGrid.Children.Add(driveImage);
Grid.SetColumn(infoGrid, 1);
driveEntryGrid.Children.Add(infoGrid);
// Add drive entry grid to main stack panel
mainStackPanel.Children.Add(driveEntryGrid);
driveEntry.NeedsUpdating = false;
}
updateTimer.Change(TimeSpan.Zero, TimeSpan.FromMinutes(1));
}
}
}
private KeyValuePair<double, string> GetDriveSizeStandard(double size)
{
KeyValuePair<double, string> rtn = new KeyValuePair<double, string>();
if (size / TERABYTES < 1024)
{
rtn = new KeyValuePair<double, string>(size / TERABYTES, "TB");
}
if (size / GIGABYTES < 1024)
{
rtn = new KeyValuePair<double, string>(size / GIGABYTES, "GB");
}
if (size / MEGABYTES < 1024)
{
rtn = new KeyValuePair<double, string>(size / MEGABYTES, "MB");
}
if (size / BYTES < 1024)
{
rtn = new KeyValuePair<double, string>(size / BYTES, "BYTES");
}
return rtn;
}
private void SysMonWindow_Loaded(object sender, RoutedEventArgs e)
{
//Variable to hold the handle for the form
var helper = new WindowInteropHelper(this).Handle;
//Performing some magic to hide the form from Alt+Tab
App.SetWindowLong(helper, App.GWL_EX_STYLE, (App.GetWindowLong(helper, App.GWL_EX_STYLE) | App.WS_EX_TOOLWINDOW) & ~App.WS_EX_APPWINDOW);
this.Top = Properties.Settings.Default.SystemMonitorTop;
this.Left = Properties.Settings.Default.SystemMonitorLeft;
this.Width = Properties.Settings.Default.SystemMonitorWidth;
this.Height = Properties.Settings.Default.SystemMonitorHeight;
ShiftWindowOntoScreenHelper.ShiftWindowOntoScreen(this);
Properties.Settings.Default.SystemMonitorTop = this.Top;
Properties.Settings.Default.SystemMonitorLeft = this.Left;
Properties.Settings.Default.SystemMonitorHeight = this.Height;
Properties.Settings.Default.SystemMonitorWidth = this.Width;
Properties.Settings.Default.Save();
bottomLine = new Line
{
Name = "BottomLine",
Stroke = Brushes.White,
StrokeThickness = 4,
X1 = Settings.settingsInstance!.GetValue<UInt32>("Global", "IconMargins"),
X2 = this.Width - Settings.settingsInstance!.GetValue<UInt32>("Global", "IconMargins"),
Y1 = this.Height-8,
Y2 = this.Height-8,
Margin = new Thickness(0)
};
mainGrid.Children.Add(bottomLine);
foreach (var grid in mainStackPanel.Children.OfType<Grid>())
{
foreach (var grid2 in grid.Children.OfType<Grid>())
{
foreach (var progressBar in grid2.Children.OfType<ProgressBar>())
{
progressBar.Width = this.Width - driveEntryGrid.ColumnDefinitions[0].Width.Value - 20;
progressBar.HorizontalAlignment = HorizontalAlignment.Left;
}
}
}
this.MinHeight = mainStackPanel.ActualHeight;
this.MinWidth = 350;
}
private void SystemMonitor_MouseUp(object sender, MouseButtonEventArgs e)
{
if (isResizing) return;
if (EnteredResizing) return;
if (App.settings == null) return;
if (App.settings.IsDockLocked) return;
ShiftWindowOntoScreenHelper.ShiftWindowOntoScreen(this);
Properties.Settings.Default.SystemMonitorTop = this.Top;
Properties.Settings.Default.SystemMonitorLeft = this.Left;
Properties.Settings.Default.Save();
}
private void SystemMonitor_MouseDown(object sender, MouseButtonEventArgs e)
{
if (isResizing) return;
if (EnteredResizing) return;
if (App.settings == null) return;
if (App.settings.IsDockLocked) return;
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
private void ResizeGrip_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isResizing = true;
startPoint = e.GetPosition(this);
Mouse.Capture(resizeGripBottomRight);
}
private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing)
{
double deltaX = e.GetPosition(this).X - startPoint.X;
double deltaY = e.GetPosition(this).Y - startPoint.Y;
double newWidth = Width + deltaX;
double newHeight = Height + deltaY;
if (newWidth > MinWidth)
Width = newWidth;
if (newHeight > MinHeight)
Height = newHeight;
startPoint = e.GetPosition(this);
foreach (var grid in mainStackPanel.Children.OfType<Grid>())
{
foreach (var grid2 in grid.Children.OfType<Grid>())
{
foreach (var progressBar in grid2.Children.OfType<ProgressBar>())
{
progressBar.MaxWidth = this.Width - driveEntryGrid.ColumnDefinitions[0].Width.Value - 20;
}
}
}
foreach (var item in mainGrid.Children.OfType<Line>())
{
item.X1 = Settings.settingsInstance!.GetValue<UInt32>("Global", "IconMargins");
item.X2 = this.Width - Settings.settingsInstance!.GetValue<UInt32>("Global", "IconMargins");
item.Y1 = this.Height - 8;
item.Y2 = this.Height - 8;
}
}
}
private void ResizeGrip_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isResizing = false;
Mouse.Capture(null);
Properties.Settings.Default.SystemMonitorHeight = this.Height;
Properties.Settings.Default.SystemMonitorWidth = this.Width;
Properties.Settings.Default.Save();
}
private void resizeGripBottomRight_MouseEnter(object sender, MouseEventArgs e)
{
EnteredResizing = true;
}
private void resizeGripBottomRight_MouseLeave(object sender, MouseEventArgs e)
{
EnteredResizing = false;
}
}
}