Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion windows/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
43 changes: 32 additions & 11 deletions windows/OpenHaze.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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.
Expand All @@ -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)
{
Expand All @@ -435,7 +443,7 @@ private Plan AnchorPlan(IntPtr highlight, List<WinInfo> 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<WinInfo> candidates, out Dictionary<IntPtr, int> zIndex)
{
candidates = new List<WinInfo>();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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<int> onHotkey;
private readonly Action onEndSession;
private readonly List<int> registered = new List<int>();

public HotkeyWindow(Action<int> onHotkey)
public HotkeyWindow(Action<int> onHotkey, Action onEndSession)
{
this.onHotkey = onHotkey;
this.onEndSession = onEndSession;
CreateHandle(new CreateParams());
}

Expand All @@ -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);
}
}
Expand Down
18 changes: 17 additions & 1 deletion windows/installer.iss
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;
Loading