rust-windowing / rust-windowing/winit

cursor grab no longer grabbed when mouse leave and enter

Open
#242 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

B - bug D - easy DS - x11 H - help wanted P - normal
Dominant language
Rust
Stars
6.2k
Forks
1.3k
Avg merge
2d 19h
Merged PRs (30d)
9

Description

On X11 using Xmonad window manager:

minimal program: a winit window with cursor state set to grab:

fn main() {
    let mut events_loop = winit::EventsLoop::new();
    let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
    window.set_cursor_state(winit::CursorState::Grab).unwrap();
    events_loop.run_forever(|event| {
        ControlFlow::Continue
    });
}

issue:
I run the program. (cursor is grabbed)
I leave the window with xmonad key.
I enter the window: cursor is not longer grabbed

to solve this issue and grab the cursor again I wrote: set cursor grab on mouse entered

fn main() {
    let mut events_loop = winit::EventsLoop::new();
    let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
    window.set_cursor_state(winit::CursorState::Grab).unwrap();
    events_loop.run_forever(|event| {
        match event {
            winit::Event::WindowEvent { event: winit::WindowEvent::MouseEntered { .. }, .. } => window.set_cursor_state(winit::CursorState::Grab).unwrap(),
            _ => (),
        }
        ControlFlow::Continue
    });
}

That didn't solve anything. But then I wrote: set cursor state to normal and then to grab on mouse entered
and it works (cursor is grabbed again):

fn main() {
    let mut events_loop = winit::EventsLoop::new();
    let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
    window.set_cursor_state(winit::CursorState::Grab).unwrap();
    events_loop.run_forever(|event| {
        match event {
            winit::Event::WindowEvent { event: winit::WindowEvent::MouseEntered { .. }, .. } => {
                window.set_cursor_state(winit::CursorState::Normal).unwrap();
                window.set_cursor_state(winit::CursorState::Grab).unwrap();
            },
            _ => (),
        }
        ControlFlow::Continue
    });
}

I think this is an issue and winit should grab the cursor again automatically on mouse entered.
If so I can look to make a PR.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Reproduce the minimal Rust program under X11 with Xmonad, then trace the cursor-grab handling around mouse leave and MouseEntered events. Done means a window configured with Grab automatically regains the grab after leaving and re-entering without the Normal-then-Grab workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
linux, rust
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.