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 Web, fix pen treated as mouse input.
- On Web, send mouse position on button release as well.
- On Web, fix touch input not gaining or loosing focus.
# 0.28.6

View file

@ -345,58 +345,73 @@ impl<T> EventLoopWindowTarget<T> {
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(
move |pointer_id, position, button, active_modifiers| {
let focus_changed =
(!has_focus_clone.replace(true)).then_some(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
{
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 {
window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
// 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([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
position,
// 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([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
position,
},
},
},
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseInput {
device_id: RootDeviceId(DeviceId(pointer_id)),
state: ElementState::Pressed,
button,
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseInput {
device_id: RootDeviceId(DeviceId(pointer_id)),
state: ElementState::Pressed,
button,
},
},
},
]));
]));
}
},
move |device_id, location, force| {
runner_touch.send_event(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,
}),
});
{
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 {
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,
}),
},
)));
}
},
);