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 /// See [`InputHandler::character_index_for_point`] for details
67 fn character_index_for_point(
68 &mut self,
69 point: crate::Point<Pixels>,
70 window: &mut Window,
71 cx: &mut Context<Self>,
72 ) -> Option<usize>;
73
74 /// See [`InputHandler::accepts_text_input`] for details
75 fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
76 true
77 }
78}
79
80/// The canonical implementation of [`crate::PlatformInputHandler`]. Call [`Window::handle_input`]
81/// with an instance during your element's paint.
82pub struct ElementInputHandler<V> {
83 view: Entity<V>,
84 element_bounds: Bounds<Pixels>,
85}
86
87impl<V: 'static> ElementInputHandler<V> {
88 /// Used in [`Element::paint`][element_paint] with the element's bounds, a `Window`, and a `App` context.
89 ///
90 /// [element_paint]: crate::Element::paint
91 pub fn new(element_bounds: Bounds<Pixels>, view: Entity<V>) -> Self {
92 ElementInputHandler {
93 view,
94 element_bounds,
95 }
96 }
97}
98
99impl<V: EntityInputHandler> InputHandler for ElementInputHandler<V> {
100 fn selected_text_range(
101 &mut self,
102 ignore_disabled_input: bool,
103 window: &mut Window,
104 cx: &mut App,
105 ) -> Option<UTF16Selection> {
106 self.view.update(cx, |view, cx| {
107 view.selected_text_range(ignore_disabled_input, window, cx)
108 })
109 }
110
111 fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>> {
112 self.view
113 .update(cx, |view, cx| view.marked_text_range(window, cx))
114 }
115
116 fn text_for_range(
117 &mut self,
118 range_utf16: Range<usize>,
119 adjusted_range: &mut Option<Range<usize>>,
120 window: &mut Window,
121 cx: &mut App,
122 ) -> Option<String> {
123 self.view.update(cx, |view, cx| {
124 view.text_for_range(range_utf16, adjusted_range, window, cx)
125 })
126 }
127
128 fn replace_text_in_range(
129 &mut self,
130 replacement_range: Option<Range<usize>>,
131 text: &str,
132 window: &mut Window,
133 cx: &mut App,
134 ) {
135 self.view.update(cx, |view, cx| {
136 view.replace_text_in_range(replacement_range, text, window, cx)
137 });
138 }
139
140 fn replace_and_mark_text_in_range(
141 &mut self,
142 range_utf16: Option<Range<usize>>,
143 new_text: &str,
144 new_selected_range: Option<Range<usize>>,
145 window: &mut Window,
146 cx: &mut App,
147 ) {
148 self.view.update(cx, |view, cx| {
149 view.replace_and_mark_text_in_range(
150 range_utf16,
151 new_text,
152 new_selected_range,
153 window,
154 cx,
155 )
156 });
157 }
158
159 fn unmark_text(&mut self, window: &mut Window, cx: &mut App) {
160 self.view
161 .update(cx, |view, cx| view.unmark_text(window, cx));
162 }
163
164 fn bounds_for_range(
165 &mut self,
166 range_utf16: Range<usize>,
167 window: &mut Window,
168 cx: &mut App,
169 ) -> Option<Bounds<Pixels>> {
170 self.view.update(cx, |view, cx| {
171 view.bounds_for_range(range_utf16, self.element_bounds, window, cx)
172 })
173 }
174
175 fn character_index_for_point(
176 &mut self,
177 point: crate::Point<Pixels>,
178 window: &mut Window,
179 cx: &mut App,
180 ) -> Option<usize> {
181 self.view.update(cx, |view, cx| {
182 view.character_index_for_point(point, window, cx)
183 })
184 }
185
186 fn accepts_text_input(&mut self, window: &mut Window, cx: &mut App) -> bool {
187 self.view
188 .update(cx, |view, cx| view.accepts_text_input(window, cx))
189 }
190}