zenthra-widgets 0.2.2

Immediate-mode UI widgets (buttons, text fields, lists) for the Zenthra UI framework
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
use crate::ui::{Ui, DrawCommand, OverlayRectDraw, ScrollDrag};
use crate::text::TextBuilder;
use zenthra_core::{Color, EdgeInsets, Id, Role, SemanticNode, Rect};
use zenthra_platform::event::PlatformEvent;
use zenthra_text::prelude::{TextOptions, CosmicFontProvider, Padding, ShapedGlyph};
use zenthra_text::traits::FontProvider;

pub struct InputBuilder<'u, 'a, 'b> {
    ui: &'u mut Ui<'a>,
    buffer: &'b mut String,
    id: Id,
    x: f32,
    y: f32,
    font_size: f32,
    color: Color,
    bg: Option<Color>,
    text_bg: Option<Color>,
    highlight: Option<Color>,
    padding: EdgeInsets,
    text_padding: EdgeInsets,
    line_height: f32,
    width: Option<f32>,
    min_width: f32,
    scrollable: bool,
    fill_x: bool,
    text_bg_fill_x: bool,
    radius: [f32; 4],
    border_color: Option<Color>,
    border_width: f32,
    opacity: f32,
    shadow_color: Option<Color>,
    shadow_offset: [f32; 2],
    shadow_blur: f32,
    shadow_opacity: f32,
    render_mode: Option<zenthra_core::RenderMode>,
}

impl<'u, 'a, 'b> InputBuilder<'u, 'a, 'b> {
    pub fn new(ui: &'u mut Ui<'a>, buffer: &'b mut String) -> Self {
        let id = ui.id();
        let x = ui.cursor_x;
        let y = ui.cursor_y;
        Self {
            ui,
            buffer,
            id,
            x,
            y,
            font_size: 18.0,
            color: Color::WHITE,
            bg: Some(Color::rgb(0.2, 0.2, 0.2)),
            text_bg: None,
            padding: EdgeInsets::ZERO,
            text_padding: EdgeInsets::ZERO,
            line_height: 1.2,
            width: None,
            min_width: 200.0,
            scrollable: true,
            fill_x: false,
            text_bg_fill_x: false,
            highlight: None,
            radius: [4.0; 4],
            border_color: None,
            border_width: 0.0,
            opacity: 1.0,
            shadow_color: None,
            shadow_offset: [0.0; 2],
            shadow_blur: 0.0,
            shadow_opacity: 1.0,
            render_mode: None,
        }
    }

    pub fn id(mut self, id: impl std::hash::Hash) -> Self {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        use std::hash::Hasher;
        id.hash(&mut hasher);
        self.id = Id::from_u64(hasher.finish());
        self
    }

    pub fn size(mut self, size: f32) -> Self {
        self.font_size = size;
        self
    }

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

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

    pub fn text_bg(mut self, bg: Color) -> Self {
        self.text_bg = Some(bg);
        self
    }

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

    pub fn padding(mut self, t: f32, r: f32, b: f32, l: f32) -> Self {
        self.padding = EdgeInsets { top: t, right: r, bottom: b, left: l };
        self
    }
    pub fn padding_x(mut self, x: f32) -> Self {
        self.padding.left = x;
        self.padding.right = x;
        self
    }
    pub fn padding_y(mut self, y: f32) -> Self {
        self.padding.top = y;
        self.padding.bottom = y;
        self
    }
    pub fn padding_top(mut self, t: f32) -> Self {
        self.padding.top = t;
        self
    }
    pub fn padding_bottom(mut self, b: f32) -> Self {
        self.padding.bottom = b;
        self
    }
    pub fn padding_left(mut self, l: f32) -> Self {
        self.padding.left = l;
        self
    }
    pub fn padding_right(mut self, r: f32) -> Self {
        self.padding.right = r;
        self
    }

    pub fn text_padding(mut self, t: f32, r: f32, b: f32, l: f32) -> Self {
        self.text_padding = EdgeInsets { top: t, right: r, bottom: b, left: l };
        self
    }
    pub fn text_padding_x(mut self, x: f32) -> Self {
        self.text_padding.left = x;
        self.text_padding.right = x;
        self
    }
    pub fn text_padding_y(mut self, y: f32) -> Self {
        self.text_padding.top = y;
        self.text_padding.bottom = y;
        self
    }
    pub fn text_padding_top(mut self, t: f32) -> Self {
        self.text_padding.top = t;
        self
    }
    pub fn text_padding_bottom(mut self, b: f32) -> Self {
        self.text_padding.bottom = b;
        self
    }
    pub fn text_padding_left(mut self, l: f32) -> Self {
        self.text_padding.left = l;
        self
    }
    pub fn text_padding_right(mut self, r: f32) -> Self {
        self.text_padding.right = r;
        self
    }

    pub fn line_height(mut self, lh: f32) -> Self {
        self.line_height = lh;
        self
    }

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

    pub fn min_width(mut self, w: f32) -> Self {
        self.min_width = w;
        self
    }

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

    pub fn fill_x(mut self) -> Self {
        self.fill_x = true;
        self
    }

    pub fn radius(mut self, tl: f32, tr: f32, br: f32, bl: f32) -> Self {
        self.radius = [tl, tr, br, bl];
        self
    }

    pub fn radius_all(mut self, r: f32) -> Self {
        self.radius = [r, r, r, r];
        self
    }

    pub fn radius_top(mut self, r: f32) -> Self {
        self.radius[0] = r;
        self.radius[1] = r;
        self
    }

    pub fn radius_bottom(mut self, r: f32) -> Self {
        self.radius[2] = r;
        self.radius[3] = r;
        self
    }

    pub fn radius_top_left(mut self, r: f32) -> Self {
        self.radius[0] = r;
        self
    }

    pub fn radius_top_right(mut self, r: f32) -> Self {
        self.radius[1] = r;
        self
    }

    pub fn radius_bottom_right(mut self, r: f32) -> Self {
        self.radius[2] = r;
        self
    }

    pub fn radius_bottom_left(mut self, r: f32) -> Self {
        self.radius[3] = r;
        self
    }

    pub fn radius_left(mut self, r: f32) -> Self {
        self.radius[0] = r;
        self.radius[3] = r;
        self
    }

    pub fn radius_right(mut self, r: f32) -> Self {
        self.radius[1] = r;
        self.radius[2] = r;
        self
    }

    pub fn border(mut self, color: Color, width: f32) -> Self {
        self.border_color = Some(color);
        self.border_width = width;
        self
    }

    pub fn shadow(mut self, color: Color, ox: f32, oy: f32, blur: f32) -> Self {
        self.shadow_color = Some(color);
        self.shadow_offset = [ox, oy];
        self.shadow_blur = blur;
        self
    }

    pub fn opacity(mut self, o: f32) -> Self {
        self.opacity = o;
        self
    }

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

    pub fn render_mode(mut self, mode: zenthra_core::RenderMode) -> Self {
        self.render_mode = Some(mode);
        self
    }

    pub fn on_change<F>(self, mut f: F) -> Self
    where
        F: FnMut(String) + 'a,
    {
        self.ui.add_listener(self.id, crate::ui::EventPhase::Bubble, move |_, event| {
            if let crate::ui::WidgetEvent::Change(crate::ui::EventValue::String(val)) = event {
                f(val.clone());
            }
        });
        self
    }

    pub fn on_hover<F>(self, mut f: F) -> Self
    where
        F: FnMut(bool) + 'a,
    {
        self.ui.add_listener(self.id, crate::ui::EventPhase::Bubble, move |_, event| {
            if let crate::ui::WidgetEvent::Hover(hovered) = event {
                f(*hovered);
            }
        });
        self
    }

    pub fn on_event<F>(self, phase: crate::ui::EventPhase, f: F) -> Self
    where
        F: FnMut(&mut crate::ui::EventContext, &crate::ui::WidgetEvent) + 'a,
    {
        self.ui.add_listener(self.id, phase, f);
        self
    }

    pub fn show(self) -> zenthra_core::Response {
        if let Some(mode) = self.render_mode {
            self.ui.render_mode_stack.push(mode);
        }
        let is_focused = self.ui.focused_id == Some(self.id);
        
        // --- 1. Initial Measure (for hit-testing and initial sizing) ---
        let (mut w_text_raw, h_content, mut shaped_buffer) = if let Some(fs) = self.ui.font_system.as_ref() {
            let mut adapter = CosmicFontProvider::new_with_system(fs.clone());
            let t_padding = Padding::from(self.text_padding);
            adapter.set_layout_size(1000000.0, 10000.0);
            let options = TextOptions::new()
                .font_size(self.font_size)
                .line_height(self.line_height)
                .wrap(zenthra_text::prelude::TextWrap::None);
            let buffer = adapter.shape(&self.buffer, &options);
            let (cw, _ch) = buffer.content_size();
            let m = adapter.metrics(&options);
            (cw + t_padding.horizontal(), m.line_height() + t_padding.vertical(), Some(buffer))
        } else {
            (self.min_width, 20.0, None)
        };

        let max_available_w = (self.ui.max_x - self.x).max(self.min_width);
        let mut w_box = if self.fill_x { max_available_w } else { self.width.unwrap_or_else(|| (w_text_raw + self.padding.horizontal()).min(max_available_w)).max(self.min_width) };
        let h_box = h_content + self.padding.vertical();
        let mut w_view = w_box - self.padding.horizontal();

        // --- 2. Hit Testing & Event Handling ---
        let mut cursor_index = *self.ui.cursor_state.get(&self.id).unwrap_or(&self.buffer.len());
        cursor_index = cursor_index.min(self.buffer.len());
        if !self.buffer.is_char_boundary(cursor_index) {
            cursor_index = self.buffer.len();
        }

        let (actual_x, actual_y) = if let Some((rect, _)) = self.ui.get_recorded_layout(self.id) {
            (rect.origin.x + self.ui.offset_x, rect.origin.y + self.ui.offset_y)
        } else {
            (self.x + self.ui.offset_x, self.y + self.ui.offset_y)
        };
        let is_hovered = self.ui.is_hovered(self.id, actual_x, actual_y, w_box, h_box);
        if is_hovered {
            self.ui.cursor_icon = crate::text::CursorIcon::Text;
        }
        self.ui.dispatch_event(self.id, crate::ui::WidgetEvent::Hover(is_hovered));
        let mut needs_auto_scroll = false;
        let mut changed = false;

        if is_focused || is_hovered || self.ui.active_drag.is_some() {
            let events = std::mem::take(&mut self.ui.input_events);
            for event in &events {
                match event {
                    PlatformEvent::CharTyped(c) if is_focused => {
                        if *c != '\r' && *c != '\n' {
                             self.buffer.insert(cursor_index, *c);
                             cursor_index += c.len_utf8();
                             needs_auto_scroll = true;
                             changed = true;
                             self.ui.interaction_state.insert(self.id, self.ui.elapsed_time);
                        }
                    }
                    PlatformEvent::KeyDown { key } if is_focused => {
                        match key {
                            winit::keyboard::KeyCode::Backspace => {
                                if cursor_index > 0 {
                                    let mut chars = self.buffer[..cursor_index].chars();
                                    if let Some(c) = chars.next_back() {
                                        let len = c.len_utf8();
                                        self.buffer.remove(cursor_index - len);
                                        cursor_index -= len;
                                        needs_auto_scroll = true;
                                        changed = true;
                                        self.ui.interaction_state.insert(self.id, self.ui.elapsed_time);
                                    }
                                }
                            }
                            winit::keyboard::KeyCode::ArrowLeft => {
                                if cursor_index > 0 {
                                    let mut chars = self.buffer[..cursor_index].chars();
                                    if let Some(c) = chars.next_back() {
                                        cursor_index -= c.len_utf8();
                                        self.ui.interaction_state.insert(self.id, self.ui.elapsed_time);
                                        self.ui.needs_redraw = true;
                                        needs_auto_scroll = true;
                                    }
                                }
                            }
                            winit::keyboard::KeyCode::ArrowRight => {
                                if cursor_index < self.buffer.len() {
                                    let mut chars = self.buffer[cursor_index..].chars();
                                    if let Some(c) = chars.next() {
                                        cursor_index += c.len_utf8();
                                        self.ui.interaction_state.insert(self.id, self.ui.elapsed_time);
                                        self.ui.needs_redraw = true;
                                        needs_auto_scroll = true;
                                    }
                                }
                            }
                            _ => {}
                        }
                    }
                    PlatformEvent::MouseWheel { delta_x, delta_y } if is_hovered => {
                        let _id = self.id;
                        let (mut sx, sy) = *self.ui.scroll_state.get(&self.id).unwrap_or(&(0.0, 0.0));
                        let effect = if delta_x.abs() < 0.001 { *delta_y } else { *delta_x };
                        sx -= effect * 30.0;
                        self.ui.scroll_state.insert(self.id, (sx, sy));
                    }
                    _ => {}
                }
            }
            
            if self.ui.clicked && is_hovered {
                self.ui.focused_id = Some(self.id);
                needs_auto_scroll = true;
                self.ui.dispatch_event(self.id, crate::ui::WidgetEvent::Click);
            }
            
            self.ui.input_events = events;
            self.ui.cursor_state.insert(self.id, cursor_index);

            if changed {
                self.ui.dispatch_event(self.id, crate::ui::WidgetEvent::Change(crate::ui::EventValue::String(self.buffer.clone())));
            }
        }

        // --- 3. Re-Measure if content changed ---
        if changed {
             if let Some(fs) = self.ui.font_system.as_ref() {
                let mut adapter = CosmicFontProvider::new_with_system(fs.clone());
                let t_padding = Padding::from(self.text_padding);
                adapter.set_layout_size(1000000.0, 10000.0);
                let options = TextOptions::new()
                    .font_size(self.font_size)
                    .line_height(self.line_height)
                    .wrap(zenthra_text::prelude::TextWrap::None);
                let buffer = adapter.shape(&self.buffer, &options);
                let (cw, _ch) = buffer.content_size();
                w_text_raw = cw + t_padding.horizontal();
                shaped_buffer = Some(buffer);
                
                // Re-calculate boxes
                w_box = if self.fill_x { max_available_w } else { self.width.unwrap_or_else(|| (w_text_raw + self.padding.horizontal()).min(max_available_w)).max(self.min_width) };
                w_view = w_box - self.padding.horizontal();
            }
        }

        // --- 4. Scroll & Auto-Scroll Calculation ---
        let scroll_state_id = self.id;
        let (mut scroll_x, scroll_y) = *self.ui.scroll_state.get(&scroll_state_id).unwrap_or(&(0.0, 0.0));
        let max_scroll = (w_text_raw - w_view).max(0.0f32);

        let scroll_bar_h = 4.0;
        let scroll_bar_y = self.y + h_box - scroll_bar_h - 2.0;
        let thumb_w = if w_text_raw > w_view { (w_view / w_text_raw) * w_view } else { w_view };
        let thumb_w = thumb_w.max(20.0);
        let scroll_percent = if max_scroll > 0.0 { scroll_x / max_scroll } else { 0.0 };
        let track_width = (w_view - thumb_w).max(0.0);
        let thumb_x = self.x + self.padding.left + scroll_percent * track_width;

        let actual_thumb_x = actual_x + self.padding.left + scroll_percent * track_width;
        let actual_scroll_bar_y = actual_y + h_box - scroll_bar_h - 2.0;
        let is_over_thumb = self.ui.mouse_in_rect(actual_thumb_x, actual_scroll_bar_y - 2.0, thumb_w, scroll_bar_h + 4.0);

        // Handle dragging
        if let Some(drag) = self.ui.active_drag {
            if drag.id == scroll_state_id {
                let delta_mouse = self.ui.mouse_x - drag.start_mouse;
                if track_width > 0.0 {
                    let scroll_delta = (delta_mouse / track_width) * max_scroll;
                    scroll_x = (drag.start_scroll + scroll_delta).clamp(0.0, max_scroll);
                }
            }
        }
        
        if self.ui.clicked && is_over_thumb {
            self.ui.active_drag = Some(ScrollDrag {
                id: scroll_state_id,
                start_mouse: self.ui.mouse_x,
                start_scroll: scroll_x,
            });
        }

        // Auto-scroll logic (Now uses FRESH buffer)
        let is_dragging_this = self.ui.active_drag.map(|d| d.id == scroll_state_id).unwrap_or(false);
        if is_focused && !is_dragging_this && needs_auto_scroll {
            if let Some(sb) = &shaped_buffer {
                let mut clx = 0.0;
                let mut found = false;
                for g in sb.glyphs() {
                    if g.cluster == cursor_index {
                        clx = g.x;
                        found = true;
                        break;
                    }
                }
                if !found && cursor_index == self.buffer.len() {
                    clx = sb.glyphs().last().map(|g: &ShapedGlyph| g.x + g.width).unwrap_or(0.0);
                }

                let cursor_x_v = clx + self.text_padding.left;
                if cursor_x_v > scroll_x + w_view - 40.0 {
                    scroll_x = cursor_x_v - w_view + 40.0;
                } else if cursor_x_v < scroll_x + 10.0 {
                    scroll_x = (cursor_x_v - 10.0).max(0.0);
                }
            }
        }

        scroll_x = scroll_x.clamp(0.0, max_scroll);
        self.ui.scroll_state.insert(scroll_state_id, (scroll_x, scroll_y));

        // --- 4. Render Background ---
        let start_draw = self.ui.draws.len();
        if let Some(bg) = self.bg {
            use crate::ui::RectDraw;
            use zenthra_render::RectInstance;
            self.ui.draws.push(DrawCommand::Rect(RectDraw {
                instance: RectInstance {
                    pos: [self.x, self.y],
                    size: [w_box, h_box],
                    color: bg.to_array(),
                    radius: [
                        self.radius[3],
                        self.radius[2],
                        self.radius[1],
                        self.radius[0],
                    ],
                    border_width: if is_focused { self.border_width.max(1.0) } else { self.border_width },
                    border_color: if is_focused { self.border_color.unwrap_or(Color::rgb(0.4, 0.7, 1.0)).to_array() } else { self.border_color.unwrap_or(Color::TRANSPARENT).to_array() },
                    shadow_color: self.shadow_color.map(|mut c| { c.a *= self.shadow_opacity; c.to_array() }).unwrap_or([0.0; 4]),
                    shadow_offset: self.shadow_offset,
                    shadow_blur: self.shadow_blur,
                    clip_rect: [0.0, 0.0, 9999.0, 9999.0],
                    grayscale: 0.0,
                    brightness: 1.0,
                    opacity: self.opacity,
                    ..Default::default()
                }
            }));
        }

        // --- 5. Render Text ---
        let mut text_builder = TextBuilder::new(self.ui, &self.buffer)
            .size(self.font_size)
            .line_height(self.line_height)
            .color(self.color)
            .fill_x(self.fill_x)
            .padding(self.text_padding.top, self.text_padding.right, self.text_padding.bottom, self.text_padding.left)
            .wrap(zenthra_text::prelude::TextWrap::None)
            .max_width(1000000.0) 
            .pos(self.x + self.padding.left - scroll_x, self.y + self.padding.top)
            .clip_rect(self.x, self.y, w_box, h_box);

        if self.text_bg_fill_x {
            text_builder = text_builder.min_width(w_view);
        }
        
        if let Some(tbg) = self.text_bg {
            text_builder = text_builder.bg(tbg).fill_x(false);
        }

        if let Some(h) = self.highlight {
            text_builder = text_builder.highlight(h);
        }
        
        let (_, _, final_sb, _) = text_builder.draw_and_measure();
        
        // --- 6. Cursor Rendering ---
        if is_focused {
            let font_size = self.font_size;
            let lh = self.line_height;
            let cursor_height = font_size * lh; 

            if let Some(sb) = final_sb {
                let mut lx = 0.0;
                let mut found = false;
                for g in sb.glyphs() {
                    if g.cluster == cursor_index {
                        lx = g.x;
                        found = true;
                        break;
                    }
                }

                if !found && cursor_index == self.buffer.len() {
                    lx = sb.glyphs().last().map(|g: &ShapedGlyph| g.x + g.width).unwrap_or(0.0);
                }

                let cx = lx + self.x + self.padding.left + self.text_padding.left - scroll_x;
                let cy = self.y + self.padding.top + self.text_padding.top;
                
                // Smart Blink: Solid while typing, blink when idle
                let last_activity = *self.ui.interaction_state.get(&self.id).unwrap_or(&0.0);
                let time_since_activity = self.ui.elapsed_time - last_activity;
                
                let is_blink_visible = if time_since_activity < 0.5 {
                    true // Solid during and just after activity
                } else {
                    // Start blinking after 500ms of idle. 
                    // Offset by last_activity so the cycle always starts "ON" when you stop.
                    (self.ui.elapsed_time - last_activity).fract() < 0.5
                };

                if is_blink_visible {
                    self.ui.draws.push(DrawCommand::OverlayRect(OverlayRectDraw {
                        x: cx,
                        y: cy,
                        width: 2.0,
                        height: cursor_height,
                        color: Color::WHITE,
                        clip: [self.x, self.y, w_box, h_box],
                    }));
                }
            }
        }

        // --- 7. Render Horizontal Scroll Bar ---
        if self.scrollable && w_text_raw > w_view {
            let is_dragging = self.ui.active_drag.map(|d| d.id == scroll_state_id).unwrap_or(false);
            
            self.ui.draws.push(DrawCommand::OverlayRect(OverlayRectDraw {
                x: thumb_x,
                y: scroll_bar_y,
                width: thumb_w,
                height: scroll_bar_h,
                color: if is_dragging { Color::rgba(1.0, 1.0, 1.0, 0.8) } else { Color::rgba(1.0, 1.0, 1.0, 0.4) },
                clip: [self.x, self.y, w_box, h_box],
            }));
        }

        // --- 8. Semantic Registration ---
        self.ui.register_semantic(
            SemanticNode::new(self.id, Role::TextInput, Rect::new(self.x, self.y, w_box, h_box))
                .with_label(self.buffer.clone())
                .with_focus(is_focused)
        );

        // --- 9. Advance UI ---
        self.ui.record_layout(self.id, Rect::new(self.x, self.y, w_box, h_box));
        self.ui.advance(w_box, h_box, start_draw);

        if self.render_mode.is_some() {
            self.ui.render_mode_stack.pop();
        }

        zenthra_core::Response {
            clicked: self.ui.clicked && is_hovered,
            hovered: is_hovered,
            pressed: is_hovered && self.ui.mouse_down,
        }
    }
}