euv-core 0.53.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
use super::*;

/// Range helpers for `TextSelection`.
impl TextSelection {
    /// Creates a collapsed caret selection at `byte_offset`.
    ///
    /// # Arguments
    ///
    /// - `usize` - Caret UTF-8 byte offset.
    ///
    /// # Returns
    ///
    /// - `Self` - Collapsed selection where `anchor == focus == byte_offset`.
    #[must_use]
    pub const fn collapsed(byte_offset: usize) -> Self {
        Self {
            anchor: byte_offset,
            focus: byte_offset,
        }
    }

    /// Returns the smaller (inclusive start) UTF-8 byte offset of the selection.
    ///
    /// # Returns
    ///
    /// - `usize` - Ordered start byte offset.
    #[must_use]
    pub fn start(&self) -> usize {
        self.get_anchor().min(self.get_focus())
    }

    /// Returns the larger (exclusive end) UTF-8 byte offset of the selection.
    ///
    /// # Returns
    ///
    /// - `usize` - Ordered end byte offset.
    #[must_use]
    pub fn end(&self) -> usize {
        self.get_anchor().max(self.get_focus())
    }

    /// Returns `true` when the selection has zero width (`anchor == focus`).
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when collapsed to a caret.
    #[must_use]
    pub fn is_collapsed(&self) -> bool {
        self.get_anchor() == self.get_focus()
    }

    /// Returns the selected byte length (`end - start`).
    ///
    /// # Returns
    ///
    /// - `usize` - Selected UTF-8 byte length.
    #[must_use]
    pub fn byte_len(&self) -> usize {
        self.end().saturating_sub(self.start())
    }

    /// Returns a copy of this selection with both endpoints snapped to valid UTF-8 boundaries in `text`.
    ///
    /// # Arguments
    ///
    /// - `&str` - Backing UTF-8 text buffer.
    ///
    /// # Returns
    ///
    /// - `Self` - UTF-8 boundary-safe selection.
    #[must_use]
    pub fn clamped(&self, text: &str) -> Self {
        Self::new(
            clamp_utf8_boundary(text, self.get_anchor()),
            clamp_utf8_boundary(text, self.get_focus()),
        )
    }
}

/// Composition update and sanitization methods for `ImePreeditState`.
impl ImePreeditState {
    /// Starts or updates an active IME preedit composition string, cursor range, underline clauses, and candidate anchor rectangle.
    ///
    /// # Arguments
    ///
    /// - `String` - Uncommitted UTF-8 composition string.
    /// - `usize` - Inclusive UTF-8 cursor start offset within `composition`.
    /// - `usize` - Exclusive UTF-8 cursor end offset within `composition`.
    /// - `Vec<ImeUnderlineSpan>` - Clause underline spans within `composition`.
    /// - `Rect` - Screen-space logical anchor rectangle for OS candidate popups.
    pub fn update(
        &mut self,
        composition: String,
        cursor_start_byte: usize,
        cursor_end_byte: usize,
        underlines: Vec<ImeUnderlineSpan>,
        anchor_rect: Rect,
    ) {
        if composition.is_empty() {
            self.clear();
            self.set_candidate_anchor_rect_safe(anchor_rect);
            return;
        }
        let clamped_start: usize = clamp_utf8_boundary(&composition, cursor_start_byte);
        let clamped_end: usize =
            clamp_utf8_boundary(&composition, cursor_end_byte).max(clamped_start);
        let clean_underlines: Vec<ImeUnderlineSpan> = underlines
            .into_iter()
            .filter_map(|span: ImeUnderlineSpan| {
                let start: usize = clamp_utf8_boundary(&composition, span.get_start_byte());
                let end: usize = clamp_utf8_boundary(&composition, span.get_end_byte());
                if end > start {
                    Some(ImeUnderlineSpan::new(start, end, span.get_style()))
                } else {
                    None
                }
            })
            .collect();
        self.set_active(true);
        self.set_composition(composition);
        self.set_cursor_start_byte(clamped_start);
        self.set_cursor_end_byte(clamped_end);
        self.set_underlines(clean_underlines);
        self.set_candidate_anchor_rect_safe(anchor_rect);
    }

    /// Updates `candidate_anchor_rect` with finite non-negative dimensions.
    ///
    /// # Arguments
    ///
    /// - `Rect` - Screen-space candidate anchor rectangle.
    pub fn set_candidate_anchor_rect_safe(&mut self, anchor_rect: Rect) {
        let x_val: f32 = if anchor_rect.get_x().is_finite() {
            anchor_rect.get_x()
        } else {
            0.0
        };
        let y_val: f32 = if anchor_rect.get_y().is_finite() {
            anchor_rect.get_y()
        } else {
            0.0
        };
        let width_val: f32 = if anchor_rect.get_width().is_finite() {
            anchor_rect.get_width().max(0.0)
        } else {
            0.0
        };
        let height_val: f32 = if anchor_rect.get_height().is_finite() {
            anchor_rect.get_height().max(0.0)
        } else {
            0.0
        };
        self.set_candidate_anchor_rect(Rect::new(x_val, y_val, width_val, height_val));
    }

    /// Clears the active IME composition text, cursor offsets, and underline clauses while keeping the anchor rectangle.
    pub fn clear(&mut self) {
        self.set_active(false);
        self.get_mut_composition().clear();
        self.set_cursor_start_byte(0);
        self.set_cursor_end_byte(0);
        self.get_mut_underlines().clear();
    }

    /// Returns `true` when an active non-empty IME composition is in progress.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when actively composing preedit text.
    #[must_use]
    pub fn is_composing(&self) -> bool {
        self.get_active() && !self.get_composition().is_empty()
    }
}

/// Default empty `TextEditHistory` configured with standard Undo limit and coalesce window.
impl Default for TextEditHistory {
    /// Creates an empty `TextEditHistory`.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty editor history buffer.
    fn default() -> Self {
        Self::with_limits(DEFAULT_TEXT_HISTORY_LIMIT, DEFAULT_TEXT_COALESCE_WINDOW_MS)
    }
}

/// Coalesced Undo/Redo, UTF-8 word navigation, selection manipulation, and IME preedit operations for `TextEditHistory`.
impl TextEditHistory {
    /// Creates an empty `TextEditHistory` with explicit history capacity and coalesce window.
    ///
    /// # Arguments
    ///
    /// - `usize` - Maximum number of Undo snapshots retained (clamped to `>= 1`).
    /// - `u64` - Coalescing time window in milliseconds (`0` disables coalescing).
    ///
    /// # Returns
    ///
    /// - `Self` - Configured empty `TextEditHistory`.
    #[must_use]
    pub fn with_limits(max_history: usize, coalesce_window_ms: u64) -> Self {
        Self {
            text: String::new(),
            selection: TextSelection::collapsed(0),
            undo_stack: VecDeque::new(),
            redo_stack: Vec::new(),
            max_history: max_history.max(1),
            coalesce_window_ms,
            last_edit_kind: TextEditKind::Insert,
            last_edit_timestamp_ms: 0,
            has_coalesce_anchor: false,
            ime_preedit: ImePreeditState::default(),
        }
    }

    /// Creates a `TextEditHistory` initialized with `initial_text` and caret positioned at the end.
    ///
    /// # Arguments
    ///
    /// - `TextType` - Initial UTF-8 text content.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized text editor history.
    #[must_use]
    pub fn from_text<TextType>(initial_text: TextType) -> Self
    where
        TextType: Into<String>,
    {
        let content: String = initial_text.into();
        let end_offset: usize = content.len();
        let mut history: Self = Self::default();
        history.set_text(content);
        history.set_selection(TextSelection::collapsed(end_offset));
        history
    }

    /// Breaks the current Undo coalescing run so the next edit always records a fresh Undo point.
    pub fn break_coalesce(&mut self) {
        self.set_has_coalesce_anchor(false);
    }

    /// Pushes a pre-edit Undo snapshot unless the edit qualifies to coalesce with the top Undo entry.
    ///
    /// # Arguments
    ///
    /// - `TextEditKind` - Kind of edit being applied.
    /// - `u64` - Monotonic timestamp in milliseconds.
    /// - `bool` - Whether this individual edit is eligible for time-window coalescing.
    fn record_undo_checkpoint(
        &mut self,
        edit_kind: TextEditKind,
        timestamp_ms: u64,
        coalescable: bool,
    ) {
        self.get_mut_redo_stack().clear();
        let within_window: bool = timestamp_ms.saturating_sub(self.get_last_edit_timestamp_ms())
            <= self.get_coalesce_window_ms();
        let can_coalesce: bool = coalescable
            && self.get_coalesce_window_ms() > 0
            && self.get_has_coalesce_anchor()
            && self.get_last_edit_kind() == edit_kind
            && within_window
            && !self.get_undo_stack().is_empty();
        if !can_coalesce {
            let snapshot: TextEditSnapshot = TextEditSnapshot::new(
                self.get_text().clone(),
                self.get_selection(),
                edit_kind,
                timestamp_ms,
            );
            let limit: usize = self.get_max_history();
            let undo_stack: &mut VecDeque<TextEditSnapshot> = self.get_mut_undo_stack();
            while undo_stack.len() >= limit {
                let _: Option<TextEditSnapshot> = undo_stack.pop_front();
            }
            undo_stack.push_back(snapshot);
        }
        self.set_last_edit_kind(edit_kind);
        self.set_last_edit_timestamp_ms(timestamp_ms);
        self.set_has_coalesce_anchor(coalescable);
    }

    /// Inserts `inserted` at the active caret or replaces the active selection, coalescing rapid non-whitespace typing.
    ///
    /// # Arguments
    ///
    /// - `&str` - UTF-8 text to insert.
    /// - `u64` - Monotonic edit timestamp in milliseconds.
    pub fn insert_text(&mut self, inserted: &str, timestamp_ms: u64) {
        if inserted.is_empty() && self.get_selection().is_collapsed() {
            return;
        }
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        let is_single_word_char: bool = sel.is_collapsed()
            && inserted.chars().count() == 1
            && inserted.chars().all(|ch: char| !ch.is_whitespace());
        let kind: TextEditKind = if sel.is_collapsed() {
            TextEditKind::Insert
        } else {
            TextEditKind::Replace
        };
        self.record_undo_checkpoint(kind, timestamp_ms, is_single_word_char);
        let start: usize = sel.start();
        let end: usize = sel.end();
        self.get_mut_text().replace_range(start..end, inserted);
        let next_caret: usize = start.saturating_add(inserted.len());
        self.set_selection(TextSelection::collapsed(next_caret));
    }

    /// Replaces the current selection (or inserts at caret) as an uncoalesced Undo step.
    ///
    /// # Arguments
    ///
    /// - `&str` - Replacement UTF-8 text.
    /// - `u64` - Monotonic edit timestamp in milliseconds.
    pub fn replace_selection(&mut self, replacement: &str, timestamp_ms: u64) {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        self.record_undo_checkpoint(TextEditKind::Replace, timestamp_ms, false);
        let start: usize = sel.start();
        let end: usize = sel.end();
        self.get_mut_text().replace_range(start..end, replacement);
        let next_caret: usize = start.saturating_add(replacement.len());
        self.set_selection(TextSelection::collapsed(next_caret));
    }

    /// Deletes the active selection or the single UTF-8 character preceding the caret (Backspace).
    ///
    /// # Arguments
    ///
    /// - `u64` - Monotonic edit timestamp in milliseconds.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the text buffer changed.
    pub fn delete_backward(&mut self, timestamp_ms: u64) -> bool {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        if !sel.is_collapsed() {
            self.record_undo_checkpoint(TextEditKind::DeleteBackward, timestamp_ms, false);
            let start: usize = sel.start();
            let end: usize = sel.end();
            self.get_mut_text().replace_range(start..end, "");
            self.set_selection(TextSelection::collapsed(start));
            return true;
        }
        let caret: usize = sel.get_focus();
        if caret == 0 {
            return false;
        }
        let prev_offset: usize = prev_char_boundary(self.get_text(), caret);
        self.record_undo_checkpoint(TextEditKind::DeleteBackward, timestamp_ms, true);
        self.get_mut_text().replace_range(prev_offset..caret, "");
        self.set_selection(TextSelection::collapsed(prev_offset));
        true
    }

    /// Deletes the active selection or the single UTF-8 character following the caret (Forward Delete).
    ///
    /// # Arguments
    ///
    /// - `u64` - Monotonic edit timestamp in milliseconds.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the text buffer changed.
    pub fn delete_forward(&mut self, timestamp_ms: u64) -> bool {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        if !sel.is_collapsed() {
            self.record_undo_checkpoint(TextEditKind::DeleteForward, timestamp_ms, false);
            let start: usize = sel.start();
            let end: usize = sel.end();
            self.get_mut_text().replace_range(start..end, "");
            self.set_selection(TextSelection::collapsed(start));
            return true;
        }
        let caret: usize = sel.get_focus();
        if caret >= self.get_text().len() {
            return false;
        }
        let next_offset: usize = next_char_boundary(self.get_text(), caret);
        self.record_undo_checkpoint(TextEditKind::DeleteForward, timestamp_ms, true);
        self.get_mut_text().replace_range(caret..next_offset, "");
        self.set_selection(TextSelection::collapsed(caret));
        true
    }

    /// Deletes the active selection or the word preceding the caret (Option/Ctrl+Backspace).
    ///
    /// # Arguments
    ///
    /// - `u64` - Monotonic edit timestamp in milliseconds.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when at least one byte was deleted.
    pub fn delete_word_backward(&mut self, timestamp_ms: u64) -> bool {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        if !sel.is_collapsed() {
            return self.delete_backward(timestamp_ms);
        }
        let caret: usize = sel.get_focus();
        let word_start: usize = prev_word_boundary(self.get_text(), caret);
        if word_start == caret {
            return false;
        }
        self.record_undo_checkpoint(TextEditKind::DeleteBackward, timestamp_ms, false);
        self.get_mut_text().replace_range(word_start..caret, "");
        self.set_selection(TextSelection::collapsed(word_start));
        true
    }

    /// Deletes the active selection or the word following the caret (Option/Ctrl+Delete).
    ///
    /// # Arguments
    ///
    /// - `u64` - Monotonic edit timestamp in milliseconds.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when at least one byte was deleted.
    pub fn delete_word_forward(&mut self, timestamp_ms: u64) -> bool {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        if !sel.is_collapsed() {
            return self.delete_forward(timestamp_ms);
        }
        let caret: usize = sel.get_focus();
        let word_end: usize = next_word_boundary(self.get_text(), caret);
        if word_end == caret {
            return false;
        }
        self.record_undo_checkpoint(TextEditKind::DeleteForward, timestamp_ms, false);
        self.get_mut_text().replace_range(caret..word_end, "");
        self.set_selection(TextSelection::collapsed(caret));
        true
    }

    /// Returns the currently selected UTF-8 slice (empty when the selection is collapsed).
    ///
    /// # Returns
    ///
    /// - `&str` - Borrowed selected text slice.
    #[must_use]
    pub fn selected_text(&self) -> &str {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        &self.get_text()[sel.start()..sel.end()]
    }

    /// Selects the entire committed text buffer (`0..text.len()`).
    pub fn select_all(&mut self) {
        let end: usize = self.get_text().len();
        self.set_selection(TextSelection::new(0, end));
        self.break_coalesce();
    }

    /// Moves the caret to `byte_offset` (clamped to UTF-8 boundaries), optionally extending the selection anchor.
    ///
    /// # Arguments
    ///
    /// - `usize` - Target UTF-8 byte offset.
    /// - `bool` - When `true`, preserves the existing anchor to extend or shrink the selection.
    pub fn set_cursor(&mut self, byte_offset: usize, extend_selection: bool) {
        let clamped: usize = clamp_utf8_boundary(self.get_text(), byte_offset);
        let anchor: usize = if extend_selection {
            clamp_utf8_boundary(self.get_text(), self.get_selection().get_anchor())
        } else {
            clamped
        };
        self.set_selection(TextSelection::new(anchor, clamped));
        self.break_coalesce();
    }

    /// Moves the caret one UTF-8 character left, or collapses to selection start when not extending.
    ///
    /// # Arguments
    ///
    /// - `bool` - When `true`, extends the active selection.
    pub fn move_left(&mut self, extend_selection: bool) {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        if !extend_selection && !sel.is_collapsed() {
            self.set_selection(TextSelection::collapsed(sel.start()));
            self.break_coalesce();
            return;
        }
        let target: usize = prev_char_boundary(self.get_text(), sel.get_focus());
        self.set_cursor(target, extend_selection);
    }

    /// Moves the caret one UTF-8 character right, or collapses to selection end when not extending.
    ///
    /// # Arguments
    ///
    /// - `bool` - When `true`, extends the active selection.
    pub fn move_right(&mut self, extend_selection: bool) {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        if !extend_selection && !sel.is_collapsed() {
            self.set_selection(TextSelection::collapsed(sel.end()));
            self.break_coalesce();
            return;
        }
        let target: usize = next_char_boundary(self.get_text(), sel.get_focus());
        self.set_cursor(target, extend_selection);
    }

    /// Moves the caret one word boundary left, optionally extending the selection.
    ///
    /// # Arguments
    ///
    /// - `bool` - When `true`, extends the active selection.
    pub fn move_word_left(&mut self, extend_selection: bool) {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        let target: usize = prev_word_boundary(self.get_text(), sel.get_focus());
        self.set_cursor(target, extend_selection);
    }

    /// Moves the caret one word boundary right, optionally extending the selection.
    ///
    /// # Arguments
    ///
    /// - `bool` - When `true`, extends the active selection.
    pub fn move_word_right(&mut self, extend_selection: bool) {
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        let target: usize = next_word_boundary(self.get_text(), sel.get_focus());
        self.set_cursor(target, extend_selection);
    }

    /// Restores the most recent Undo snapshot and pushes the current buffer state onto the Redo stack.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when an Undo step was applied.
    pub fn undo(&mut self) -> bool {
        let Some(previous) = self.get_mut_undo_stack().pop_back() else {
            return false;
        };
        let current_snapshot: TextEditSnapshot = TextEditSnapshot::new(
            self.get_text().clone(),
            self.get_selection(),
            previous.get_edit_kind(),
            previous.get_timestamp_ms(),
        );
        self.get_mut_redo_stack().push(current_snapshot);
        let restored_sel: TextSelection = previous.get_selection().clamped(previous.get_text());
        self.set_text(previous.get_text().clone());
        self.set_selection(restored_sel);
        self.break_coalesce();
        true
    }

    /// Re-applies the most recently undone snapshot from the Redo stack.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when a Redo step was applied.
    pub fn redo(&mut self) -> bool {
        let Some(next_state) = self.get_mut_redo_stack().pop() else {
            return false;
        };
        let current_snapshot: TextEditSnapshot = TextEditSnapshot::new(
            self.get_text().clone(),
            self.get_selection(),
            next_state.get_edit_kind(),
            next_state.get_timestamp_ms(),
        );
        let limit: usize = self.get_max_history();
        let undo_stack: &mut VecDeque<TextEditSnapshot> = self.get_mut_undo_stack();
        while undo_stack.len() >= limit {
            let _: Option<TextEditSnapshot> = undo_stack.pop_front();
        }
        undo_stack.push_back(current_snapshot);
        let restored_sel: TextSelection = next_state.get_selection().clamped(next_state.get_text());
        self.set_text(next_state.get_text().clone());
        self.set_selection(restored_sel);
        self.break_coalesce();
        true
    }

    /// Returns `true` when at least one Undo step is available.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` if `undo()` will restore a prior state.
    #[must_use]
    pub fn can_undo(&self) -> bool {
        !self.get_undo_stack().is_empty()
    }

    /// Returns `true` when at least one Redo step is available.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` if `redo()` will re-apply an undone state.
    #[must_use]
    pub fn can_redo(&self) -> bool {
        !self.get_redo_stack().is_empty()
    }

    /// Updates the attached OS IME preedit composition state without mutating the committed text buffer.
    ///
    /// # Arguments
    ///
    /// - `String` - Uncommitted IME composition string.
    /// - `usize` - Inclusive UTF-8 cursor start inside `composition`.
    /// - `usize` - Exclusive UTF-8 cursor end inside `composition`.
    /// - `Vec<ImeUnderlineSpan>` - Clause underline spans.
    /// - `Rect` - Screen-space candidate window anchor rectangle.
    pub fn update_ime_preedit(
        &mut self,
        composition: String,
        cursor_start_byte: usize,
        cursor_end_byte: usize,
        underlines: Vec<ImeUnderlineSpan>,
        anchor_rect: Rect,
    ) {
        self.get_mut_ime_preedit().update(
            composition,
            cursor_start_byte,
            cursor_end_byte,
            underlines,
            anchor_rect,
        );
    }

    /// Clears active IME preedit state and commits `committed` as a discrete non-coalesced Undo step.
    ///
    /// # Arguments
    ///
    /// - `&str` - Final UTF-8 string committed by the OS IME.
    /// - `u64` - Monotonic commit timestamp in milliseconds.
    pub fn commit_ime_preedit(&mut self, committed: &str, timestamp_ms: u64) {
        self.get_mut_ime_preedit().clear();
        if committed.is_empty() && self.get_selection().is_collapsed() {
            return;
        }
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        self.record_undo_checkpoint(TextEditKind::ImeCommit, timestamp_ms, false);
        let start: usize = sel.start();
        let end: usize = sel.end();
        self.get_mut_text().replace_range(start..end, committed);
        let next_caret: usize = start.saturating_add(committed.len());
        self.set_selection(TextSelection::collapsed(next_caret));
    }

    /// Cancels any active IME preedit session without modifying committed text or Undo history.
    pub fn cancel_ime_preedit(&mut self) {
        self.get_mut_ime_preedit().clear();
    }

    /// Produces the full display string with any active IME preedit composition spliced at the current selection range.
    ///
    /// # Returns
    ///
    /// - `String` - Renderable text containing uncommitted IME composition inline.
    #[must_use]
    pub fn composed_display_text(&self) -> String {
        if !self.get_ime_preedit().is_composing() {
            return self.get_text().clone();
        }
        let sel: TextSelection = self.get_selection().clamped(self.get_text());
        let composition: &str = self.get_ime_preedit().get_composition();
        let mut combined: String =
            String::with_capacity(self.get_text().len().saturating_add(composition.len()));
        combined.push_str(&self.get_text()[..sel.start()]);
        combined.push_str(composition);
        combined.push_str(&self.get_text()[sel.end()..]);
        combined
    }
}