zng_wgt_text/node/
resolve.rs

1use std::{borrow::Cow, num::Wrapping, sync::Arc};
2
3use parking_lot::RwLock;
4use zng_app::{
5    access::{ACCESS_SELECTION_EVENT, ACCESS_TEXT_EVENT},
6    event::CommandHandle,
7    render::FontSynthesis,
8    update::UpdateOp,
9    widget::{
10        WIDGET,
11        info::WIDGET_TREE_CHANGED_EVENT,
12        node::{UiNode, UiNodeOp, match_node},
13    },
14    window::WINDOW,
15};
16use zng_ext_clipboard::{CLIPBOARD, COPY_CMD, CUT_CMD, PASTE_CMD};
17use zng_ext_font::{CaretIndex, FONT_CHANGED_EVENT, FONTS, FontFaceList, SegmentedText};
18use zng_ext_input::{
19    focus::{FOCUS, FOCUS_CHANGED_EVENT, FocusInfoBuilder, WidgetInfoFocusExt as _},
20    keyboard::{KEY_INPUT_EVENT, KEYBOARD},
21};
22use zng_ext_l10n::LANG_VAR;
23use zng_ext_undo::UNDO;
24use zng_ext_window::{IME_EVENT, WINDOW_Ext as _, WindowLoadingHandle, cmd::CANCEL_IME_CMD};
25use zng_layout::context::{DIRECTION_VAR, LayoutDirection};
26use zng_view_api::keyboard::{Key, KeyState};
27use zng_wgt::prelude::*;
28
29use crate::{
30    ACCEPTS_ENTER_VAR, ACCEPTS_TAB_VAR, AUTO_SELECTION_VAR, AutoSelection, FONT_FAMILY_VAR, FONT_STRETCH_VAR, FONT_STYLE_VAR,
31    FONT_SYNTHESIS_VAR, FONT_WEIGHT_VAR, MAX_CHARS_COUNT_VAR, OBSCURE_TXT_VAR, TEXT_EDITABLE_VAR, TEXT_SELECTABLE_VAR, TEXT_TRANSFORM_VAR,
32    WHITE_SPACE_VAR,
33    cmd::{EDIT_CMD, SELECT_ALL_CMD, SELECT_CMD, TextEditOp, TextSelectOp, UndoTextEditOp},
34};
35
36use super::{CaretInfo, ImePreview, PendingLayout, RESOLVED_TEXT, ResolvedText, RichTextCopyParam, SelectionBy, TEXT};
37
38/// An UI node that resolves the text context vars, applies the text transform and white space correction and segments the `text`.
39///
40/// This node setups the [`ResolvedText`] for all inner nodes, the `Text!` widget includes this node in the [`NestGroup::EVENT`] group,
41/// so all properties except [`NestGroup::CONTEXT`] have access using the [`TEXT::resolved`] method.
42///
43/// This node also sets the accessibility label to the resolved text.
44///
45/// [`NestGroup::EVENT`]: zng_wgt::prelude::NestGroup::EVENT
46/// [`NestGroup::CONTEXT`]: zng_wgt::prelude::NestGroup::CONTEXT
47pub fn resolve_text(child: impl IntoUiNode, text: impl IntoVar<Txt>) -> UiNode {
48    let child = resolve_text_font(child);
49    let child = resolve_text_access(child);
50    let child = resolve_text_edit(child);
51    let child = resolve_text_segments(child);
52    resolve_text_context(child, text.into_var())
53}
54fn resolve_text_context(child: impl IntoUiNode, text: Var<Txt>) -> UiNode {
55    let mut resolved = None;
56    match_node(child, move |child, op| match op {
57        UiNodeOp::Init => {
58            resolved = Some(Arc::new(RwLock::new(ResolvedText {
59                txt: text.clone(),
60                ime_preview: None,
61                synthesis: FontSynthesis::empty(),
62                faces: FontFaceList::empty(),
63                segmented_text: SegmentedText::new(Txt::from_static(""), LayoutDirection::LTR),
64                pending_layout: PendingLayout::empty(),
65                pending_edit: false,
66                caret: CaretInfo {
67                    opacity: var(0.fct()).read_only(),
68                    index: None,
69                    selection_index: None,
70                    initial_selection: None,
71                    index_version: Wrapping(0),
72                    used_retained_x: false,
73                    skip_next_scroll: false,
74                },
75                selection_by: SelectionBy::Command,
76                selection_toolbar_is_open: false,
77            })));
78
79            RESOLVED_TEXT.with_context(&mut resolved, || child.init());
80        }
81        UiNodeOp::Deinit => {
82            RESOLVED_TEXT.with_context(&mut resolved, || child.deinit());
83
84            resolved = None;
85        }
86        UiNodeOp::Layout { wl, final_size } => RESOLVED_TEXT.with_context(&mut resolved, || {
87            *final_size = child.layout(wl);
88            TEXT.resolve().pending_layout = PendingLayout::empty();
89        }),
90        op => RESOLVED_TEXT.with_context(&mut resolved, || child.op(op)),
91    })
92}
93fn resolve_text_font(child: impl IntoUiNode) -> UiNode {
94    enum State {
95        Reload,
96        Loading {
97            response: ResponseVar<FontFaceList>,
98            _update_handle: VarHandle,
99            _window_load_handle: Option<WindowLoadingHandle>,
100        },
101        Loaded,
102    }
103    let mut state = State::Reload;
104
105    match_node(child, move |_, op| {
106        match op {
107            UiNodeOp::Init => {
108                WIDGET
109                    .sub_var(&FONT_FAMILY_VAR)
110                    .sub_var(&FONT_STYLE_VAR)
111                    .sub_var(&FONT_WEIGHT_VAR)
112                    .sub_var(&FONT_STRETCH_VAR)
113                    .sub_event(&FONT_CHANGED_EVENT)
114                    .sub_var(&FONT_SYNTHESIS_VAR)
115                    .sub_var(&LANG_VAR);
116            }
117            UiNodeOp::Update { .. } => {
118                if FONT_CHANGED_EVENT.has_update(true) | FONT_FAMILY_VAR.is_new()
119                    || FONT_STYLE_VAR.is_new()
120                    || FONT_WEIGHT_VAR.is_new()
121                    || FONT_STRETCH_VAR.is_new()
122                    || LANG_VAR.is_new()
123                {
124                    state = State::Reload;
125                } else if let State::Loading { response, .. } = &state {
126                    if let Some(f) = response.rsp() {
127                        tracing::trace!("text {:?} fonts finished loading", WIDGET.id());
128                        let mut txt = TEXT.resolve();
129                        txt.synthesis = FONT_SYNTHESIS_VAR.get() & f.best().synthesis_for(FONT_STYLE_VAR.get(), FONT_WEIGHT_VAR.get());
130                        txt.faces = f;
131                        state = State::Loaded;
132
133                        WIDGET.layout();
134                    }
135                } else if let State::Loaded = &state
136                    && FONT_SYNTHESIS_VAR.is_new()
137                {
138                    let mut txt = TEXT.resolve();
139                    txt.synthesis = FONT_SYNTHESIS_VAR.get() & txt.faces.best().synthesis_for(FONT_STYLE_VAR.get(), FONT_WEIGHT_VAR.get());
140
141                    WIDGET.render();
142                }
143            }
144            UiNodeOp::Deinit => {
145                state = State::Reload;
146                return;
147            }
148            _ => {}
149        }
150
151        if let State::Reload = &state {
152            let font_list = FONT_FAMILY_VAR.with(|family| {
153                LANG_VAR.with(|lang| {
154                    tracing::trace!("text {:?} begin load font list", WIDGET.id());
155                    FONTS.list(
156                        family,
157                        FONT_STYLE_VAR.get(),
158                        FONT_WEIGHT_VAR.get(),
159                        FONT_STRETCH_VAR.get(),
160                        lang.best(),
161                    )
162                })
163            });
164
165            if let Some(f) = font_list.rsp() {
166                tracing::trace!("font list already loaded");
167                let mut txt = TEXT.resolve();
168                txt.synthesis = FONT_SYNTHESIS_VAR.get() & f.best().synthesis_for(FONT_STYLE_VAR.get(), FONT_WEIGHT_VAR.get());
169                txt.faces = f;
170                state = State::Loaded;
171
172                WIDGET.layout();
173            } else {
174                tracing::trace!("font list loading");
175                state = State::Loading {
176                    _update_handle: font_list.subscribe(UpdateOp::Update, WIDGET.id()),
177                    response: font_list,
178                    _window_load_handle: WINDOW.loading_handle(1.secs(), "font-list"),
179                };
180            }
181        }
182    })
183}
184fn resolve_text_access(child: impl IntoUiNode) -> UiNode {
185    match_node(child, |child, op| match op {
186        UiNodeOp::Init => {
187            WIDGET
188                .sub_var_info(&TEXT.resolved().txt)
189                .sub_var_info(&TEXT_EDITABLE_VAR)
190                .sub_var_info(&TEXT_SELECTABLE_VAR)
191                .sub_var_info(&OBSCURE_TXT_VAR);
192        }
193        UiNodeOp::Info { info } => {
194            let editable = TEXT_EDITABLE_VAR.get();
195            if editable || TEXT_SELECTABLE_VAR.get() {
196                FocusInfoBuilder::new(info).focusable_passive(true);
197            }
198
199            child.info(info);
200
201            if !editable
202                && !OBSCURE_TXT_VAR.get()
203                && let Some(mut a) = info.access()
204            {
205                a.set_label(TEXT.resolved().segmented_text.text().clone());
206            }
207        }
208        _ => {}
209    })
210}
211fn resolve_text_segments(child: impl IntoUiNode) -> UiNode {
212    match_node(child, |_, op| {
213        let mut segment = false;
214        match op {
215            UiNodeOp::Init => {
216                WIDGET
217                    .sub_var(&TEXT.resolved().txt)
218                    .sub_var(&TEXT_TRANSFORM_VAR)
219                    .sub_var(&WHITE_SPACE_VAR)
220                    .sub_var(&DIRECTION_VAR)
221                    .sub_var(&TEXT_EDITABLE_VAR);
222
223                segment = true;
224            }
225            UiNodeOp::Update { .. } => {
226                segment = TEXT.resolved().txt.is_new()
227                    || TEXT_TRANSFORM_VAR.is_new()
228                    || WHITE_SPACE_VAR.is_new()
229                    || DIRECTION_VAR.is_new()
230                    || TEXT_EDITABLE_VAR.is_new();
231            }
232            _ => {}
233        }
234        if segment {
235            let mut ctx = TEXT.resolve();
236
237            let mut txt = ctx.txt.get();
238
239            if !TEXT_EDITABLE_VAR.get() {
240                TEXT_TRANSFORM_VAR.with(|t| {
241                    if let Cow::Owned(t) = t.transform(&txt) {
242                        txt = t;
243                    }
244                });
245                WHITE_SPACE_VAR.with(|t| {
246                    if let Cow::Owned(t) = t.transform(&txt) {
247                        txt = t;
248                    }
249                });
250            }
251
252            let direction = DIRECTION_VAR.get();
253            if ctx.segmented_text.text() != &txt || ctx.segmented_text.base_direction() != direction {
254                ctx.segmented_text = SegmentedText::new(txt, direction);
255
256                ctx.pending_layout = PendingLayout::RESHAPE;
257                WIDGET.layout();
258            }
259        }
260    })
261}
262fn resolve_text_edit(child: impl IntoUiNode) -> UiNode {
263    // Use `ResolveTextEdit::get` to access.
264    let mut edit = None::<Box<ResolveTextEdit>>;
265
266    match_node(child, move |child, op| {
267        let mut enable = false;
268        match op {
269            UiNodeOp::Init => {
270                WIDGET
271                    .sub_var(&TEXT_EDITABLE_VAR)
272                    .sub_var(&TEXT_SELECTABLE_VAR)
273                    .sub_var(&MAX_CHARS_COUNT_VAR);
274                enable = TEXT_EDITABLE_VAR.get() || TEXT_SELECTABLE_VAR.get();
275            }
276            UiNodeOp::Deinit => {
277                edit = None;
278            }
279            UiNodeOp::Update { updates } => {
280                if TEXT_EDITABLE_VAR.is_new() || TEXT_SELECTABLE_VAR.is_new() {
281                    enable = TEXT_EDITABLE_VAR.get() || TEXT_SELECTABLE_VAR.get();
282                    if !enable && edit.is_some() {
283                        edit = None;
284                        TEXT.resolve().caret.opacity = var(0.fct()).read_only();
285                    }
286                }
287
288                if let Some(edit) = &mut edit {
289                    child.update(updates);
290
291                    resolve_text_edit_update(edit);
292
293                    if TEXT_EDITABLE_VAR.get() && TEXT.resolved().txt.capabilities().can_modify() {
294                        resolve_text_edit_events(edit);
295                    }
296                    if TEXT_EDITABLE_VAR.get() || TEXT_SELECTABLE_VAR.get() {
297                        resolve_text_edit_or_select_events(edit);
298                    }
299
300                    let enable = !OBSCURE_TXT_VAR.get() && TEXT.resolved().caret.selection_range().is_some();
301                    edit.cut.enabled().set(enable);
302                    edit.copy.enabled().set(enable);
303                }
304            }
305            _ => {}
306        }
307        if enable {
308            let edit = ResolveTextEdit::get(&mut edit);
309
310            let editable = TEXT_EDITABLE_VAR.get();
311            if editable {
312                let id = WIDGET.id();
313
314                edit.focus_changed = FOCUS_CHANGED_EVENT.subscribe(UpdateOp::Update, id);
315                edit.key_input = KEY_INPUT_EVENT.subscribe(UpdateOp::Update, id);
316                edit.access_text = ACCESS_TEXT_EVENT.subscribe(UpdateOp::Update, id);
317                edit.ime = IME_EVENT.subscribe(UpdateOp::Update, id);
318
319                let win_id = WINDOW.id();
320                let i = WIDGET_TREE_CHANGED_EVENT.var_map(
321                    move |args| {
322                        if args.tree.window_id() == win_id
323                            && let Some(wgt) = args.tree.get(id)
324                        {
325                            Some(wgt.interactivity())
326                        } else {
327                            None
328                        }
329                    },
330                    Interactivity::empty,
331                );
332                i.subscribe(UpdateOp::Update, id).perm();
333                edit.interactivity = Some(i);
334
335                edit.paste = PASTE_CMD.scoped(id).subscribe(true);
336                edit.edit = EDIT_CMD.scoped(id).subscribe(true);
337
338                edit.max_count = MAX_CHARS_COUNT_VAR.subscribe(UpdateOp::Update, id);
339
340                let mut ctx = TEXT.resolve();
341
342                enforce_max_count(&ctx.txt);
343
344                if FOCUS.is_focused(WIDGET.id()).get() {
345                    ctx.caret.opacity = KEYBOARD.caret_animation();
346                    edit.caret_animation = ctx.caret.opacity.subscribe(UpdateOp::Update, WIDGET.id());
347                }
348            }
349
350            if TEXT_SELECTABLE_VAR.get() {
351                let id = WIDGET.id();
352
353                edit.access_selection = ACCESS_SELECTION_EVENT.subscribe(UpdateOp::Update, id);
354
355                let enabled = !OBSCURE_TXT_VAR.get() && TEXT.resolved().caret.selection_range().is_some();
356                edit.copy = COPY_CMD.scoped(id).subscribe(enabled);
357                if editable {
358                    edit.cut = CUT_CMD.scoped(id).subscribe(enabled);
359                } else {
360                    // used in `render_selection`
361                    edit.focus_changed = FOCUS_CHANGED_EVENT.subscribe(UpdateOp::Update, id);
362
363                    edit.key_input = KEY_INPUT_EVENT.subscribe(UpdateOp::Update, id);
364                }
365            }
366        }
367    })
368}
369/// Data allocated only when `editable`.
370#[derive(Default)]
371struct ResolveTextEdit {
372    focus_changed: VarHandle,
373    key_input: VarHandle,
374    access_text: VarHandle,
375    access_selection: VarHandle,
376    ime: VarHandle,
377
378    interactivity: Option<Var<Interactivity>>,
379    caret_animation: VarHandle,
380
381    max_count: VarHandle,
382    cut: CommandHandle,
383    copy: CommandHandle,
384    paste: CommandHandle,
385    edit: CommandHandle,
386}
387impl ResolveTextEdit {
388    fn get(edit_data: &mut Option<Box<Self>>) -> &mut Self {
389        &mut *edit_data.get_or_insert_with(Default::default)
390    }
391}
392fn enforce_max_count(text: &Var<Txt>) {
393    let max_count = MAX_CHARS_COUNT_VAR.get();
394    if max_count > 0 {
395        let count = text.with(|t| t.chars().count());
396        if count > max_count {
397            tracing::debug!("txt var set to text longer than can be typed");
398            text.modify(move |t| {
399                if let Some((i, _)) = t.as_str().char_indices().nth(max_count) {
400                    t.to_mut().truncate(i);
401                }
402            });
403        }
404    }
405}
406fn resolve_text_edit_events(edit: &mut ResolveTextEdit) {
407    if let Some(i) = edit.interactivity.as_ref().unwrap().get_new()
408        && i.is_disabled()
409    {
410        edit.caret_animation = VarHandle::dummy();
411        TEXT.resolve().caret.opacity = var(0.fct()).read_only();
412    }
413
414    if TEXT.resolved().pending_edit {
415        return;
416    }
417    let widget = WIDGET.info();
418    if !widget.interactivity().is_enabled() {
419        return;
420    }
421
422    let prev_caret = {
423        let r = TEXT.resolved();
424        (r.caret.index, r.caret.index_version, r.caret.selection_index)
425    };
426
427    KEY_INPUT_EVENT.each_update(false, |args| {
428        let mut ctx = TEXT.resolve();
429        if args.state == KeyState::Pressed && args.target.widget_id() == widget.id() {
430            match &args.key {
431                Key::Backspace => {
432                    let caret = &mut ctx.caret;
433                    if caret.selection_index.is_some() || caret.index.unwrap_or(CaretIndex::ZERO).index > 0 {
434                        if args.modifiers.is_only_ctrl() {
435                            args.propagation.stop();
436                            ctx.selection_by = SelectionBy::Keyboard;
437                            drop(ctx);
438                            TextEditOp::backspace_word().call_edit_op();
439                        } else if args.modifiers.is_empty() {
440                            args.propagation.stop();
441                            ctx.selection_by = SelectionBy::Keyboard;
442                            drop(ctx);
443                            TextEditOp::backspace().call_edit_op();
444                        }
445                    }
446                }
447                Key::Delete => {
448                    let caret = &mut ctx.caret;
449                    let caret_idx = caret.index.unwrap_or(CaretIndex::ZERO);
450                    if caret.selection_index.is_some() || caret_idx.index < ctx.segmented_text.text().len() {
451                        if args.modifiers.is_only_ctrl() {
452                            args.propagation.stop();
453                            ctx.selection_by = SelectionBy::Keyboard;
454                            drop(ctx);
455                            TextEditOp::delete_word().call_edit_op();
456                        } else if args.modifiers.is_empty() {
457                            args.propagation.stop();
458                            ctx.selection_by = SelectionBy::Keyboard;
459                            drop(ctx);
460                            TextEditOp::delete().call_edit_op();
461                        }
462                    }
463                }
464                _ => {
465                    let insert = args.insert_str();
466                    if !insert.is_empty() {
467                        let skip = (args.is_tab() && !ACCEPTS_TAB_VAR.get()) || (args.is_line_break() && !ACCEPTS_ENTER_VAR.get());
468                        if !skip {
469                            args.propagation.stop();
470                            ctx.selection_by = SelectionBy::Keyboard;
471                            drop(ctx);
472                            TextEditOp::insert(Txt::from_str(insert)).call_edit_op();
473                        }
474                    }
475                }
476            }
477        }
478    });
479    FOCUS_CHANGED_EVENT.each_update(true, |args| {
480        let mut ctx = TEXT.resolve();
481        let caret = &mut ctx.caret;
482        let caret_index = &mut caret.index;
483
484        if args.is_focused(widget.id()) {
485            if caret_index.is_none() {
486                *caret_index = Some(CaretIndex::ZERO);
487            } else {
488                // restore animation when the caret_index did not change
489                caret.opacity = KEYBOARD.caret_animation();
490                edit.caret_animation = caret.opacity.subscribe(UpdateOp::RenderUpdate, widget.id());
491            }
492        } else {
493            edit.caret_animation = VarHandle::dummy();
494            caret.opacity = var(0.fct()).read_only();
495        }
496
497        let auto_select = AUTO_SELECTION_VAR.get();
498        if auto_select != AutoSelection::DISABLED && caret.selection_index.is_some() && TEXT_SELECTABLE_VAR.get() {
499            if auto_select.contains(AutoSelection::CLEAR_ON_BLUR) {
500                if let Some(rich) = TEXT.try_rich() {
501                    if args.is_focus_leave(rich.root_id) {
502                        // deselect if the ALT return and parent scope return are not inside the rich text context
503
504                        if let Some(rich_root) = rich.root_info() {
505                            let alt_return = FOCUS.alt_return().with(|p| p.as_ref().map(|p| p.widget_id()));
506                            if alt_return.is_none() || rich_root.descendants().all(|d| d.id() != alt_return.unwrap()) {
507                                // not ALT return
508                                if let Some(info) = WIDGET.info().into_focusable(true, true)
509                                    && let Some(scope) = info.scope()
510                                {
511                                    let parent_return = FOCUS.return_focused(scope.info().id()).with(|p| p.as_ref().map(|p| p.widget_id()));
512                                    if parent_return.is_none() || rich_root.descendants().all(|d| d.id() != alt_return.unwrap()) {
513                                        // not parent scope return
514                                        SELECT_CMD.scoped(widget.id()).notify_param(TextSelectOp::next());
515                                    }
516                                }
517                            }
518                        }
519                    }
520                } else if args.is_blur(widget.id()) {
521                    // deselect if the widget is not the ALT return focus and is not the parent scope return focus.
522
523                    let us = Some(widget.id());
524                    let alt_return = FOCUS.alt_return().with(|p| p.as_ref().map(|p| p.widget_id()));
525                    if alt_return != us {
526                        // not ALT return
527                        if let Some(info) = WIDGET.info().into_focusable(true, true)
528                            && let Some(scope) = info.scope()
529                        {
530                            let parent_return = FOCUS.return_focused(scope.info().id()).with(|p| p.as_ref().map(|p| p.widget_id()));
531                            if parent_return != us {
532                                // not parent scope return
533                                SELECT_CMD.scoped(widget.id()).notify_param(TextSelectOp::next());
534                            }
535                        }
536                    }
537                }
538            }
539
540            if auto_select.contains(AutoSelection::ALL_ON_FOCUS_KEYBOARD) && args.highlight && args.is_focus(widget.id()) {
541                // select all on keyboard caused focus
542                SELECT_ALL_CMD.scoped(widget.id()).notify();
543            }
544
545            // ALL_ON_FOCUS_POINTER handled by `layout_text_edit_events`
546        }
547    });
548
549    CUT_CMD.scoped(widget.id()).each_update(true, false, |args| {
550        let mut ctx = TEXT.resolve();
551        if let Some(range) = ctx.caret.selection_char_range() {
552            args.propagation.stop();
553            ctx.selection_by = SelectionBy::Command;
554            CLIPBOARD.set_text(Txt::from_str(&ctx.segmented_text.text()[range]));
555            drop(ctx);
556            TextEditOp::delete().call_edit_op();
557        }
558    });
559    PASTE_CMD.scoped(widget.id()).each_update(true, false, |args| {
560        if let Some(paste) = CLIPBOARD.text().ok().flatten()
561            && !paste.is_empty()
562        {
563            args.propagation.stop();
564            TEXT.resolve().selection_by = SelectionBy::Command;
565            TextEditOp::insert(paste).call_edit_op();
566        }
567    });
568    EDIT_CMD.scoped(widget.id()).each_update(true, false, |args| {
569        if let Some(op) = args.param::<UndoTextEditOp>() {
570            args.propagation.stop();
571
572            op.call();
573            if !TEXT.resolved().pending_edit {
574                TEXT.resolve().pending_edit = true;
575                WIDGET.update();
576            }
577        } else if let Some(op) = args.param::<TextEditOp>() {
578            args.propagation.stop();
579
580            op.clone().call_edit_op();
581        }
582    });
583    ACCESS_TEXT_EVENT.each_update(false, |args| {
584        if args.target.widget_id() == widget.id() {
585            args.propagation.stop();
586
587            if args.selection_only {
588                TextEditOp::insert(args.txt.clone())
589            } else {
590                let current_len = TEXT.resolved().txt.with(|t| t.len());
591                let new_len = args.txt.len();
592                TextEditOp::replace(0..current_len, args.txt.clone(), new_len..new_len)
593            }
594            .call_edit_op();
595        }
596    });
597    IME_EVENT.each_update(false, |args| {
598        let mut resegment = false;
599
600        if let Some((start, end)) = args.preview_caret {
601            // update preview txt
602
603            let mut ctx = TEXT.resolve();
604            let ctx = &mut *ctx;
605
606            if args.txt.is_empty() {
607                if let Some(preview) = ctx.ime_preview.take() {
608                    resegment = true;
609                    let caret = &mut ctx.caret;
610                    caret.set_index(preview.prev_caret);
611                    caret.selection_index = preview.prev_selection;
612                }
613            } else if let Some(preview) = &mut ctx.ime_preview {
614                resegment = preview.txt != args.txt;
615                if resegment {
616                    preview.txt = args.txt.clone();
617                }
618            } else {
619                resegment = true;
620                let caret = &mut ctx.caret;
621                ctx.ime_preview = Some(ImePreview {
622                    txt: args.txt.clone(),
623                    prev_caret: caret.index.unwrap_or(CaretIndex::ZERO),
624                    prev_selection: caret.selection_index,
625                });
626            }
627
628            // update preview caret/selection indexes.
629            if let Some(preview) = &ctx.ime_preview {
630                let caret = &mut ctx.caret;
631                let ime_start = if let Some(s) = preview.prev_selection {
632                    preview.prev_caret.index.min(s.index)
633                } else {
634                    preview.prev_caret.index
635                };
636                if start != end {
637                    let start = ime_start + start;
638                    let end = ime_start + end;
639                    resegment |= caret.selection_char_range() != Some(start..end);
640                    caret.set_char_selection(start, end);
641                } else {
642                    let start = ime_start + start;
643                    resegment |= caret.selection_index.is_some() || caret.index.map(|c| c.index) != Some(start);
644                    caret.set_char_index(start);
645                    caret.selection_index = None;
646                }
647            }
648        } else {
649            // commit IME insert
650
651            args.propagation.stop();
652            {
653                let mut ctx = TEXT.resolve();
654                if let Some(preview) = ctx.ime_preview.take() {
655                    // restore caret
656                    let caret = &mut ctx.caret;
657                    caret.set_index(preview.prev_caret);
658                    caret.selection_index = preview.prev_selection;
659
660                    if args.txt.is_empty() {
661                        // the actual insert already re-segments, except in this case
662                        // where there is nothing to insert.
663                        resegment = true;
664                    }
665                }
666            }
667
668            if !args.txt.is_empty() {
669                // actual insert
670                let mut ctx = TEXT.resolve();
671                ctx.selection_by = SelectionBy::Keyboard;
672
673                // if the committed text is equal the last preview reshape is skipped
674                // leaving behind the IME underline highlight.
675                ctx.pending_layout |= PendingLayout::UNDERLINE;
676                WIDGET.layout();
677
678                drop(ctx);
679                TextEditOp::insert(args.txt.clone()).call_edit_op();
680            }
681        }
682
683        if resegment {
684            let mut ctx = TEXT.resolve();
685
686            // re-segment text to insert or remove the preview
687            let mut text = ctx.txt.get();
688            if let Some(preview) = &ctx.ime_preview {
689                if let Some(s) = preview.prev_selection {
690                    let range = if preview.prev_caret.index < s.index {
691                        preview.prev_caret.index..s.index
692                    } else {
693                        s.index..preview.prev_caret.index
694                    };
695                    text.to_mut().replace_range(range, preview.txt.as_str());
696                } else {
697                    text.to_mut().insert_str(preview.prev_caret.index, preview.txt.as_str());
698                }
699                text.end_mut();
700            }
701            ctx.segmented_text = SegmentedText::new(text, DIRECTION_VAR.get());
702
703            ctx.pending_layout |= PendingLayout::RESHAPE;
704            WIDGET.layout();
705        }
706    });
707
708    let mut ctx = TEXT.resolve();
709    let caret = &mut ctx.caret;
710
711    if (caret.index, caret.index_version, caret.selection_index) != prev_caret {
712        caret.used_retained_x = false;
713        if caret.index.is_none() || !FOCUS.is_focused(widget.id()).get() {
714            edit.caret_animation = VarHandle::dummy();
715            caret.opacity = var(0.fct()).read_only();
716        } else {
717            caret.opacity = KEYBOARD.caret_animation();
718            edit.caret_animation = caret.opacity.subscribe(UpdateOp::RenderUpdate, widget.id());
719        }
720        ctx.pending_layout |= PendingLayout::CARET;
721        WIDGET.layout(); // update caret_origin
722    }
723}
724fn resolve_text_edit_or_select_events(_: &mut ResolveTextEdit) {
725    let widget_id = WIDGET.id();
726
727    COPY_CMD.scoped(widget_id).each_update(true, false, |args| {
728        let ctx = TEXT.resolved();
729        if let Some(range) = ctx.caret.selection_char_range() {
730            args.propagation.stop();
731            let txt = Txt::from_str(&ctx.segmented_text.text()[range]);
732            if let Some(rt) = args.param::<RichTextCopyParam>() {
733                rt.set_text(txt);
734            } else {
735                let _ = CLIPBOARD.set_text(txt);
736            }
737        }
738    });
739    ACCESS_SELECTION_EVENT.each_update(false, |args| {
740        if args.start.0.widget_id() == widget_id && args.caret.0.widget_id() == widget_id {
741            args.propagation.stop();
742
743            let mut ctx = TEXT.resolve();
744
745            ctx.caret.set_char_selection(args.start.1, args.caret.1);
746
747            ctx.pending_layout |= PendingLayout::CARET;
748            WIDGET.layout();
749        }
750    });
751}
752fn resolve_text_edit_update(_: &mut ResolveTextEdit) {
753    let mut ctx = TEXT.resolve();
754    let ctx = &mut *ctx;
755    if ctx.txt.is_new() {
756        if !ctx.pending_edit && UNDO.scope() == Some(WIDGET.id()) {
757            UNDO.clear();
758        }
759
760        if let Some(p) = ctx.ime_preview.take() {
761            ctx.caret.index = Some(p.prev_caret);
762            ctx.caret.selection_index = p.prev_selection;
763
764            CANCEL_IME_CMD.scoped(WINDOW.id()).notify();
765        }
766
767        enforce_max_count(&ctx.txt);
768
769        // prevent invalid indexes
770        let caret = &mut ctx.caret;
771        if let Some(i) = &mut caret.index {
772            i.index = ctx.segmented_text.snap_grapheme_boundary(i.index);
773        }
774        if let Some(i) = &mut caret.selection_index {
775            i.index = ctx.segmented_text.snap_grapheme_boundary(i.index);
776        }
777        if let Some((cr, _)) = &mut caret.initial_selection {
778            cr.start.index = ctx.segmented_text.snap_grapheme_boundary(cr.start.index);
779            cr.end.index = ctx.segmented_text.snap_grapheme_boundary(cr.end.index);
780        }
781    }
782
783    if TEXT_EDITABLE_VAR.get() && MAX_CHARS_COUNT_VAR.is_new() {
784        enforce_max_count(&TEXT.resolved().txt);
785    }
786
787    // either txt was new or the edit did not change the text.
788    ctx.pending_edit = false;
789}