diff --git a/CHANGELOG.md b/CHANGELOG.md index b4e3b1f9..deb0d492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index ff76d356..3e52e63f 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -345,58 +345,73 @@ impl EventLoopWindowTarget { 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, + }), + }, + ))); + } }, );