-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
463 lines (396 loc) · 18.2 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
463 lines (396 loc) · 18.2 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using MessageBox = System.Windows.MessageBox;
using DataFormats = System.Windows.Forms.DataFormats;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using Image = System.Windows.Controls.Image;
using Panel = System.Windows.Controls.Panel;
using Cursors = System.Windows.Input.Cursors;
using IconD = System.Drawing.Icon;
using CancelEventArgs = System.ComponentModel.CancelEventArgs;
using System.Diagnostics;
using System.Windows.Shapes;
using Path = System.IO.Path;
namespace CrystalDock
{
public partial class MainWindow : Window
{
public static MainWindow? _instance;
[DllImport("shell32.dll", SetLastError = true)]
private static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string? lpParameters, string? lpDirectory, int nShowCmd);
public MainWindow()
{
InitializeComponent();
InitializeUI();
LoadButtons();
}
private void MainWindow_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);
}
private void InitializeUI()
{
if (App.settings == null)
{
this.Close();
return;
}
this.Top = Properties.Settings.Default.AppDockTop;
this.Left = Properties.Settings.Default.AppDockLeft;
this.AllowDrop = true;
_instance = this;
int iconSize = (int)App.settings.GetValue<UInt32>("Global", "IconSize");
int iconMargins = (int)App.settings.GetValue<UInt32>("Global", "IconMargins");
this.Width = iconSize * 2 + iconMargins;
this.Height = iconSize * 2;
mainGrid.Margin = new Thickness(0, Height - 16 - iconSize - iconMargins, 0, 0);
ShiftWindowOntoScreenHelper.ShiftWindowOntoScreen(this);
Properties.Settings.Default.AppDockTop = this.Top;
Properties.Settings.Default.AppDockLeft = this.Left;
Properties.Settings.Default.Save();
Line 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);
}
private void MainWindow_MouseLeave(object sender, MouseEventArgs e)
{
if (App.settings == null) return;
if (!App.settings.IsDockLocked)
{
IconGrid.Background.Opacity = 0.1;
return;
}
IconGrid.Background.Opacity = 0.005;
foreach (Image iconImg in IconGrid.Children.OfType<Image>())
{
iconImg.Opacity = 0.5;
}
canvasOverlay.Children.Clear();
}
private void MainWindow_MouseEnter(object sender, MouseEventArgs e)
{
if (App.settings == null) return;
if (!App.settings.IsDockLocked)
{
IconGrid.Background.Opacity = 0.1;
return;
}
IconGrid.Background.Opacity = 0.005;
foreach (Image iconImg in IconGrid.Children.OfType<Image>())
{
iconImg.Opacity = 1;
}
}
private void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
{
if (App.settings == null) return;
if (App.settings.IsDockLocked) return;
ShiftWindowOntoScreenHelper.ShiftWindowOntoScreen(this);
Properties.Settings.Default.AppDockTop = this.Top;
Properties.Settings.Default.AppDockLeft = this.Left;
Properties.Settings.Default.Save();
}
private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
if (App.settings == null) return;
if (App.settings.IsDockLocked) return;
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
protected override void OnDrop(System.Windows.DragEventArgs e)
{
e.Handled = true;
base.OnDrop(e);
if (App.settings == null)
return;
if (App.settings.IsDockLocked) return;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
if (filePaths.Length > 0)
{
string filePath = filePaths[0];
string extension = Path.GetExtension(filePath).ToLower().Substring(1);
string savePath = Directory.GetCurrentDirectory() + "\\" + Properties.Resources.IconFolder + App.settings.NextEntry;
if(File.Exists(savePath + ".png"))
{
File.Delete(savePath + ".png");
File.Delete(savePath + "_over.png");
}
string URL = "";
IconInfo iconInfo = new IconInfo();
switch (extension)
{
case "url":
string[] lines = File.ReadAllLines(filePath);
URL = (lines.FirstOrDefault(x => x.StartsWith("URL")) ?? "").Split('=').Last();
string iconPath = (lines.FirstOrDefault(x => x.StartsWith("IconFile")) ?? "").Split('=').Last();
if (!File.Exists(iconPath)) return;
IconD.ExtractAssociatedIcon(iconPath)?.ToBitmap().Save(savePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
case "lnk":
ShortcutInfo? info = ShortcutInfo.FromShortcutFile(filePath);
if (info == null) return;
URL = info.TargetPath;
if (info.TargetPath.Contains("Update.exe") && info.Arguments.Contains("--processStart Discord.exe"))
{
URL = info.WorkingDirectory + "\\" + "Discord.exe";
}
IconD.ExtractAssociatedIcon(URL)?.ToBitmap().Save(savePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
case "exe":
URL = filePath;
IconD.ExtractAssociatedIcon(URL)?.ToBitmap().Save(savePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
break;
}
if (!File.Exists(savePath + ".png")) return;
File.Copy(savePath + ".png", savePath + "_over.png");
iconInfo.IconImage = App.settings.NextEntry + ".png";
iconInfo.IconImageHover = App.settings.NextEntry + "_over.png";
iconInfo.Action = URL;
App.settings.AddEntry(iconInfo);
LoadButtons();
}
}
}
private void IconImg_MouseEnter(object sender, MouseEventArgs e)
{
if (App.settings == null) return;
if (sender is Image img)
{
KeyValuePair<string, IconInfo> iconInfo = (KeyValuePair<string, IconInfo>)img.Tag;
if (iconInfo.Value == null) return;
int columnIndex = Grid.GetColumn(img);
// Get the position based on the icon's column index within the IconGrid
double left = columnIndex * (App.settings.GetValue<UInt32>("Global", "IconSize") + App.settings.GetValue<int>("Global", "IconMargins"));
double top = 0;
// Create a new Image for the zoomed icon
Image zoomedImg = new Image
{
Width = img.ActualWidth + 10, // Width + ZoomFactor
Height = img.ActualHeight + 10, // Height + ZoomFactor
ContextMenu = CreateContextMenu(iconInfo.Key),
Cursor = Cursors.Hand
};
zoomedImg.MouseLeave += ZoomedImg_MouseLeave;
zoomedImg.MouseLeftButtonUp += (_s, _e) => IconImg_MouseLeftButtonUp(sender, _e);
zoomedImg.ContextMenuOpening += IconImg_ContextMenuOpening;
zoomedImg.AllowDrop = true;
zoomedImg.Drop += (_s, _e) => OnDrop(_e);
using (FileStream fileStream = new FileStream(Properties.Resources.IconFolder + iconInfo.Value.IconImageHover, FileMode.Open, FileAccess.Read))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = fileStream;
bitmapImage.EndInit();
zoomedImg.Source = bitmapImage;
}
double xOffset = (zoomedImg.Width - img.Width) / 2;
double yOffset = (zoomedImg.Height - img.Height) / 2;
zoomedImg.RenderTransform = new TranslateTransform(xOffset, yOffset);
Panel.SetZIndex(zoomedImg, int.MaxValue);
// Set the position of the zoomed icon within the Canvas overlay
Canvas.SetLeft(zoomedImg, left);
Canvas.SetTop(zoomedImg, top);
// Add the zoomed icon to the Canvas overlay
canvasOverlay.Children.Clear();
canvasOverlay.Children.Add(zoomedImg);
}
}
private void ZoomedImg_MouseLeave(object sender, MouseEventArgs e)
{
// Remove the zoomed icon from the Canvas overlay
MainWindow_MouseLeave(sender, e);
canvasOverlay.Children.Clear();
}
private void IconImg_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
if (App.settings == null) return;
e.Handled = App.settings.IsDockLocked;
}
private void IconImg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (App.settings == null) return;
if (!App.settings.IsDockLocked) return;
if (sender is Image iconImg)
{
if (iconImg.Tag is KeyValuePair<string, IconInfo> tag)
{
if (tag.Value is IconInfo iconInfo)
{
e.Handled = true;
if (File.Exists(iconInfo.Action) && Path.GetExtension(iconInfo.Action).Substring(1) == "exe")
{
Process.Start(iconInfo.Action);
}
else
{
ShellExecute(IntPtr.Zero, "open", iconInfo.Action, null, null, 1);
}
}
}
}
}
private ContextMenu CreateContextMenu(string iconKey)
{
ContextMenu contextMenu = new ContextMenu();
MenuItem removeItem = new MenuItem()
{
Header = "Remove",
Command = new RelayCommand<string>(App.settings!.RemoveEntry),
CommandParameter = iconKey
};
MenuItem RefreshItem = new MenuItem()
{
Header = "Refresh",
Command = new RelayCommand(LoadButtons)
};
MenuItem ModifyItem = new MenuItem()
{
Header = "Modify",
Command = new RelayCommand<string>(ShowSettingsWindow),
CommandParameter = iconKey
};
contextMenu.Items.Add(ModifyItem);
contextMenu.Items.Add(RefreshItem);
contextMenu.Items.Add(new ToolStripSeparator());
contextMenu.Items.Add(removeItem);
return contextMenu;
}
private void ShowSettingsWindow(string infoKey)
{
if (App.settings == null) return;
using (ModifyIcon modifyIcon = new ModifyIcon(infoKey))
{
modifyIcon.Owner = this;
MainWindow_MouseLeave(null!, null!);
canvasOverlay.Children.Clear();
if (modifyIcon.ShowDialog() == true)
{
if (modifyIcon.iconInfo == null) return;
IconInfo info = App.settings.GetIconInfo(infoKey)!;
if (modifyIcon.iconInfo.IconImage != info.IconImage) File.Delete(Properties.Resources.IconFolder + info!.IconImage);
if (modifyIcon.iconInfo.IconImageHover != info.IconImageHover) File.Delete(Properties.Resources.IconFolder + info!.IconImageHover);
App.settings.UpdateEntry(infoKey, modifyIcon.iconInfo);
LoadButtons();
}
}
}
public void LoadButtons()
{
if (App.settings == null) return;
ClearIconGrid();
int column = 0;
foreach (var iconEntry in App.settings.GetIconEntries())
{
IconInfo iconInfo = iconEntry.Value;
string iconImagePath = Path.Combine(Directory.GetCurrentDirectory(), Properties.Resources.IconFolder, iconInfo.IconImage);
if (File.Exists(iconImagePath))
{
Image? iconImg = CreateIconImage(iconEntry, iconImagePath, column);
if (iconImg == null) throw new NullReferenceException(nameof(iconImg));
IconGrid.Children.Add(iconImg);
column++;
}
else if (File.Exists(Properties.Resources.MissingIconPath))
{
Image? iconImg = CreateIconImage(iconEntry, Properties.Resources.MissingIconPath, column);
if (iconImg == null) throw new NullReferenceException(nameof(iconImg));
IconGrid.Children.Add(iconImg);
column++;
}
}
AdjustGridSizes();
}
private void ClearIconGrid()
{
foreach (var item in IconGrid.Children.OfType<Image>()) item.Source = null;
IconGrid.Children.Clear();
IconGrid.ColumnDefinitions.Clear();
}
private Image? CreateIconImage(KeyValuePair<string, IconInfo> iconInfo, string imagePath, int column)
{
if (App.settings == null) return null;
Image iconImg = new Image
{
Name = iconInfo.Value.IconImage.Substring(0, iconInfo.Value.IconImage.Length - 4),
Width = App.settings.GetValue<UInt32>("Global", "IconSize"),
Height = App.settings.GetValue<UInt32>("Global", "IconSize"),
//Cursor = System.Windows.Input.Cursors.Hand,
Tag = iconInfo,
ContextMenu = CreateContextMenu(iconInfo.Key),
Opacity = 0.5
};
// Load the image using a stream
using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = fileStream;
bitmapImage.EndInit();
iconImg.Source = bitmapImage;
}
iconImg.MouseLeftButtonUp += IconImg_MouseLeftButtonUp;
iconImg.ContextMenuOpening += IconImg_ContextMenuOpening;
iconImg.MouseEnter += IconImg_MouseEnter;
iconImg.AllowDrop = true;
iconImg.Drop += (_s, _e) => OnDrop(_e);
Grid.SetRow(iconImg, 0);
Grid.SetColumn(iconImg, column);
ColumnDefinition colDefinition = new ColumnDefinition();
IconGrid.ColumnDefinitions.Add(colDefinition);
return iconImg;
}
private void AdjustGridSizes()
{
if (App.settings == null) return;
int iconSizeWithMargin = (int)App.settings.GetValue<UInt32>("Global", "IconSize") + App.settings.GetValue<int>("Global", "IconMargins");
int iconCount = IconGrid.Children.Count;
if (iconCount > 0)
{
this.Width = (App.settings.GetValue<UInt32>("Global", "IconSize") + (App.settings.GetValue<int>("Global", "IconMargins") * 2)) * iconCount;
IconGrid.MaxWidth = iconSizeWithMargin * iconCount;
IconGrid.Width = IconGrid.MaxWidth + App.settings.GetValue<int>("Global", "IconMargins");
IconGrid.Height = iconSizeWithMargin;
mainGrid.MaxWidth = IconGrid.MaxWidth + App.settings.GetValue<int>("Global", "IconMargins");
mainGrid.Width = mainGrid.MaxWidth;
mainGrid.Height = IconGrid.Height + App.settings.GetValue<int>("Global", "IconMargins");
foreach (var c in mainGrid.Children)
{
if (c is Line BottomLine)
{
BottomLine.X1 = 0;
BottomLine.X2 = mainGrid.Width;
BottomLine.Y1 = mainGrid.Height - 4;
BottomLine.Y2 = mainGrid.Height - 4;
break;
}
}
}
}
}
}