|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Erik Darling, Darling Data LLC |
| 3 | + * |
| 4 | + * This file is part of the SQL Server Performance Monitor. |
| 5 | + * |
| 6 | + * Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Windows; |
| 11 | +using Microsoft.Win32; |
| 12 | + |
| 13 | +namespace PerformanceMonitor.Ui |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Restores a window that was auto-hidden to the system tray when Windows turned a |
| 17 | + /// sleep- or lock-driven minimize into a hidden window (#1050). On resume from sleep or |
| 18 | + /// on session unlock, if the window was visible before the transition but is now hidden |
| 19 | + /// or minimized, the supplied <paramref name="restoreToForeground"/> action is invoked |
| 20 | + /// on the window's UI thread. |
| 21 | + /// |
| 22 | + /// This guard does NO rendering work. The companion "blank window after resume" symptom |
| 23 | + /// is handled separately by disabling hardware acceleration |
| 24 | + /// (<c>RenderOptions.ProcessRenderMode = SoftwareOnly</c>) at application startup. |
| 25 | + /// |
| 26 | + /// It subscribes to the static <see cref="SystemEvents"/> events, which hold strong |
| 27 | + /// references to their handlers and raise on a dedicated (non-UI) thread. Callers MUST |
| 28 | + /// <see cref="Dispose"/> the guard when the window really closes, or it leaks the window |
| 29 | + /// and fires against a destroyed HWND. Construct it once per window (e.g. guard the |
| 30 | + /// construction site against a double <c>Loaded</c>). |
| 31 | + /// </summary> |
| 32 | + public sealed class WindowResumeGuard : IDisposable |
| 33 | + { |
| 34 | + private readonly Window _window; |
| 35 | + private readonly Action _restoreToForeground; |
| 36 | + |
| 37 | + /* Seeded true because the app starts visible (App.OnStartup → MainWindow.Show()). This |
| 38 | + covers a Resume/SessionUnlock that arrives with no preceding Suspend/Lock snapshot; the |
| 39 | + normal sleep/lock path recomputes it first. */ |
| 40 | + private bool _wasVisibleBeforeSuspend = true; |
| 41 | + private bool _disposed; |
| 42 | + |
| 43 | + public WindowResumeGuard(Window window, Action restoreToForeground) |
| 44 | + { |
| 45 | + _window = window ?? throw new ArgumentNullException(nameof(window)); |
| 46 | + _restoreToForeground = restoreToForeground ?? throw new ArgumentNullException(nameof(restoreToForeground)); |
| 47 | + |
| 48 | + SystemEvents.PowerModeChanged += OnPowerModeChanged; |
| 49 | + SystemEvents.SessionSwitch += OnSessionSwitch; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Pure decision: restore only when the window was visible before the transition but is |
| 54 | + /// now hidden or minimized. A window the user deliberately sent to the tray beforehand |
| 55 | + /// (<paramref name="wasVisibleBeforeSuspend"/> == false) is left alone. |
| 56 | + /// </summary> |
| 57 | + internal static bool ShouldRestore(bool wasVisibleBeforeSuspend, bool isVisibleNow, WindowState state) |
| 58 | + => wasVisibleBeforeSuspend && (!isVisibleNow || state == WindowState.Minimized); |
| 59 | + |
| 60 | + private void OnPowerModeChanged(object? sender, PowerModeChangedEventArgs e) |
| 61 | + { |
| 62 | + if (_disposed) return; |
| 63 | + |
| 64 | + if (e.Mode == PowerModes.Suspend) |
| 65 | + Snapshot(); |
| 66 | + else if (e.Mode == PowerModes.Resume) |
| 67 | + RestoreIfNeeded(); |
| 68 | + } |
| 69 | + |
| 70 | + private void OnSessionSwitch(object? sender, SessionSwitchEventArgs e) |
| 71 | + { |
| 72 | + if (_disposed) return; |
| 73 | + |
| 74 | + if (e.Reason == SessionSwitchReason.SessionLock) |
| 75 | + Snapshot(); |
| 76 | + else if (e.Reason == SessionSwitchReason.SessionUnlock) |
| 77 | + RestoreIfNeeded(); |
| 78 | + } |
| 79 | + |
| 80 | + /* SystemEvents raise on a dedicated, non-UI thread — marshal to the window's dispatcher |
| 81 | + before touching it. BeginInvoke (not Invoke) avoids a deadlock if the UI thread is mid |
| 82 | + resume-pump; the try/catch covers a late event hitting a dispatcher that is shutting |
| 83 | + down between Closing and teardown. */ |
| 84 | + private void Snapshot() |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + _window.Dispatcher.BeginInvoke(new Action(() => |
| 89 | + { |
| 90 | + if (_disposed) return; |
| 91 | + _wasVisibleBeforeSuspend = _window.IsVisible && _window.WindowState != WindowState.Minimized; |
| 92 | + })); |
| 93 | + } |
| 94 | + catch { /* dispatcher shutting down */ } |
| 95 | + } |
| 96 | + |
| 97 | + private void RestoreIfNeeded() |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + _window.Dispatcher.BeginInvoke(new Action(() => |
| 102 | + { |
| 103 | + if (_disposed) return; |
| 104 | + if (ShouldRestore(_wasVisibleBeforeSuspend, _window.IsVisible, _window.WindowState)) |
| 105 | + _restoreToForeground(); |
| 106 | + })); |
| 107 | + } |
| 108 | + catch { /* dispatcher shutting down */ } |
| 109 | + } |
| 110 | + |
| 111 | + public void Dispose() |
| 112 | + { |
| 113 | + if (_disposed) return; |
| 114 | + _disposed = true; |
| 115 | + SystemEvents.PowerModeChanged -= OnPowerModeChanged; |
| 116 | + SystemEvents.SessionSwitch -= OnSessionSwitch; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments