This commit is contained in:
Nathan Sobo 2023-11-08 22:03:26 -07:00
parent 7c922ad6ee
commit d52c5646b4
2 changed files with 11 additions and 0 deletions

View file

@ -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<V>].
/// 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<usize>, cx: &mut ViewContext<Self>)
-> Option<String>;
@ -28,6 +32,8 @@ pub trait InputHandler: 'static + Sized {
) -> Option<Bounds<Pixels>>;
}
/// The canonical implementation of `PlatformInputHandler`. Call `WindowContext::handle_input`
/// with an instance during your element's paint.
pub struct ElementInputHandler<V> {
view: View<V>,
element_bounds: Bounds<Pixels>,
@ -35,6 +41,8 @@ pub struct ElementInputHandler<V> {
}
impl<V: 'static> ElementInputHandler<V> {
/// Used in [Element::paint] with the element's bounds and a view context for its
/// containing view.
pub fn new(element_bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) -> Self {
ElementInputHandler {
view: cx.view(),

View file

@ -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,