Skip to content

Commit

Permalink
Hide cursor when turning knob
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed May 8, 2023
1 parent 0140f67 commit bfdb923
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/views/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
cx.touches[*id] = vid;
cx.starts[*id] = *position;
cx.previous_position[*id] = *position;
cx.grab_cursor = true;
cx.grab_cursor = self.grab;
}
}
Event::TouchMove { id, position } => {
Expand Down Expand Up @@ -178,6 +178,7 @@ where
cx.touches[*id] = vid;
cx.starts[*id] = *position;
cx.previous_position[*id] = *position;
cx.grab_cursor = self.grab;
}
}
Event::TouchMove { id, position } => {
Expand All @@ -196,6 +197,7 @@ where
Event::TouchEnd { id, position } => {
if cx.touches[*id] == vid {
cx.touches[*id] = ViewId::default();
cx.grab_cursor = false;
let delta = *position - cx.previous_position[*id];
let button = cx.mouse_button;
actions.push(Box::new((self.func)(
Expand Down
2 changes: 1 addition & 1 deletion src/views/knob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn knob(value: impl Binding<f32>) -> impl View {
.color(CLEAR_COLOR)
.drag_s(value, move |v, delta, _, _| {
*v = (*v + (delta.x + delta.y) / 400.0).clamp(0.0, 1.0)
}),
}).grab_cursor(),
canvas(move |cx, sz, vger| {
let c = sz.center();
let r = sz.width().min(sz.height()) / 2.0;
Expand Down
22 changes: 18 additions & 4 deletions src/winit_event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ 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();
window.set_cursor_visible(false);
} else {
// window.set_cursor_grab(winit::window::CursorGrabMode::None).unwrap();
window.set_cursor_visible(true);
}
}

/// Call this function to run your UI.
pub fn rui(view: impl View) {
let event_loop = EventLoop::new();
Expand Down Expand Up @@ -270,15 +284,15 @@ pub fn rui(view: impl View) {
id: 0,
position: mouse_position,
};
cx.process(&view, &event)
process_event(&mut cx, &view, &event, &window)
}
ElementState::Released => {
cx.mouse_button = None;
let event = Event::TouchEnd {
id: 0,
position: mouse_position,
};
cx.process(&view, &event)
process_event(&mut cx, &view, &event, &window)
}
};
}
Expand Down Expand Up @@ -312,7 +326,7 @@ pub fn rui(view: impl View) {
};

if let Some(event) = event {
cx.process(&view, &event);
process_event(&mut cx, &view, &event, &window);
}
}
WEvent::WindowEvent {
Expand All @@ -329,7 +343,7 @@ pub fn rui(view: impl View) {
id: 0,
position: mouse_position,
};
cx.process(&view, &event)
process_event(&mut cx, &view, &event, &window)
}

WEvent::WindowEvent {
Expand Down

0 comments on commit bfdb923

Please sign in to comment.