teksilo-widgets 0.9.1

Widget library for Teksilo — over a hundred widgets and layout primitives, from Button to TreeTableView.
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! Keyboard dispatch for the text input widget.
//!
//! Simplified from `rich_text::keyboard`: no vertical navigation, no
//! format toggles, no select-all escalation ladder, no preferred-X. Enter
//! fires the on_submit command instead of inserting a newline.

use teksilo_core::event::{EventResponse, Key, WidgetEvent};
use teksilo_core::widget::EventContext;
use teksilo_platform::clipboard::ClipboardHandle;
use teksilo_text::CursorAffinity;
use teksilo_text::text_document::{MoveMode, MoveOperation, SelectionType};

use crate::common::text_nav::{CaretStep, caret_step, deletes_word};

use super::state::{SharedState, TextInputState, sync_cursor_signals};

/// Report the caret's window-space rectangle to the platform so the OS IME
/// candidate window tracks the insertion point. No-op when unfocused or the
/// engine has not been laid out yet. Called whenever the caret moves.
pub(crate) fn report_ime_cursor_area(state: &SharedState, ctx: &mut EventContext) {
    let area = {
        let st = state.borrow();
        if !st.has_focus || !st.engine.has_full_layout() {
            return;
        }
        let caret = st
            .engine
            .caret_rect(st.cursor.position(), CursorAffinity::Downstream);
        teksilo_canvas::Rect::new(
            st.viewport_origin.x + caret[0] - st.scroll_x,
            st.viewport_origin.y + caret[1],
            caret[2].max(1.0),
            caret[3],
        )
    };
    // Dedup against the last reported area. The platform-side
    // `WindowOps::set_ime_cursor_area` does not dedup; re-forwarding an
    // unchanged area is wasted work and, on some winit IME backends (ibus /
    // fcitx), echoes back a fresh empty `Ime::Preedit`, sustaining a feedback
    // loop. Only forward a genuinely new position.
    {
        let mut st = state.borrow_mut();
        if st.last_ime_area == Some(area) {
            return;
        }
        st.last_ime_area = Some(area);
    }
    ctx.set_ime_cursor_area(area);
}

pub(crate) fn handle_key(
    state: &SharedState,
    event: &WidgetEvent,
    ctx: &mut EventContext,
) -> EventResponse {
    // IME composition (preedit) — tentative, replaceable text shown inline.
    if let WidgetEvent::ImeComposition { text, .. } = event {
        return handle_ime_composition(state, ctx, text);
    }
    // IME commit — finalized grapheme cluster. Drop any live preedit first
    // (some backends skip the empty-preedit clear), then batch into
    // pending_chars like ordinary typed input.
    if let WidgetEvent::ImeCommit { text } = event {
        clear_ime_preedit(state);
        return push_pending_chars(state, ctx, text);
    }

    let WidgetEvent::KeyDown {
        key,
        modifiers,
        text,
        ..
    } = event
    else {
        return EventResponse::Ignored;
    };

    let shift = modifiers.shift();
    // The platform accelerator — ⌘ on macOS, Ctrl elsewhere. See the
    // rich-text sibling for why this is not `ctrl() || super_key()`.
    let ctrl = modifiers.command();
    let mode = if shift {
        MoveMode::KeepAnchor
    } else {
        MoveMode::MoveAnchor
    };

    let read_only = state.borrow().read_only;

    let handled = {
        let mut st = state.borrow_mut();
        match key {
            // ── Navigation ──────────────────────────────────────────
            // Word-jump rides ⌥ on macOS and Ctrl elsewhere, and ⌘←/→ reach
            // the field's edges — the single-line reading of "line edge". See
            // `common::text_nav`.
            Key::ArrowLeft => {
                let op = match caret_step(*modifiers) {
                    CaretStep::Character => MoveOperation::Left,
                    CaretStep::Word => MoveOperation::WordLeft,
                    CaretStep::LineEdge => MoveOperation::Start,
                };
                st.cursor.move_position(op, mode, 1);
                true
            }
            Key::ArrowRight => {
                let op = match caret_step(*modifiers) {
                    CaretStep::Character => MoveOperation::Right,
                    CaretStep::Word => MoveOperation::WordRight,
                    CaretStep::LineEdge => MoveOperation::End,
                };
                st.cursor.move_position(op, mode, 1);
                true
            }
            Key::Home => {
                // Single-line: Home = start of document.
                st.cursor.move_position(MoveOperation::Start, mode, 1);
                true
            }
            Key::End => {
                // Single-line: End = end of document.
                st.cursor.move_position(MoveOperation::End, mode, 1);
                true
            }

            // ── Select all ──────────────────────────────────────────
            Key::A if ctrl => {
                st.cursor.select(SelectionType::Document);
                true
            }

            // ── Clipboard ───────────────────────────────────────────
            Key::C if ctrl => {
                clipboard_copy(&mut st, ctx);
                true
            }
            Key::X if ctrl && !read_only => {
                clipboard_cut(&mut st, ctx);
                true
            }
            Key::V if ctrl && !read_only => {
                clipboard_paste(&mut st, ctx);
                true
            }

            // ── Editing (gated on !read_only) ───────────────────────
            Key::Backspace if !read_only => {
                if deletes_word(*modifiers) {
                    if !st.cursor.has_selection() {
                        st.cursor
                            .move_position(MoveOperation::WordLeft, MoveMode::KeepAnchor, 1);
                    }
                    let _ = st.cursor.remove_selected_text();
                } else if st.cursor.has_selection() {
                    let _ = st.cursor.remove_selected_text();
                } else {
                    let _ = st.cursor.delete_previous_char();
                }
                true
            }
            Key::Delete if !read_only => {
                if deletes_word(*modifiers) {
                    if !st.cursor.has_selection() {
                        st.cursor
                            .move_position(MoveOperation::WordRight, MoveMode::KeepAnchor, 1);
                    }
                    let _ = st.cursor.remove_selected_text();
                } else if st.cursor.has_selection() {
                    let _ = st.cursor.remove_selected_text();
                } else {
                    let _ = st.cursor.delete_char();
                }
                true
            }

            // ── Undo / Redo ─────────────────────────────────────────
            Key::Z if ctrl && !shift && !read_only => {
                let _ = st.document.undo();
                true
            }
            Key::Y if ctrl && !read_only => {
                let _ = st.document.redo();
                true
            }
            Key::Z if ctrl && shift && !read_only => {
                let _ = st.document.redo();
                true
            }

            // ── Enter → on_submit ───────────────────────────────────
            Key::Enter => {
                if let Some(ref on_submit) = st.on_submit {
                    let submit = on_submit.clone();
                    drop(st);
                    submit(ctx);
                    return EventResponse::Handled;
                }
                // No on_submit: still consume Enter to prevent it
                // from bubbling and accidentally triggering dialogs.
                true
            }

            // ── Tab → let it bubble for focus navigation ────────────
            Key::Tab => {
                return EventResponse::Ignored;
            }

            // ── Printable characters ────────────────────────────────
            _ => {
                if ctrl {
                    false // don't eat unhandled ctrl combos
                } else if let Some(t) = text.as_deref() {
                    if !read_only {
                        // Strip newlines (single-line enforcement),
                        // control characters, and anything the
                        // optional per-character filter rejects.
                        let clean: String = t
                            .chars()
                            .filter(|c| !c.is_control() && *c != '\n' && *c != '\r')
                            .filter(|c| st.char_filter_admits(*c))
                            .collect();
                        if !clean.is_empty() {
                            // Max-length enforcement: compute remaining capacity.
                            if let Some(max) = st.max_length {
                                let current_len = st
                                    .document
                                    .to_plain_text()
                                    .unwrap_or_default()
                                    .chars()
                                    .count();
                                let sel_len = if st.cursor.has_selection() {
                                    st.cursor
                                        .selected_text()
                                        .ok()
                                        .unwrap_or_default()
                                        .chars()
                                        .count()
                                } else {
                                    0
                                };
                                let remaining =
                                    max.saturating_sub(current_len.saturating_sub(sel_len));
                                if remaining == 0 {
                                    return EventResponse::Handled;
                                }
                                let truncated: String = clean.chars().take(remaining).collect();
                                st.pending_chars.push_str(&truncated);
                            } else {
                                st.pending_chars.push_str(&clean);
                            }
                            true
                        } else if t.chars().any(|c| !c.is_control()) {
                            // Real text, rejected wholesale by the
                            // filter or the max length: swallow it, so a
                            // digits-only field doesn't let a rejected
                            // "s" fall through and match a Save
                            // shortcut.
                            return EventResponse::Handled;
                        } else {
                            // Nothing but control characters, so this
                            // key never carried text at all — the
                            // platform simply attaches some to a named
                            // key, and winit hands Escape through as
                            // U+001B. Stripping that to nothing is not a
                            // rejection, and swallowing it on that
                            // ground is what stopped Escape from ever
                            // leaving a focused field: every "Escape
                            // closes this" affordance above one was dead
                            // — the cell editor that would not shut, and
                            // any dialog whose cancel sits outside the
                            // field. Bubble instead, the way `Key::Tab`
                            // above already does.
                            //
                            // Not keyed on the `Key` variant: a typed
                            // letter arrives as `Key::A`, not
                            // `Key::Character('a')`, so "is it a
                            // character key" would quietly stop
                            // swallowing rejected letters.
                            false
                        }
                    } else {
                        false
                    }
                } else {
                    false
                }
            }
        }
    };

    if handled {
        sync_cursor_signals(state);
        report_ime_cursor_area(state, ctx);
        ctx.request_frame();
        EventResponse::Handled
    } else {
        EventResponse::Ignored
    }
}

/// Apply an `ImeComposition` event. Removes the previous preedit range (if
/// any) from the document, inserts the new cleaned preedit text at the
/// cursor, and records the resulting range so the next composition event can
/// replace it. Single-line rules apply (newlines / control chars stripped,
/// `char_filter` enforced, `max_length` respected); secure-field masking is
/// handled at the engine layer so a password preedit renders as bullets. An
/// empty composition cancels the preedit without inserting.
fn handle_ime_composition(
    state: &SharedState,
    ctx: &mut EventContext,
    text: &str,
) -> EventResponse {
    if state.borrow().read_only {
        return EventResponse::Handled;
    }
    // A composition carrying no insertable text while there is no active preedit
    // is a genuine no-op. Some Linux IME backends (ibus / fcitx via winit) flood
    // empty `Ime::Preedit("")` events while a field is focused; processing each
    // one — an undo block, a signal sync, an un-deduped IME-area report that
    // re-arms that very loop, a repaint — is pure waste. Bail before any of it.
    {
        let st = state.borrow();
        let empty = text
            .chars()
            .all(|c| c.is_control() || c == '\n' || c == '\r');
        if empty && st.ime_preedit_range.is_none() {
            return EventResponse::Ignored;
        }
    }
    {
        let mut st = state.borrow_mut();
        // Group remove+reinsert as one undo step so an undo after
        // composition pops the whole preedit, not each candidate.
        st.cursor.begin_edit_block();
        if let Some(range) = st.ime_preedit_range.take() {
            let doc_end = st.document.character_count();
            let start = range.start.min(doc_end);
            let end = range.end.min(doc_end);
            if start < end {
                st.cursor.set_position(start, MoveMode::MoveAnchor);
                st.cursor.set_position(end, MoveMode::KeepAnchor);
                let _ = st.cursor.remove_selected_text();
            }
        }
        // Single-line cleaning + per-character filter.
        let clean: String = text
            .chars()
            .filter(|c| !c.is_control() && *c != '\n' && *c != '\r')
            .filter(|c| st.char_filter_admits(*c))
            .collect();
        // Max-length: cap against remaining capacity (the old preedit has
        // already been removed, so the doc length here excludes it).
        let clean = if let Some(max) = st.max_length {
            let current_len = st.document.character_count();
            let sel_len = if st.cursor.has_selection() {
                st.cursor
                    .selected_text()
                    .ok()
                    .unwrap_or_default()
                    .chars()
                    .count()
            } else {
                0
            };
            let remaining = max.saturating_sub(current_len.saturating_sub(sel_len));
            clean.chars().take(remaining).collect::<String>()
        } else {
            clean
        };
        if !clean.is_empty() {
            let start = st.cursor.position();
            let _ = st.cursor.insert_text(&clean);
            let end = st.cursor.position();
            st.ime_preedit = Some(clean);
            st.ime_preedit_range = Some(start..end);
        } else {
            st.ime_preedit = None;
        }
        st.cursor.end_edit_block();
        st.pending_text_changed = true;
    }
    sync_cursor_signals(state);
    report_ime_cursor_area(state, ctx);
    ctx.request_frame();
    EventResponse::Handled
}

/// Drop any active preedit, removing its tentative text from the document.
/// Called on commit (before the finalised insert) and on focus loss.
pub(crate) fn clear_ime_preedit(state: &SharedState) {
    let mut st = state.borrow_mut();
    if let Some(range) = st.ime_preedit_range.take() {
        let doc_end = st.document.character_count();
        let start = range.start.min(doc_end);
        let end = range.end.min(doc_end);
        if start < end {
            st.cursor.set_position(start, MoveMode::MoveAnchor);
            st.cursor.set_position(end, MoveMode::KeepAnchor);
            let _ = st.cursor.remove_selected_text();
        }
    }
    st.ime_preedit = None;
}

/// Batch pending characters from an IME commit.
fn push_pending_chars(state: &SharedState, ctx: &mut EventContext, text: &str) -> EventResponse {
    let read_only = state.borrow().read_only;
    if read_only {
        return EventResponse::Handled;
    }
    // Strip newlines (single-line enforcement), control characters,
    // and anything the optional filter rejects.
    let clean: String = {
        let st = state.borrow();
        text.chars()
            .filter(|c| !c.is_control() && *c != '\n' && *c != '\r')
            .filter(|c| st.char_filter_admits(*c))
            .collect()
    };
    if clean.is_empty() {
        return EventResponse::Handled;
    }
    {
        let mut st = state.borrow_mut();
        // Max-length enforcement.
        if let Some(max) = st.max_length {
            let current_len = st
                .document
                .to_plain_text()
                .unwrap_or_default()
                .chars()
                .count();
            let sel_len = if st.cursor.has_selection() {
                st.cursor
                    .selected_text()
                    .ok()
                    .unwrap_or_default()
                    .chars()
                    .count()
            } else {
                0
            };
            let remaining = max.saturating_sub(current_len.saturating_sub(sel_len));
            if remaining == 0 {
                return EventResponse::Handled;
            }
            let truncated: String = clean.chars().take(remaining).collect();
            st.pending_chars.push_str(&truncated);
        } else {
            st.pending_chars.push_str(&clean);
        }
    }
    sync_cursor_signals(state);
    report_ime_cursor_area(state, ctx);
    ctx.request_frame();
    EventResponse::Handled
}

// ── Plain-text clipboard helpers ────────────────────────────────────

pub(crate) fn clipboard_copy(state: &mut TextInputState, ctx: &EventContext) {
    // Secure fields suppress copy while masked — the plaintext must not
    // reach the system clipboard. Allowed when revealed or opted in.
    if !state.copy_allowed() {
        return;
    }
    if !state.cursor.has_selection() {
        return;
    }
    let plain = state.cursor.selected_text().ok().unwrap_or_default();
    if let Some(cb) = ctx.app_state::<ClipboardHandle>() {
        let _ = cb.set_text(&plain);
    }
}

pub(crate) fn clipboard_cut(state: &mut TextInputState, ctx: &EventContext) {
    // Block the whole cut (not just the copy half) on a masked secure
    // field, so the delete doesn't happen without the clipboard write.
    if !state.copy_allowed() {
        return;
    }
    if !state.cursor.has_selection() {
        return;
    }
    clipboard_copy(state, ctx);
    let _ = state.cursor.remove_selected_text();
    state.pending_text_changed = true;
}

pub(crate) fn clipboard_paste(state: &mut TextInputState, ctx: &EventContext) {
    let Some(cb) = ctx.app_state::<ClipboardHandle>() else {
        return;
    };
    let Ok(system) = cb.get_text() else {
        return;
    };
    if system.is_empty() {
        return;
    }
    // Strip control characters (single-line enforcement — covers newlines,
    // tabs, NUL, …) and anything the optional filter rejects. This matches the
    // typing / IME paths exactly; previously paste let tabs and other control
    // chars through, an asymmetry a single-line field shouldn't have (and the
    // `char_filter` hook can't re-admit them — it only rejects).
    let clean: String = system
        .chars()
        .filter(|c| !c.is_control() && *c != '\n' && *c != '\r')
        .filter(|c| state.char_filter_admits(*c))
        .collect();
    if clean.is_empty() {
        return;
    }
    // Max-length enforcement.
    if let Some(max) = state.max_length {
        let current_len = state
            .document
            .to_plain_text()
            .unwrap_or_default()
            .chars()
            .count();
        let sel_len = if state.cursor.has_selection() {
            state
                .cursor
                .selected_text()
                .ok()
                .unwrap_or_default()
                .chars()
                .count()
        } else {
            0
        };
        let remaining = max.saturating_sub(current_len.saturating_sub(sel_len));
        if remaining == 0 {
            return;
        }
        let truncated: String = clean.chars().take(remaining).collect();
        let _ = state.cursor.insert_text(&truncated);
    } else {
        let _ = state.cursor.insert_text(&clean);
    }
    state.cursor.clear_selection();
    state.pending_text_changed = true;
}