Skip to content

Commit

Permalink
Try to grab cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed May 9, 2023
1 parent 673b4a6 commit 3ffbf67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pub struct Context {

/// Lock the cursor in position. Useful for dragging knobs.
pub(crate) grab_cursor: bool,

/// Value of grab_cursor before processing event.
pub(crate) prev_grab_cursor: bool,
}

impl Default for Context {
Expand Down Expand Up @@ -147,6 +150,7 @@ impl Context {
render_dirty: false,
access_node_classes: accesskit::NodeClassSet::default(),
grab_cursor: false,
prev_grab_cursor: false,
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/winit_event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,21 @@ async fn setup(window: &Window) -> Setup {
fn process_event(cx: &mut Context, view: &impl View, event: &Event, window: &Window) {
cx.process(view, event);

if cx.grab_cursor {
//window.set_cursor_grab(winit::window::CursorGrabMode::Confined)
// .or_else(|_e| window.set_cursor_grab(winit::window::CursorGrabMode::Locked))
// .unwrap();
if cx.grab_cursor && !cx.prev_grab_cursor {
println!("grabbing cursor");
window.set_cursor_grab(winit::window::CursorGrabMode::Confined)
.or_else(|_e| window.set_cursor_grab(winit::window::CursorGrabMode::Locked))
.unwrap();
window.set_cursor_visible(false);
} else {
// window.set_cursor_grab(winit::window::CursorGrabMode::None).unwrap();
}

if !cx.grab_cursor && cx.prev_grab_cursor {
println!("releasing cursor");
window.set_cursor_grab(winit::window::CursorGrabMode::None).unwrap();
window.set_cursor_visible(true);
}

cx.prev_grab_cursor = cx.grab_cursor;
}

/// Call this function to run your UI.
Expand Down

0 comments on commit 3ffbf67

Please sign in to comment.