input.rs

  1use crate::{App, Bounds, Context, Entity, InputHandler, Pixels, UTF16Selection, Window};
  2use std::ops::Range;
  3
  4/// Implement this trait to allow views to handle textual input when implementing an editor, field, etc.
  5///
  6/// Once your view implements this trait, you can use it to construct an [`ElementInputHandler<V>`].
  7/// This input handler can then be assigned during paint by calling [`Window::handle_input`].
  8///
  9/// See [`InputHandler`] for details on how to implement each method.
 10pub trait EntityInputHandler: 'static + Sized {
 11    /// See [`InputHandler::text_for_range`] for details
 12    fn text_for_range(
 13        &mut self,
 14        range: Range<usize>,
 15        adjusted_range: &mut Option<Range<usize>>,
 16        window: &mut Window,
 17        cx: &mut Context<Self>,
 18    ) -> Option<String>;
 19
 20    /// See [`InputHandler::selected_text_range`] for details
 21    fn selected_text_range(
 22        &mut self,
 23        ignore_disabled_input: bool,
 24        window: &mut Window,
 25        cx: &mut Context<Self>,
 26    ) -> Option<UTF16Selection>;
 27
 28    /// See [`InputHandler::marked_text_range`] for details
 29    fn marked_text_range(
 30        &self,
 31        window: &mut Window,
 32        cx: &mut Context<Self>,
 33    ) -> Option<Range<usize>>;
 34
 35    /// See [`InputHandler::unmark_text`] for details
 36    fn unmark_text(&mut self, window: &mut Window, cx: &mut Context<Self>);
 37
 38    /// See [`InputHandler::replace_text_in_range`] for details
 39    fn replace_text_in_range(
 40        &mut self,
 41        range: Option<Range<usize>>,
 42        text: &str,
 43        window: &mut Window,
 44        cx: &mut Context<Self>,
 45    );
 46
 47    /// See [`InputHandler::replace_and_mark_text_in_range`] for details
 48    fn replace_and_mark_text_in_range(
 49        &mut self,
 50        range: Option<Range<usize>>,
 51        new_text: &str,
 52        new_selected_range: Option<Range<usize>>,
 53        window: &mut Window,
 54        cx: &mut Context<Self>,
 55    );
 56
 57    /// See [`InputHandler::bounds_for_range`] for details
 58    fn bounds_for_range(
 59        &mut self,
 60        range_utf16: Range<usize>,
 61        element_bounds: Bounds<Pixels>,
 62        window: &mut Window,
 63        cx: &mut Context<Self>,
 64    ) -> Option<Bounds<Pixels>>;
 65}
 66
 67/// The canonical implementation of [`PlatformInputHandler`]. Call [`Window::handle_input`]
 68/// with an instance during your element's paint.
 69pub struct ElementInputHandler<V> {
 70    view: Entity<V>,
 71    element_bounds: Bounds<Pixels>,
 72}
 73
 74impl<V: 'static> ElementInputHandler<V> {
 75    /// Used in [`Element::paint`][element_paint] with the element's bounds, a `Window`, and a `App` context.
 76    ///
 77    /// [element_paint]: crate::Element::paint
 78    pub fn new(element_bounds: Bounds<Pixels>, view: Entity<V>) -> Self {
 79        ElementInputHandler {
 80            view,
 81            element_bounds,
 82        }
 83    }
 84}
 85
 86impl<V: EntityInputHandler> InputHandler for ElementInputHandler<V> {
 87    fn selected_text_range(
 88        &mut self,
 89        ignore_disabled_input: bool,
 90        window: &mut Window,
 91        cx: &mut App,
 92    ) -> Option<UTF16Selection> {
 93        self.view.update(cx, |view, cx| {
 94            view.selected_text_range(ignore_disabled_input, window, cx)
 95        })
 96    }
 97
 98    fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>> {
 99        self.view
100            .update(cx, |view, cx| view.marked_text_range(window, cx))
101    }
102
103    fn text_for_range(
104        &mut self,
105        range_utf16: Range<usize>,
106        adjusted_range: &mut Option<Range<usize>>,
107        window: &mut Window,
108        cx: &mut App,
109    ) -> Option<String> {
110        self.view.update(cx, |view, cx| {
111            view.text_for_range(range_utf16, adjusted_range, window, cx)
112        })
113    }
114
115    fn replace_text_in_range(
116        &mut self,
117        replacement_range: Option<Range<usize>>,
118        text: &str,
119        window: &mut Window,
120        cx: &mut App,
121    ) {
122        self.view.update(cx, |view, cx| {
123            view.replace_text_in_range(replacement_range, text, window, cx)
124        });
125    }
126
127    fn replace_and_mark_text_in_range(
128        &mut self,
129        range_utf16: Option<Range<usize>>,
130        new_text: &str,
131        new_selected_range: Option<Range<usize>>,
132        window: &mut Window,
133        cx: &mut App,
134    ) {
135        self.view.update(cx, |view, cx| {
136            view.replace_and_mark_text_in_range(
137                range_utf16,
138                new_text,
139                new_selected_range,
140                window,
141                cx,
142            )
143        });
144    }
145
146    fn unmark_text(&mut self, window: &mut Window, cx: &mut App) {
147        self.view
148            .update(cx, |view, cx| view.unmark_text(window, cx));
149    }
150
151    fn bounds_for_range(
152        &mut self,
153        range_utf16: Range<usize>,
154        window: &mut Window,
155        cx: &mut App,
156    ) -> Option<Bounds<Pixels>> {
157        self.view.update(cx, |view, cx| {
158            view.bounds_for_range(range_utf16, self.element_bounds, window, cx)
159        })
160    }
161}