@samitouri / QOSAMI-WSL / commits / ea5c29ea

wslsettings: allow OOBE window to close with escape key (#13686)

* wslsettings: allow OOBE windows to close with escape key * fix memory leak --------- Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>

Ben Hillis committed Nov 7, 2025 at 12:53 UTC ea5c29eae74ac02b5ef6baf5dc763974ec6ed560
1 file changed +31
src/windows/wslsettings/Windows/OOBEWindow.xaml.cs
+31
@@ -2,8 +2,10 @@
2
3 using Microsoft.UI.Windowing;
4 using Microsoft.UI.Xaml;
5 +using Microsoft.UI.Xaml.Input;
6 using System.Runtime.InteropServices;
7 using Windows.Graphics;
8 +using Windows.System;
9 using WinUIEx.Messaging;
10 using Windows.UI.ViewManagement;
11 using Windows.UI.WindowManagement;
@@ -60,6 +62,8 @@ public sealed partial class OOBEWindow : WindowEx, IDisposable
62 e.Handled = true;
63 }
64 };
65 +
66 + this.Activated += OnWindowActivated;
67 }
68
69 // this handles updating the caption button colors correctly when windows system theme is changed
@@ -108,6 +112,12 @@ public sealed partial class OOBEWindow : WindowEx, IDisposable
112 {
113 msgMonitor?.Dispose();
114 settings.ColorValuesChanged -= Settings_ColorValuesChanged;
115 + this.Activated -= OnWindowActivated;
116 + if (this.Content is Microsoft.UI.Xaml.Controls.Page page)
117 + {
118 + page.KeyboardAccelerators.Clear();
119 + }
120 +
121 disposedValue = true;
122 }
123 }
@@ -118,4 +128,25 @@ public sealed partial class OOBEWindow : WindowEx, IDisposable
128 Dispose(disposing: true);
129 GC.SuppressFinalize(this);
130 }
131 +
132 + private void OnWindowActivated(object sender, WindowActivatedEventArgs args)
133 + {
134 + if (args.WindowActivationState != WindowActivationState.Deactivated && this.Content != null)
135 + {
136 + this.Activated -= OnWindowActivated;
137 +
138 + if (this.Content is Microsoft.UI.Xaml.Controls.Page page)
139 + {
140 + var escapeAccelerator = new KeyboardAccelerator() { Key = VirtualKey.Escape };
141 + escapeAccelerator.Invoked += OnCloseKeyboardAcceleratorInvoked;
142 + page.KeyboardAccelerators.Add(escapeAccelerator);
143 + }
144 + }
145 + }
146 +
147 + private void OnCloseKeyboardAcceleratorInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
148 + {
149 + Close();
150 + args.Handled = true;
151 + }
152 }
\ No newline at end of file