cranpose-ui 0.1.34

UI primitives for Cranpose
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Text field handler for O(1) focus dispatch.
//!
//! This module provides `TextFieldHandler` which implements `FocusedTextFieldHandler`
//! to enable O(1) keyboard and clipboard event dispatch to the focused text field,
//! avoiding O(N) tree scans.

use cranpose_foundation::text::{TextFieldLineLimits, TextFieldState};
use std::rc::Rc;

use crate::text_field_input::handle_key_event_impl;

/// Handler wrapper for O(1) focus dispatch.
/// Implements FocusedTextFieldHandler by delegating to TextFieldState operations.
pub(crate) struct TextFieldHandler {
    state: TextFieldState,
    /// Node ID for scoped layout invalidation (avoids O(app size) global invalidation)
    node_id: Option<cranpose_core::NodeId>,
    /// Line limits configuration
    line_limits: TextFieldLineLimits,
}

impl TextFieldHandler {
    pub(crate) fn new(
        state: TextFieldState,
        node_id: Option<cranpose_core::NodeId>,
        line_limits: TextFieldLineLimits,
    ) -> Rc<Self> {
        Rc::new(Self {
            state,
            node_id,
            line_limits,
        })
    }
}

impl crate::text_field_focus::FocusedTextFieldHandler for TextFieldHandler {
    fn handle_key(&self, event: &crate::key_event::KeyEvent) -> bool {
        use crate::key_event::KeyEventType;

        // Only handle key-down events
        if event.event_type != KeyEventType::KeyDown {
            return false;
        }

        // Delegate to shared implementation with line limits
        let consumed = handle_key_event_impl(&self.state, event, self.line_limits);

        if consumed {
            crate::cursor_animation::reset_cursor_blink();
            // Use scoped invalidation for O(subtree) instead of O(app size)
            if let Some(node_id) = self.node_id {
                crate::schedule_layout_repass(node_id);
            }
            crate::request_render_invalidation();
        }

        consumed
    }

    fn insert_text(&self, text: &str) {
        self.state.edit(|buffer| {
            buffer.insert(text);
        });
        crate::cursor_animation::reset_cursor_blink();
        // Text content changed - use scoped invalidation for O(subtree)
        if let Some(node_id) = self.node_id {
            crate::schedule_layout_repass(node_id);
        }
        crate::request_render_invalidation();
    }

    fn delete_surrounding(&self, before_bytes: usize, after_bytes: usize) {
        if before_bytes == 0 && after_bytes == 0 {
            return;
        }

        self.state.edit(|buffer| {
            buffer.delete_surrounding(before_bytes, after_bytes);
        });
        self.state.set_desired_column(None);
        crate::cursor_animation::reset_cursor_blink();
        if let Some(node_id) = self.node_id {
            crate::schedule_layout_repass(node_id);
        }
        crate::request_render_invalidation();
    }

    fn copy_selection(&self) -> Option<String> {
        let value = self.state.value();
        let selection = value.selection;

        if selection.collapsed() {
            return None;
        }

        let text = selection.safe_slice(&value.text);
        if text.is_empty() {
            return None;
        }

        Some(text.to_string())
    }

    fn cut_selection(&self) -> Option<String> {
        let value = self.state.value();
        let selection = value.selection;

        if selection.collapsed() {
            return None;
        }

        let text = selection.safe_slice(&value.text);
        if text.is_empty() {
            return None;
        }

        let text = text.to_string();
        self.state.edit(|buffer| {
            buffer.delete(selection);
        });
        crate::cursor_animation::reset_cursor_blink();
        crate::request_render_invalidation();
        Some(text)
    }

    fn set_composition(&self, text: &str, cursor: Option<(usize, usize)>) {
        self.state.edit(|buffer| {
            if text.is_empty() {
                // Clear composition
                if let Some(range) = buffer.composition() {
                    buffer.delete(range);
                }
                buffer.set_composition(None);
            } else {
                // Successive preedit updates must *replace* the previous
                // composing text (IMEs resend the whole preedit on every
                // keystroke); without an active composition the preedit
                // replaces the current selection, like Android's
                // `setComposingText` and winit's `Ime::Preedit`.
                let target = buffer.composition().unwrap_or_else(|| buffer.selection());
                let insert_pos = target.min();
                let comp_end = insert_pos + text.len();

                buffer.replace(target, text);

                // Set composition range to highlight the preedit text
                let comp_range = cranpose_foundation::text::TextRange::new(insert_pos, comp_end);
                buffer.set_composition(Some(comp_range));

                // If cursor position within composition is specified, adjust cursor
                if let Some((cursor_start, _cursor_end)) = cursor {
                    let cursor_pos = insert_pos + cursor_start.min(text.len());
                    buffer.place_cursor_before_char(cursor_pos);
                }
            }
        });

        // Request redraw for composition underline
        crate::request_render_invalidation();
    }

    fn finish_composition(&self) {
        // Android `finishComposingText`: keep the composed text as committed
        // text, only drop the composing region (unlike `set_composition("")`,
        // which deletes the preedit text).
        if self.state.composition().is_none() {
            return;
        }
        self.state.edit(|buffer| {
            buffer.set_composition(None);
        });
        crate::request_render_invalidation();
    }

    fn set_composing_region(&self, start_bytes: usize, end_bytes: usize) {
        self.state.edit(|buffer| {
            let text = buffer.text();
            let start = floor_char_boundary(text, start_bytes.min(end_bytes));
            let end = floor_char_boundary(text, start_bytes.max(end_bytes));
            if start == end {
                // An empty region clears the composing state (keeping the
                // text), matching Android's setComposingRegion contract.
                buffer.set_composition(None);
            } else {
                buffer.set_composition(Some(cranpose_foundation::text::TextRange::new(start, end)));
            }
        });
        crate::request_render_invalidation();
    }

    fn editor_state(&self) -> Option<crate::text_field_focus::ImeEditorState> {
        let value = self.state.value();
        Some(crate::text_field_focus::ImeEditorState {
            text: value.text.clone(),
            selection_start: value.selection.min(),
            selection_end: value.selection.max(),
            composition: value.composition.map(|range| (range.min(), range.max())),
            single_line: matches!(self.line_limits, TextFieldLineLimits::SingleLine),
        })
    }
}

/// Largest byte index `<= index` that lies on a `char` boundary of `text`.
fn floor_char_boundary(text: &str, index: usize) -> usize {
    let mut index = index.min(text.len());
    while index > 0 && !text.is_char_boundary(index) {
        index -= 1;
    }
    index
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::key_event::{KeyCode, KeyEvent, KeyEventType, Modifiers};
    use crate::text_field_focus;
    use cranpose_core::{DefaultScheduler, Runtime};
    use std::cell::RefCell;
    use std::sync::Arc;

    /// Sets up a test runtime and keeps it alive for the duration of the test.
    /// TextFieldState uses MutableState, which requires an active runtime.
    fn with_test_runtime<T>(f: impl FnOnce() -> T) -> T {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        f()
    }

    fn key_down(key_code: KeyCode, text: &str) -> KeyEvent {
        KeyEvent::new(key_code, text, Modifiers::NONE, KeyEventType::KeyDown)
    }

    /// Creates a focused text field state. The returned focus flag must stay
    /// alive for the duration of the test: the focus manager only holds a
    /// weak reference to it.
    fn focused_state(
        initial: &str,
        line_limits: TextFieldLineLimits,
    ) -> (TextFieldState, Rc<RefCell<bool>>) {
        let state = TextFieldState::new(initial);
        let handler = TextFieldHandler::new(state.clone(), None, line_limits);
        let focus = Rc::new(RefCell::new(false));
        text_field_focus::request_focus(focus.clone(), handler);
        (state, focus)
    }

    /// Headless commit path for soft-keyboard input on Android:
    /// `NativeActivity` IMEs without an `InputConnection` deliver characters
    /// as key events whose text comes from `KeyCharacterMap`. Framework key
    /// codes may be `Unknown` for layout-specific keys; the text must still
    /// commit.
    #[test]
    fn android_style_key_events_commit_text() {
        let _app_context = crate::render_state::app_context_test_scope();
        with_test_runtime(|| {
            let (state, _focus) = focused_state(
                "",
                TextFieldLineLimits::MultiLine {
                    min_lines: 1,
                    max_lines: usize::MAX,
                },
            );

            assert!(text_field_focus::dispatch_key_event(&key_down(
                KeyCode::H,
                "h"
            )));
            assert!(text_field_focus::dispatch_key_event(&key_down(
                KeyCode::I,
                "i"
            )));
            // Layout-specific key with no framework key code still commits its
            // KeyCharacterMap character.
            assert!(text_field_focus::dispatch_key_event(&key_down(
                KeyCode::Unknown,
                "\u{00f6}"
            )));
            assert_eq!(state.text(), "hi\u{00f6}");

            // Key-up must not double-commit.
            let key_up = KeyEvent::new(KeyCode::H, "h", Modifiers::NONE, KeyEventType::KeyUp);
            assert!(!text_field_focus::dispatch_key_event(&key_up));
            assert_eq!(state.text(), "hi\u{00f6}");

            // Backspace and Enter arrive without text (control keys).
            assert!(text_field_focus::dispatch_key_event(&key_down(
                KeyCode::Backspace,
                ""
            )));
            assert_eq!(state.text(), "hi");

            assert!(text_field_focus::dispatch_key_event(&key_down(
                KeyCode::Enter,
                ""
            )));
            assert_eq!(state.text(), "hi\n");

            text_field_focus::clear_focus();
        });
    }

    /// IMEs resend the whole preedit on every keystroke; successive preedit
    /// updates must replace the previous composing text, not accumulate.
    #[test]
    fn successive_preedit_updates_replace_previous_composition() {
        let _app_context = crate::render_state::app_context_test_scope();
        with_test_runtime(|| {
            let (state, _focus) = focused_state("ab", TextFieldLineLimits::SingleLine);
            state.edit(|buffer| buffer.place_cursor_at_end());

            assert!(text_field_focus::dispatch_ime_preedit("k", Some((1, 1))));
            assert_eq!(state.text(), "abk");
            assert!(text_field_focus::dispatch_ime_preedit("ka", Some((2, 2))));
            assert_eq!(state.text(), "abka");
            assert!(text_field_focus::dispatch_ime_preedit(
                "\u{304b}",
                Some((3, 3))
            ));
            assert_eq!(state.text(), "ab\u{304b}");
            let comp = state.composition().expect("composition active");
            assert_eq!((comp.min(), comp.max()), (2, 2 + "\u{304b}".len()));

            // Commit path: clear preedit (deletes it), then insert the final text.
            assert!(text_field_focus::dispatch_ime_preedit("", None));
            assert_eq!(state.text(), "ab");
            assert!(text_field_focus::dispatch_paste("\u{304b}\u{306a}"));
            assert_eq!(state.text(), "ab\u{304b}\u{306a}");
            assert_eq!(state.composition(), None);

            text_field_focus::clear_focus();
        });
    }

    /// `finishComposingText` keeps the composed text and only clears the
    /// composing region, unlike an empty preedit which deletes the text.
    #[test]
    fn finish_composition_keeps_text() {
        let _app_context = crate::render_state::app_context_test_scope();
        with_test_runtime(|| {
            let (state, _focus) = focused_state("", TextFieldLineLimits::SingleLine);

            assert!(text_field_focus::dispatch_ime_preedit("hello", None));
            assert_eq!(state.text(), "hello");
            assert!(state.composition().is_some());

            assert!(text_field_focus::dispatch_ime_finish_composing());
            assert_eq!(state.text(), "hello");
            assert_eq!(state.composition(), None);

            // Finishing again is a no-op.
            assert!(text_field_focus::dispatch_ime_finish_composing());
            assert_eq!(state.text(), "hello");

            text_field_focus::clear_focus();
        });
    }

    /// Autocorrect re-composes an already committed word via
    /// `setComposingRegion`; the text must not change, only the region.
    #[test]
    fn set_composing_region_marks_text_without_changing_it() {
        let _app_context = crate::render_state::app_context_test_scope();
        with_test_runtime(|| {
            let (state, _focus) = focused_state("hello world", TextFieldLineLimits::SingleLine);

            assert!(text_field_focus::dispatch_ime_set_composing_region(0, 5));
            assert_eq!(state.text(), "hello world");
            let comp = state.composition().expect("composition active");
            assert_eq!((comp.min(), comp.max()), (0, 5));

            // Replacing the composing region rewrites the marked word.
            assert!(text_field_focus::dispatch_ime_preedit("Hello", None));
            assert_eq!(state.text(), "Hello world");

            // Offsets that fall inside a multi-byte character are clamped to
            // the previous boundary instead of panicking.
            assert!(text_field_focus::dispatch_ime_preedit("", None));
            state.edit(|buffer| {
                buffer.clear();
                buffer.insert("\u{00e9}x");
            });
            assert!(text_field_focus::dispatch_ime_set_composing_region(1, 3));
            let comp = state.composition().expect("composition active");
            assert_eq!((comp.min(), comp.max()), (0, 3));

            // An empty region clears composing state but keeps the text.
            assert!(text_field_focus::dispatch_ime_set_composing_region(1, 1));
            assert_eq!(state.composition(), None);
            assert_eq!(state.text(), "\u{00e9}x");

            text_field_focus::clear_focus();
        });
    }

    #[test]
    fn editor_state_reflects_text_selection_and_composition() {
        let _app_context = crate::render_state::app_context_test_scope();
        with_test_runtime(|| {
            let (state, _focus) = focused_state("hi", TextFieldLineLimits::SingleLine);
            state.edit(|buffer| buffer.place_cursor_at_end());

            let snapshot = text_field_focus::focused_editor_state().expect("focused field");
            assert_eq!(snapshot.text, "hi");
            assert_eq!(
                (snapshot.selection_start, snapshot.selection_end),
                (2usize, 2usize)
            );
            assert_eq!(snapshot.composition, None);
            assert!(snapshot.single_line);

            assert!(text_field_focus::dispatch_ime_preedit("ab", None));
            let snapshot = text_field_focus::focused_editor_state().expect("focused field");
            assert_eq!(snapshot.text, "hiab");
            assert_eq!(snapshot.composition, Some((2, 4)));

            text_field_focus::clear_focus();
            assert_eq!(text_field_focus::focused_editor_state(), None);
        });
    }

    #[test]
    fn enter_on_single_line_field_is_not_consumed() {
        let _app_context = crate::render_state::app_context_test_scope();
        with_test_runtime(|| {
            let (state, _focus) = focused_state("abc", TextFieldLineLimits::SingleLine);

            assert!(!text_field_focus::dispatch_key_event(&key_down(
                KeyCode::Enter,
                ""
            )));
            assert_eq!(state.text(), "abc");

            text_field_focus::clear_focus();
        });
    }
}