Skip to main content

cranpose_ui/
text_field_focus.rs

1//! Focus manager for text fields.
2//!
3//! This module tracks which text field currently has focus, ensuring only one
4//! text field is focused at a time. When a new field requests focus, the
5//! previously focused field is automatically unfocused.
6//!
7//! O(1) key dispatch: The focused field's handler is stored for direct invocation,
8//! avoiding O(N) tree scans on every keystroke.
9
10use std::{
11    cell::{Cell, RefCell},
12    rc::{Rc, Weak},
13};
14
15use crate::key_event::KeyEvent;
16
17/// Snapshot of the focused text field's editable state for platform IMEs.
18///
19/// All offsets are UTF-8 byte offsets into `text`. Platform layers that talk
20/// to UTF-16 based IMEs (Android's `InputConnection`, web composition events)
21/// convert on their side of the boundary.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct ImeEditorState {
24    /// Full text content of the field.
25    pub text: String,
26    /// Selection start (min) in bytes. Equal to `selection_end` for a caret.
27    pub selection_start: usize,
28    /// Selection end (max) in bytes.
29    pub selection_end: usize,
30    /// Active composing (preedit) region in bytes, if any.
31    pub composition: Option<(usize, usize)>,
32    /// Whether the field is single-line (platforms use this to pick the
33    /// keyboard action, e.g. Android `IME_ACTION_DONE` vs newline).
34    pub single_line: bool,
35}
36
37/// Window-space caret geometry for the focused field, used by platforms whose
38/// native text input positions the caret by coordinates rather than key events
39/// (iOS `UITextInput`: spacebar-trackpad cursor movement and tap-to-position).
40/// Caret advances are local logical pixels. Use the conversion methods for
41/// native window rectangles and pointer positions, including transformed fields.
42#[derive(Clone, Debug, Default, PartialEq)]
43pub struct ImeCaretGeometry {
44    /// Local caret x for each character-boundary offset, in text order: entry `k` is
45    /// the caret after `k` characters, so `caret_xs.len() == chars + 1`.
46    pub caret_xs: Vec<f32>,
47    /// Local top y of the (single) caret line.
48    pub top: f32,
49    /// Local height of one line (the caret's height).
50    pub line_height: f32,
51    /// Maps the field's local coordinates into its window.
52    pub local_to_window: cranpose_ui_graphics::ProjectiveTransform,
53}
54
55impl ImeCaretGeometry {
56    /// The native caret's window rectangle after `character` Unicode scalars.
57    pub fn caret_rect(&self, character: usize) -> cranpose_ui_graphics::Rect {
58        self.range_rect(character, character)
59    }
60
61    /// Window bounds of a range whose endpoints count Unicode scalars.
62    pub fn range_rect(&self, start: usize, end: usize) -> cranpose_ui_graphics::Rect {
63        let x = |index: usize| {
64            self.caret_xs
65                .get(index)
66                .or_else(|| self.caret_xs.last())
67                .copied()
68                .unwrap_or(0.0)
69        };
70        let (left, right) = (x(start), x(end));
71        self.local_to_window
72            .bounds_for_rect(cranpose_ui_graphics::Rect {
73                x: left.min(right),
74                y: self.top,
75                width: (right - left).abs().max(2.0),
76                height: self.line_height,
77            })
78    }
79
80    /// Character boundary nearest a window-space pointer position.
81    /// A singular transform has no visible editable plane and returns `None`.
82    pub fn closest_character(&self, position: cranpose_ui_graphics::Point) -> Option<usize> {
83        let local = self.local_to_window.inverse()?.map_point(position);
84        self.caret_xs
85            .iter()
86            .enumerate()
87            .min_by(|(_, a), (_, b)| (*a - local.x).abs().total_cmp(&(*b - local.x).abs()))
88            .map(|(index, _)| index)
89    }
90}
91
92/// Handler trait for focused text field operations.
93/// Stored in focus module for O(1) key/clipboard dispatch.
94pub trait FocusedTextFieldHandler {
95    /// The composition node whose recorded draws show this field's caret and
96    /// selection. Focus changes and caret-blink flips schedule a scoped draw
97    /// repass on it — that state lives outside the draw-observation system,
98    /// so nothing else can name the stale node. `None` only for handlers with
99    /// no scene presence (test doubles, the platform no-op handler).
100    fn node_id(&self) -> Option<cranpose_core::NodeId> {
101        None
102    }
103    /// The node focus and semantics know the field by: the decoration box
104    /// around a decorated field, else [`Self::node_id`].
105    fn target_node(&self) -> Option<cranpose_core::NodeId> {
106        self.node_id()
107    }
108    /// Whether a focus request should open the software keyboard.
109    /// Pointer taps request the keyboard independently of this preference.
110    fn show_keyboard_on_focus(&self) -> bool {
111        true
112    }
113    /// Handle a key event. Returns true if consumed.
114    fn handle_key(&self, event: &KeyEvent) -> bool;
115    /// Insert pasted text.
116    fn insert_text(&self, text: &str);
117    /// Delete text surrounding the cursor or selection.
118    fn delete_surrounding(&self, before_bytes: usize, after_bytes: usize);
119    /// Copy current selection. Returns None if nothing selected.
120    fn copy_selection(&self) -> Option<String>;
121    /// Cut current selection (copy + delete). Returns None if nothing selected.
122    fn cut_selection(&self) -> Option<String>;
123    /// Selects all the field's text (contextual-menu "Select all").
124    fn select_all(&self) {}
125    /// Set IME composition (preedit) state.
126    /// - `text`: The composition text being typed (empty string to clear,
127    ///   which *deletes* the preedit text)
128    /// - `cursor`: Optional cursor position within composition (start, end)
129    fn set_composition(&self, text: &str, cursor: Option<(usize, usize)>);
130    /// Finish the active composition, keeping the composed text as regular
131    /// committed text (Android `finishComposingText` semantics). No-op when
132    /// no composition is active.
133    fn finish_composition(&self) {}
134    /// Mark existing text as the composing region without changing it
135    /// (Android `setComposingRegion` semantics, used by autocorrect to
136    /// re-compose an already committed word). Offsets are UTF-8 bytes;
137    /// implementations clamp them to valid character boundaries.
138    fn set_composing_region(&self, start_bytes: usize, end_bytes: usize) {
139        let _ = (start_bytes, end_bytes);
140    }
141    /// Move the selection/caret to `[start_bytes, end_bytes)` without editing
142    /// text (Android `InputConnection.setSelection` semantics). Used by
143    /// Gboard's spacebar-swipe cursor control, which scrubs the caret by
144    /// repeatedly setting the selection. Offsets are UTF-8 bytes; implementations
145    /// clamp them to valid character boundaries.
146    fn set_selection(&self, start_bytes: usize, end_bytes: usize) {
147        let _ = (start_bytes, end_bytes);
148    }
149    /// Snapshot of the current editable state for platform IMEs
150    /// (`InputConnection` text queries, session seeding). `None` when the
151    /// handler cannot expose its state.
152    fn editor_state(&self) -> Option<ImeEditorState> {
153        None
154    }
155    /// Window-space caret geometry (see [`ImeCaretGeometry`]) for coordinate-based
156    /// platform text input. `None` when the handler cannot expose it (e.g. the
157    /// field has not been laid out yet).
158    fn caret_geometry(&self) -> Option<ImeCaretGeometry> {
159        None
160    }
161}
162
163pub(crate) struct TextFieldFocusState {
164    focused_field: RefCell<Option<Weak<RefCell<bool>>>>,
165    focused_handler: RefCell<Option<Rc<dyn FocusedTextFieldHandler>>>,
166    focused_modal_depth: Cell<usize>,
167}
168
169impl TextFieldFocusState {
170    pub(crate) fn new() -> Self {
171        Self {
172            focused_field: RefCell::new(None),
173            focused_handler: RefCell::new(None),
174            focused_modal_depth: Cell::new(0),
175        }
176    }
177
178    fn request_focus(
179        &self,
180        is_focused: Rc<RefCell<bool>>,
181        handler: Rc<dyn FocusedTextFieldHandler>,
182        modal_depth: usize,
183    ) {
184        let mut current = self.focused_field.borrow_mut();
185
186        if let Some(ref weak) = *current
187            && let Some(old_focused) = weak.upgrade()
188        {
189            *old_focused.borrow_mut() = false;
190        }
191
192        *is_focused.borrow_mut() = true;
193        *current = Some(Rc::downgrade(&is_focused));
194        *self.focused_handler.borrow_mut() = Some(handler);
195        self.focused_modal_depth.set(modal_depth);
196    }
197
198    fn clear_focus(&self) {
199        let mut current = self.focused_field.borrow_mut();
200
201        if let Some(ref weak) = *current
202            && let Some(focused) = weak.upgrade()
203        {
204            *focused.borrow_mut() = false;
205        }
206
207        *current = None;
208        *self.focused_handler.borrow_mut() = None;
209        self.focused_modal_depth.set(0);
210    }
211
212    fn focused_at_depth(&self, depth: usize) -> bool {
213        self.has_focused_field() && self.focused_modal_depth.get() == depth
214    }
215
216    fn has_focused_field(&self) -> bool {
217        if self.focused_field_is_live() {
218            return true;
219        }
220        self.clear_stale_focus();
221        false
222    }
223
224    fn focused_field_is_live(&self) -> bool {
225        self.focused_field
226            .borrow()
227            .as_ref()
228            .is_some_and(|weak| weak.upgrade().is_some())
229    }
230
231    fn clear_stale_focus(&self) {
232        let stale_node = self
233            .focused_handler
234            .borrow()
235            .as_ref()
236            .and_then(|handler| handler.node_id());
237        let had_entry = self.focused_field.borrow_mut().take().is_some();
238        if !had_entry {
239            return;
240        }
241        self.focused_handler.borrow_mut().take();
242        if let Some(node_id) = stale_node {
243            crate::schedule_draw_repass(node_id);
244        }
245        crate::cursor_animation::stop_cursor_blink();
246    }
247
248    fn focused_handler(&self) -> Option<Rc<dyn FocusedTextFieldHandler>> {
249        if !self.has_focused_field() {
250            return None;
251        }
252        self.focused_handler.borrow().as_ref().cloned()
253    }
254
255    fn dispatch_key_event(&self, event: &KeyEvent) -> bool {
256        if let Some(handler) = self.focused_handler() {
257            handler.handle_key(event)
258        } else {
259            false
260        }
261    }
262
263    fn dispatch_paste(&self, text: &str) -> bool {
264        if let Some(handler) = self.focused_handler() {
265            handler.insert_text(text);
266            true
267        } else {
268            false
269        }
270    }
271
272    fn dispatch_delete_surrounding(&self, before_bytes: usize, after_bytes: usize) -> bool {
273        if let Some(handler) = self.focused_handler() {
274            handler.delete_surrounding(before_bytes, after_bytes);
275            true
276        } else {
277            false
278        }
279    }
280
281    fn dispatch_copy(&self) -> Option<String> {
282        self.focused_handler()
283            .and_then(|handler| handler.copy_selection())
284    }
285
286    fn dispatch_cut(&self) -> Option<String> {
287        self.focused_handler()
288            .and_then(|handler| handler.cut_selection())
289    }
290
291    fn dispatch_select_all(&self) -> bool {
292        if let Some(handler) = self.focused_handler() {
293            handler.select_all();
294            true
295        } else {
296            false
297        }
298    }
299
300    fn dispatch_ime_preedit(&self, text: &str, cursor: Option<(usize, usize)>) -> bool {
301        if let Some(handler) = self.focused_handler() {
302            handler.set_composition(text, cursor);
303            true
304        } else {
305            false
306        }
307    }
308
309    fn dispatch_ime_finish_composing(&self) -> bool {
310        if let Some(handler) = self.focused_handler() {
311            handler.finish_composition();
312            true
313        } else {
314            false
315        }
316    }
317
318    fn dispatch_ime_set_composing_region(&self, start_bytes: usize, end_bytes: usize) -> bool {
319        if let Some(handler) = self.focused_handler() {
320            handler.set_composing_region(start_bytes, end_bytes);
321            true
322        } else {
323            false
324        }
325    }
326
327    fn dispatch_ime_set_selection(&self, start_bytes: usize, end_bytes: usize) -> bool {
328        if let Some(handler) = self.focused_handler() {
329            handler.set_selection(start_bytes, end_bytes);
330            true
331        } else {
332            false
333        }
334    }
335
336    fn focused_editor_state(&self) -> Option<ImeEditorState> {
337        self.focused_handler()
338            .and_then(|handler| handler.editor_state())
339    }
340
341    fn focused_caret_geometry(&self) -> Option<ImeCaretGeometry> {
342        self.focused_handler()
343            .and_then(|handler| handler.caret_geometry())
344    }
345}
346
347/// Requests focus for a text field composed at [`crate::modal::local_modal_depth`]
348/// `modal_depth`.
349///
350/// Refused (a no-op) when `modal_depth` is shallower than the modal depth
351/// that is open right now — a field behind an open dialog must not be
352/// able to steal focus from it. A field inside the innermost dialog (or
353/// outside any dialog, while none is open) has `modal_depth` equal to the
354/// current modal depth and is granted focus normally.
355///
356/// If another text field was previously focused, it will be unfocused first.
357/// The provided `is_focused` handle should be the field's focus state.
358/// The handler is stored for O(1) key dispatch.
359pub fn request_focus(
360    is_focused: Rc<RefCell<bool>>,
361    handler: Rc<dyn FocusedTextFieldHandler>,
362    modal_depth: usize,
363) {
364    if modal_depth < crate::modal::current_modal_depth() {
365        return;
366    }
367
368    let previous_field = focused_field_node();
369    let gaining_field = handler.node_id();
370    let show_keyboard = handler.show_keyboard_on_focus();
371
372    crate::render_state::with_text_field_focus(|state| {
373        state.request_focus(is_focused, handler, modal_depth);
374    });
375
376    for node_id in [previous_field, gaining_field].into_iter().flatten() {
377        crate::schedule_draw_repass(node_id);
378    }
379
380    crate::cursor_animation::start_cursor_blink();
381
382    if show_keyboard {
383        crate::text_input_session::notify_text_input_focus_gained();
384    }
385
386    crate::request_render_invalidation();
387}
388
389pub(crate) fn clear_focus_for_closed_modal(depth: usize) {
390    let owns_focus =
391        crate::render_state::with_text_field_focus(|state| state.focused_at_depth(depth));
392    if owns_focus {
393        clear_focus();
394    }
395}
396
397/// Clears focus from the currently focused text field.
398pub fn clear_focus() {
399    let previous = focused_field_node();
400    let previous_target = focused_field_target();
401    if let Some(node_id) = previous {
402        crate::schedule_draw_repass(node_id);
403    }
404    crate::render_state::with_text_field_focus(TextFieldFocusState::clear_focus);
405    if previous_target.is_some() && crate::focus_dispatch::active_focus_target() == previous_target
406    {
407        crate::focus_dispatch::clear_active_focus();
408    }
409
410    crate::cursor_animation::stop_cursor_blink();
411
412    crate::text_input_session::notify_text_input_focus_lost();
413
414    crate::request_render_invalidation();
415}
416
417/// Returns the composition node of the currently focused text field, if a
418/// field is focused and its handler knows its node.
419pub fn focused_field_node() -> Option<cranpose_core::NodeId> {
420    crate::render_state::with_text_field_focus(|state| {
421        state
422            .focused_handler()
423            .and_then(|handler| handler.node_id())
424    })
425}
426
427/// Returns the node focus and semantics know the focused text field by (see
428/// [`FocusedTextFieldHandler::target_node`]), if a field is focused.
429pub fn focused_field_target() -> Option<cranpose_core::NodeId> {
430    crate::render_state::with_text_field_focus(|state| {
431        state
432            .focused_handler()
433            .and_then(|handler| handler.target_node())
434    })
435}
436
437/// Returns true if any text field currently has focus.
438/// Checks weak ref liveness and clears stale focus state.
439pub fn has_focused_field() -> bool {
440    let has_focus =
441        crate::render_state::with_text_field_focus(TextFieldFocusState::has_focused_field);
442    if !has_focus {
443        crate::text_input_session::notify_text_input_focus_lost();
444    }
445    has_focus
446}
447
448/// Dispatches a key event to the focused text field. Returns true if consumed.
449/// O(1) operation using stored handler.
450pub fn dispatch_key_event(event: &KeyEvent) -> bool {
451    crate::render_state::with_text_field_focus(|state| state.dispatch_key_event(event))
452}
453
454/// Inserts text into the focused text field (paste operation).
455/// O(1) operation using stored handler.
456pub fn dispatch_paste(text: &str) -> bool {
457    crate::render_state::with_text_field_focus(|state| state.dispatch_paste(text))
458}
459
460/// Deletes text surrounding the cursor or selection.
461/// O(1) operation using stored handler.
462pub fn dispatch_delete_surrounding(before_bytes: usize, after_bytes: usize) -> bool {
463    crate::render_state::with_text_field_focus(|state| {
464        state.dispatch_delete_surrounding(before_bytes, after_bytes)
465    })
466}
467
468/// Copies selection from focused text field.
469/// O(1) operation using stored handler.
470pub fn dispatch_copy() -> Option<String> {
471    crate::render_state::with_text_field_focus(TextFieldFocusState::dispatch_copy)
472}
473
474/// Cuts selection from focused text field (copy + delete).
475/// O(1) operation using stored handler.
476pub fn dispatch_cut() -> Option<String> {
477    crate::render_state::with_text_field_focus(TextFieldFocusState::dispatch_cut)
478}
479
480/// Selects all the text in the focused text field (contextual-menu "Select
481/// all"). Returns true if a text field was focused.
482pub fn dispatch_select_all() -> bool {
483    crate::render_state::with_text_field_focus(TextFieldFocusState::dispatch_select_all)
484}
485
486/// Dispatches IME preedit (composition) state to the focused text field.
487/// O(1) operation using stored handler.
488/// Returns true if a text field was focused and received the event.
489pub fn dispatch_ime_preedit(text: &str, cursor: Option<(usize, usize)>) -> bool {
490    crate::render_state::with_text_field_focus(|state| state.dispatch_ime_preedit(text, cursor))
491}
492
493/// Finishes the active composition in the focused text field, keeping the
494/// composed text (Android `finishComposingText` semantics).
495/// Returns true if a text field was focused and received the event.
496pub fn dispatch_ime_finish_composing() -> bool {
497    crate::render_state::with_text_field_focus(TextFieldFocusState::dispatch_ime_finish_composing)
498}
499
500/// Marks existing text in the focused field as the composing region without
501/// changing it (Android `setComposingRegion` semantics).
502/// Returns true if a text field was focused and received the event.
503pub fn dispatch_ime_set_composing_region(start_bytes: usize, end_bytes: usize) -> bool {
504    crate::render_state::with_text_field_focus(|state| {
505        state.dispatch_ime_set_composing_region(start_bytes, end_bytes)
506    })
507}
508
509/// Moves the focused field's selection/caret to `[start_bytes, end_bytes)`
510/// without editing text (Android `setSelection` semantics; the path Gboard's
511/// spacebar-swipe uses to scrub the cursor).
512/// Returns true if a text field was focused and received the event.
513pub fn dispatch_ime_set_selection(start_bytes: usize, end_bytes: usize) -> bool {
514    crate::render_state::with_text_field_focus(|state| {
515        state.dispatch_ime_set_selection(start_bytes, end_bytes)
516    })
517}
518
519/// Returns a snapshot of the focused text field's editable state for
520/// platform IMEs, or `None` when no field is focused (or the handler does
521/// not expose its state).
522pub fn focused_editor_state() -> Option<ImeEditorState> {
523    crate::render_state::with_text_field_focus(TextFieldFocusState::focused_editor_state)
524}
525
526/// Window-space caret geometry of the focused field (see [`ImeCaretGeometry`]),
527/// or `None` when no field is focused or it exposes no geometry.
528pub fn focused_caret_geometry() -> Option<ImeCaretGeometry> {
529    crate::render_state::with_text_field_focus(TextFieldFocusState::focused_caret_geometry)
530}
531
532#[cfg(test)]
533#[path = "tests/text_field_focus_tests.rs"]
534mod tests;