Prevent text selection

This commit is contained in:
dAxpeDDa 2023-06-04 01:50:30 +02:00 committed by daxpedda
parent a134a59917
commit 964e342f69
4 changed files with 39 additions and 28 deletions

View file

@ -66,6 +66,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Web, send mouse position on button release as well. - On Web, send mouse position on button release as well.
- On Web, fix touch input not gaining or loosing focus. - On Web, fix touch input not gaining or loosing focus.
- **Breaking:** On Web, dropped support for Safari versions below 13. - **Breaking:** On Web, dropped support for Safari versions below 13.
- On Web, prevent clicks on the canvas to select text.
# 0.28.6 # 0.28.6

View file

@ -349,14 +349,8 @@ impl<T> EventLoopWindowTarget<T> {
{ {
let runner = self.runner.clone(); let runner = self.runner.clone();
let modifiers = self.modifiers.clone(); let modifiers = self.modifiers.clone();
let has_focus = has_focus.clone();
move |pointer_id, position, button, active_modifiers| { move |pointer_id, position, button, active_modifiers| {
let focus_changed = (!has_focus.replace(true)).then_some(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
});
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| { let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers); modifiers.set(active_modifiers);
Event::WindowEvent { Event::WindowEvent {
@ -368,7 +362,7 @@ impl<T> EventLoopWindowTarget<T> {
// A mouse down event may come in without any prior CursorMoved events, // A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the // therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position. // user code has the correct cursor position.
runner.send_events(focus_changed.into_iter().chain(modifiers_changed).chain([ runner.send_events(modifiers_changed.into_iter().chain([
Event::WindowEvent { Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
event: WindowEvent::CursorMoved { event: WindowEvent::CursorMoved {
@ -389,28 +383,21 @@ impl<T> EventLoopWindowTarget<T> {
}, },
{ {
let runner = self.runner.clone(); let runner = self.runner.clone();
let has_focus = has_focus.clone();
move |device_id, location, force| { move |device_id, location, force| {
let focus_changed = (!has_focus.replace(true)).then_some(Event::WindowEvent { runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
event: WindowEvent::Focused(true), event: WindowEvent::Touch(Touch {
}); id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
runner.send_events(focus_changed.into_iter().chain(iter::once( phase: TouchPhase::Started,
Event::WindowEvent { force: Some(force),
window_id: RootWindowId(id), location,
event: WindowEvent::Touch(Touch { }),
id: device_id as u64, })
device_id: RootDeviceId(DeviceId(device_id)),
phase: TouchPhase::Started,
force: Some(force),
location,
}),
},
)));
} }
}, },
prevent_default,
); );
canvas.on_mouse_release( canvas.on_mouse_release(

View file

@ -235,13 +235,21 @@ impl Canvas {
.on_mouse_release(&self.common, mouse_handler, touch_handler) .on_mouse_release(&self.common, mouse_handler, touch_handler)
} }
pub fn on_mouse_press<M, T>(&mut self, mouse_handler: M, touch_handler: T) pub fn on_mouse_press<M, T>(
where &mut self,
mouse_handler: M,
touch_handler: T,
prevent_default: bool,
) where
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState), M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{ {
self.pointer_handler self.pointer_handler.on_mouse_press(
.on_mouse_press(&self.common, mouse_handler, touch_handler) &self.common,
mouse_handler,
touch_handler,
prevent_default,
)
} }
pub fn on_cursor_move<MOD, M, T, B>( pub fn on_cursor_move<MOD, M, T, B>(

View file

@ -104,6 +104,7 @@ impl PointerHandler {
canvas_common: &Common, canvas_common: &Common,
mut mouse_handler: M, mut mouse_handler: M,
mut touch_handler: T, mut touch_handler: T,
prevent_default: bool,
) where ) where
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState), M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
@ -112,6 +113,13 @@ impl PointerHandler {
self.on_pointer_press = Some(canvas_common.add_user_event( self.on_pointer_press = Some(canvas_common.add_user_event(
"pointerdown", "pointerdown",
move |event: PointerEvent| { move |event: PointerEvent| {
if prevent_default {
// prevent text selection
event.prevent_default();
// but still focus element
let _ = canvas.focus();
}
match event.pointer_type().as_str() { match event.pointer_type().as_str() {
"touch" => { "touch" => {
touch_handler( touch_handler(
@ -192,6 +200,13 @@ impl PointerHandler {
"expect pointer type of a chorded button event to be a mouse" "expect pointer type of a chorded button event to be a mouse"
); );
if prevent_default {
// prevent text selection
event.prevent_default();
// but still focus element
let _ = canvas.focus();
}
button_handler( button_handler(
id, id,
event::mouse_position(&event).to_physical(super::scale_factor()), event::mouse_position(&event).to_physical(super::scale_factor()),