fix(cua-driver-rs)(windows): overlay stops escaping into topmost band, foreground app renders above pin - #1688
Conversation
…band, lets user's foreground render above the pin
Repro: agent cursor overlay pinned over Calculator. User brings their
terminal in front of the Calculator. The overlay renders on top of
the terminal too — even though it should be hovering above the
Calculator (pinned target) and BELOW the terminal (user's foreground).
Root cause: `reapply_z_order` used `HWND_TOPMOST` as a fallback when
the pin wasn't set (or had gone away). Once Windows promotes a window
into the topmost band via SetWindowPos(HWND_TOPMOST), the topmost
flag SETS on the window — a later SetWindowPos with a normal
target_hwnd does NOT drop it back out. The overlay was effectively
topmost-forever after the first frame where no pin was live (which
happens at startup, between pin commands, etc.).
Fix:
1. Always issue SetWindowPos(overlay, HWND_NOTOPMOST, …) first to
drop out of the topmost band if we landed there.
2. Then SetWindowPos(overlay, target_hwnd OR HWND_TOP, …) to land
just above the pin (or top of non-topmost band when no pin).
HWND_TOP (the no-pin fallback) means "top of non-topmost band", NOT
the topmost band. Overlay still renders above other normal windows
when no pin is set, but is no longer hardcoded above the user's
foreground app.
Two-call shape is necessary because HWND_NOTOPMOST itself is a
band-transition pseudo-value (-2) that doesn't represent a target;
the second call is what positions us relative to the pinned target.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe overlay's ChangesPinned-target Z-order Maintenance
Sequence DiagramsequenceDiagram
participant reapply_z_order
participant IsWindow
participant SetWindowPos
reapply_z_order->>IsWindow: Check if pinned window is valid
alt Pinned window exists
IsWindow-->>reapply_z_order: true, use pinned_target
else No valid pinned window
IsWindow-->>reapply_z_order: false, fall back to HWND_TOP
end
reapply_z_order->>SetWindowPos: SetWindowPos with HWND_NOTOPMOST
SetWindowPos-->>reapply_z_order: Demote from topmost band
reapply_z_order->>SetWindowPos: SetWindowPos with computed insertion point
SetWindowPos-->>reapply_z_order: Apply final z-order position
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
User report: agent cursor overlay pinned over Calculator. User brings their terminal in front of the Calculator. Overlay renders on top of the terminal too — should be above the Calculator (pinned target) but below the terminal (user's foreground).
Root cause
`reapply_z_order` used `HWND_TOPMOST` as a fallback when no pin was live:
```rust
let insert_after = if let Some(wid) = pinned_wid {
let target = HWND(wid as *mut _);
if IsWindow(target).as_bool() { target } else { HWND_TOPMOST }
} else {
HWND_TOPMOST // ← bug
};
SetWindowPos(overlay, insert_after, ...);
```
Once Windows promotes a window into the topmost band via `SetWindowPos(HWND_TOPMOST)`, `WS_EX_TOPMOST` gets set on the window. A later `SetWindowPos(overlay, target_hwnd, …)` with a normal target does not drop the overlay back out of the topmost band — it stays above every non-topmost window, including the user's foreground app.
This happens whenever no pin is live: at startup, between `PinAbove` commands, briefly when the pinned window is destroyed and recreated. Once it happens, the overlay is topmost-forever.
Fix — two-call shape
```rust
// 1. Drop out of topmost band (idempotent if already non-topmost).
SetWindowPos(overlay, HWND_NOTOPMOST, …);
// 2. Position relative to the live target (or HWND_TOP if no pin).
SetWindowPos(overlay, pinned_or_HWND_TOP, …);
```
Why two calls: `HWND_NOTOPMOST` is a band-transition pseudo-value (-2) that doesn't represent a positioning target. The second call positions relative to the actual pin (or the top of the non-topmost band when no pin).
Fallback when no pin is now `HWND_TOP` (top of non-topmost band) instead of `HWND_TOPMOST` — overlay still renders above other normal windows, but no longer hardcoded above the user's foreground.
Expected Z-order after fix
For the user's repro (terminal foreground, Calculator pinned):
```
top terminal ← user's foreground (non-topmost)
overlay ← just above Calculator, below terminal
calculator ← pinned target
...
bottom
```
Verification
🤖 Generated with Claude Code
Summary by CodeRabbit