sequoia-view 0.1.3

Sequoia GUI framework — view tree and runtime
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
use std::rc::Rc;

use sequoia_core::Edges;
use sequoia_core::{Color, Length, Size};
use sequoia_input::{CursorIcon, Event, EventPhase, PointerButton, PointerEvent};
use sequoia_layout::{LayoutCtx, LayoutStyle};
use sequoia_text::{LetterSpacing, LineHeight, RichAlign, TextSpan, TextVAlign, WrapMode};

use crate::view::{EventCtx, PaintCtx, View};
use crate::views::{Unit, text_wrap_limit};

pub struct RichText {
    spans: Box<dyn Fn() -> Vec<TextSpan>>,
    font_size: Option<f32>,
    color: Option<Color>,
    wrap_width: Option<f32>,
    wrap_mode: WrapMode,
    align: RichAlign,
    align_y: TextVAlign,
    line_height: LineHeight,
    max_lines: Option<usize>,
    font_scale: f32,
    auto_wrap: bool,
    letter_spacing: f32,
    on_span_click: Option<Rc<dyn Fn(usize)>>,
    style: LayoutStyle,
}

impl RichText {
    pub fn new(spans: Vec<TextSpan>) -> Self {
        Self {
            spans: Box::new(move || spans.clone()),
            font_size: None,
            color: None,
            wrap_width: None,
            wrap_mode: WrapMode::default(),
            align: RichAlign::Left,
            align_y: TextVAlign::Top,
            line_height: LineHeight::default(),
            max_lines: None,
            font_scale: 1.0,
            auto_wrap: false,
            letter_spacing: 0.0,
            on_span_click: None,
            style: LayoutStyle {
                width: Length::Fit,
                height: Length::Fit,
                ..LayoutStyle::default()
            },
        }
    }

    pub fn with(spans: impl Fn() -> Vec<TextSpan> + 'static) -> Self {
        Self {
            spans: Box::new(spans),
            font_size: None,
            color: None,
            wrap_width: None,
            wrap_mode: WrapMode::default(),
            align: RichAlign::Left,
            align_y: TextVAlign::Top,
            line_height: LineHeight::default(),
            max_lines: None,
            font_scale: 1.0,
            auto_wrap: false,
            letter_spacing: 0.0,
            on_span_click: None,
            style: LayoutStyle {
                width: Length::Fit,
                height: Length::Fit,
                ..LayoutStyle::default()
            },
        }
    }

    pub fn spans(&self) -> Vec<TextSpan> {
        (self.spans)()
    }

    pub fn span(mut self, span: TextSpan) -> Self {
        let prev = std::mem::replace(&mut self.spans, Box::new(Vec::new));
        self.spans = Box::new(move || {
            let mut out = prev();
            out.push(span.clone());
            out
        });
        self
    }

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

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

    pub fn wrap_width(mut self, width: f32) -> Self {
        self.wrap_width = if width > 0.0 { Some(width) } else { None };
        self
    }

    pub fn wrap_mode(mut self, mode: WrapMode) -> Self {
        self.wrap_mode = mode;
        self
    }

    /// Enables wrapping at the width available from the parent container.
    pub fn wrap(mut self) -> Self {
        self.auto_wrap = true;
        self
    }

    /// Sets the line height as a multiple of the font size.
    pub fn line_height(mut self, factor: f32) -> Self {
        if factor.is_finite() && factor > 0.0 {
            self.line_height = LineHeight::Factor(factor);
        }
        self
    }

    /// Sets an absolute line height in logical pixels.
    pub fn line_height_px(mut self, px: f32) -> Self {
        if px.is_finite() && px > 0.0 {
            self.line_height = LineHeight::Absolute(px);
        }
        self
    }

    /// Sets vertical alignment within the text box.
    pub fn align_y(mut self, align: TextVAlign) -> Self {
        self.align_y = align;
        self
    }

    /// Limits visible wrapped text to `lines`, appending an ellipsis.
    pub fn max_lines(mut self, lines: usize) -> Self {
        self.max_lines = if lines == 0 { None } else { Some(lines) };
        self
    }

    /// Scales the resolved font size without changing layout styles.
    pub fn font_scale(mut self, scale: f32) -> Self {
        if scale.is_finite() && scale > 0.0 {
            self.font_scale = scale;
        }
        self
    }

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

    pub fn centered(mut self) -> Self {
        self.align = RichAlign::Center;
        self
    }

    pub fn right_aligned(mut self) -> Self {
        self.align = RichAlign::Right;
        self
    }

    pub fn letter_spacing(mut self, spacing: LetterSpacing) -> Self {
        self.letter_spacing = if spacing.is_finite() { spacing } else { 0.0 };
        self
    }

    pub fn on_span_click(mut self, handler: impl Fn(usize) + 'static) -> Self {
        self.on_span_click = Some(Rc::new(handler));
        self
    }

    pub fn width(mut self, width: impl Into<Length>) -> Self {
        self.style = self.style.width(width.into());
        self
    }

    pub fn height(mut self, height: impl Into<Length>) -> Self {
        self.style = self.style.height(height.into());
        self
    }

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

    pub fn max_width(mut self, v: f32) -> Self {
        self.style = self.style.max_width(v);
        self
    }

    pub fn min_height(mut self, v: f32) -> Self {
        self.style = self.style.min_height(v);
        self
    }

    pub fn max_height(mut self, v: f32) -> Self {
        self.style = self.style.max_height(v);
        self
    }

    pub fn flex(mut self, weight: f32) -> Self {
        self.style = self.style.flex(weight);
        self
    }

    pub fn expand(self) -> Self {
        self.flex(1.0)
    }

    pub fn fill_width(mut self) -> Self {
        self.style = self.style.width(Length::Fill(1.0));
        self
    }

    pub fn fill_height(mut self) -> Self {
        self.style = self.style.height(Length::Fill(1.0));
        self
    }

    pub fn fill_size(self) -> Self {
        self.fill_width().fill_height()
    }

    pub fn padding(mut self, padding: impl Into<Edges>) -> Self {
        self.style = self.style.padding(padding);
        self
    }

    pub fn pad(self, padding: impl Into<Edges>) -> Self {
        self.padding(padding)
    }

    pub fn padding_xy(mut self, x: f32, y: f32) -> Self {
        self.style = self.style.padding_xy(x, y);
        self
    }

    fn resolved(&self, theme_size: f32) -> (f32, f32) {
        (
            self.font_size.unwrap_or(theme_size) * self.font_scale,
            self.wrap_width.unwrap_or(0.0),
        )
    }
}

impl View for RichText {
    fn body(&self) -> impl View + 'static {
        let _ = (self.spans)();
        Unit
    }

    fn is_leaf(&self) -> bool {
        true
    }

    fn clip_self(&self) -> bool {
        true
    }

    fn cursor_icon(&self, _state: sequoia_style::VisualState) -> CursorIcon {
        if self.on_span_click.is_some() {
            CursorIcon::Pointer
        } else {
            CursorIcon::Default
        }
    }

    fn layout_style(&self) -> LayoutStyle {
        self.style
    }

    fn intrinsic_size(&self, ctx: &mut LayoutCtx) -> Option<Size> {
        let (size, _wrap) = self.resolved(ctx.default_font_size());
        let spans = (self.spans)();
        let pad = self.style.padding;
        let auto_wrap = if self.auto_wrap {
            text_wrap_limit(self.style, ctx.available_width())
        } else {
            None
        };
        let mut width = self.wrap_width.or(auto_wrap).unwrap_or(0.0);
        if self.max_lines.is_some() && width <= 0.0 {
            width = ctx
                .rich_size_lh(
                    &spans,
                    size,
                    0.0,
                    self.wrap_mode,
                    self.letter_spacing,
                    self.align,
                    self.line_height,
                )
                .width
                .max(1.0);
        }
        let shown = match self.max_lines {
            Some(max_lines) => ctx.rich_elide_lines(
                &spans,
                size,
                width,
                self.wrap_mode,
                self.letter_spacing,
                self.align,
                self.line_height,
                max_lines,
            ),
            None => spans,
        };
        let text = ctx.rich_size_lh(
            &shown,
            size,
            width,
            self.wrap_mode,
            self.letter_spacing,
            self.align,
            self.line_height,
        );
        Some(Size::new(
            text.width + pad.horizontal_sum(),
            text.height + pad.vertical_sum(),
        ))
    }

    fn baseline_offset(&self, ctx: &mut LayoutCtx, width: f32) -> Option<f32> {
        let (size, wrap) = self.resolved(ctx.default_font_size());
        let spans = (self.spans)();
        let pad = self.style.padding;
        let width = if wrap > 0.0 {
            wrap
        } else {
            (width - pad.horizontal_sum()).max(0.0)
        };
        let baseline = ctx.rich_first_baseline(
            &spans,
            size,
            width,
            self.wrap_mode,
            self.letter_spacing,
            self.align,
            self.line_height,
        );
        Some(pad.top + baseline)
    }

    fn on_event(&self, event: &Event, ctx: &mut EventCtx) {
        if ctx.phase != EventPhase::Target {
            return;
        }
        let Some(handler) = &self.on_span_click else {
            return;
        };
        if let Event::Pointer(PointerEvent::Down {
            button: PointerButton::Left,
            pos,
            ..
        }) = event
        {
            let size = self.font_size.unwrap_or_else(|| {
                ctx.environment
                    .get::<sequoia_style::Theme>()
                    .map(|t| t.font_size)
                    .unwrap_or(14.0)
            }) * self.font_scale;
            let spans = (self.spans)();
            let pad = self.style.padding;
            let wrap = self.wrap_width.unwrap_or_else(|| {
                if self.auto_wrap {
                    (ctx.rect.size.width - pad.horizontal_sum()).max(0.0)
                } else {
                    0.0
                }
            });
            let shown = match self.max_lines {
                Some(max_lines) => ctx.text.elide_rich_lines(
                    &spans,
                    size,
                    wrap,
                    self.wrap_mode,
                    self.letter_spacing,
                    self.align,
                    self.line_height,
                    max_lines,
                ),
                None => spans,
            };
            let rich = ctx.text.shape_rich_full_lh(
                &shown,
                size,
                wrap,
                self.wrap_mode,
                self.letter_spacing,
                self.align,
                self.line_height,
            );
            let text_h = rich.height();
            let first_baseline = rich.line(0).map(|line| line.baseline).unwrap_or(0.0);
            let block_top = match self.align_y {
                TextVAlign::Baseline => pad.top - first_baseline,
                TextVAlign::Top => pad.top,
                TextVAlign::Center => {
                    pad.top + (ctx.rect.size.height - pad.vertical_sum() - text_h) * 0.5
                }
                TextVAlign::Bottom => {
                    pad.top + (ctx.rect.size.height - pad.vertical_sum() - text_h)
                }
                _ => pad.top,
            };
            let hit = rich.hit_point(
                pos.x - ctx.rect.left() - pad.left,
                pos.y - ctx.rect.top() - block_top,
            );
            if let Some(span) = hit.span {
                handler(span);
                ctx.handled();
            }
        }
    }

    fn paint_self(&self, ctx: &mut PaintCtx) {
        let theme = ctx.theme();
        let (size, wrap) = self.resolved(theme.font_size);
        let color = self.color.unwrap_or(theme.text);
        let spans = (self.spans)();
        if spans.iter().all(|s| s.text.is_empty()) {
            return;
        }
        let pad = self.style.padding;
        let content_w = (ctx.rect.size.width - pad.horizontal_sum()).max(0.0);
        let wrap = if wrap > 0.0 {
            wrap
        } else if self.auto_wrap {
            content_w
        } else {
            0.0
        };
        let shown = match self.max_lines {
            Some(max_lines) => ctx.elide_rich_lines(
                &spans,
                size,
                wrap,
                self.wrap_mode,
                self.letter_spacing,
                self.align,
                self.line_height,
                max_lines,
            ),
            None => spans,
        };
        let rich = ctx.shape_rich_full_lh(
            &shown,
            size,
            wrap,
            self.wrap_mode,
            self.letter_spacing,
            self.align,
            self.line_height,
        );
        let text_h = rich.height();
        let first_baseline = rich.line(0).map(|line| line.baseline).unwrap_or(0.0);
        let block_top = match self.align_y {
            TextVAlign::Baseline => ctx.rect.top() + pad.top - first_baseline,
            TextVAlign::Top => ctx.rect.top() + pad.top,
            TextVAlign::Center => {
                ctx.rect.top()
                    + pad.top
                    + (ctx.rect.size.height - pad.vertical_sum() - text_h) * 0.5
            }
            TextVAlign::Bottom => {
                ctx.rect.top() + pad.top + (ctx.rect.size.height - pad.vertical_sum() - text_h)
            }
            _ => ctx.rect.top() + pad.top,
        };
        ctx.draw_rich(&rich, (ctx.rect.left() + pad.left, block_top), color);
    }
}