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