-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
690 lines (591 loc) · 27.3 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
690 lines (591 loc) · 27.3 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Windows.Graphics;
using Windows.UI;
using WinUIEx;
using WinUIEx.Messaging;
using XrayUI.Helpers;
using XrayUI.Services;
namespace XrayUI
{
public sealed partial class MainWindow
{
private readonly FrameworkElement _rootElement;
private readonly Border _miniDragRegion;
private readonly Button _miniExpandButton;
private readonly WindowMessageMonitor _windowMessageMonitor;
// We own the tray icon directly (rather than WindowManager.IsVisibleInTray) so the
// tooltip can track connection state; see ConfigureTray.
private TrayIcon? _trayIcon;
// Connection-state icon variants (idle = blue, running = green). Both the tray icon
// and the taskbar/window icon swap between them; see ApplyConnectionIcon.
private string? _idleIconPath;
private string? _runningIconPath;
// Last running-state we pushed to the icon, so we only issue a Shell_NotifyIcon icon
// modify on an actual transition (see OnViewModelPropertyChanged for why that matters).
private bool _trayShowsRunning;
private bool _isSessionEnding;
private bool _allowClose;
private bool _initialized;
private bool _isHiddenToTray;
// Set when mini mode was entered from a maximized window, so expanding
// returns to maximized instead of the plain windowed size.
private bool _restoreMaximizedOnExpand;
private bool _personalizeRealized;
private readonly bool _startMinimized;
// Set when we parked the window off-screen at startup; cleared after
// we re-center it on the first user-initiated show (tray click).
private bool _needsCenterOnFirstShow;
private const uint WmQueryEndSession = 0x0011;
private const uint WmEndSession = 0x0016;
private const uint WmHotkey = 0x0312;
private const uint WmNclButtonDown = 0x00A1;
private const uint WmNclButtonDblClk = 0x00A3;
private const int HtCaption = 0x0002;
private const int FullWindowWidth = 950;
private const int FullWindowHeight = 600;
private const int FullModeMinWidth = 430;
private const int FullModeMinHeight = 260;
private const int MiniWindowWidth = 330;
private const int MiniWindowHeight = 136;
// Unique id for our own tray icon, independent of WinUIEx's WindowManager tray.
// Must be a positive 16-bit value: the Shell_NotifyIcon v4 callback reports the icon id
// in HIWORD(lParam) (a signed short), so a wider id is truncated on the way back and
// every click would fail the id match in TrayIcon.ProcessTrayIconEvents (icon still
// shows, but left/right clicks do nothing).
private const uint TrayIconId = 0x5852;
public MainViewModel ViewModel { get; }
public MainWindow(bool startMinimized = false)
{
_startMinimized = startMinimized;
_needsCenterOnFirstShow = startMinimized;
// Build services before InitializeComponent so ViewModel is ready for x:Bind
var settingsService = new SettingsService();
var xrayService = new XrayService();
var tunService = new TunService();
var startupService = new StartupService();
var dialogService = new DialogService(() => _initialized ? Content?.XamlRoot : null);
var updateService = new UpdateService();
ViewModel = new MainViewModel(dialogService, settingsService, xrayService, tunService, startupService, updateService);
InitializeComponent();
Title = L.MainWindow_Title;
ToolTipService.SetToolTip(DockButton, L.MainWindow_ToggleMini);
ToolTipService.SetToolTip(MiniExpandButton, L.MainWindow_ExpandFull);
ToolTipService.SetToolTip(MinicloseButton, L.MainWindow_Close);
// Initial size and per-mode min-size constraints are established by
// ApplyWindowMode(isMini: false) below. Get attaches WinUIEx window
// management to this window for its side effects (min-size clamping,
// placement); we don't keep the reference — the tray icon is owned
// directly via _trayIcon so we can drive its tooltip from connection state.
WindowManager.Get(this);
_windowMessageMonitor = new WindowMessageMonitor(this);
_windowMessageMonitor.WindowMessageReceived += OnWindowMessageReceived;
GlobalHotkeyStore.HotkeysChanged += OnGlobalHotkeysChanged;
_rootElement = (FrameworkElement)Content;
ThemeHelper.RootElement = _rootElement;
ThemeHelper.MainWindow = this;
ThemeHelper.ApplyBackdrop("Mica");
_rootElement.ActualThemeChanged += OnRootElementActualThemeChanged;
// PersonalizeControl is heavy (5x ColorPicker, Expander, etc.). We
// realize it lazily into PersonalizeHost on first show — saves the
// entire subtree from cold-start construction. See OnViewModelPropertyChanged.
ViewModel.PropertyChanged += OnViewModelPropertyChanged;
_miniDragRegion = (Border)_rootElement.FindName("MiniDragRegion");
_miniExpandButton = (Button)_rootElement.FindName("MiniExpandButton");
_miniDragRegion.PointerPressed += MiniDragRegion_PointerPressed;
_miniExpandButton.Click += MiniExpandButton_Click;
var miniCloseButton = (Button)_rootElement.FindName("MinicloseButton");
miniCloseButton.Click += (_, _) =>
{
if (!HideToTray())
ExitApplication();
};
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
ConfigureTray();
ApplyWindowMode(isMini: false);
UpdateCaptionButtonColors();
Activated += OnFirstActivated;
Closed += OnClosed;
}
private async void OnFirstActivated(object sender, WindowActivatedEventArgs args)
{
Activated -= OnFirstActivated;
_initialized = true;
// For --startup-minimized we only hide the window here so the XamlRoot
// stays alive for any dialogs raised during InitializeAsync (e.g.
// auto-connect errors). The full tray transition (ReleaseUiResources)
// runs after init so resources are still freed in the minimized case.
if (_startMinimized)
{
AppWindow.IsShownInSwitchers = false;
AppWindow.Hide();
}
await ViewModel.InitializeAsync(isBootLaunch: _startMinimized);
RegisterGlobalHotkeys();
if (_startMinimized && !HideToTray())
{
RestoreFromTray();
}
}
private void AppTitleBar_BackRequested(object sender, RoutedEventArgs e)
{
if (ViewModel.GoBackCommand.CanExecute(null))
{
ViewModel.GoBackCommand.Execute(null);
}
}
private void DockButton_Click(object sender, RoutedEventArgs e)
{
SetMiniMode(!ViewModel.IsMiniMode);
}
private void ConfigureTray()
{
var iconsDir = Path.Combine(AppContext.BaseDirectory, "Assets", "icons");
_idleIconPath = Path.Combine(iconsDir, "output.ico");
var runningPath = Path.Combine(iconsDir, "running.ico");
// Fall back to the idle icon if the running variant is missing, so a bad deploy
// degrades to "icon never changes" rather than no icon at all.
_runningIconPath = File.Exists(runningPath) ? runningPath : _idleIconPath;
_trayShowsRunning = ViewModel.TrayShowsRunning;
var iconPath = _trayShowsRunning ? _runningIconPath : _idleIconPath;
if (File.Exists(iconPath))
{
AppWindow.SetIcon(iconPath);
_trayIcon = TryCreateTrayIcon(iconPath);
}
AppWindow.Closing += (_, args) =>
{
if (_allowClose || _isSessionEnding)
{
return;
}
if (HideToTray())
{
args.Cancel = true;
}
};
}
// Own the tray icon directly instead of WindowManager.IsVisibleInTray: WinUIEx ties that
// built-in tooltip to the static window Title and never exposes it, so we create our own
// TrayIcon and push the tooltip via Tooltip (NIM_MODIFY) — that updates szTip only,
// leaving the taskbar/window icon untouched. Returns null if creation fails so the window
// is never stranded in a tray that isn't there (HideToTray checks for it).
private TrayIcon? TryCreateTrayIcon(string iconPath)
{
TrayIcon? trayIcon = null;
try
{
trayIcon = new TrayIcon(TrayIconId, iconPath, ViewModel.TrayTooltip);
trayIcon.Selected += (_, _) => RestoreFromTray();
trayIcon.ContextMenu += (_, e) => e.Flyout = BuildTrayContextMenu();
trayIcon.IsVisible = true;
return trayIcon;
}
catch (Exception ex)
{
trayIcon?.Dispose();
Debug.WriteLine($"[Tray] Failed to create tray icon: {ex.Message}");
return null;
}
}
// Swap both the tray icon and the taskbar/window icon to reflect connection state.
// Called on the UI thread from the TrayTooltip PropertyChanged handler. SetIcon updates
// the existing tray icon in place (NIM_MODIFY) — no dispose/recreate needed.
private void ApplyConnectionIcon(bool running)
{
var path = running ? _runningIconPath : _idleIconPath;
if (path is null || !File.Exists(path)) return;
AppWindow.SetIcon(path);
_trayIcon?.SetIcon(path);
}
private MenuFlyout BuildTrayContextMenu()
{
var flyout = new MenuFlyout();
var openItem = new MenuFlyoutItem { Text = L.Tray_Open };
openItem.Click += (_, _) => RestoreFromTray();
flyout.Items.Add(openItem);
flyout.Items.Add(new MenuFlyoutSeparator());
var exitItem = new MenuFlyoutItem { Text = L.Tray_Exit };
exitItem.Click += (_, _) => ExitApplication();
flyout.Items.Add(exitItem);
return flyout;
}
private bool HideToTray()
{
if (_isHiddenToTray)
{
return true;
}
if (_trayIcon is null)
{
Debug.WriteLine("[Tray] Hide requested but tray icon is unavailable.");
return false;
}
_isHiddenToTray = true;
ControlPanel?.CloseLogWindow();
ControlPanel?.CloseCustomRulesWindow();
AppWindow.IsShownInSwitchers = false;
AppWindow.Hide();
// Collapse the root content so the compositor drops cached materials
// for the visual tree while we're in the tray. Object graph stays
// intact; restoring is one synchronous frame.
_rootElement.Visibility = Visibility.Collapsed;
ReleaseUiResources();
return true;
}
internal void RestoreFromTray()
{
_isHiddenToTray = false;
AppWindow.IsShownInSwitchers = true;
_rootElement.Visibility = Visibility.Visible;
if (_needsCenterOnFirstShow)
{
_needsCenterOnFirstShow = false;
CenterOnPrimaryDisplay();
}
Activate();
}
private void HandleHotkeyMessage(int id)
{
if (id == GlobalHotkeyStore.ToggleId)
{
var cmd = ViewModel.ControlPanel.StartStopCommand;
if (cmd.CanExecute(null))
_ = cmd.ExecuteAsync(null);
}
else if (id == GlobalHotkeyStore.RestoreId)
{
RestoreFromTray();
}
}
private void OnGlobalHotkeysChanged(object? sender, EventArgs e) => RegisterGlobalHotkeys();
/// <summary>
/// Re-register after an elevation/process handoff. The outgoing process can still own
/// the configured combinations when this window performs its normal startup registration;
/// once App confirms that process has exited, retry on this window's UI thread.
/// </summary>
internal void RegisterGlobalHotkeysAfterProcessTakeover()
{
if (DispatcherQueue.HasThreadAccess)
{
RegisterGlobalHotkeys();
return;
}
if (!DispatcherQueue.TryEnqueue(RegisterGlobalHotkeys))
Debug.WriteLine("[Hotkey] Failed to enqueue post-takeover hotkey registration.");
}
// Idempotent: always unregisters both ids first, then re-registers whichever have a
// combo assigned (no separate enabled flag — presence of a combo means active). Safe to
// call at startup and any time the Personalize page commits a hotkey change.
private void RegisterGlobalHotkeys()
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
foreach (var id in new[] { GlobalHotkeyStore.ToggleId, GlobalHotkeyStore.RestoreId })
{
HotkeyInterop.UnregisterHotKey(hWnd, id);
// On failure, the combo is left in the store as-is (not cleared) — a conflict may
// be temporary (another app releases it, or the system state changes), and the
// next call to this method retries with the same combo. Nothing else reads a
// "successfully registered" flag: the UI only reflects whether a combo is assigned,
// and WM_HOTKEY simply never arrives for an id Windows didn't actually register.
var (mods, vk) = GlobalHotkeyStore.GetCombo(id);
if (vk != 0)
RegisterHotkeyOrLog(hWnd, id, mods, vk);
}
}
private static void RegisterHotkeyOrLog(IntPtr hWnd, int id, uint modifiers, uint virtualKey)
{
if (HotkeyInterop.RegisterHotKey(hWnd, id, modifiers | GlobalHotkeyStore.ModNoRepeat, virtualKey)) return;
Debug.WriteLine($"[Hotkey] Failed to register hotkey id={id} ({modifiers}:{virtualKey}). LastWin32Error={Marshal.GetLastWin32Error()}");
}
private void CenterOnPrimaryDisplay()
{
var displayArea = DisplayArea.GetFromWindowId(AppWindow.Id, DisplayAreaFallback.Primary);
var workArea = displayArea.WorkArea;
var size = AppWindow.Size;
var x = workArea.X + (workArea.Width - size.Width) / 2;
var y = workArea.Y + (workArea.Height - size.Height) / 2;
AppWindow.Move(new PointInt32(x, y));
}
private void ExitApplication()
{
if (_allowClose)
{
return;
}
_allowClose = true;
_isHiddenToTray = false;
try
{
_trayIcon?.Dispose();
_trayIcon = null;
}
catch (Exception ex)
{
Debug.WriteLine($"[Tray] Failed to remove tray icon during exit: {ex.Message}");
}
_ = Task.Run(async () =>
{
await Task.Delay(100);
try
{
if (Application.Current is App app)
{
app.RequestShutdown();
return;
}
SystemProxyService.ClearProxy();
StopBackgroundServicesOnExit();
}
catch (Exception ex)
{
Debug.WriteLine($"[Tray] RequestShutdown failed: {ex}");
}
Environment.Exit(0);
});
}
private void OnRootElementActualThemeChanged(FrameworkElement sender, object args)
{
UpdateCaptionButtonColors();
}
private void SetMiniMode(bool isMini)
{
if (ViewModel.IsMiniMode == isMini) return;
var incoming = isMini ? MiniModePanel : FullModePanel;
// Snap to the new mode immediately so the click feels as instant as it
// did before — no blocking fade-out. The only animation is a quick
// entrance on the new content: stage it hidden so it doesn't flash when
// its bound Visibility flips, switch modes + resize the HWND, then let
// the new panel scale/fade in as a non-blocking flourish.
WindowModeTransition.PrepareHidden(incoming);
ViewModel.IsMiniMode = isMini;
ApplyWindowMode(isMini);
WindowModeTransition.FadeIn(incoming);
}
private void ApplyWindowMode(bool isMini)
{
var presenter = (OverlappedPresenter)AppWindow.Presenter;
// Un-maximize first: resizing alone keeps the zoomed flag set, and
// dragging a zoomed window restores it back to full-mode size.
if (isMini)
{
_restoreMaximizedOnExpand = presenter.State == OverlappedPresenterState.Maximized;
if (_restoreMaximizedOnExpand)
presenter.Restore();
}
var width = isMini ? MiniWindowWidth : FullWindowWidth;
var height = isMini ? MiniWindowHeight : FullWindowHeight;
// The WinUIEx min-size clamp (WM_GETMINMAXINFO) applies to SetWindowSize
// too, so the full-mode minimum must be relaxed before shrinking to mini.
var windowManager = WindowManager.Get(this);
windowManager.MinWidth = isMini ? MiniWindowWidth : FullModeMinWidth;
windowManager.MinHeight = isMini ? MiniWindowHeight : FullModeMinHeight;
presenter.SetBorderAndTitleBar(hasBorder: true, hasTitleBar: !isMini);
presenter.IsResizable = !isMini;
presenter.IsMaximizable = !isMini;
AppTitleBar.Visibility = isMini ? Visibility.Collapsed : Visibility.Visible;
this.SetWindowSize(width, height);
// Maximize only after the normal rect above is in place, so the restore
// rect the system captures is the windowed size, not the mini rect.
if (!isMini && _restoreMaximizedOnExpand)
{
_restoreMaximizedOnExpand = false;
presenter.Maximize();
}
}
private void MiniExpandButton_Click(object sender, RoutedEventArgs e)
{
SetMiniMode(isMini: false);
}
private void MiniDragRegion_PointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
{
if (!ViewModel.IsMiniMode) return;
var currentPoint = e.GetCurrentPoint((UIElement)sender);
if (!currentPoint.Properties.IsLeftButtonPressed) return;
e.Handled = true;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
ReleaseCapture();
SendMessage(hWnd, WmNclButtonDown, (IntPtr)HtCaption, IntPtr.Zero);
}
private void UpdateCaptionButtonColors()
{
var tb = AppWindow.TitleBar;
var isDarkTheme = _rootElement.ActualTheme == ElementTheme.Dark;
var foregroundColor = isDarkTheme
? Colors.White
: Color.FromArgb(230, 0, 0, 0);
var inactiveForegroundColor = isDarkTheme
? Color.FromArgb(153, 255, 255, 255)
: Color.FromArgb(138, 0, 0, 0);
var hoverBackgroundColor = isDarkTheme
? Color.FromArgb(30, 255, 255, 255)
: Color.FromArgb(18, 0, 0, 0);
var pressedBackgroundColor = isDarkTheme
? Color.FromArgb(60, 255, 255, 255)
: Color.FromArgb(36, 0, 0, 0);
tb.ButtonBackgroundColor = Colors.Transparent;
tb.ButtonInactiveBackgroundColor = Colors.Transparent;
tb.ButtonHoverBackgroundColor = hoverBackgroundColor;
tb.ButtonPressedBackgroundColor = pressedBackgroundColor;
tb.ButtonForegroundColor = foregroundColor;
tb.ButtonInactiveForegroundColor = inactiveForegroundColor;
tb.ButtonHoverForegroundColor = foregroundColor;
tb.ButtonPressedForegroundColor = foregroundColor;
}
private void OnClosed(object sender, WindowEventArgs args)
{
ViewModel.StopSubscriptionRefreshScheduler();
ViewModel.PropertyChanged -= OnViewModelPropertyChanged;
_rootElement.ActualThemeChanged -= OnRootElementActualThemeChanged;
_windowMessageMonitor.WindowMessageReceived -= OnWindowMessageReceived;
_windowMessageMonitor.Dispose();
GlobalHotkeyStore.HotkeysChanged -= OnGlobalHotkeysChanged;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
HotkeyInterop.UnregisterHotKey(hWnd, GlobalHotkeyStore.ToggleId);
HotkeyInterop.UnregisterHotKey(hWnd, GlobalHotkeyStore.RestoreId);
AppWindow.IsShownInSwitchers = true;
}
private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(MainViewModel.TrayTooltip))
{
// Runs on the UI thread (VM raises PropertyChanged there); no dispatch needed.
// Order matters: swap the icon FIRST, write the tooltip LAST. WinUIEx's SetIcon
// issues a NIM_MODIFY carrying only the icon (no NIF_SHOWTIP); under
// NOTIFYICON_VERSION_4 that suppresses the standard tooltip. The Tooltip setter
// re-sends NIF_TIP|NIF_SHOWTIP, so it has to be the last modify we issue. We also
// only swap on an actual running-state transition, so a tooltip-text change while
// already running (e.g. a node switch) never drops the tooltip.
var running = ViewModel.TrayShowsRunning;
if (running != _trayShowsRunning)
{
_trayShowsRunning = running;
ApplyConnectionIcon(running);
}
if (_trayIcon is not null)
_trayIcon.Tooltip = ViewModel.TrayTooltip;
return;
}
if (_personalizeRealized) return;
if (e.PropertyName != nameof(MainViewModel.PersonalizeVisibility)) return;
if (ViewModel.PersonalizeVisibility != Visibility.Visible) return;
_personalizeRealized = true;
// Set ViewModel before adding to the visual tree so that, when the
// host fires Loading, the UserControl's x:Bind initializers see a
// non-null ViewModel and bind correctly the first time.
PersonalizeHost.Children.Add(new Views.PersonalizeControl
{
ViewModel = ViewModel.Personalize,
});
}
private void OnWindowMessageReceived(object? sender, WindowMessageEventArgs e)
{
if (e.Message.MessageId == WmNclButtonDblClk && ViewModel.IsMiniMode)
{
e.Handled = true;
return;
}
if (e.Message.MessageId == WmHotkey)
{
HandleHotkeyMessage(unchecked((int)e.Message.WParam));
e.Handled = true;
return;
}
if (e.Message.MessageId == WmQueryEndSession)
{
Debug.WriteLine("[Shutdown] WM_QUERYENDSESSION received");
PrepareForSessionEnding();
e.Result = new IntPtr(1);
e.Handled = true;
return;
}
if (e.Message.MessageId != WmEndSession)
{
return;
}
if (e.Message.WParam == 0)
{
Debug.WriteLine("[Shutdown] WM_ENDSESSION cancelled");
RestoreAfterSessionEndingCancelled();
return;
}
Debug.WriteLine("[Shutdown] WM_ENDSESSION received");
PrepareForSessionEnding();
var cleanupTask = Task.Run(() =>
{
try
{
if (Application.Current is App app)
{
app.HandleSessionEnding();
}
else
{
SystemProxyService.ClearProxy();
StopBackgroundServicesOnExit(fastShutdown: true);
}
}
catch (Exception ex)
{
Debug.WriteLine($"[Shutdown] cleanup error: {ex}");
}
});
cleanupTask.Wait(TimeSpan.FromMilliseconds(600));
Environment.Exit(0);
}
private void PrepareForSessionEnding()
{
_isSessionEnding = true;
_allowClose = true;
_isHiddenToTray = false;
}
private void RestoreAfterSessionEndingCancelled()
{
_isSessionEnding = false;
_allowClose = false;
}
private static void ReleaseUiResources()
{
try
{
// Compact LOH in this single GC pass — subscription/JSON paths
// routinely allocate >85KB buffers that pin into LOH and never
// compact under default settings.
System.Runtime.GCSettings.LargeObjectHeapCompactionMode =
System.Runtime.GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, blocking: true, compacting: true);
GC.WaitForPendingFinalizers();
using var process = Process.GetCurrentProcess();
SetProcessWorkingSetSize(process.Handle, (IntPtr)(-1), (IntPtr)(-1));
}
catch (Exception ex)
{
Debug.WriteLine($"[Tray] Failed to release UI resources: {ex.Message}");
}
}
public void StopBackgroundServicesOnExit(bool fastShutdown = false)
{
ViewModel.StopSubscriptionRefreshScheduler();
ViewModel.ControlPanel.XrayService.StopForShutdown();
ViewModel.ControlPanel.CleanupTunOnExit(fastShutdown);
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetProcessWorkingSetSize(
IntPtr process,
IntPtr minimumWorkingSetSize,
IntPtr maximumWorkingSetSize);
}
}