sequoia-elements 0.1.3

Sequoia GUI framework — built-in UI elements
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
use std::cell::RefCell;
use std::rc::Rc;

use sequoia_core::{Color, Edges, Length, Point, Size};
use sequoia_input::{
    CursorIcon, Event, EventPhase, Key, KeyEvent, Modifiers, PointerButton, PointerEvent,
};
use sequoia_layout::{Axis, LayoutCtx, LayoutStyle};
use sequoia_reactive::Signal;
use sequoia_style::{DragValueStyle, Theme, VisualState};
use sequoia_view::{AnyView, EventCtx, FontWeight, PaintCtx, Unit, View};

use crate::underline;

const HEIGHT: f32 = 24.0;

const KEY_FRACTION: f32 = 20.0;

const DRAG_PX_FULL_RANGE: f32 = 200.0;

const MAX_PRECISION: usize = 9;

const CLICK_SLOP: f32 = 3.0;

const CARET: char = '|';

#[derive(Clone, Copy)]
struct DragState {
    origin: Point,
    value: f32,
}

#[derive(Clone, Default)]
struct Gesture {
    drag: Option<DragState>,
    edit: Option<String>,

    fresh: bool,

    announced: bool,
}

pub struct DragValue {
    value: Signal<f32>,
    reset: Option<f32>,
    min: f32,
    max: f32,
    sensitivity: Option<f32>,
    step: Option<f32>,
    precision: usize,
    orientation: Axis,
    text_color: Option<Color>,
    line_color: Option<Color>,
    line_width: Option<f32>,
    line_full: bool,
    trim_zeros: bool,
    font_size: Option<f32>,
    weight: Option<FontWeight>,
    appearance: Option<DragValueStyle>,
    style: LayoutStyle,
    margin: Edges,

    revision: Signal<u32>,

    gesture: Rc<RefCell<Gesture>>,

    on_change_start: Option<Rc<dyn Fn()>>,

    on_commit: Option<Rc<dyn Fn(f32)>>,
}

impl DragValue {
    pub fn new(value: Signal<f32>) -> Self {
        Self {
            value,
            reset: None,
            min: 0.0,
            max: 1.0,
            sensitivity: None,
            step: None,
            precision: 2,
            orientation: Axis::Horizontal,
            text_color: None,
            line_color: None,
            line_width: None,
            line_full: false,
            trim_zeros: false,
            font_size: None,
            weight: None,
            appearance: None,
            style: LayoutStyle {
                width: Length::Fit,
                height: Length::Px(HEIGHT),
                ..LayoutStyle::default()
            },
            margin: Edges::ZERO,
            revision: Signal::new(0),
            gesture: Rc::new(RefCell::new(Gesture::default())),
            on_change_start: None,
            on_commit: None,
        }
    }

    pub fn reset_to(mut self, value: f32) -> Self {
        self.reset = Some(value);
        self
    }

    pub fn min(mut self, min: f32) -> Self {
        if min.is_finite() {
            self.min = min;
        }
        self
    }

    pub fn max(mut self, max: f32) -> Self {
        if max.is_finite() {
            self.max = max;
        }
        self
    }

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

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

    pub fn precision(mut self, precision: usize) -> Self {
        self.precision = precision.min(MAX_PRECISION);
        self
    }

    pub fn orientation(mut self, axis: Axis) -> Self {
        self.orientation = axis;
        self
    }

    pub fn appearance(mut self, style: DragValueStyle) -> Self {
        self.appearance = Some(style);
        self
    }

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

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

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

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

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

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

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

    pub fn bold(mut self, bold: bool) -> Self {
        self.weight = Some(if bold {
            FontWeight::BOLD
        } else {
            FontWeight::NORMAL
        });
        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 margin(mut self, margin: impl Into<Edges>) -> Self {
        let m = margin.into();
        self.margin = Edges::only(
            sanitize_margin(m.left),
            sanitize_margin(m.top),
            sanitize_margin(m.right),
            sanitize_margin(m.bottom),
        );
        self
    }

    pub fn margin_xy(self, x: f32, y: f32) -> Self {
        self.margin(Edges::xy(x, y))
    }

    pub fn margin_x(mut self, x: f32) -> Self {
        let x = sanitize_margin(x);
        self.margin.left = x;
        self.margin.right = x;
        self
    }

    pub fn margin_y(mut self, y: f32) -> Self {
        let y = sanitize_margin(y);
        self.margin.top = y;
        self.margin.bottom = y;
        self
    }

    pub fn margin_left(mut self, v: f32) -> Self {
        self.margin.left = sanitize_margin(v);
        self
    }

    pub fn margin_right(mut self, v: f32) -> Self {
        self.margin.right = sanitize_margin(v);
        self
    }

    pub fn margin_top(mut self, v: f32) -> Self {
        self.margin.top = sanitize_margin(v);
        self
    }

    pub fn margin_bottom(mut self, v: f32) -> Self {
        self.margin.bottom = sanitize_margin(v);
        self
    }

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

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

    fn announce(&self) {
        let need = self.with_gesture(|g| {
            if g.announced {
                false
            } else {
                g.announced = true;
                true
            }
        });
        if need && let Some(f) = &self.on_change_start {
            f();
        }
    }

    fn commit_if_changed(&self, before: f32) {
        let after = self.value.get_untracked();
        if after != before
            && let Some(f) = &self.on_commit
        {
            f(after);
        }
    }

    fn with_gesture<R>(&self, f: impl FnOnce(&mut Gesture) -> R) -> R {
        f(&mut self.gesture.borrow_mut())
    }

    fn anchor(&self) -> Option<DragState> {
        self.with_gesture(|g| g.drag)
    }

    fn is_editing(&self) -> bool {
        self.with_gesture(|g| g.edit.is_some())
    }

    fn edit_buffer(&self) -> Option<String> {
        self.with_gesture(|g| g.edit.clone())
    }

    fn start_edit(&self, buffer: String, fresh: bool) {
        self.with_gesture(|g| {
            g.edit = Some(buffer);
            g.fresh = fresh;
        });
        self.bump();
    }

    fn stop_edit(&self) {
        self.with_gesture(|g| {
            g.edit = None;
            g.fresh = false;
        });
        self.bump();
    }

    fn push_char(&self, c: char) {
        self.with_gesture(|g| {
            if g.edit.is_none() {
                return;
            }
            if g.fresh {
                g.edit = Some(String::new());
                g.fresh = false;
            }
            if let Some(buffer) = g.edit.as_mut() {
                buffer.push(c);
            }
        });
        self.bump();
    }

    fn pop_char(&self) {
        self.with_gesture(|g| {
            g.fresh = false;
            if let Some(buffer) = g.edit.as_mut() {
                buffer.pop();
            }
        });
        self.bump();
    }

    fn commit_edit(&self) {
        let Some(buffer) = self.edit_buffer() else {
            return;
        };
        let before = self.value.get_untracked();
        self.stop_edit();
        if let Ok(parsed) = buffer.trim().replace(',', ".").parse::<f32>() {
            self.set_value(parsed);
        }
        self.commit_if_changed(before);
    }

    fn bump(&self) {
        self.revision
            .set(self.revision.get_untracked().wrapping_add(1));
    }

    fn span(&self) -> f32 {
        (self.max - self.min).abs().max(f32::EPSILON)
    }

    fn units_per_px(&self) -> f32 {
        self.sensitivity.unwrap_or(self.span() / DRAG_PX_FULL_RANGE)
    }

    fn key_step(&self) -> f32 {
        self.step.unwrap_or(self.span() / KEY_FRACTION)
    }

    fn clamped(&self, v: f32) -> f32 {
        let lo = self.min.min(self.max);
        let hi = self.min.max(self.max);
        let scale = 10f32.powi(self.precision as i32);
        let q = ((v.clamp(lo, hi) * scale).round() / scale).clamp(lo, hi);
        if q == 0.0 { 0.0 } else { q }
    }

    fn set_value(&self, v: f32) {
        let next = self.clamped(v);

        if !next.is_finite() {
            return;
        }
        if next != self.value.get_untracked() {
            self.announce();
            self.value.set(next);
        }
    }

    fn text(&self, value: f32) -> String {
        let s = format!("{value:.prec$}", prec = self.precision);
        if self.trim_zeros && self.precision > 0 && (s.contains('.') || s.contains(',')) {
            let t = s.trim_end_matches('0');
            t.strip_suffix('.')
                .or_else(|| t.strip_suffix(','))
                .unwrap_or(t)
                .to_owned()
        } else {
            s
        }
    }

    fn display(&self, value: f32) -> String {
        match self.edit_buffer() {
            Some(buffer) => format!("{buffer}{CARET}"),
            None => self.text(value),
        }
    }

    fn font_weight(&self) -> FontWeight {
        self.weight
            .or_else(|| {
                self.appearance
                    .and_then(|s| s.font_weight)
                    .map(FontWeight::new)
            })
            .unwrap_or_default()
    }

    fn resolved_font_size(&self, theme: &Theme) -> f32 {
        self.font_size
            .or_else(|| self.appearance.and_then(|s| s.font_size))
            .unwrap_or(theme.font_size)
    }

    fn resolved_line_width(&self) -> f32 {
        if let Some(w) = self
            .line_width
            .or_else(|| self.appearance.and_then(|s| s.line_width))
        {
            return w;
        }
        let has_line_color =
            self.line_color.is_some() || self.appearance.and_then(|s| s.line).is_some();
        if has_line_color {
            return underline::LINE_W;
        }
        0.0
    }

    fn resolved_text_color(&self) -> Option<Color> {
        self.text_color
            .or_else(|| self.appearance.and_then(|s| s.text))
    }

    fn resolved_line_color(&self) -> Option<Color> {
        self.line_color
            .or_else(|| self.appearance.and_then(|s| s.line))
    }
}

fn is_value_char(c: char) -> bool {
    c.is_ascii_digit() || c == '.' || c == ',' || c == '-'
}

fn normalize_char(c: char) -> char {
    if c == ',' { '.' } else { c }
}

impl View for DragValue {
    fn body(&self) -> impl View + 'static {
        Unit
    }

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

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

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

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

    fn keyed_children(&self) -> Option<Vec<(u64, AnyView)>> {
        let _ = self.value.get();
        let _ = self.revision.get();
        Some(Vec::new())
    }

    fn cursor_icon(&self, state: VisualState) -> CursorIcon {
        if state.pressed {
            match self.orientation {
                Axis::Horizontal => CursorIcon::ColResize,
                Axis::Vertical => CursorIcon::RowResize,
            }
        } else {
            CursorIcon::Grab
        }
    }

    fn layout_style(&self) -> LayoutStyle {
        let mut s = self.style;
        s.margin = self.margin;
        s
    }

    fn intrinsic_size(&self, ctx: &mut LayoutCtx) -> Option<Size> {
        let appearance = self.appearance.unwrap_or_default();
        let size = self.font_size.or(appearance.font_size).unwrap_or(14.0);
        let text = self.display(self.clamped(self.value.get_untracked()));
        let w = ctx.text_width_weighted(&text, size, self.font_weight());
        let line_width = self.resolved_line_width();
        let h = if line_width > 0.0 {
            underline::content_height(size, line_width)
        } else {
            LayoutCtx::text_line_height(size)
        };
        Some(Size::new(w + underline::PAD_X * 2.0, h))
    }

    fn reuse_state(&self, old: &Self) {
        if !Rc::ptr_eq(&self.gesture, &old.gesture) {
            *self.gesture.borrow_mut() = old.gesture.borrow().clone();
        }
        self.revision.set(old.revision.get_untracked());
    }

    fn on_event(&self, event: &Event, ctx: &mut EventCtx) {
        if ctx.phase != EventPhase::Target {
            return;
        }
        match event {
            Event::Pointer(PointerEvent::Down {
                button: PointerButton::Left,
                pos,
                modifiers,
                ..
            }) => {
                if modifiers.contains(Modifiers::CONTROL) {
                    if let Some(reset) = self.reset {
                        let before = self.value.get_untracked();
                        self.stop_edit();
                        self.with_gesture(|g| g.drag = None);
                        self.set_value(reset);
                        self.commit_if_changed(before);
                        ctx.handled();
                    }
                    return;
                }

                if self.is_editing() {
                    self.commit_edit();
                }
                self.with_gesture(|g| {
                    g.drag = Some(DragState {
                        origin: *pos,
                        value: self.value.get_untracked(),
                    });
                    g.announced = false;
                });
                ctx.capture_pointer();
                ctx.handled();
            }
            Event::Pointer(PointerEvent::Move { pos, .. }) => {
                let Some(drag) = self.anchor() else { return };

                if !ctx.state.pressed {
                    self.with_gesture(|g| g.drag = None);
                    self.commit_if_changed(drag.value);
                    return;
                }
                let delta = match self.orientation {
                    Axis::Horizontal => pos.x - drag.origin.x,

                    Axis::Vertical => drag.origin.y - pos.y,
                };
                self.set_value(drag.value + delta * self.units_per_px());
                ctx.handled();
            }
            Event::Pointer(PointerEvent::Up { pos, .. }) => {
                let Some(drag) = self.anchor() else { return };
                self.with_gesture(|g| g.drag = None);

                let moved = (pos.x - drag.origin.x)
                    .abs()
                    .max((pos.y - drag.origin.y).abs());
                if moved <= CLICK_SLOP {
                    self.start_edit(self.text(self.value.get_untracked()), true);
                } else {
                    self.bump();
                    self.commit_if_changed(drag.value);
                }
                ctx.handled();
            }
            Event::Key(key) => {
                if self.is_editing() {
                    self.on_edit_key(key);
                    ctx.handled();
                    return;
                }

                if let Some(c) = key.key.char().filter(|c| is_value_char(*c)) {
                    self.start_edit(normalize_char(c).to_string(), false);
                    ctx.request_focus();
                    ctx.handled();
                    return;
                }
                let delta = match key.key {
                    Key::ARROW_LEFT | Key::ARROW_DOWN => -self.key_step(),
                    Key::ARROW_RIGHT | Key::ARROW_UP => self.key_step(),
                    _ => return,
                };
                let before = self.value.get_untracked();
                self.set_value(self.value.get_untracked() + delta);
                self.commit_if_changed(before);
                ctx.handled();
            }

            Event::Pointer(PointerEvent::Wheel {
                delta, modifiers, ..
            }) => {
                if self.is_editing() {
                    return;
                }
                let mult = if modifiers.contains(Modifiers::SHIFT) {
                    0.1
                } else if modifiers.contains(Modifiers::CONTROL)
                    || modifiers.contains(Modifiers::SUPER)
                {
                    10.0
                } else {
                    1.0
                };
                let dir = if delta.y < 0.0 { 1.0 } else { -1.0 };
                let before = self.value.get_untracked();
                self.set_value(before + self.span() / 100.0 * mult * dir);
                self.commit_if_changed(before);
                ctx.handled();
            }
            _ => {}
        }
    }

    fn paint_self(&self, ctx: &mut PaintCtx) {
        if !ctx.state.focused && self.is_editing() {
            self.commit_edit();
        }

        let rect = ctx.rect;
        if rect.size.width <= 0.0 || rect.size.height <= 0.0 {
            return;
        }
        let theme = ctx.theme();
        let size = self.resolved_font_size(&theme);
        let weight = self.font_weight();
        let line_width = self.resolved_line_width();
        let editing = self.is_editing();
        let colors = underline::field_colors(
            &theme,
            ctx.state.hovered || ctx.state.pressed || editing,
            ctx.state.pressed || editing,
            self.resolved_text_color(),
            self.resolved_line_color(),
        );
        let text = self.display(self.clamped(self.value.get_untracked()));
        let text_width = ctx.text_width_weighted(&text, size, weight);

        if line_width > 0.0 && colors.line.a > 0.0 {
            let w = if self.line_full {
                (rect.size.width - underline::PAD_X * 2.0).max(0.0)
            } else {
                text_width
            };
            let line = underline::line_rect(rect, size, w, line_width);
            ctx.quad(line, colors.line, line_width * 0.5);
        }

        let x = rect.left() + underline::PAD_X;
        let y = underline::baseline(rect, size);
        ctx.draw_text_weighted(&text, (x, y), size, weight, colors.text);
    }
}

impl DragValue {
    fn on_edit_key(&self, key: &KeyEvent) {
        match key.key {
            Key::ENTER => self.commit_edit(),
            Key::ESCAPE => self.stop_edit(),
            Key::BACKSPACE => self.pop_char(),
            other => {
                if let Some(c) = other.char().filter(|c| is_value_char(*c)) {
                    self.push_char(normalize_char(c));
                }
            }
        }
    }
}

fn sanitize_margin(v: f32) -> f32 {
    if v.is_finite() { v.max(0.0) } else { 0.0 }
}