From 7e981e0c2628025a99faf690ec9e33bac0f2091d Mon Sep 17 00:00:00 2001 From: Ryan Breen Date: Tue, 10 Mar 2026 06:01:10 -0400 Subject: [PATCH] fix: appbar selection matches foreground window on discovery When discover_windows() adds or removes windows, focus the topmost visible window (last non-minimized in Vec = highest z-order) instead of keeping a stale focused_win index. This ensures the appbar highlight always matches the visually foregrounded window. Co-Authored-By: Claude Opus 4.6 --- userspace/programs/src/bwm.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/userspace/programs/src/bwm.rs b/userspace/programs/src/bwm.rs index eb3e0b8b..1e52dca3 100644 --- a/userspace/programs/src/bwm.rs +++ b/userspace/programs/src/bwm.rs @@ -898,9 +898,7 @@ fn main() { // Initial window discovery (before entering event loop) if discover_windows(&mut windows, screen_w, screen_h, &mut next_creation_order) { - if focused_win >= windows.len() { - focused_win = windows.len().saturating_sub(1); - } + focused_win = next_visible_window(&windows, 0); composite_buf.copy_from_slice(&bg_cache); redraw_all_windows(&mut fb, &windows, focused_win, &clock_text); full_redraw = true; @@ -920,9 +918,10 @@ fn main() { // ── 1. Discover new/removed client windows (only when registry changed) ── if ready & graphics::COMPOSITOR_READY_REGISTRY != 0 { if discover_windows(&mut windows, screen_w, screen_h, &mut next_creation_order) { - if focused_win >= windows.len() { - focused_win = windows.len().saturating_sub(1); - } + // New windows are pushed to end of Vec (top of z-order). + // Always focus the topmost visible window so appbar selection + // matches the visually foregrounded window. + focused_win = next_visible_window(&windows, 0); composite_buf.copy_from_slice(&bg_cache); redraw_all_windows(&mut fb, &windows, focused_win, &clock_text); full_redraw = true;