-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
395 lines (371 loc) · 16 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
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
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using System;
using WinUIEx;
using Windows.Graphics;
using VMsApp.Dialogs;
using VMsApp.Wizards;
using System.Runtime.InteropServices;
using Microsoft.UI.Content;
using System.ComponentModel;
using VMsApp.PInvoke.Comctl32;
using Microsoft.UI.Xaml.Media;
using VMsApp.PInvoke.User32;
using VMsApp.Helpers;
using Windows.Foundation;
using Microsoft.UI;
using Windows.UI;
using VMsApp.PInvoke.Uxtheme;
namespace VMsApp
{
public sealed partial class MainWindow : WindowEx, INotifyPropertyChanged
{
private readonly SUBCLASSPROC mainWindowSubClassProc;
private readonly SUBCLASSPROC inputNonClientPointerSourceSubClassProc;
private readonly ContentCoordinateConverter contentCoordinateConverter;
private readonly OverlappedPresenter overlappedPresenter;
private bool _isWindowMaximized;
public bool IsWindowMaximized
{
get { return _isWindowMaximized; }
set
{
if (!Equals(_isWindowMaximized, value))
{
_isWindowMaximized = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsWindowMaximized)));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public Window m_window;
public MainWindow()
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
AppWindow.Resize(new SizeInt32(1200, 700));
this.CenterOnScreen();
SetTitleBar(AppTitleBar);
FolderView.Visibility = Visibility.Collapsed;
TabsGrid.Margin = new Thickness(201, 80, 0, 32);
if (ShowHideLibrary.IsChecked == false)
{
ShowHideLibrary.IsChecked = true;
}
overlappedPresenter = AppWindow.Presenter as OverlappedPresenter;
IsWindowMaximized = overlappedPresenter.State is OverlappedPresenterState.Maximized;
contentCoordinateConverter = ContentCoordinateConverter.CreateForWindowId(AppWindow.Id);
SetClassicMenuTheme((Content as FrameworkElement).ActualTheme);
mainWindowSubClassProc = new SUBCLASSPROC(MainWindowSubClassProc);
Comctl32Library.SetWindowSubclass((IntPtr)AppWindow.Id.Value, Marshal.GetFunctionPointerForDelegate(mainWindowSubClassProc), 0, IntPtr.Zero);
IntPtr inputNonClientPointerSourceHandle = User32Library.FindWindowEx((IntPtr)AppWindow.Id.Value, IntPtr.Zero, "InputNonClientPointerSource", null);
if (inputNonClientPointerSourceHandle != IntPtr.Zero)
{
inputNonClientPointerSourceSubClassProc = new SUBCLASSPROC(InputNonClientPointerSourceSubClassProc);
Comctl32Library.SetWindowSubclass((IntPtr)AppWindow.Id.Value, Marshal.GetFunctionPointerForDelegate(inputNonClientPointerSourceSubClassProc), 0, IntPtr.Zero);
}
AppWindow.Changed += OnAppWindowChanged;
SizeChanged += OnSizeChanged;
}
private void OnActualThemeChanged(FrameworkElement sender, object args)
{
SetTitleBarTheme(sender.ActualTheme);
SetClassicMenuTheme(sender.ActualTheme);
}
private void SetTitleBarTheme(ElementTheme theme)
{
AppWindowTitleBar titleBar = AppWindow.TitleBar;
titleBar.BackgroundColor = Colors.Transparent;
titleBar.ForegroundColor = Colors.Transparent;
titleBar.InactiveBackgroundColor = Colors.Transparent;
titleBar.InactiveForegroundColor = Colors.Transparent;
titleBar.ButtonBackgroundColor = Colors.Transparent;
titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
if (theme is ElementTheme.Light)
{
titleBar.ButtonForegroundColor = Colors.Black;
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(20, 0, 0, 0);
titleBar.ButtonHoverForegroundColor = Colors.Black;
titleBar.ButtonPressedBackgroundColor = Color.FromArgb(30, 0, 0, 0);
titleBar.ButtonPressedForegroundColor = Colors.Black;
titleBar.ButtonInactiveForegroundColor = Colors.Black;
}
else
{
titleBar.ButtonForegroundColor = Colors.White;
titleBar.ButtonHoverBackgroundColor = Color.FromArgb(20, 255, 255, 255);
titleBar.ButtonHoverForegroundColor = Colors.White;
titleBar.ButtonPressedBackgroundColor = Color.FromArgb(30, 255, 255, 255);
titleBar.ButtonPressedForegroundColor = Colors.White;
titleBar.ButtonInactiveForegroundColor = Colors.White;
}
}
private static void SetClassicMenuTheme(ElementTheme theme)
{
if (theme is ElementTheme.Light)
{
UxthemeLibrary.SetPreferredAppMode(PreferredAppMode.ForceLight);
}
else
{
UxthemeLibrary.SetPreferredAppMode(PreferredAppMode.ForceDark);
}
UxthemeLibrary.FlushMenuThemes();
}
private void OnSizeChanged(object sender, WindowSizeChangedEventArgs args)
{
if (TitlebarMenuFlyout.IsOpen)
{
TitlebarMenuFlyout.Hide();
}
if (overlappedPresenter is not null)
{
IsWindowMaximized = overlappedPresenter.State is OverlappedPresenterState.Maximized;
}
}
private void OnAppWindowChanged(AppWindow sender, AppWindowChangedEventArgs args)
{
if (args.DidPositionChange)
{
if (TitlebarMenuFlyout.IsOpen)
{
TitlebarMenuFlyout.Hide();
}
if (overlappedPresenter is not null)
{
IsWindowMaximized = overlappedPresenter.State is OverlappedPresenterState.Maximized;
}
}
}
private void OnRestoreClicked(object sender, RoutedEventArgs args)
{
overlappedPresenter.Restore();
}
private void OnMoveClicked(object sender, RoutedEventArgs args)
{
MenuFlyoutItem menuItem = sender as MenuFlyoutItem;
if (menuItem.Tag is not null)
{
((MenuFlyout)menuItem.Tag).Hide();
User32Library.SendMessage((IntPtr)AppWindow.Id.Value, WindowMessage.WM_SYSCOMMAND, 0xF010, 0);
}
}
private void OnSizeClicked(object sender, RoutedEventArgs args)
{
MenuFlyoutItem menuItem = sender as MenuFlyoutItem;
if (menuItem.Tag is not null)
{
((MenuFlyout)menuItem.Tag).Hide();
User32Library.SendMessage((IntPtr)AppWindow.Id.Value, WindowMessage.WM_SYSCOMMAND, 0xF000, 0);
}
}
private void OnMinimizeClicked(object sender, RoutedEventArgs args)
{
overlappedPresenter.Minimize();
}
private void OnMaximizeClicked(object sender, RoutedEventArgs args)
{
overlappedPresenter.Maximize();
}
private void OnCloseClicked(object sender, RoutedEventArgs args)
{
Application.Current.Exit();
}
private IntPtr MainWindowSubClassProc(IntPtr hWnd, WindowMessage Msg, UIntPtr wParam, IntPtr lParam, uint uIdSubclass, IntPtr dwRefData)
{
if (Msg is WindowMessage.WM_SYSCOMMAND)
{
SYSTEMCOMMAND sysCommand = (SYSTEMCOMMAND)(wParam.ToUInt32() & 0xFFF0);
if (sysCommand is SYSTEMCOMMAND.SC_MOUSEMENU)
{
FlyoutShowOptions options = new()
{
Position = new Point(0, 15),
ShowMode = FlyoutShowMode.Standard
};
TitlebarMenuFlyout.ShowAt(null, options);
return 0;
}
else if (sysCommand is SYSTEMCOMMAND.SC_KEYMENU)
{
FlyoutShowOptions options = new()
{
Position = new Point(0, 45),
ShowMode = FlyoutShowMode.Standard
};
TitlebarMenuFlyout.ShowAt(null, options);
return 0;
}
}
return Comctl32Library.DefSubclassProc(hWnd, Msg, wParam, lParam);
}
private IntPtr InputNonClientPointerSourceSubClassProc(IntPtr hWnd, WindowMessage Msg, UIntPtr wParam, IntPtr lParam, uint uIdSubclass, IntPtr dwRefData)
{
switch (Msg)
{
case WindowMessage.WM_NCLBUTTONDOWN:
{
if (TitlebarMenuFlyout.IsOpen)
{
TitlebarMenuFlyout.Hide();
}
break;
}
case WindowMessage.WM_NCRBUTTONUP:
{
if (wParam.ToUInt32() is 2 && Content is not null && Content.XamlRoot is not null)
{
PointInt32 screenPoint = new(lParam.ToInt32() & 0xFFFF, lParam.ToInt32() >> 16);
Point localPoint = contentCoordinateConverter.ConvertScreenToLocal(screenPoint);
FlyoutShowOptions options = new()
{
ShowMode = FlyoutShowMode.Standard,
Position = InfoHelper.SystemVersion.Build >= 22000 ? new Point(localPoint.X / Content.XamlRoot.RasterizationScale, localPoint.Y / Content.XamlRoot.RasterizationScale) : new Point(localPoint.X, localPoint.Y)
};
TitlebarMenuFlyout.ShowAt(null, options);
}
return 0;
}
}
return Comctl32Library.DefSubclassProc(hWnd, Msg, wParam, lParam);
}
public static void CreateModalWindow(WindowEx parentWindow, WindowEx childWindow, bool summonWindowAutomatically = true, bool blockInput = false)
{
IntPtr hWndChildWindow = WinRT.Interop.WindowNative.GetWindowHandle(childWindow);
IntPtr hWndParentWindow = WinRT.Interop.WindowNative.GetWindowHandle(parentWindow);
SetWindowLong(hWndChildWindow, GWL_HWNDPARENT, hWndParentWindow);
(childWindow.AppWindow.Presenter as OverlappedPresenter).IsModal = true;
if (blockInput == true)
{
EnableWindow(hWndParentWindow, false);
childWindow.Closed += ChildWindow_Closed;
void ChildWindow_Closed(object sender, Microsoft.UI.Xaml.WindowEventArgs args)
{
EnableWindow(hWndParentWindow, true);
}
}
if (summonWindowAutomatically == true) childWindow.Show();
}
private static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
{
if (IntPtr.Size == 4)
{
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
private const int GWL_HWNDPARENT = (-8);
[DllImport("User32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")]
private static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("User32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr")]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
private void HideLibrary_Click(object sender, RoutedEventArgs e)
{
LibraryPanel.Visibility = Visibility.Collapsed;
TabsGrid.Margin = new Thickness(0, 80, 0, 32);
FolderView.Margin = new Thickness(0, 0, 0, 32);
if (ShowHideLibrary.IsChecked == true)
{
ShowHideLibrary.IsChecked = false;
}
}
private void CreateNewVM_Click(object sender, RoutedEventArgs e)
{
var logWin = new NewVMWizard();
CreateModalWindow(App.m_window, logWin, true, true);
App.currentWizardWin = logWin;
}
private void NewWindow_Click(object sender, RoutedEventArgs e)
{
m_window = new MainWindow();
m_window.Activate();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
var logWin = new FeatureNotAvailable();
CreateModalWindow(App.m_window, logWin, true, true);
}
private void ShowHideLibrary_Click(object sender, RoutedEventArgs e)
{
Button btnE = sender as Button;
if (LibraryPanel.Visibility == Visibility.Visible)
{
LibraryPanel.Visibility = Visibility.Collapsed;
TabsGrid.Margin = new Thickness(0, 80, 0, 32);
FolderView.Margin = new Thickness(0, 0, 0, 32);
ShowHideLibrary.IsChecked = false;
}
else
{
LibraryPanel.Visibility = Visibility.Visible;
TabsGrid.Margin = new Thickness(201, 80, 0, 32);
FolderView.Margin = new Thickness(202, 0, 0, 32);
ShowHideLibrary.IsChecked = true;
}
}
private void HideFolderView_Click(object sender, RoutedEventArgs e)
{
FolderView.Visibility = Visibility.Collapsed;
TabsView.Margin = new Thickness(0, 0, 0, 0);
ShowHideFolderView.IsChecked = false;
}
private void ShowHideFolderView_Click(object sender, RoutedEventArgs e)
{
Button btnE = sender as Button;
if (FolderView.Visibility == Visibility.Visible)
{
FolderView.Visibility = Visibility.Collapsed;
TabsView.Margin = new Thickness(0, 0, 0, 0);
ShowHideFolderView.IsChecked = false;
}
else
{
FolderView.Visibility = Visibility.Visible;
TabsView.Margin = new Thickness(0, 0, 0, 152);
ShowHideFolderView.IsChecked = true;
}
}
private void VMSettings_Click(object sender, RoutedEventArgs e)
{
var logWin = new VMSettings();
CreateModalWindow(App.m_window, logWin, true, true);
}
private void About_Click(object sender, RoutedEventArgs e)
{
var logWin = new AboutApp();
CreateModalWindow(App.m_window, logWin, true, true);
}
private void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
private void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void MessageLog_Click(object sender, RoutedEventArgs e)
{
var logWin = new MessageLog();
CreateModalWindow(App.m_window, logWin, true, true);
}
private void VMMessageLog_Click(object sender, RoutedEventArgs e)
{
var logWin = new MessageLog();
CreateModalWindow(App.m_window, logWin, true, true);
}
private void Preferences_Click(object sender, RoutedEventArgs e)
{
var logWin = new Preferences();
CreateModalWindow(App.m_window, logWin, true, true);
}
}
}