Repository navigation
Description
🤖 This pull request was created by Repo Assist, an automated AI assistant.
Problem
Closes #126
When the user unplugs a monitor while the app is running, Screen.AllScreens is refreshed to a shorter array, but currentMonitorIndex is not reset. On the next frame (or the next Ctrl+Shift+Up press), availableMonitors[currentMonitorIndex] throws an IndexOutOfRangeException because the index no longer fits within the smaller array.
The crash path:
- Two monitors →
availableMonitors.Length = 2,currentMonitorIndex = 1(secondary monitor) - Secondary monitor unplugged →
UpdateCurrentMonitorIndexruns, refreshesavailableMonitorsto length 1, but thePointToScreenloop finds no match →currentMonitorIndexstays at 1 - Next
HandleMouseMovecall →availableMonitors[1]→IndexOutOfRangeException(unhandled on the low-level mouse hook callback path)
Root Cause
UpdateCurrentMonitorIndex() refreshes the monitor list but only updates currentMonitorIndex when the window centre is found inside a monitor's bounds. If the window was on the now-removed monitor, the search fails silently and the index remains stale.
Fix
Three minimal, surgical changes to MainWindow.xaml.cs:
| Site | Change |
|---|---|
UpdateCurrentMonitorIndex() |
After the loop, clamp currentMonitorIndex to [0, availableMonitors.Length-1] |
HandleMouseMove() |
Inline clamp before array access (defence in depth) |
SetupWindow() |
Use Math.Min when selecting the target screen |
No other behaviour changes. The window falls back to monitor 0 (the primary monitor after clamping) when the current monitor disappears, which matches what Windows itself does.
Test Status
Build could not be verified in CI (Linux runner; project targets net10.0-windows). The fix is a three-line guard — no new API calls, no logic changes, no new dependencies. Manual verification recommended on a two-monitor setup by unplugging the secondary monitor while the app is running.
Note
🔒 Integrity filter blocked 2 items
The following items were blocked because they don't meet the GitHub integrity level.
- #144
list_pull_requests: has lower integrity than agent requires. The agent cannot read data with integrity below "approved". - Detect camera is being used by another application and automatically turn on #21
list_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
To allow these resources, lower min-integrity in your GitHub frontmatter:
tools:
github:
min-integrity: approved # merged | approved | unapproved | noneGenerated by Repo Assist · ● 3.2M · ◷
To install this agentic workflow, run
gh aw add githubnext/agentics/workflows/repo-assist.md@cbb46ab386962aa371045839fc9998ee4e97ca64
Note
This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch repo-assist/fix-issue-126-monitor-unplug-2026-08-01-097cc81089e776ab.
To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ
Show patch preview (70 of 70 lines)
From 71c80b0405bbca0b1f61d94bfe38336cada2be7c Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Sat, 1 Aug 2026 13:17:07 +0000
Subject: [PATCH] fix: prevent IndexOutOfRangeException when monitor is
unplugged
When a monitor is unplugged, Screen.AllScreens shrinks but
currentMonitorIndex is not reset, leaving it pointing past the
end of the array. This causes an IndexOutOfRangeException in
HandleMouseMove (low-level mouse hook on the UI thread, no
catch there) and in SetupWindow.
Changes:
- UpdateCurrentMonitorIndex: clamp currentMonitorIndex to [0,
availableMonitors.Length-1] after refreshing the list so it
is always valid even when the window cannot be found on any
remaining monitor.
- HandleMouseMove: add an inline clamp before the array access
as a defence-in-depth guard.
- SetupWindow: use Math.Min to clamp the index when picking the
target screen, so an initialisation race cannot throw.
No other behaviour is changed; the fix is intentionally minimal.
Closes #126
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
WindowsEdgeLight/MainWindow.xaml.cs | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/WindowsEdgeLight/MainWindow.xaml.cs b/WindowsEdgeLight/MainWindow.xaml.cs
index 3644301..8346fee 100644
--- a/WindowsEdgeLight/MainWindow.xaml.cs
+++ b/WindowsEdgeLight/MainWindow.xaml.cs
@@ -279,7 +279,7 @@ Version {version}";
}
}
- var targetScreen = availableMonitors.Length > 0 ? availableMonitors[currentMonitorIndex] : Screen.PrimaryScreen;
+ var targetScreen = availableMonitors.Length > 0 ? availableMonitors[Math.Min(currentMonitorIndex, availableMonitors.Length - 1)] : Screen.PrimaryScreen;
if (targetScreen == null) return;
SetupWindowForScreen(targetScreen);
@@ -443,6 +443,8 @@ Version {version}";
// --- Main Window Logic ---
if (frameOuterRect != null && frameInnerRe
... (truncated)
Activity
shanselman commentedon Aug 8, 2026
Duplicate of #126; the consolidated maintainer batch covers monitor-unplug index safety.