diff --git a/crates/gpui2/src/input.rs b/crates/gpui2/src/input.rs index 8d9e9b01ad6626c57dc2eb940527e00d52f2a5b9..d768ce946a157cbb0e7015cc10afa922e1f1e844 100644 --- a/crates/gpui2/src/input.rs +++ b/crates/gpui2/src/input.rs @@ -1,6 +1,10 @@ use crate::{AsyncWindowContext, Bounds, Pixels, PlatformInputHandler, View, ViewContext}; use std::ops::Range; +/// Implement this trait to allow views to handle textual input when implementing an editor, field, etc. +/// +/// Once your view `V` implements this trait, you can use it to construct an [ElementInputHandler]. +/// This input handler can then be assigned during paint by calling [WindowContext::handle_input]. pub trait InputHandler: 'static + Sized { fn text_for_range(&mut self, range: Range, cx: &mut ViewContext) -> Option; @@ -28,6 +32,8 @@ pub trait InputHandler: 'static + Sized { ) -> Option>; } +/// The canonical implementation of `PlatformInputHandler`. Call `WindowContext::handle_input` +/// with an instance during your element's paint. pub struct ElementInputHandler { view: View, element_bounds: Bounds, @@ -35,6 +41,8 @@ pub struct ElementInputHandler { } impl ElementInputHandler { + /// Used in [Element::paint] with the element's bounds and a view context for its + /// containing view. pub fn new(element_bounds: Bounds, cx: &mut ViewContext) -> Self { ElementInputHandler { view: cx.view(), diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index b72793b99869f32c4131b67e608b62b57c433d87..29980a486b7c6ed9977efe6931d5fab41cd3f73b 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -2170,6 +2170,9 @@ 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,