lieui 0.2.0-beta.1

A Rust GUI library with C/S architecture and builder-driven UI
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! Input — 单行文本输入框
//!
//! MVP 实现:基于 `PlainEditor` 管理文本/光标/IME/选区,
//! 在 `build()` 里用 `PlainEditor::cursor_geometry` 计算 caret 坐标,
//! 用 `selection_geometry` 生成选区高亮矩形,均以绝对定位 `Div` 呈现。
//!
//! 鼠标选取:单击定位光标、拖拽选择(鼠标捕获,可拖出控件)、
//! Shift+单击扩选、双击选词、三击选行。

use std::rc::Rc;
use std::time::{Duration, Instant};

use crate::animation::Animation;
use parley::BoundingBox;

use crate::clipboard;
use crate::event::{Event, EventContext, Key, MouseButton};
use crate::geometry::Color;
use crate::layout::style::FlexStyle;
use crate::layout::types::FlexAlign;
use crate::text::{
    PlainTextEditor, TextEngine, align_to_utf8_boundary, apply_plain_editor_style,
    create_plain_editor, editor_cursor_geometry, with_text_contexts,
};
use crate::theme;
use crate::view::node::{Listener, ViewNode};
use crate::view::paint::{PaintStyle, TextStyle};
use crate::widget::layout::LayoutAttr;
use crate::widget::{BuildContext, Stateful, Widget};

/// Input 组件的运行时状态。
pub struct InputState {
    pub editor: PlainTextEditor,
    pub focused: bool,
    /// 是否正在鼠标拖拽选择(MouseDown 置位,MouseUp 清除)。
    pub selecting: bool,
    /// 最近一次用户输入 / 聚焦时刻,用于闪烁光标的"常亮 → 开始闪"计时。
    pub last_activity: Option<Instant>,
    /// 闪烁定时器句柄;聚焦期间存在,失焦时置 `None` 自动取消注册。
    anim: Option<Animation>,
}

/// 闪烁光标参数:聚焦后先常亮 `BLINK_SOLID`,随后以 `BLINK_HALF` 为半周期闪烁。
const BLINK_SOLID: Duration = Duration::from_millis(500);
const BLINK_HALF: Duration = Duration::from_millis(530);
/// 动画 tick 间隔,与半周期一致,保证每次翻转都恰好触发一次重建。
const BLINK_INTERVAL: Duration = BLINK_HALF;

impl InputState {
    pub fn new(style: &TextStyle) -> Self {
        Self {
            editor: create_plain_editor(style),
            focused: false,
            selecting: false,
            last_activity: None,
            anim: None,
        }
    }
}

/// PatternFly 文本输入校验状态(validation state)。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputStatus {
    /// 普通
    #[default]
    Default,
    /// 校验通过
    Success,
    /// 警告
    Warning,
    /// 错误
    Danger,
}

/// PatternFly 文本输入尺寸。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputSize {
    /// 小
    Sm,
    /// 中(默认)
    #[default]
    Md,
    /// 大
    Lg,
}

pub struct Input {
    placeholder: String,
    layout: LayoutAttr,
    text_style: TextStyle,
    paint: PaintStyle,
    padding_h: Option<f32>,
    padding_v: Option<f32>,
    status: InputStatus,
    size: InputSize,
    multiline: bool,
    on_change: Option<Rc<dyn Fn(String)>>,
    on_submit: Option<Rc<dyn Fn(String)>>,
    /// 聚焦回调。
    on_focus: Option<Rc<dyn Fn()>>,
    /// 失焦回调。
    on_blur: Option<Rc<dyn Fn()>>,
    listeners: Vec<Listener>,
}

impl Input {
    pub fn new(placeholder: impl Into<String>) -> Self {
        let t = theme::current();
        Self {
            placeholder: placeholder.into(),
            layout: LayoutAttr::new().width(200.0),
            text_style: TextStyle {
                font_size: 0.0,
                color: t.text.regular_default,
                wrap: false,
                text_align: crate::view::paint::TextAlign::Start,
                ..TextStyle::default()
            },
            paint: PaintStyle::new()
                .background(t.background.primary_default)
                .border(1.0, t.border.default)
                .radius(t.radius.small),
            padding_h: None,
            padding_v: None,
            status: InputStatus::Default,
            size: InputSize::Md,
            multiline: false,
            on_change: None,
            on_submit: None,
            on_focus: None,
            on_blur: None,
            listeners: Vec::new(),
        }
    }

    pub fn multiline(mut self, v: bool) -> Self {
        self.multiline = v;
        self
    }

    pub fn width(mut self, w: f32) -> Self {
        self.layout = self.layout.width(w.max(0.0));
        self
    }

    pub fn height(mut self, h: f32) -> Self {
        self.layout = self.layout.height(h.max(0.0));
        self
    }

    /// 用完整布局属性(builder 式)设置本输入框的布局。
    pub fn layout(mut self, l: LayoutAttr) -> Self {
        self.layout = l;
        self
    }

    pub fn padding_h(mut self, v: f32) -> Self {
        self.padding_h = Some(v.max(0.0));
        self
    }

    pub fn padding_v(mut self, v: f32) -> Self {
        self.padding_v = Some(v.max(0.0));
        self
    }

    /// PatternFly 校验状态(非 focus 时以此着色边框)。
    pub fn status(mut self, s: InputStatus) -> Self {
        self.status = s;
        self
    }

    /// PatternFly 尺寸(决定默认 padding 与字号)。
    pub fn size(mut self, s: InputSize) -> Self {
        self.size = s;
        self
    }

    pub fn font_size(mut self, s: f64) -> Self {
        self.text_style.font_size = s;
        self
    }

    pub fn color(mut self, c: Color) -> Self {
        self.text_style.color = c;
        self
    }

    pub fn text_style(mut self, s: TextStyle) -> Self {
        self.text_style = s;
        self
    }

    pub fn placeholder_color(mut self, c: Color) -> Self {
        // placeholder 用更淡的文本色,用户可显式覆盖。
        self.text_style.color = c;
        self
    }

    pub fn background(mut self, c: Color) -> Self {
        self.paint.background_color = Some(c);
        self
    }

    pub fn border_color(mut self, c: Color) -> Self {
        self.paint.border_color = Some(c);
        self
    }

    pub fn radius(mut self, r: f32) -> Self {
        self.paint.border_radius = r;
        self
    }

    pub fn on_change<F: Fn(String) + 'static>(mut self, f: F) -> Self {
        self.on_change = Some(Rc::new(f));
        self
    }

    pub fn on_submit<F: Fn(String) + 'static>(mut self, f: F) -> Self {
        self.on_submit = Some(Rc::new(f));
        self
    }

    /// 聚焦回调(输入框获得焦点时触发)。
    pub fn on_focus<F: Fn() + 'static>(mut self, f: F) -> Self {
        self.on_focus = Some(Rc::new(f));
        self
    }

    /// 失焦回调(输入框失去焦点时触发)。
    pub fn on_blur<F: Fn() + 'static>(mut self, f: F) -> Self {
        self.on_blur = Some(Rc::new(f));
        self
    }

    pub fn on_click<F: Fn() + 'static>(mut self, f: F) -> Self {
        self.listeners.push(Listener::on_click(Rc::new(f)));
        self
    }
    pub fn on_click_with_ctx<F: Fn(&mut EventContext) + 'static>(mut self, f: F) -> Self {
        self.listeners.push(Listener::on_click_with_ctx(Rc::new(f)));
        self
    }

    fn focus_listener(
        state: &Stateful<InputState>,
        focused: bool,
        on_focus: Option<Rc<dyn Fn()>>,
        on_blur: Option<Rc<dyn Fn()>>,
    ) -> Listener {
        let state = state.clone();
        let event = if focused {
            crate::event::EventType::FocusIn
        } else {
            crate::event::EventType::FocusOut
        };
        Listener {
            event,
            kind: crate::view::node::ListenerKind::BuiltIn,
            callback: crate::view::node::Callback::WithCtx(Rc::new(
                move |ctx: &mut EventContext| {
                    state.update(|s| {
                        s.focused = focused;
                        let now = Instant::now();
                        if focused {
                            // 聚焦:进入"常亮"相位并启动闪烁定时器。
                            s.last_activity = Some(now);
                            if s.anim.is_none() {
                                s.anim = Some(Animation::new(BLINK_INTERVAL, || {
                                    crate::state::request_rebuild();
                                }));
                            }
                            if let Some(cb) = &on_focus {
                                cb();
                            }
                        } else {
                            // 失焦:停止闪烁定时器(Drop 自动取消注册)。
                            s.anim = None;
                            s.last_activity = None;
                            if let Some(cb) = &on_blur {
                                cb();
                            }
                        }
                    });
                    ctx.request_render();
                },
            )),
        }
    }

    fn key_listener(&self, state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        let on_change = self.on_change.clone();
        let on_submit = self.on_submit.clone();
        let multiline = self.multiline;
        Listener::on_key_down(Rc::new(move |ctx: &mut EventContext| {
            let Some(Event::KeyDown { key, modifiers }) = ctx.event() else {
                return;
            };
            let key = *key;
            let modifiers = *modifiers;
            let mut text_changed = false;
            {
                let mut st = state.get_mut();
                st.last_activity = Some(Instant::now());
                let editor = &mut st.editor;
                match key {
                    Key::Backspace => {
                        with_text_contexts(|fc, lc| editor.driver(fc, lc).backdelete());
                        text_changed = true;
                    }
                    Key::Delete => {
                        with_text_contexts(|fc, lc| editor.driver(fc, lc).delete());
                        text_changed = true;
                    }
                    Key::ArrowLeft => {
                        if modifiers.ctrl && modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_word_left());
                        } else if modifiers.ctrl {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_word_left());
                        } else if modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_left());
                        } else {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_left());
                        }
                    }
                    Key::ArrowRight => {
                        if modifiers.ctrl && modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_word_right());
                        } else if modifiers.ctrl {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_word_right());
                        } else if modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_right());
                        } else {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_right());
                        }
                    }
                    Key::ArrowUp => {
                        if modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_up());
                        } else {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_up());
                        }
                    }
                    Key::ArrowDown => {
                        if modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_down());
                        } else {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_down());
                        }
                    }
                    Key::Home => {
                        if modifiers.shift {
                            with_text_contexts(|fc, lc| {
                                editor.driver(fc, lc).select_to_text_start()
                            });
                        } else {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_to_text_start());
                        }
                    }
                    Key::End => {
                        if modifiers.shift {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_to_text_end());
                        } else {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).move_to_text_end());
                        }
                    }
                    Key::Character(c) => {
                        if modifiers.ctrl && (c == 'a' || c == 'A') {
                            with_text_contexts(|fc, lc| editor.driver(fc, lc).select_all());
                        } else if modifiers.ctrl && (c == 'c' || c == 'C') {
                            if let Some(selected) = editor.selected_text() {
                                clipboard::copy_text(selected);
                            }
                        } else if modifiers.ctrl && (c == 'x' || c == 'X') {
                            if let Some(selected) = editor.selected_text() {
                                clipboard::copy_text(selected);
                                with_text_contexts(|fc, lc| {
                                    editor.driver(fc, lc).delete_selection()
                                });
                                text_changed = true;
                            }
                        } else if modifiers.ctrl && (c == 'v' || c == 'V') {
                            if let Some(text) = clipboard::paste_text() {
                                with_text_contexts(|fc, lc| {
                                    editor.driver(fc, lc).insert_or_replace_selection(&text)
                                });
                                text_changed = true;
                            }
                        } else if !modifiers.ctrl && !modifiers.alt && !modifiers.meta {
                            let s = c.to_string();
                            with_text_contexts(|fc, lc| {
                                editor.driver(fc, lc).insert_or_replace_selection(&s)
                            });
                            text_changed = true;
                        }
                    }
                    Key::Enter => {
                        if multiline {
                            with_text_contexts(|fc, lc| {
                                editor.driver(fc, lc).insert_or_replace_selection("\n")
                            });
                            text_changed = true;
                        } else if let Some(cb) = &on_submit {
                            let text = editor.raw_text().to_string();
                            cb(text);
                        }
                    }
                    Key::Escape => {
                        // 取消选区,光标停在 focus 端。
                        with_text_contexts(|fc, lc| editor.driver(fc, lc).collapse_selection());
                    }
                    _ => {}
                }
            }
            if text_changed && let Some(cb) = &on_change {
                let text = state.get().editor.raw_text().to_string();
                cb(text);
            }
            ctx.request_render();
            ctx.request_rebuild();
        }))
        .builtin()
    }

    fn ime_preedit_listener(state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        Listener::on_ime_preedit(Rc::new(move |ctx: &mut EventContext| {
            let Some(Event::ImePreedit {
                text,
                cursor_start,
                cursor_end,
            }) = ctx.event()
            else {
                return;
            };
            let text = text.clone();
            state.update(|s| {
                with_text_contexts(|fc, lc| {
                    let mut driver = s.editor.driver(fc, lc);
                    if text.is_empty() {
                        // 空预编辑文本表示合成结束或取消,清理即可。
                        driver.clear_compose();
                    } else {
                        let start = cursor_start
                            .map(|i| align_to_utf8_boundary(&text, i))
                            .unwrap_or(text.len());
                        let end = cursor_end
                            .map(|i| align_to_utf8_boundary(&text, i))
                            .unwrap_or(start);
                        let start = start.min(end);
                        let end = end.max(start);
                        driver.set_compose(&text, Some((start, end)));
                    }
                });
            });
            ctx.request_render();
            ctx.request_rebuild();
        }))
        .builtin()
    }

    fn ime_commit_listener(state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        Listener::on_ime_commit(Rc::new(move |ctx: &mut EventContext| {
            let Some(Event::ImeCommit { text }) = ctx.event() else {
                return;
            };
            let text = text.clone();
            state.update(|s| {
                with_text_contexts(|fc, lc| {
                    let mut driver = s.editor.driver(fc, lc);
                    driver.clear_compose();
                    driver.insert_or_replace_selection(&text);
                });
            });
            ctx.request_render();
            ctx.request_rebuild();
        }))
        .builtin()
    }

    fn ime_disabled_listener(state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        Listener::on_ime_disabled(Rc::new(move |ctx: &mut EventContext| {
            state.update(|s| {
                with_text_contexts(|fc, lc| {
                    s.editor.driver(fc, lc).clear_compose();
                });
            });
            ctx.request_render();
            ctx.request_rebuild();
        }))
        .builtin()
    }

    /// 鼠标按下:定位光标 / Shift+单击扩选 / 双击选词 / 三击选行,
    /// 并请求鼠标捕获以支持拖出控件继续选择。
    fn mouse_down_listener(&self, state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        let (sz_pv, sz_ph, _sz_font) = input_size_metrics(theme::current(), self.size);
        let (pad_h, pad_v) = (
            self.padding_h.unwrap_or(sz_ph),
            self.padding_v.unwrap_or(sz_pv),
        );
        Listener::on_mouse_down(Rc::new(move |ctx: &mut EventContext| {
            let (x, y, shift, clicks) = match ctx.event() {
                Some(Event::MouseDown {
                    x,
                    y,
                    button: MouseButton::Left,
                    modifiers,
                    click_count,
                }) => (*x, *y, modifiers.shift, *click_count),
                _ => return,
            };
            let Some(rect) = ctx.current_rect() else {
                return;
            };
            // 窗口坐标 → 文本局部坐标(扣除控件位置与内边距)。
            let lx = x - rect.x - pad_h;
            let ly = y - rect.y - pad_v;
            state.update(|s| {
                s.selecting = true;
                s.last_activity = Some(Instant::now());
                with_text_contexts(|fc, lc| {
                    let mut driver = s.editor.driver(fc, lc);
                    match clicks {
                        2 => driver.select_word_at_point(lx, ly),
                        3 => driver.select_line_at_point(lx, ly),
                        _ if shift => driver.shift_click_extension(lx, ly),
                        _ => driver.move_to_point(lx, ly),
                    }
                });
            });
            ctx.capture_mouse();
            ctx.request_render();
            ctx.request_rebuild();
        }))
        .builtin()
    }

    /// 鼠标移动:拖拽中持续扩展选区(仅 selecting 期间生效)。
    fn mouse_move_listener(&self, state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        let (sz_pv, sz_ph, _sz_font) = input_size_metrics(theme::current(), self.size);
        let (pad_h, pad_v) = (
            self.padding_h.unwrap_or(sz_ph),
            self.padding_v.unwrap_or(sz_pv),
        );
        Listener::on_mouse_move(Rc::new(move |ctx: &mut EventContext| {
            if !state.get().selecting {
                return;
            }
            let (x, y) = match ctx.event() {
                Some(Event::MouseMove { x, y }) => (*x, *y),
                _ => return,
            };
            let Some(rect) = ctx.current_rect() else {
                return;
            };
            let lx = x - rect.x - pad_h;
            let ly = y - rect.y - pad_v;
            state.update(|s| {
                s.last_activity = Some(Instant::now());
                with_text_contexts(|fc, lc| {
                    s.editor.driver(fc, lc).extend_selection_to_point(lx, ly);
                });
            });
            ctx.request_render();
            ctx.request_rebuild();
        }))
        .builtin()
    }

    /// 鼠标释放:结束拖拽选择(捕获由 EventManager 自动解除)。
    fn mouse_up_listener(state: &Stateful<InputState>) -> Listener {
        let state = state.clone();
        Listener::on_mouse_up(Rc::new(move |_ctx: &mut EventContext| {
            if state.get().selecting {
                state.update(|s| s.selecting = false);
            }
        }))
        .builtin()
    }
}

/// 返回 (垂直 padding, 水平 padding, 字号)。
fn input_size_metrics(t: theme::Theme, s: InputSize) -> (f32, f32, f64) {
    match s {
        InputSize::Sm => (4.0, 8.0, t.font.sm),
        InputSize::Md => (6.0, 12.0, t.font.sm),
        InputSize::Lg => (8.0, 16.0, t.font.md),
    }
}

/// 校验状态对应的边框色。
fn input_status_color(t: theme::Theme, s: InputStatus) -> Color {
    match s {
        InputStatus::Default => t.border.default,
        InputStatus::Success => t.status.success,
        InputStatus::Warning => t.status.warning,
        InputStatus::Danger => t.status.danger,
    }
}

impl Widget for Input {
    fn build(&self, ctx: &mut BuildContext) -> ViewNode {
        let t = theme::current();

        // 尺寸解析:size 决定默认 padding/字号,可被显式覆盖。
        let (sz_pv, sz_ph, sz_font) = input_size_metrics(t, self.size);
        let pv = self.padding_v.unwrap_or(sz_pv);
        let ph = self.padding_h.unwrap_or(sz_ph);
        let mut text_style = self.text_style.clone();
        if text_style.font_size <= 0.0 {
            text_style.font_size = sz_font;
        }

        let state = ctx.use_state(|| InputState::new(&text_style));

        let input_width = self.layout.width.unwrap_or(200.0);
        let input_height = self.layout.height;
        let content_width = (input_width - ph * 2.0).max(0.0);
        // 单行内容高度以实际文本行高(含 ascent/descent/行距)为准,而非仅 font_size;
        // 否则盒子比文本矮,文本会溢出并贴底。
        let line_h = TextEngine::measure_text("Mg", &text_style).1 as f32;
        let inner_height = input_height.unwrap_or(line_h + pv * 2.0);

        // 同步样式/宽度,并计算光标与选区几何。
        let (text_content, caret, sel_rects, focused) = {
            let mut st = state.get_mut();
            apply_plain_editor_style(&mut st.editor, &text_style);
            let caret =
                editor_cursor_geometry(&mut st.editor, &text_style, Some(content_width), 1.0);
            // editor_cursor_geometry 已 refresh_layout,选区几何直接可取。
            let sel_rects = st.editor.selection_geometry();
            let text_content = st.editor.raw_text().to_string();
            (text_content, caret, sel_rects, st.focused)
        };

        // 闪烁光标:聚焦后先常亮一段时间,再按半周期翻转显隐。
        let show_caret = if focused {
            match state.get().last_activity {
                Some(t) => {
                    let elapsed = Instant::now().duration_since(t);
                    if elapsed < BLINK_SOLID {
                        true
                    } else {
                        let half = BLINK_HALF.as_millis();
                        ((elapsed - BLINK_SOLID).as_millis() / half).is_multiple_of(2)
                    }
                }
                None => true,
            }
        } else {
            false
        };

        let is_empty = text_content.is_empty();
        let show_placeholder = is_empty && !focused;
        let display_text = if show_placeholder {
            self.placeholder.clone()
        } else {
            text_content
        };
        let display_style = {
            let mut s = if show_placeholder {
                let mut s = text_style.clone();
                s.color = t.text.subtle_default;
                s
            } else {
                text_style.clone()
            };
            s.wrap = self.multiline;
            s
        };

        // 单行时让文本垂直居中:以一致的文本行高(line_h)计算居中偏移,
        // 避免空文本时 measure 返回 0 导致光标错位。多行时无需偏移。
        let content_inner_h = inner_height - pv * 2.0;
        let center_offset = if self.multiline {
            0.0
        } else {
            ((content_inner_h - line_h) / 2.0).max(0.0)
        };

        let text_node = ViewNode::Text {
            content: display_text,
            style: display_style,
            layout: FlexStyle::default().flex_grow(1.0),
            key: None,
            listeners: vec![],
        };

        // 选区高亮:排在文本之前(先绘),品牌色半透明,不遮挡字形。
        let mut children: Vec<ViewNode> = Vec::new();
        if focused && !sel_rects.is_empty() {
            let b = t.text.brand_default;
            let sel_color = Color::rgba(b.r, b.g, b.b, 0x4d);
            for (i, (bb, _line)) in sel_rects.iter().enumerate() {
                let w = (bb.x1 - bb.x0) as f32;
                let h = (bb.y1 - bb.y0) as f32;
                if w <= 0.0 || h <= 0.0 {
                    continue;
                }
                children.push(ViewNode::Div {
                    layout: FlexStyle::default()
                        .width(w)
                        .height(h)
                        .absolute()
                        .position_left(ph + bb.x0 as f32)
                        .position_top(pv + center_offset + bb.y0.max(0.0) as f32),
                    paint: PaintStyle::new().background(sel_color),
                    key: Some(format!("__sel_{}__", i)),
                    children: vec![],
                    listeners: vec![],
                });
            }
        }
        children.push(text_node);

        // 光标:聚焦、几何可用且当前处于"点亮"相位时显示。
        if focused
            && show_caret
            && let Some(BoundingBox { x0, y0, y1, .. }) = caret
        {
            let caret_y = y0.max(0.0);
            // 光标高度取该行块高度(与文本行一致),不再撑满整个输入框。
            let caret_height = (y1 - y0).max(8.0);
            let caret = ViewNode::Div {
                layout: FlexStyle::default()
                    .width(1.0)
                    .height(caret_height as f32)
                    .absolute()
                    .position_left(ph + x0 as f32)
                    .position_top(pv + center_offset + caret_y as f32),
                paint: PaintStyle::new().background(text_style.color),
                key: Some("__ime_caret__".to_string()),
                children: vec![],
                listeners: vec![],
            };
            children.push(caret);
        }

        // 焦点时边框用品牌色;否则按校验状态着色(非 Default)。
        let mut paint = self.paint.clone();
        if focused {
            paint.border_color = Some(t.text.brand_default);
        } else if self.status != InputStatus::Default {
            paint.border_color = Some(input_status_color(t, self.status));
        }

        let mut layout = FlexStyle::row()
            .align_items(if self.multiline {
                FlexAlign::Start
            } else {
                FlexAlign::Center
            })
            .width(input_width)
            .height(inner_height)
            .padding_left(ph)
            .padding_right(ph)
            .padding_top(pv)
            .padding_bottom(pv);
        // 应用通用布局属性(覆盖 width/height 及其它 margin/align 等)。
        layout = self.layout.apply(layout);

        let mut listeners = self.listeners.clone();
        let on_focus = self.on_focus.clone();
        let on_blur = self.on_blur.clone();
        listeners.push(Self::focus_listener(
            &state,
            true,
            on_focus.clone(),
            on_blur.clone(),
        ));
        listeners.push(Self::focus_listener(&state, false, on_focus, on_blur));
        listeners.push(self.key_listener(&state));
        listeners.push(self.mouse_down_listener(&state));
        listeners.push(self.mouse_move_listener(&state));
        listeners.push(Self::mouse_up_listener(&state));
        listeners.push(Self::ime_preedit_listener(&state));
        listeners.push(Self::ime_commit_listener(&state));
        listeners.push(Self::ime_disabled_listener(&state));

        ViewNode::Div {
            layout,
            paint,
            key: None,
            children,
            listeners,
        }
    }
}