Skip to main content

nativeview_engine/editing/
focus.rs

1//! Platform-agnostic text focus, caret, selection, and IME composition state.
2
3use nativeview_abi::NodeId;
4
5
6/// Active focus, caret, range selection, and IME composition state tracked by retained backends.
7#[derive(Debug, Clone, Default, PartialEq, Eq)]
8pub struct TextFocusState {
9    pub focused_node: Option<NodeId>,
10    pub caret_byte_index: usize,
11    pub selection_anchor: usize,
12    pub ime_preedit: Option<String>,
13    pub ime_preedit_cursor: Option<(usize, usize)>,
14    pub uncontrolled_value: Option<String>,
15}
16
17impl TextFocusState {
18    #[inline]
19    pub fn selection_range(&self) -> Option<(usize, usize)> {
20        if self.selection_anchor != self.caret_byte_index {
21            Some((
22                self.selection_anchor.min(self.caret_byte_index),
23                self.selection_anchor.max(self.caret_byte_index),
24            ))
25        } else {
26            None
27        }
28    }
29}
30