[PR #6410] [MERGED] Lock the pointer again when the window regains focus #6852

Closed
opened 2026-11-03 06:28:07 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6410
Author: @MSedgeMC
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @ludovic111

Base: main ← Head: fix/ui-mouse-capture


📝 Commits (1)

  • e7d70fb Lock the pointer again when the window regains focus

📊 Changes

2 files changed (+98 additions, -29 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+50 -24)
📝 tests/test_world_input_capture.py (+48 -5)

📄 Description

Fixes #6259.

Vanilla. The pointer grab belongs to the game, not to the window's focus. MouseHandler.grabMouse sets mouseGrabbed and the disabled cursor mode and puts xpos/ypos at the window's centre; nothing releases either when the window loses focus (releaseMouse runs only when a screen opens), so when the focus returns the cursor mode is still disabled and the pointer is locked and centred again without a click. MouseHandler.onMove ignores movement while !isWindowActive. The click path is only the fallback: MouseHandler.onButton with no screen open and !mouseGrabbed calls grabMouse and returns, so that click never reaches the world.

Horizon. WindowEvent::Focused(false) released the pointer (capture(false): ungrabs the cursor, shows it) and nothing took it back. winit's Windows backend applies a cursor clip only while the window is focused (refresh_os_cursor: if util::is_focused(window)) and does not re-apply one on WM_SETFOCUS, so the grab was gone: after alt-tabbing out and back the pointer stayed wherever the system left it, usually outside the window, and clicks went to whatever was under it. Screen::Playing's press-to-grab path was already there, but that is vanilla's fallback: with the window focused and the pointer free the first click reached the world instead of locking the pointer.

The window now remembers whether it held the pointer when the focus went and takes it back on the focus return, through the same capture(true) the rest of the game uses: the pointer goes to the window's centre, the cursor is hidden and the first move is ignored. A screen that is open keeps the pointer free, and the press-to-grab path stays.

Proven:

  • cargo test -q -p horizon-client --lib (build box): 582 passed; 0 failed; 4 ignored, so the crate compiles with the change and its tests still pass.
  • python3 tests/test_world_input_capture.py -v (build box): Ran 2 tests ... OK. The new block in that test drives capture, capture_loaded_world and regrab_on_refocus together: a window that held the pointer grabs again on the focus return (one grab attempt, the pointer at the window's centre, ignore_first_move set), a focus return after a screen's release grabs nothing, an open screen keeps the pointer free, and so does the server pack prompt.

Not proven:

  • Native delivery on Windows. The capture test's fixture substitutes winit and the window, so it proves the grab is asked for, not that the system applies the clip. The re-grab is expected to work because winit's set_cursor_grab(Locked) runs refresh_os_cursor on every call and applies the clip while the window is focused; that was read in winit 0.30.13's window_state.rs, not measured on a Windows desktop.
  • The reporter's exact sequence (focus back, pointer outside, click) on Windows: a manual check.

Known limits:

  • The pointer jumps to the window's centre when the focus returns, as MouseHandler.grabMouse puts it there.
  • If the system refuses the grab, the existing press-to-grab path locks the pointer on the next click, as vanilla does.
  • With --control the pointer is not released on focus loss, so the re-grab only re-applies the clip.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/6410 **Author:** [@MSedgeMC](https://github.com/MSedgeMC) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/ui-mouse-capture` --- ### 📝 Commits (1) - [`e7d70fb`](https://github.com/horizon-rust/horizon/commit/e7d70fbe7c31590d1f69a614d8ae892d5f2efc3c) Lock the pointer again when the window regains focus ### 📊 Changes **2 files changed** (+98 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+50 -24) 📝 `tests/test_world_input_capture.py` (+48 -5) </details> ### 📄 Description Fixes #6259. **Vanilla.** The pointer grab belongs to the game, not to the window's focus. `MouseHandler.grabMouse` sets `mouseGrabbed` and the disabled cursor mode and puts `xpos`/`ypos` at the window's centre; nothing releases either when the window loses focus (`releaseMouse` runs only when a screen opens), so when the focus returns the cursor mode is still disabled and the pointer is locked and centred again without a click. `MouseHandler.onMove` ignores movement while `!isWindowActive`. The click path is only the fallback: `MouseHandler.onButton` with no screen open and `!mouseGrabbed` calls `grabMouse` and returns, so that click never reaches the world. **Horizon.** `WindowEvent::Focused(false)` released the pointer (`capture(false)`: ungrabs the cursor, shows it) and nothing took it back. winit's Windows backend applies a cursor clip only while the window is focused (`refresh_os_cursor`: `if util::is_focused(window)`) and does not re-apply one on `WM_SETFOCUS`, so the grab was gone: after alt-tabbing out and back the pointer stayed wherever the system left it, usually outside the window, and clicks went to whatever was under it. `Screen::Playing`'s press-to-grab path was already there, but that is vanilla's fallback: with the window focused and the pointer free the first click reached the world instead of locking the pointer. The window now remembers whether it held the pointer when the focus went and takes it back on the focus return, through the same `capture(true)` the rest of the game uses: the pointer goes to the window's centre, the cursor is hidden and the first move is ignored. A screen that is open keeps the pointer free, and the press-to-grab path stays. **Proven:** - `cargo test -q -p horizon-client --lib` (build box): `582 passed; 0 failed; 4 ignored`, so the crate compiles with the change and its tests still pass. - `python3 tests/test_world_input_capture.py -v` (build box): `Ran 2 tests ... OK`. The new block in that test drives `capture`, `capture_loaded_world` and `regrab_on_refocus` together: a window that held the pointer grabs again on the focus return (one grab attempt, the pointer at the window's centre, `ignore_first_move` set), a focus return after a screen's release grabs nothing, an open screen keeps the pointer free, and so does the server pack prompt. **Not proven:** - Native delivery on Windows. The capture test's fixture substitutes winit and the window, so it proves the grab is asked for, not that the system applies the clip. The re-grab is expected to work because winit's `set_cursor_grab(Locked)` runs `refresh_os_cursor` on every call and applies the clip while the window is focused; that was read in winit 0.30.13's `window_state.rs`, not measured on a Windows desktop. - The reporter's exact sequence (focus back, pointer outside, click) on Windows: a manual check. **Known limits:** - The pointer jumps to the window's centre when the focus returns, as `MouseHandler.grabMouse` puts it there. - If the system refuses the grab, the existing press-to-grab path locks the pointer on the next click, as vanilla does. - With `--control` the pointer is not released on focus loss, so the re-grab only re-applies the clip. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:28:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#6852
No description provided.