From 3c06c73cee81387cdf4cb2bf6c7410b82881af44 Mon Sep 17 00:00:00 2001 From: Jayakrishna Konda Date: Mon, 9 Nov 2026 17:15:17 -0500 Subject: [PATCH] Let the installer close a running OpenHaze Upgrading over a running copy stopped on Setup's "applications are using files" page with a blank entry, and "Automatically close" then failed with "DeleteFile failed; code 5". Three causes, three fixes: - The exe had no file description, so Windows had no name to show. It now carries a title, product, company and copyright. - OpenHaze ignored the close request that Restart Manager sends to every top-level window. The hidden hotkey window now answers WM_QUERYENDSESSION and exits through the normal cleanup on WM_ENDSESSION, which also makes logoff clean. - installer.iss uses CloseApplications=force as a backstop, which loses nothing because settings are saved on every change, and hides "Do not close the applications", since a per-user install can only fail on that path. Also replaces the em dashes in OpenHaze.cs and NativeMethods.cs. --- windows/NativeMethods.cs | 7 ++++++- windows/OpenHaze.cs | 43 ++++++++++++++++++++++++++++++---------- windows/installer.iss | 18 ++++++++++++++++- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/windows/NativeMethods.cs b/windows/NativeMethods.cs index 4f8e2f7..dbd2c4a 100644 --- a/windows/NativeMethods.cs +++ b/windows/NativeMethods.cs @@ -96,7 +96,7 @@ public static string ClassNameOf(IntPtr hwnd) return sb.ToString(); } - // ---- WinEvent hook (foreground changes — instant, no permissions) ---- + // ---- WinEvent hook (foreground changes: instant, no permissions) ---- public delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); @@ -132,6 +132,11 @@ public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, public const uint MOD_WIN = 0x0008; public const int WM_HOTKEY = 0x0312; + // Sent to every top-level window at logoff, and by Restart Manager when + // an installer needs a running OpenHaze to close. + public const int WM_QUERYENDSESSION = 0x0011; + public const int WM_ENDSESSION = 0x0016; + // ---- Low-level mouse hook (scroll over tray icon adjusts intensity) ---- public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); diff --git a/windows/OpenHaze.cs b/windows/OpenHaze.cs index 9b44dc7..b9a542e 100644 --- a/windows/OpenHaze.cs +++ b/windows/OpenHaze.cs @@ -1,4 +1,4 @@ -// OpenHaze for Windows — dims background windows so the active one stands out. +// OpenHaze for Windows: dims background windows so the active one stands out. // Personal-use recreation of HazeOver (hazeover.com). C# 5 / .NET Framework 4.8 / // WinForms: builds with the csc.exe bundled in every Windows 10/11 install. using System; @@ -14,6 +14,14 @@ using Microsoft.Win32; using FormsTimer = System.Windows.Forms.Timer; +// Windows shows the title as the file description, which Task Manager and the +// installer's "applications using files" list use as the app's name. build.bat +// generates the version attributes. +[assembly: AssemblyTitle("OpenHaze")] +[assembly: AssemblyProduct("OpenHaze")] +[assembly: AssemblyCompany("Jayakrishna Konda")] +[assembly: AssemblyCopyright("Copyright (c) 2026 Jayakrishna Konda")] + namespace OpenHaze { internal static class Program @@ -334,7 +342,7 @@ public void Recompute() // A UAC consent prompt (or the lock screen / Ctrl+Alt+Del screen) has // switched to the secure desktop. We can't draw on it, so the closest // equivalent to "only that prompt stays lit" is to full-dim everything - // on ours, regardless of DesktopMode — previously this read as "desktop + // on ours, regardless of DesktopMode. Previously this read as "desktop // focused" and suppressed dimming entirely instead. if (settings.Enabled && Native.IsSecureDesktopActive()) { @@ -392,7 +400,7 @@ private Plan ComputePlan(MonitorState state, IntPtr fg, bool desktopFocused, Lis bool isFgMonitor = fgDevice == device; var onThis = candidates.Where(delegate(WinInfo w) { return w.Device == device; }).ToList(); - // A fullscreen window (covering its whole monitor, taskbar included — + // A fullscreen window (covering its whole monitor, taskbar included, // not just maximized, which stops at the work area) is treated like // "focus one monitor" regardless of MonitorMode: a movie or game on // one screen should dim the others even in independent-focus mode. @@ -413,7 +421,7 @@ private Plan ComputePlan(MonitorState state, IntPtr fg, bool desktopFocused, Lis /// True if hwnd's window rect covers screen's full bounds (taskbar /// included). A plain maximized window stops at the work area, which /// excludes the taskbar, so this only matches genuine fullscreen content - /// (borderless or exclusive) — the case the "Known limitations" section + /// (borderless or exclusive), the case the "Known limitations" section /// of the README used to say never dimmed other monitors at all. private static bool IsFullscreenOnMonitor(IntPtr hwnd, Screen screen) { @@ -435,7 +443,7 @@ private Plan AnchorPlan(IntPtr highlight, List onThisMonitor) } /// Snapshot the top-level z-order (top to bottom), filtered to windows a - /// user would consider "real" — the same set Alt-Tab roughly shows. + /// user would consider "real", roughly the same set Alt-Tab shows. private void Snapshot(out List candidates, out Dictionary zIndex) { candidates = new List(); @@ -592,13 +600,13 @@ public TrayContext() menu = BuildMenu(); tray = new NotifyIcon(); tray.Icon = TrayIconRenderer.Make(settings.Enabled); - tray.Text = "OpenHaze — double-click to toggle, scroll to adjust"; + tray.Text = "OpenHaze: double-click to toggle, scroll to adjust"; tray.ContextMenuStrip = menu; tray.Visible = true; tray.MouseUp += OnTrayMouseUp; tray.DoubleClick += delegate { ToggleEnabled(); }; - hotkeys = new HotkeyWindow(OnHotkey); + hotkeys = new HotkeyWindow(OnHotkey, ExitThread); ReRegisterHotkeys(); settings.Changed += OnSettingsChanged; @@ -654,8 +662,8 @@ private ContextMenuStrip BuildMenu() MessageBox.Show( "OpenHaze for Windows " + Application.ProductVersion + "\n\n" + "Dims background windows so you can focus on the one that matters.\n\n" + - "A personal, open-source recreation of HazeOver (hazeover.com) —\n" + - "if you love the idea, support the original on the Mac.\n\n" + + "A personal, open-source recreation of HazeOver (hazeover.com).\n" + + "If you love the idea, support the original on the Mac.\n\n" + "Tray: double-click toggles, scroll wheel adjusts intensity.", "About OpenHaze", MessageBoxButtons.OK, MessageBoxIcon.Information); }; @@ -789,15 +797,20 @@ protected override void ExitThreadCore() } } - // Message-only window that owns the RegisterHotKey registrations. + // Hidden window that owns the RegisterHotKey registrations and answers + // Windows' request to close. It must stay a top-level window (not + // message-only): logoff and Restart Manager, which the installer uses to + // close a running OpenHaze, only ask top-level windows. internal class HotkeyWindow : NativeWindow { private readonly Action onHotkey; + private readonly Action onEndSession; private readonly List registered = new List(); - public HotkeyWindow(Action onHotkey) + public HotkeyWindow(Action onHotkey, Action onEndSession) { this.onHotkey = onHotkey; + this.onEndSession = onEndSession; CreateHandle(new CreateParams()); } @@ -815,6 +828,14 @@ public void UnregisterAll() protected override void WndProc(ref Message m) { if (m.Msg == Native.WM_HOTKEY) onHotkey(m.WParam.ToInt32()); + else if (m.Msg == Native.WM_QUERYENDSESSION) { m.Result = (IntPtr)1; return; } + else if (m.Msg == Native.WM_ENDSESSION) + { + // Exit through the normal cleanup before Windows ends the process. + if (m.WParam != IntPtr.Zero) onEndSession(); + m.Result = IntPtr.Zero; + return; + } base.WndProc(ref m); } } diff --git a/windows/installer.iss b/windows/installer.iss index 26238d9..9a38ee5 100644 --- a/windows/installer.iss +++ b/windows/installer.iss @@ -28,7 +28,9 @@ OutputBaseFilename=OpenHaze-{#OPENHAZE_VERSION}-Setup WizardStyle=modern ; OpenHaze runs continuously as a tray app; close it automatically (via ; Restart Manager) rather than failing to overwrite/delete a running .exe. -CloseApplications=yes +; OpenHaze exits cleanly when Restart Manager asks; "force" ends it if that +; ever fails, which loses nothing because settings are saved on every change. +CloseApplications=force RestartApplications=no [Languages] @@ -50,3 +52,17 @@ Root: HKCU; Subkey: "Software\Microsoft\Windows\CurrentVersion\Run"; ValueType: [Run] Filename: "{app}\OpenHaze.exe"; Description: "Launch OpenHaze"; Flags: nowait postinstall skipifsilent + +[Code] +// A per-user install cannot replace a running OpenHaze.exe, and cannot defer +// that to a restart either, so "Do not close the applications" could only end +// in a file-in-use error. When Setup lists a running copy, keep the one choice +// that works. +procedure CurPageChanged(CurPageID: Integer); +begin + if (CurPageID = wpPreparing) and WizardForm.PreparingNoRadio.Visible then + begin + WizardForm.PreparingYesRadio.Checked := True; + WizardForm.PreparingNoRadio.Visible := False; + end; +end;