rosace-widgets 0.1.0

40+ built-in widgets for ROSACE: Button, Card, Dialog, Dropdown, and more
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
use std::sync::Arc;

use rosace_core::types::{Point, Rect, Size};
use rosace_render::{Color, DrawCommand, FontWeight};
use super::{Widget, LayoutCtx, PaintCtx, ScrollAxes};
use super::container::draw_rounded_rect_pub;
use super::text_input::paint_caret;
use super::text_edit::{
    char_byte_offset, grapheme_boundaries, style_runs, CursorStyle, EditController, EditableDecl,
    LineLayout, SpanFn, TextLayoutSnapshot,
};

/// A multi-line, wrapped, virtualized-paint text field (D116 Step 4).
///
/// Built entirely on the same D116 core `TextInput` uses — `Transaction`/
/// `Selection`/`Command`/`EditController` are unchanged; `TextLayoutSnapshot`
/// already models "a list of visual lines" so `TextArea` just populates one
/// [`LineLayout`] per WRAPPED line instead of `TextInput`'s single entry.
/// Click/drag/double/triple-click dispatch in `engine.rs` is untouched —
/// it only ever talks to `editable.layout`, never knows which widget it's
/// looking at.
///
/// New in this widget: word-wrapping (`wrap_char_ranges` below), Enter
/// inserts a real newline (`engine.rs`, gated on `multiline`), Up/Down
/// cross wrapped lines with goal-column memory (`TextEditState::goal_x`),
/// and vertical scrolling via the same zero-wiring [`rosace_scroll::ScrollController`]
/// `ListView`/`ScrollView` use (D101) — wheel events mutate the
/// controller's atoms directly, never the render tree, so there's no
/// `!Sync`/`!Send` wall to work around here the way click dispatch has.
///
/// Paint is virtualized: every visual line's geometry is computed each
/// frame (needed for correct click-anywhere/goal-column behavior), but
/// only lines intersecting the current viewport actually emit paint
/// commands — a large document's PAINT cost stays bounded even though its
/// LAYOUT cost does not yet (a named follow-up, not this step's exit bar).
pub struct TextArea {
    pub value: String,
    pub placeholder: String,
    pub focused: bool,
    pub width: Option<f32>,
    pub height: f32,
    /// `None` = read from the active theme's `typography.body_medium`
    /// (D127 "environment" track — see `Checkbox::resolved_font_size`'s doc
    /// for the reasoning).
    pub font_size: Option<f32>,
    pub radius: f32,
    background: Option<Color>,
    border_color: Option<Color>,
    focus_color: Option<Color>,
    on_change: Option<Arc<dyn Fn(String) + Send + Sync>>,
    controller: Option<EditController>,
    spans: Option<Arc<SpanFn>>,
    cursor_style: Option<CursorStyle>,
    field: Option<rosace_forms::FormField>,
    filters: Vec<super::text_edit::InputFilter>,
    show_scrollbar: bool,
    scrollbar_color: Color,
}

impl TextArea {
    pub fn new() -> Self {
        Self {
            value: String::new(),
            placeholder: String::from("Type here..."),
            focused: false,
            width: None,
            height: 160.0,
            font_size: None,
            radius: 6.0,
            background: None,
            border_color: None,
            focus_color: None,
            on_change: None,
            controller: None,
            spans: None,
            cursor_style: None,
            field: None,
            filters: Vec::new(),
            show_scrollbar: true,
            scrollbar_color: Color::rgb(60, 65, 95),
        }
    }
    pub fn value(mut self, v: impl Into<String>) -> Self { self.value = v.into(); self }
    pub fn placeholder(mut self, p: impl Into<String>) -> Self { self.placeholder = p.into(); self }
    pub fn focused(mut self) -> Self { self.focused = true; self }
    pub fn width(mut self, w: f32) -> Self { self.width = Some(w); self }
    pub fn height(mut self, h: f32) -> Self { self.height = h; self }
    /// See `TextInput::background` — same seam, same contract.
    pub fn background(mut self, c: Color) -> Self { self.background = Some(c); self }
    /// See `TextInput::border` — same seam, same contract.
    pub fn border(mut self, c: Color) -> Self { self.border_color = Some(c); self }
    /// See `TextInput::focus_color` — same seam, same contract.
    pub fn focus_color(mut self, c: Color) -> Self { self.focus_color = Some(c); self }
    pub fn on_change(mut self, f: impl Fn(String) + Send + Sync + 'static) -> Self {
        self.on_change = Some(Arc::new(f));
        self
    }
    pub fn controller(mut self, c: EditController) -> Self {
        self.controller = Some(c);
        self
    }
    /// See `TextInput::spans` — same seam, same contract (D116 Step 5).
    pub fn spans(mut self, f: impl Fn(&str, Option<(usize, usize)>) -> Vec<super::text_edit::Span> + Send + Sync + 'static) -> Self {
        self.spans = Some(Arc::new(f));
        self
    }
    /// See `TextInput::cursor_style` — same seam, same contract.
    pub fn cursor_style(mut self, s: CursorStyle) -> Self {
        self.cursor_style = Some(s);
        self
    }
    /// See `TextInput::field` — same seam, same contract (D116 Step 8).
    pub fn field(mut self, f: rosace_forms::FormField) -> Self {
        self.value = f.get();
        let bound = f.clone();
        self.on_change = Some(Arc::new(move |v| {
            bound.set(v);
            bound.validate();
        }));
        // See `TextInput::field`'s identical eager-validate comment.
        f.validate();
        self.field = Some(f);
        self
    }
    /// See `TextInput::filters` — same seam, same contract.
    pub fn filters(mut self, filters: Vec<super::text_edit::InputFilter>) -> Self {
        self.filters = filters;
        self
    }
    /// Hide the vertical scroll-position thumb (see `ScrollView::no_scrollbar`
    /// — same convention). Content still scrolls; only the indicator is gone.
    pub fn no_scrollbar(mut self) -> Self { self.show_scrollbar = false; self }
    pub fn scrollbar_color(mut self, c: Color) -> Self { self.scrollbar_color = c; self }

    fn resolved_font_size(&self, theme: &rosace_theme::ThemeData) -> f32 {
        self.font_size.unwrap_or(theme.typography.body_medium.size)
    }
}

impl Default for TextArea {
    fn default() -> Self { Self::new() }
}

/// Greedy word-wrap that preserves EXACT char offsets into `chars` (no
/// text is dropped or normalized) — every char index belongs to exactly
/// one returned `(start, end)` line range, so click/caret placement is
/// always well-defined. Hard breaks (`\n`) always start a new line,
/// including a trailing empty line when `chars` ends with `\n`.
///
/// Known limitation: a single word wider than `max_width` overflows its
/// line rather than hard-breaking mid-word — acceptable for prose, a
/// named follow-up for pathological input (Step 4 exit bar doesn't
/// require it).
fn wrap_char_ranges(chars: &[char], max_width: f32, measure: &dyn Fn(&str) -> f32) -> Vec<(usize, usize)> {
    let n = chars.len();
    let mut ranges = Vec::new();
    let mut para_start = 0usize;
    loop {
        let mut para_end = para_start;
        while para_end < n && chars[para_end] != '\n' { para_end += 1; }
        wrap_paragraph(chars, para_start, para_end, max_width, measure, &mut ranges);
        if para_end >= n { break; }
        para_start = para_end + 1; // skip the '\n'
        if para_start == n {
            ranges.push((n, n)); // value ends with '\n' -> trailing empty line
            break;
        }
    }
    if ranges.is_empty() { ranges.push((0, 0)); }
    ranges
}

fn wrap_paragraph(
    chars: &[char], start: usize, end: usize, max_width: f32,
    measure: &dyn Fn(&str) -> f32, ranges: &mut Vec<(usize, usize)>,
) {
    if start == end {
        ranges.push((start, end));
        return;
    }
    let mut line_start = start;
    let mut cursor = start;
    while cursor < end {
        // Extend to the end of the next token: a run of non-space chars
        // plus its trailing spaces (spaces stay attached to the word
        // BEFORE the break, matching every real editor's wrap).
        let mut tok_end = cursor;
        while tok_end < end && chars[tok_end] != ' ' { tok_end += 1; }
        while tok_end < end && chars[tok_end] == ' ' { tok_end += 1; }
        if tok_end == cursor { tok_end = end; }
        if cursor > line_start {
            let candidate: String = chars[line_start..tok_end].iter().collect();
            if measure(&candidate) > max_width {
                ranges.push((line_start, cursor));
                line_start = cursor;
            }
        }
        cursor = tok_end;
    }
    ranges.push((line_start, end));
}

impl Widget for TextArea {
    fn layout(&self, ctx: &LayoutCtx) -> Size {
        let constraints = ctx.constraints;
        let show_error = self.field.as_ref().is_some_and(|f| f.is_touched() && !f.is_valid());
        Size {
            width: self.width.unwrap_or(super::avail_w(constraints)),
            height: self.height + if show_error { super::text_input::ERROR_ROW_H } else { 0.0 },
        }
    }

    fn paint(&self, ctx: &mut PaintCtx) {
        ctx.semantics(super::Semantics::new(rosace_core::Role::TextInput)
            .label(&self.placeholder).value(&self.value));
        let font_size = self.resolved_font_size(&ctx.theme);

        let focus = ctx.focus_node_seeded(self.focused);
        ctx.register_focus(focus.clone());
        let is_focused = focus.is_focused();

        // The scroll VIEWPORT only — `ctx.rect` may be taller than
        // `self.height` when an error caption is reserved below it
        // (D116 Step 8), same convention as `TextInput`.
        let full_rect = ctx.rect;
        let r = Rect { origin: full_rect.origin, size: Size { width: full_rect.size.width, height: self.height } };
        let bg = self.background.unwrap_or(Color::rgb(15, 16, 28));
        let border = if is_focused {
            self.focus_color.unwrap_or(Color::rgb(110, 75, 210))
        } else {
            self.border_color.unwrap_or(Color::rgb(32, 35, 58))
        };
        draw_rounded_rect_pub(ctx, r, bg, self.radius);
        ctx.stroke_rrect(r, self.radius, border, if is_focused { 1.5 } else { 1.0 });

        const PAD: f32 = 10.0;
        let line_h = ctx.font.line_height(font_size);
        let max_w = (r.size.width - PAD * 2.0).max(1.0);
        let has_value = !self.value.is_empty();

        let chars: Vec<char> = self.value.chars().collect();
        let ranges = wrap_char_ranges(&chars, max_w, &|s: &str| ctx.font.measure_text(s, font_size));
        let n_lines = ranges.len();
        let content_h = n_lines as f32 * line_h;
        // The scrollable extent is the text PLUS the top and bottom padding
        // it's drawn inside (lines render at `PAD + i*line_h - scroll_y`).
        // Using bare `content_h` here left the last line's bottom exactly
        // PAD px past the clip at max scroll — a real clipped-last-line
        // bug caught live.
        let content_extent = content_h + PAD * 2.0;

        let ctrl = ctx.scroll_controller();
        let vp_s = [r.size.width, r.size.height];
        if ctrl.viewport_size.get() != vp_s { ctrl.viewport_size.set(vp_s); }
        let cs = [r.size.width, content_extent];
        if ctrl.content_size.get() != cs { ctrl.content_size.set(cs); }

        let max_scroll = (content_extent - r.size.height).max(0.0);
        let mut scroll_y = ctrl.offset.get()[1].clamp(0.0, max_scroll);

        let state = ctx.text_edit();
        let cursor = state.cursor();
        let cursor_line = ranges.iter().position(|&(s, e)| cursor >= s && cursor <= e).unwrap_or(0);

        // Scroll-into-view (D116 Step 4) — only meaningful while focused,
        // and only when the caret actually MOVED since the last chase
        // (typing, arrows, click). Chasing on every focused paint fought
        // the user's wheel input: the caret-blink animation repaints
        // constantly while focused, so with the caret on a bottom line
        // every wheel-up was snapped straight back within a frame ("no
        // scrolling at the bottom"), and a mid-document caret clamped
        // scrolling to a viewport-sized window around itself.
        if is_focused && state.scrolled_cursor != Some(cursor) {
            ctx.set_scrolled_cursor(Some(cursor));
            let cursor_y = cursor_line as f32 * line_h;
            if cursor_y < scroll_y {
                scroll_y = cursor_y;
            } else if cursor_y + line_h + PAD * 2.0 > scroll_y + r.size.height {
                // `+ PAD * 2.0`: the caret line must clear the bottom clip
                // edge with its padding, mirroring `content_extent` above.
                scroll_y = cursor_y + line_h + PAD * 2.0 - r.size.height;
            }
            scroll_y = scroll_y.clamp(0.0, max_scroll);
        }
        if ctrl.offset.get()[1] != scroll_y {
            ctrl.offset.set([ctrl.offset.get()[0], scroll_y]);
        }

        // `- PAD`: a line whose bottom pokes into the top padding band is
        // still partially visible and must paint (the clip trims it).
        let first_visible = (((scroll_y - PAD) / line_h).floor().max(0.0)) as usize;
        let last_visible = (((scroll_y + r.size.height) / line_h).ceil() as usize).min(n_lines);

        let boundaries = grapheme_boundaries(&self.value);
        let text_color = if has_value { Color::rgb(220, 222, 240) } else { Color::rgb(80, 85, 118) };

        // Styled spans (D116 Step 5) — computed ONCE for the whole value,
        // sliced per-line below via `style_runs`. Never applied to the
        // placeholder (there's no real text to tokenize).
        let spans = if has_value {
            self.spans.as_ref().map(|f| f(&self.value, state.last_edit_range))
        } else {
            None
        };

        let cursor_style = self.cursor_style.clone()
            .unwrap_or_else(|| ctx.theme.ext::<CursorStyle>().cloned().unwrap_or_default());

        ctx.record(DrawCommand::PushClip { rect: r });

        let mut lines: Vec<LineLayout> = Vec::with_capacity(n_lines);
        for (i, &(ls, le)) in ranges.iter().enumerate() {
            let y = r.origin.y + PAD + i as f32 * line_h - scroll_y;
            let bchars: Vec<usize> = boundaries.iter().copied().filter(|&c| c >= ls && c <= le).collect();
            let lb = char_byte_offset(&self.value, ls);
            let bx: Vec<f32> = bchars.iter().map(|&c| {
                let cb = char_byte_offset(&self.value, c);
                r.origin.x + PAD + ctx.font.measure_text(&self.value[lb..cb], font_size)
            }).collect();
            lines.push(LineLayout { char_range: (ls, le), y, height: line_h, boundary_chars: bchars, boundary_x: bx });

            if i < first_visible || i >= last_visible { continue; }
            let ll = &lines[i];

            if has_value {
                if is_focused {
                    // Standard half-open interval overlap: this line spans
                    // [ls, le); a multi-line selection highlights the
                    // portion of EACH overlapping line separately —
                    // `x_at` already clamps an out-of-range endpoint to
                    // this line's own start/end boundary.
                    if let Some((sel_s, sel_e)) = state.selection_range() {
                        if sel_s < le && sel_e > ls {
                            // Colors from the theme's SelectionStyle (D105
                            // ext, flat default = the pre-themeable look).
                            // The GLASS magnifier lens is single-line-field
                            // only for now (TextInput); a multi-line lens
                            // is a named deferral — here glass mode means
                            // its tint + lollipop handles.
                            let sel_style = ctx.theme.ext::<super::SelectionStyle>().cloned().unwrap_or_default();
                            let x0 = ll.x_at(sel_s);
                            let x1 = ll.x_at(sel_e);
                            if x1 > x0 {
                                ctx.fill_rect(Rect {
                                    origin: Point { x: x0, y },
                                    size: Size { width: x1 - x0, height: line_h },
                                }, sel_style.highlight);
                            }
                            // Draggable selection handles (D116 Step 7) —
                            // only on the line that actually OWNS each
                            // endpoint (a multi-line selection's middle
                            // lines get none). Grips stay at the line
                            // bottom in both kinds — the engine's
                            // handle_anchor targets that point.
                            let handle_y = y + line_h;
                            let glass = sel_style.kind == super::SelectionKind::Glass;
                            for (endpoint, x) in [(sel_s, x0), (sel_e, x1)] {
                                if endpoint >= ls && endpoint <= le {
                                    if glass {
                                        ctx.fill_rect(Rect {
                                            origin: Point { x: x - 1.0, y },
                                            size: Size { width: 2.0, height: line_h },
                                        }, sel_style.handle);
                                        ctx.fill_circle(Point { x, y: handle_y }, 4.5, sel_style.handle);
                                    } else {
                                        ctx.fill_circle(Point { x, y: handle_y }, 4.0, sel_style.handle);
                                    }
                                }
                            }
                        }
                    }

                    // IME preedit underline (D116 Step 6) — same
                    // half-open overlap technique as the selection above.
                    if let Some((ims, ime_)) = state.ime_range {
                        if ims < le && ime_ > ls {
                            let x0 = ll.x_at(ims);
                            let x1 = ll.x_at(ime_);
                            if x1 > x0 {
                                ctx.fill_rect(Rect {
                                    origin: Point { x: x0, y: y + line_h - 1.0 },
                                    size: Size { width: x1 - x0, height: 1.5 },
                                }, text_color);
                            }
                        }
                    }
                }
                if let Some(spans) = &spans {
                    for (rs, re, color, weight) in style_runs(spans, ls, le) {
                        if rs >= re { continue; }
                        let rb = char_byte_offset(&self.value, rs);
                        let reb = char_byte_offset(&self.value, re);
                        ctx.record(DrawCommand::DrawText {
                            text: self.value[rb..reb].to_string(),
                            origin: Point { x: ll.x_at(rs), y },
                            color: color.unwrap_or(text_color),
                            px: font_size,
                            weight: weight.unwrap_or(FontWeight::Regular),
                        });
                    }
                } else {
                    let ub = char_byte_offset(&self.value, le);
                    let line_text = &self.value[lb..ub];
                    ctx.draw_text_at(line_text, Point { x: r.origin.x + PAD, y }, text_color, font_size);
                }
            }

            if is_focused && i == cursor_line {
                // Report this field's caret rect to the platform (D116
                // Step 6) so the OS's CJK candidate window anchors near
                // it — regardless of whether the caret itself is visibly
                // blinking this frame.
                let cx = ll.x_at(cursor);
                rosace_core::set_ime_cursor_area(Some(Rect {
                    origin: Point { x: cx, y },
                    size: Size { width: 2.0, height: line_h },
                }));

                if state.selection_range().is_none() {
                    let t = super::anim_clock() - state.last_edit_at;
                    let blink_on = t < 0.5 || (((t - 0.5) / cursor_style.blink_rate) as i64 % 2 == 0);
                    if blink_on {
                        paint_caret(ctx, &cursor_style, cx, y, line_h, font_size, ll, cursor);
                    }
                }
            }
        }

        if !has_value {
            ctx.text(&self.placeholder, PAD, PAD, text_color, font_size);
        }
        if is_focused {
            super::request_animation();
        }

        ctx.record(DrawCommand::PopClip);

        // Scroll-position thumb (same convention as `ScrollView` — drawn
        // AFTER PopClip so it isn't clipped, re-reading the offset fresh
        // rather than the `scroll_y` captured above, which predates this
        // frame's wheel/scroll-into-view update).
        if self.show_scrollbar {
            let fresh_y = ctrl.offset.get()[1].clamp(0.0, max_scroll);
            let ratio = (r.size.height / content_extent.max(1.0)).min(1.0);
            if ratio < 1.0 {
                let bar_h = r.size.height * ratio;
                let max_bar_y = r.origin.y + r.size.height - bar_h;
                let bar_y = (r.origin.y + (fresh_y / content_extent) * r.size.height)
                    .clamp(r.origin.y, max_bar_y.max(r.origin.y));
                ctx.fill_rect(Rect {
                    origin: Point { x: r.origin.x + r.size.width - 4.0, y: bar_y },
                    size: Size { width: 3.0, height: bar_h },
                }, self.scrollbar_color);
            }
        }

        ctx.register_editable(EditableDecl {
            value: self.value.clone(),
            rect: r,
            multiline: true,
            obscure: false,
            on_change: self.on_change.clone().unwrap_or_else(|| Arc::new(|_| {})),
            controller: self.controller.clone(),
            layout: TextLayoutSnapshot { lines },
            filters: self.filters.clone(),
        });

        let wheel = ctrl.clone();
        ctx.register_scroll_target(r, ScrollAxes::Y, Arc::new(move |_dx, dy| {
            wheel.scroll_by(0.0, -dy);
        }));

        // Inline validation error (D116 Step 8) — see `TextInput`'s
        // identical block for the touched/`Role::Alert` reasoning.
        if let Some(field) = &self.field {
            if field.is_touched() {
                if let Some(err) = field.errors().first() {
                    ctx.semantics(super::Semantics::new(rosace_core::Role::Alert).label(&err.message));
                    ctx.record(DrawCommand::DrawText {
                        text: err.message.clone(),
                        origin: Point { x: full_rect.origin.x + 2.0, y: r.origin.y + r.size.height + 2.0 },
                        color: Color::rgb(230, 90, 90),
                        px: 10.0,
                        weight: FontWeight::Regular,
                    });
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ranges_of(s: &str, max_width: f32, char_w: f32) -> Vec<(usize, usize)> {
        let chars: Vec<char> = s.chars().collect();
        wrap_char_ranges(&chars, max_width, &|t: &str| t.chars().count() as f32 * char_w)
    }
    fn slice(s: &str, r: (usize, usize)) -> String {
        s.chars().skip(r.0).take(r.1 - r.0).collect()
    }

    #[test]
    fn short_text_that_fits_is_a_single_line() {
        let r = ranges_of("hello", 1000.0, 10.0);
        assert_eq!(r, vec![(0, 5)]);
    }

    #[test]
    fn empty_string_is_one_empty_line() {
        assert_eq!(ranges_of("", 1000.0, 10.0), vec![(0, 0)]);
    }

    #[test]
    fn long_text_wraps_at_a_word_boundary_covering_every_char_with_no_gaps() {
        // "aaaa bbbb cccc" at char_w=10, max_width=45 fits "aaaa " (50 >
        // 45 already for "aaaa b"... use generous width so exactly two
        // words fit per line): width for "aaaa bbbb " = 10*10=100.
        let s = "aaaa bbbb cccc";
        let r = ranges_of(s, 100.0, 10.0);
        assert!(r.len() >= 2, "must wrap into multiple lines, got {r:?}");
        // Every char index in [0, len) belongs to exactly one line —
        // ranges are contiguous with no gaps or overlaps.
        assert_eq!(r.first().unwrap().0, 0);
        assert_eq!(r.last().unwrap().1, s.chars().count());
        for w in r.windows(2) {
            assert_eq!(w[0].1, w[1].0, "line ranges must be contiguous: {r:?}");
        }
        // Reassembling every line's slice must reconstruct the original
        // text exactly (no characters dropped or duplicated).
        let rebuilt: String = r.iter().map(|&rg| slice(s, rg)).collect();
        assert_eq!(rebuilt, s);
    }

    #[test]
    fn explicit_newline_always_starts_a_new_line_regardless_of_width() {
        let r = ranges_of("ab\ncd", 1000.0, 10.0);
        assert_eq!(r, vec![(0, 2), (3, 5)], "the '\\n' itself (index 2) is consumed, not part of either line");
    }

    #[test]
    fn trailing_newline_produces_a_final_empty_line() {
        let r = ranges_of("ab\n", 1000.0, 10.0);
        assert_eq!(r, vec![(0, 2), (3, 3)]);
    }

    #[test]
    fn multiple_consecutive_newlines_produce_empty_lines_between_them() {
        let r = ranges_of("a\n\nb", 1000.0, 10.0);
        assert_eq!(r, vec![(0, 1), (2, 2), (3, 4)]);
    }

    #[test]
    fn background_border_focus_color_builders_do_not_change_layout_size() {
        let font = rosace_render::FontCache::embedded();
        let theme = rosace_theme::built_in::dark_theme();
        let ctx = LayoutCtx::new(rosace_layout::Constraints::loose(400.0, 400.0), &font, &theme);
        let base = TextArea::new().width(200.0);
        let customized = TextArea::new().width(200.0)
            .background(Color::rgb(10, 10, 10))
            .border(Color::rgb(200, 0, 0))
            .focus_color(Color::rgb(0, 200, 0));
        assert_eq!(base.layout(&ctx), customized.layout(&ctx));
    }
}