use crate::{App, Bounds, Context, Entity, InputHandler, Pixels, UTF16Selection, Window}; use std::ops::Range; /// Implement this trait to allow views to handle textual input when implementing an editor, field, etc. /// /// Once your view 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`]. /// /// See [`InputHandler`] for details on how to implement each method. pub trait EntityInputHandler: 'static + Sized { /// See [`InputHandler::text_for_range`] for details fn text_for_range( &mut self, range: Range, adjusted_range: &mut Option>, window: &mut Window, cx: &mut Context, ) -> Option; /// See [`InputHandler::selected_text_range`] for details fn selected_text_range( &mut self, ignore_disabled_input: bool, window: &mut Window, cx: &mut Context, ) -> Option; /// See [`InputHandler::marked_text_range`] for details fn marked_text_range( &self, window: &mut Window, cx: &mut Context, ) -> Option>; /// See [`InputHandler::unmark_text`] for details fn unmark_text(&mut self, window: &mut Window, cx: &mut Context); /// See [`InputHandler::replace_text_in_range`] for details fn replace_text_in_range( &mut self, range: Option>, text: &str, window: &mut Window, cx: &mut Context, ); /// See [`InputHandler::replace_and_mark_text_in_range`] for details fn replace_and_mark_text_in_range( &mut self, range: Option>, new_text: &str, new_selected_range: Option>, window: &mut Window, cx: &mut Context, ); /// See [`InputHandler::bounds_for_range`] for details fn bounds_for_range( &mut self, range_utf16: Range, element_bounds: Bounds, window: &mut Window, cx: &mut Context, ) -> Option>; } /// The canonical implementation of [`PlatformInputHandler`]. Call [`WindowContext::handle_input`] /// with an instance during your element's paint. pub struct ElementInputHandler { view: Entity, element_bounds: Bounds, } impl ElementInputHandler { /// Used in [`Element::paint`][element_paint] with the element's bounds and a view context for its /// containing view. /// /// [element_paint]: crate::Element::paint pub fn new(element_bounds: Bounds, view: Entity) -> Self { ElementInputHandler { view, element_bounds, } } } impl InputHandler for ElementInputHandler { fn selected_text_range( &mut self, ignore_disabled_input: bool, window: &mut Window, cx: &mut App, ) -> Option { self.view.update(cx, |view, cx| { view.selected_text_range(ignore_disabled_input, window, cx) }) } fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option> { self.view .update(cx, |view, cx| view.marked_text_range(window, cx)) } fn text_for_range( &mut self, range_utf16: Range, adjusted_range: &mut Option>, window: &mut Window, cx: &mut App, ) -> Option { self.view.update(cx, |view, cx| { view.text_for_range(range_utf16, adjusted_range, window, cx) }) } fn replace_text_in_range( &mut self, replacement_range: Option>, text: &str, window: &mut Window, cx: &mut App, ) { self.view.update(cx, |view, cx| { view.replace_text_in_range(replacement_range, text, window, cx) }); } fn replace_and_mark_text_in_range( &mut self, range_utf16: Option>, new_text: &str, new_selected_range: Option>, window: &mut Window, cx: &mut App, ) { self.view.update(cx, |view, cx| { view.replace_and_mark_text_in_range( range_utf16, new_text, new_selected_range, window, cx, ) }); } fn unmark_text(&mut self, window: &mut Window, cx: &mut App) { self.view .update(cx, |view, cx| view.unmark_text(window, cx)); } fn bounds_for_range( &mut self, range_utf16: Range, window: &mut Window, cx: &mut App, ) -> Option> { self.view.update(cx, |view, cx| { view.bounds_for_range(range_utf16, self.element_bounds, window, cx) }) } }