Focus window on touch press

This commit is contained in:
dAxpeDDa 2023-06-04 01:09:30 +02:00 committed by daxpedda
parent 61bd8b8254
commit fbba203c4a
2 changed files with 62 additions and 46 deletions

View file

@ -64,6 +64,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Windows, port to `windows-sys` version 0.48.0. - On Windows, port to `windows-sys` version 0.48.0.
- On Web, fix pen treated as mouse input. - On Web, fix pen treated as mouse input.
- 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.
# 0.28.6 # 0.28.6

View file

@ -345,58 +345,73 @@ impl<T> EventLoopWindowTarget<T> {
prevent_default, prevent_default,
); );
let runner = self.runner.clone();
let runner_touch = self.runner.clone();
let modifiers = self.modifiers.clone();
let has_focus_clone = has_focus.clone();
canvas.on_mouse_press( canvas.on_mouse_press(
move |pointer_id, position, button, active_modifiers| { {
let focus_changed = let runner = self.runner.clone();
(!has_focus_clone.replace(true)).then_some(Event::WindowEvent { let modifiers = self.modifiers.clone();
window_id: RootWindowId(id), let has_focus = has_focus.clone();
event: WindowEvent::Focused(true),
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 {
window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
}); });
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| { // A mouse down event may come in without any prior CursorMoved events,
modifiers.set(active_modifiers); // therefore we should send a CursorMoved event to make sure that the
Event::WindowEvent { // user code has the correct cursor position.
window_id: RootWindowId(id), runner.send_events(focus_changed.into_iter().chain(modifiers_changed).chain([
event: WindowEvent::ModifiersChanged(active_modifiers.into()), Event::WindowEvent {
} window_id: RootWindowId(id),
}); event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
// A mouse down event may come in without any prior CursorMoved events, position,
// 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([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
position,
}, },
}, Event::WindowEvent {
Event::WindowEvent { window_id: RootWindowId(id),
window_id: RootWindowId(id), event: WindowEvent::MouseInput {
event: WindowEvent::MouseInput { device_id: RootDeviceId(DeviceId(pointer_id)),
device_id: RootDeviceId(DeviceId(pointer_id)), state: ElementState::Pressed,
state: ElementState::Pressed, button,
button, },
}, },
}, ]));
])); }
}, },
move |device_id, location, force| { {
runner_touch.send_event(Event::WindowEvent { let runner = self.runner.clone();
window_id: RootWindowId(id), let has_focus = has_focus.clone();
event: WindowEvent::Touch(Touch {
id: device_id as u64, move |device_id, location, force| {
device_id: RootDeviceId(DeviceId(device_id)), let focus_changed =
phase: TouchPhase::Started, (!has_focus.replace(true)).then_some(Event::WindowEvent {
force: Some(force), window_id: RootWindowId(id),
location, 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,
}),
},
)));
}
}, },
); );