From d52c5646b451ae27475b3ebb3db0e3e5d772cc56 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 8 Nov 2023 22:03:26 -0700 Subject: [PATCH] Add docs --- crates/gpui2/src/input.rs | 8 ++++++++ crates/gpui2/src/window.rs | 3 +++ 2 files changed, 11 insertions(+) diff --git a/crates/gpui2/src/input.rs b/crates/gpui2/src/input.rs index 8d9e9b01ad..d768ce946a 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 b72793b998..29980a486b 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,