-
Notifications
You must be signed in to change notification settings - Fork 920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for event::Touch
on web targets
#1945
Changes from 8 commits
3ca9afc
edc3c76
994b5f7
db5bfe4
5927424
5ea5760
cfb7e67
b88bd95
1dff7c4
424c05f
8ead14f
20886a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
use super::{super::monitor, backend, device, proxy::Proxy, runner, window}; | ||||||
use crate::dpi::{PhysicalSize, Size}; | ||||||
use crate::event::{ | ||||||
DeviceEvent, DeviceId, ElementState, Event, KeyboardInput, TouchPhase, WindowEvent, | ||||||
DeviceEvent, DeviceId, ElementState, Event, KeyboardInput, Touch, TouchPhase, WindowEvent, | ||||||
}; | ||||||
use crate::event_loop::ControlFlow; | ||||||
use crate::monitor::MonitorHandle as RootMH; | ||||||
|
@@ -231,6 +231,62 @@ impl<T> WindowTarget<T> { | |||||
runner.request_redraw(WindowId(id)); | ||||||
}); | ||||||
|
||||||
let runner = self.runner.clone(); | ||||||
canvas.on_pointer_move(move |device_id, location| { | ||||||
runner.send_event(Event::WindowEvent { | ||||||
window_id: WindowId(id), | ||||||
event: WindowEvent::Touch(Touch { | ||||||
id: device_id as u64, | ||||||
device_id: DeviceId(device::Id(device_id)), | ||||||
phase: TouchPhase::Moved, | ||||||
force: None, // Todo | ||||||
dsluijk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
location, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
|
||||||
let runner = self.runner.clone(); | ||||||
canvas.on_pointer_down(move |device_id, location| { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
runner.send_event(Event::WindowEvent { | ||||||
window_id: WindowId(id), | ||||||
event: WindowEvent::Touch(Touch { | ||||||
id: device_id as u64, | ||||||
device_id: DeviceId(device::Id(device_id)), | ||||||
phase: TouchPhase::Started, | ||||||
force: None, // Todo | ||||||
location, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
|
||||||
let runner = self.runner.clone(); | ||||||
canvas.on_pointer_up(move |device_id, location| { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
runner.send_event(Event::WindowEvent { | ||||||
window_id: WindowId(id), | ||||||
event: WindowEvent::Touch(Touch { | ||||||
id: device_id as u64, | ||||||
device_id: DeviceId(device::Id(device_id)), | ||||||
phase: TouchPhase::Ended, | ||||||
force: None, // Todo | ||||||
location, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
|
||||||
let runner = self.runner.clone(); | ||||||
canvas.on_pointer_cancel(move |device_id, location| { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
runner.send_event(Event::WindowEvent { | ||||||
window_id: WindowId(id), | ||||||
event: WindowEvent::Touch(Touch { | ||||||
id: device_id as u64, | ||||||
device_id: DeviceId(device::Id(device_id)), | ||||||
phase: TouchPhase::Cancelled, | ||||||
force: None, // Todo | ||||||
location, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
|
||||||
let runner = self.runner.clone(); | ||||||
canvas.on_dark_mode(move |is_dark_mode| { | ||||||
let theme = if is_dark_mode { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -247,6 +247,46 @@ impl Canvas { | |||||
} | ||||||
} | ||||||
|
||||||
pub fn on_pointer_move<F>(&mut self, handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
match &mut self.mouse_state { | ||||||
MouseState::HasPointerEvent(h) => h.on_pointer_move(&self.common, handler), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
_ => {} | ||||||
} | ||||||
} | ||||||
|
||||||
pub fn on_pointer_down<F>(&mut self, handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
match &mut self.mouse_state { | ||||||
MouseState::HasPointerEvent(h) => h.on_pointer_down(&self.common, handler), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
_ => {} | ||||||
} | ||||||
} | ||||||
|
||||||
pub fn on_pointer_up<F>(&mut self, handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
match &mut self.mouse_state { | ||||||
MouseState::HasPointerEvent(h) => h.on_pointer_up(&self.common, handler), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
_ => {} | ||||||
} | ||||||
} | ||||||
|
||||||
pub fn on_pointer_cancel<F>(&mut self, handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
match &mut self.mouse_state { | ||||||
MouseState::HasPointerEvent(h) => h.on_pointer_cancel(&self.common, handler), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
_ => {} | ||||||
} | ||||||
} | ||||||
|
||||||
pub fn on_mouse_wheel<F>(&mut self, mut handler: F) | ||||||
where | ||||||
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState), | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,10 @@ pub(super) struct PointerHandler { | |||||
on_cursor_move: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
on_pointer_press: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
on_pointer_release: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
on_pointer_move: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
on_pointer_down: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
on_pointer_up: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
on_pointer_cancel: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>, | ||||||
} | ||||||
|
||||||
impl PointerHandler { | ||||||
|
@@ -22,6 +26,10 @@ impl PointerHandler { | |||||
on_cursor_move: None, | ||||||
on_pointer_press: None, | ||||||
on_pointer_release: None, | ||||||
on_pointer_move: None, | ||||||
on_pointer_down: None, | ||||||
on_pointer_up: None, | ||||||
on_pointer_cancel: None, | ||||||
} | ||||||
dsluijk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
|
@@ -103,11 +111,91 @@ impl PointerHandler { | |||||
)); | ||||||
} | ||||||
|
||||||
pub fn on_pointer_move<F>(&mut self, canvas_common: &super::Common, mut handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
self.on_pointer_move = Some(canvas_common.add_event( | ||||||
"pointermove", | ||||||
move |event: PointerEvent| { | ||||||
handler( | ||||||
event.pointer_id(), | ||||||
dsluijk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
PhysicalPosition { | ||||||
x: event.offset_x() as f64, | ||||||
y: event.offset_y() as f64, | ||||||
}, | ||||||
); | ||||||
}, | ||||||
)); | ||||||
} | ||||||
|
||||||
pub fn on_pointer_down<F>(&mut self, canvas_common: &super::Common, mut handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
let canvas = canvas_common.raw.clone(); | ||||||
self.on_pointer_down = Some(canvas_common.add_event( | ||||||
"pointerdown", | ||||||
move |event: PointerEvent| { | ||||||
handler( | ||||||
event.pointer_id(), | ||||||
PhysicalPosition { | ||||||
x: event.offset_x() as f64, | ||||||
y: event.offset_y() as f64, | ||||||
}, | ||||||
); | ||||||
canvas | ||||||
.set_pointer_capture(event.pointer_id()) | ||||||
.expect("Failed to set pointer capture"); | ||||||
dsluijk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}, | ||||||
)); | ||||||
} | ||||||
|
||||||
pub fn on_pointer_up<F>(&mut self, canvas_common: &super::Common, mut handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
self.on_pointer_up = Some(canvas_common.add_event( | ||||||
"pointerup", | ||||||
move |event: PointerEvent| { | ||||||
handler( | ||||||
event.pointer_id(), | ||||||
PhysicalPosition { | ||||||
x: event.offset_x() as f64, | ||||||
y: event.offset_y() as f64, | ||||||
}, | ||||||
); | ||||||
}, | ||||||
)); | ||||||
} | ||||||
|
||||||
pub fn on_pointer_cancel<F>(&mut self, canvas_common: &super::Common, mut handler: F) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
where | ||||||
F: 'static + FnMut(i32, PhysicalPosition<f64>), | ||||||
{ | ||||||
self.on_pointer_cancel = Some(canvas_common.add_event( | ||||||
"pointercancel", | ||||||
move |event: PointerEvent| { | ||||||
handler( | ||||||
event.pointer_id(), | ||||||
PhysicalPosition { | ||||||
x: event.offset_x() as f64, | ||||||
y: event.offset_y() as f64, | ||||||
}, | ||||||
); | ||||||
}, | ||||||
)); | ||||||
} | ||||||
|
||||||
pub fn remove_listeners(&mut self) { | ||||||
self.on_cursor_leave = None; | ||||||
self.on_cursor_enter = None; | ||||||
self.on_cursor_move = None; | ||||||
self.on_pointer_press = None; | ||||||
self.on_pointer_release = None; | ||||||
self.on_pointer_move = None; | ||||||
self.on_pointer_down = None; | ||||||
self.on_pointer_up = None; | ||||||
self.on_pointer_cancel = None; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.