Simplify input handling (#3282)

This PR takes a different approach to input handling.

Rather than returning the optional input handler, focus handle pair from
the element trait, we instead allow you to register an input handler
imperatively on the window context with `WindowContext::handle_input`.
You pass a focus handle reference and any implementer of
`PlatformInputHandler`. There's an `ElementInputHandler<V>` that
implements `PlatformWindowHandler` so long as `V` implements
`InputHandler`.

Release Notes:

- N/A
This commit is contained in:
Nathan Sobo 2023-11-08 22:27:36 -07:00 committed by GitHub
commit 8c44f6a814
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 145 additions and 218 deletions

View file

@ -211,7 +211,6 @@ pub struct Window {
default_prevented: bool,
mouse_position: Point<Pixels>,
requested_cursor_style: Option<CursorStyle>,
pub(crate) requested_input_handler: Option<Box<dyn PlatformInputHandler>>,
scale_factor: f32,
bounds: WindowBounds,
bounds_observers: SubscriberSet<(), AnyObserver>,
@ -311,7 +310,6 @@ impl Window {
default_prevented: true,
mouse_position,
requested_cursor_style: None,
requested_input_handler: None,
scale_factor,
bounds,
bounds_observers: SubscriberSet::new(),
@ -1052,9 +1050,6 @@ impl<'a> WindowContext<'a> {
.take()
.unwrap_or(CursorStyle::Arrow);
self.platform.set_cursor_style(cursor_style);
if let Some(handler) = self.window.requested_input_handler.take() {
self.window.platform_window.set_input_handler(handler);
}
self.window.dirty = false;
}
@ -2182,6 +2177,21 @@ impl<'a, V: 'static> ViewContext<'a, V> {
})
});
}
/// Set an input handler, such as [ElementInputHandler], which interfaces with the
/// platform to receive textual input with proper integration with concerns such
/// as IME interactions.
pub fn handle_input(
&mut self,
focus_handle: &FocusHandle,
input_handler: impl PlatformInputHandler,
) {
if focus_handle.is_focused(self) {
self.window
.platform_window
.set_input_handler(Box::new(input_handler));
}
}
}
impl<V> ViewContext<'_, V> {