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, fix touch input not gaining or loosing focus.
- **Breaking:** On Web, dropped support for Safari versions below 13.
- On Web, prevent clicks on the canvas to select text.
# 0.28.6

View file

@ -349,14 +349,8 @@ impl<T> EventLoopWindowTarget<T> {
{
let runner = self.runner.clone();
let modifiers = self.modifiers.clone();
let has_focus = has_focus.clone();
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(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
@ -368,7 +362,7 @@ impl<T> EventLoopWindowTarget<T> {
// A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// 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 {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
@ -389,28 +383,21 @@ impl<T> EventLoopWindowTarget<T> {
},
{
let runner = self.runner.clone();
let has_focus = has_focus.clone();
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),
event: WindowEvent::Focused(true),
});
runner.send_events(focus_changed.into_iter().chain(iter::once(
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
phase: TouchPhase::Started,
force: Some(force),
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(

View file

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

View file

@ -104,6 +104,7 @@ impl PointerHandler {
canvas_common: &Common,
mut mouse_handler: M,
mut touch_handler: T,
prevent_default: bool,
) where
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
@ -112,6 +113,13 @@ impl PointerHandler {
self.on_pointer_press = Some(canvas_common.add_user_event(
"pointerdown",
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() {
"touch" => {
touch_handler(
@ -192,6 +200,13 @@ impl PointerHandler {
"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(
id,
event::mouse_position(&event).to_physical(super::scale_factor()),