Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T05:36:44.298Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

input.rs

234 lines · 7.1 KB · rust
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::set_selected_text_range`] for details
75    fn set_selected_text_range(
76        &mut self,
77        _range_utf16: Range<usize>,
78        _window: &mut Window,
79        _cx: &mut Context<Self>,
80    ) {
81    }
82
83    /// See [`InputHandler::text_length_utf16`] for details
84    fn text_length_utf16(
85        &mut self,
86        _window: &mut Window,
87        _cx: &mut Context<Self>,
88    ) -> Option<usize> {
89        None
90    }
91
92    /// See [`InputHandler::accepts_text_input`] for details
93    fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
94        true
95    }
96}
97
98/// The canonical implementation of [`crate::PlatformInputHandler`]. Call [`Window::handle_input`]
99/// with an instance during your element's paint.
100pub struct ElementInputHandler<V> {
101    view: Entity<V>,
102    element_bounds: Bounds<Pixels>,
103}
104
105impl<V: 'static> ElementInputHandler<V> {
106    /// Used in [`Element::paint`][element_paint] with the element's bounds, a `Window`, and a `App` context.
107    ///
108    /// [element_paint]: crate::Element::paint
109    pub fn new(element_bounds: Bounds<Pixels>, view: Entity<V>) -> Self {
110        ElementInputHandler {
111            view,
112            element_bounds,
113        }
114    }
115}
116
117impl<V: EntityInputHandler> InputHandler for ElementInputHandler<V> {
118    fn selected_text_range(
119        &mut self,
120        ignore_disabled_input: bool,
121        window: &mut Window,
122        cx: &mut App,
123    ) -> Option<UTF16Selection> {
124        self.view.update(cx, |view, cx| {
125            view.selected_text_range(ignore_disabled_input, window, cx)
126        })
127    }
128
129    fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>> {
130        self.view
131            .update(cx, |view, cx| view.marked_text_range(window, cx))
132    }
133
134    fn text_for_range(
135        &mut self,
136        range_utf16: Range<usize>,
137        adjusted_range: &mut Option<Range<usize>>,
138        window: &mut Window,
139        cx: &mut App,
140    ) -> Option<String> {
141        self.view.update(cx, |view, cx| {
142            view.text_for_range(range_utf16, adjusted_range, window, cx)
143        })
144    }
145
146    fn replace_text_in_range(
147        &mut self,
148        replacement_range: Option<Range<usize>>,
149        text: &str,
150        window: &mut Window,
151        cx: &mut App,
152    ) {
153        self.view.update(cx, |view, cx| {
154            view.replace_text_in_range(replacement_range, text, window, cx)
155        });
156    }
157
158    fn replace_and_mark_text_in_range(
159        &mut self,
160        range_utf16: Option<Range<usize>>,
161        new_text: &str,
162        new_selected_range: Option<Range<usize>>,
163        window: &mut Window,
164        cx: &mut App,
165    ) {
166        self.view.update(cx, |view, cx| {
167            view.replace_and_mark_text_in_range(
168                range_utf16,
169                new_text,
170                new_selected_range,
171                window,
172                cx,
173            )
174        });
175    }
176
177    fn unmark_text(&mut self, window: &mut Window, cx: &mut App) {
178        self.view
179            .update(cx, |view, cx| view.unmark_text(window, cx));
180    }
181
182    fn bounds_for_range(
183        &mut self,
184        range_utf16: Range<usize>,
185        window: &mut Window,
186        cx: &mut App,
187    ) -> Option<Bounds<Pixels>> {
188        self.view.update(cx, |view, cx| {
189            view.bounds_for_range(range_utf16, self.element_bounds, window, cx)
190        })
191    }
192
193    fn character_index_for_point(
194        &mut self,
195        point: crate::Point<Pixels>,
196        window: &mut Window,
197        cx: &mut App,
198    ) -> Option<usize> {
199        self.view.update(cx, |view, cx| {
200            view.character_index_for_point(point, window, cx)
201        })
202    }
203
204    fn set_selected_text_range(
205        &mut self,
206        range_utf16: Range<usize>,
207        window: &mut Window,
208        cx: &mut App,
209    ) {
210        self.view.update(cx, |view, cx| {
211            view.set_selected_text_range(range_utf16, window, cx)
212        })
213    }
214
215    fn element_bounds(&mut self, _window: &mut Window, _cx: &mut App) -> Option<Bounds<Pixels>> {
216        Some(self.element_bounds)
217    }
218
219    fn text_length_utf16(&mut self, window: &mut Window, cx: &mut App) -> Option<usize> {
220        self.view
221            .update(cx, |view, cx| view.text_length_utf16(window, cx))
222    }
223
224    fn accepts_text_input(&mut self, window: &mut Window, cx: &mut App) -> bool {
225        self.view
226            .update(cx, |view, cx| view.accepts_text_input(window, cx))
227    }
228
229    fn prefers_ime_for_printable_keys(&mut self, window: &mut Window, cx: &mut App) -> bool {
230        self.view
231            .update(cx, |view, cx| view.accepts_text_input(window, cx))
232    }
233}
234
Served at tenant.openagents/omega Member data and write actions are omitted.