repose-ui 0.14.0

UI widgets and libs for Repose
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
//! # Scroll model
//!
//! Repose separates visual scroll containers from scroll state.
//!
//! This file implements inertial scroll states.
//!
//! Velocities are expressed in px/sec and integrated with dt,
//! so behavior is frame-rate independent.

use repose_core::*;
use std::cell::RefCell;
use std::rc::Rc;
use web_time::Instant;

/// Inertial scroll state (single axis Y).
pub struct ScrollState {
    scroll_offset: Signal<f32>,
    viewport_height: Signal<f32>,
    content_height: Signal<f32>,

    // physics (px/sec)
    vel: RefCell<f32>,
    last_t: RefCell<Instant>,
    last_input_t: RefCell<Instant>,
    animating: RefCell<bool>,
}

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

impl ScrollState {
    pub fn new() -> Self {
        let now = Instant::now();
        Self {
            scroll_offset: signal(0.0),
            viewport_height: signal(0.0),
            content_height: signal(0.0),
            vel: RefCell::new(0.0),
            last_t: RefCell::new(now),
            last_input_t: RefCell::new(now),
            animating: RefCell::new(false),
        }
    }

    pub fn set_viewport_height(&self, h: f32) {
        self.viewport_height.set(h.max(0.0));
        self.clamp_offset();
    }
    pub fn set_content_height(&self, h: f32) {
        self.content_height.set(h.max(0.0));
        self.clamp_offset();
    }
    pub fn set_offset(&self, off: f32) {
        let vh = self.viewport_height.get();
        let ch = self.content_height.get();
        let max_off = (ch - vh).max(0.0);
        self.scroll_offset.set(off.clamp(0.0, max_off));
    }

    fn clamp_offset(&self) {
        let vh = self.viewport_height.get();
        let ch = self.content_height.get();
        let max_off = (ch - vh).max(0.0);
        self.scroll_offset.update(|o| {
            if *o > max_off {
                *o = max_off;
            }
            if *o < 0.0 {
                *o = 0.0;
            }
        });
    }

    pub fn get(&self) -> f32 {
        self.scroll_offset.get()
    }

    /// Consume dy (pixels), clamp to bounds, return leftover.
    pub fn scroll_immediate(&self, dy: f32) -> f32 {
        let before = self.scroll_offset.get();
        let vh = self.viewport_height.get();
        let ch = self.content_height.get();
        let max_off = (ch - vh).max(0.0);

        let new_off = (before + dy).clamp(0.0, max_off);
        self.scroll_offset.set(new_off);

        let consumed = new_off - before;
        let leftover = dy - consumed;

        // Estimate input velocity (px/sec) based on time since last input.
        let now = Instant::now();
        let dt = (now - *self.last_input_t.borrow())
            .as_secs_f32()
            .clamp(1.0 / 240.0, 1.0 / 15.0);
        *self.last_input_t.borrow_mut() = now;

        *self.vel.borrow_mut() = consumed / dt;
        *self.animating.borrow_mut() = self.vel.borrow().abs() > 10.0;

        leftover
    }

    /// Advance physics one tick; returns true if animating.
    pub fn tick(&self) -> bool {
        if !*self.animating.borrow() {
            return false;
        }

        let now = Instant::now();
        let dt = (now - *self.last_t.borrow()).as_secs_f32().min(0.1);
        *self.last_t.borrow_mut() = now;
        if dt <= 0.0 {
            return false;
        }

        let vel0 = *self.vel.borrow();
        if vel0.abs() < 5.0 {
            *self.vel.borrow_mut() = 0.0;
            *self.animating.borrow_mut() = false;
            return false;
        }

        let before = self.scroll_offset.get();
        let vh = self.viewport_height.get();
        let ch = self.content_height.get();
        let max_off = (ch - vh).max(0.0);

        // Integrate
        let new_off = (before + vel0 * dt).clamp(0.0, max_off);
        self.scroll_offset.set(new_off);

        // If we hit an edge, stop quickly.
        if (new_off - before).abs() < 0.01 && (before <= 0.0 || before >= max_off) {
            *self.vel.borrow_mut() = 0.0;
            *self.animating.borrow_mut() = false;
            return false;
        }

        // Frame-rate independent decay; matches ~0.9 per 60Hz “frame”.
        let decay_per_60hz = 0.90f32;
        let decay = decay_per_60hz.powf(dt * 60.0);
        *self.vel.borrow_mut() = vel0 * decay;

        request_frame();

        true
    }
}

/// X-only state
pub struct HorizontalScrollState {
    scroll_offset: Signal<f32>,
    viewport_width: Signal<f32>,
    content_width: Signal<f32>,
    vel: RefCell<f32>, // px/sec
    last_t: RefCell<Instant>,
    last_input_t: RefCell<Instant>,
    animating: RefCell<bool>,
}
impl Default for HorizontalScrollState {
    fn default() -> Self {
        Self::new()
    }
}

impl HorizontalScrollState {
    pub fn new() -> Self {
        let now = Instant::now();
        Self {
            scroll_offset: signal(0.0),
            viewport_width: signal(0.0),
            content_width: signal(0.0),
            vel: RefCell::new(0.0),
            last_t: RefCell::new(now),
            last_input_t: RefCell::new(now),
            animating: RefCell::new(false),
        }
    }
    pub fn set_viewport_width(&self, w: f32) {
        self.viewport_width.set(w.max(0.0));
        self.clamp();
    }
    pub fn set_content_width(&self, w: f32) {
        self.content_width.set(w.max(0.0));
        self.clamp();
    }
    pub fn set_offset(&self, off: f32) {
        let max_off = (self.content_width.get() - self.viewport_width.get()).max(0.0);
        self.scroll_offset.set(off.clamp(0.0, max_off));
    }
    fn clamp(&self) {
        let max_off = (self.content_width.get() - self.viewport_width.get()).max(0.0);
        self.scroll_offset.update(|o| {
            *o = o.clamp(0.0, max_off);
        });
    }
    pub fn get(&self) -> f32 {
        self.scroll_offset.get()
    }
    pub fn scroll_immediate(&self, dx: f32) -> f32 {
        let before = self.scroll_offset.get();
        let max_off = (self.content_width.get() - self.viewport_width.get()).max(0.0);
        let new_off = (before + dx).clamp(0.0, max_off);
        self.scroll_offset.set(new_off);

        let consumed = new_off - before;
        let leftover = dx - consumed;

        let now = Instant::now();
        let dt = (now - *self.last_input_t.borrow())
            .as_secs_f32()
            .clamp(1.0 / 240.0, 1.0 / 15.0);
        *self.last_input_t.borrow_mut() = now;

        *self.vel.borrow_mut() = consumed / dt;
        *self.animating.borrow_mut() = self.vel.borrow().abs() > 10.0;

        leftover
    }
    pub fn tick(&self) -> bool {
        if !*self.animating.borrow() {
            return false;
        }

        let now = Instant::now();
        let dt = (now - *self.last_t.borrow()).as_secs_f32().min(0.1);
        *self.last_t.borrow_mut() = now;
        if dt <= 0.0 {
            return false;
        }

        let vel0 = *self.vel.borrow();
        if vel0.abs() < 5.0 {
            *self.animating.borrow_mut() = false;
            *self.vel.borrow_mut() = 0.0;
            return false;
        }

        let before = self.scroll_offset.get();
        let max_off = (self.content_width.get() - self.viewport_width.get()).max(0.0);
        let new_off = (before + vel0 * dt).clamp(0.0, max_off);
        self.scroll_offset.set(new_off);

        if (new_off - before).abs() < 0.01 && (before <= 0.0 || before >= max_off) {
            *self.vel.borrow_mut() = 0.0;
            *self.animating.borrow_mut() = false;
            return false;
        }

        let decay_per_60hz = 0.90f32;
        let decay = decay_per_60hz.powf(dt * 60.0);
        *self.vel.borrow_mut() = vel0 * decay;

        request_frame();

        true
    }
}

/// 2D state
pub struct ScrollStateXY {
    off_x: Signal<f32>,
    off_y: Signal<f32>,
    vp_w: Signal<f32>,
    vp_h: Signal<f32>,
    c_w: Signal<f32>,
    c_h: Signal<f32>,
    vel_x: RefCell<f32>, // px/sec
    vel_y: RefCell<f32>, // px/sec
    last_t: RefCell<Instant>,
    last_input_t: RefCell<Instant>,
    animating: RefCell<bool>,
}
impl Default for ScrollStateXY {
    fn default() -> Self {
        Self::new()
    }
}

impl ScrollStateXY {
    pub fn new() -> Self {
        let now = Instant::now();
        Self {
            off_x: signal(0.0),
            off_y: signal(0.0),
            vp_w: signal(0.0),
            vp_h: signal(0.0),
            c_w: signal(0.0),
            c_h: signal(0.0),
            vel_x: RefCell::new(0.0),
            vel_y: RefCell::new(0.0),
            last_t: RefCell::new(now),
            last_input_t: RefCell::new(now),
            animating: RefCell::new(false),
        }
    }
    pub fn set_viewport(&self, w: f32, h: f32) {
        self.vp_w.set(w.max(0.0));
        self.vp_h.set(h.max(0.0));
        self.clamp();
    }
    pub fn set_content(&self, w: f32, h: f32) {
        self.c_w.set(w.max(0.0));
        self.c_h.set(h.max(0.0));
        self.clamp();
    }
    pub fn set_offset_xy(&self, x: f32, y: f32) {
        let max_x = (self.c_w.get() - self.vp_w.get()).max(0.0);
        let max_y = (self.c_h.get() - self.vp_h.get()).max(0.0);
        self.off_x.set(x.clamp(0.0, max_x));
        self.off_y.set(y.clamp(0.0, max_y));
    }
    fn clamp(&self) {
        let max_x = (self.c_w.get() - self.vp_w.get()).max(0.0);
        let max_y = (self.c_h.get() - self.vp_h.get()).max(0.0);
        self.off_x.update(|x| *x = x.clamp(0.0, max_x));
        self.off_y.update(|y| *y = y.clamp(0.0, max_y));
    }
    pub fn get(&self) -> (f32, f32) {
        (self.off_x.get(), self.off_y.get())
    }
    pub fn scroll_immediate(&self, d: Vec2) -> Vec2 {
        let bx = self.off_x.get();
        let by = self.off_y.get();

        let max_x = (self.c_w.get() - self.vp_w.get()).max(0.0);
        let max_y = (self.c_h.get() - self.vp_h.get()).max(0.0);

        let nx = (bx + d.x).clamp(0.0, max_x);
        let ny = (by + d.y).clamp(0.0, max_y);

        self.off_x.set(nx);
        self.off_y.set(ny);

        let consumed_x = nx - bx;
        let consumed_y = ny - by;

        let now = Instant::now();
        let dt = (now - *self.last_input_t.borrow())
            .as_secs_f32()
            .clamp(1.0 / 240.0, 1.0 / 15.0);
        *self.last_input_t.borrow_mut() = now;

        *self.vel_x.borrow_mut() = consumed_x / dt;
        *self.vel_y.borrow_mut() = consumed_y / dt;
        *self.animating.borrow_mut() =
            self.vel_x.borrow().abs() > 10.0 || self.vel_y.borrow().abs() > 10.0;

        Vec2 {
            x: d.x - consumed_x,
            y: d.y - consumed_y,
        }
    }
    pub fn tick(&self) -> bool {
        if !*self.animating.borrow() {
            return false;
        }

        let now = Instant::now();
        let dt = (now - *self.last_t.borrow()).as_secs_f32().min(0.1);
        *self.last_t.borrow_mut() = now;
        if dt <= 0.0 {
            return false;
        }

        let vx0 = *self.vel_x.borrow();
        let vy0 = *self.vel_y.borrow();
        if vx0.abs() < 5.0 && vy0.abs() < 5.0 {
            *self.animating.borrow_mut() = false;
            *self.vel_x.borrow_mut() = 0.0;
            *self.vel_y.borrow_mut() = 0.0;
            return false;
        }

        let (bx, by) = (self.off_x.get(), self.off_y.get());
        let max_x = (self.c_w.get() - self.vp_w.get()).max(0.0);
        let max_y = (self.c_h.get() - self.vp_h.get()).max(0.0);

        let nx = (bx + vx0 * dt).clamp(0.0, max_x);
        let ny = (by + vy0 * dt).clamp(0.0, max_y);

        self.off_x.set(nx);
        self.off_y.set(ny);

        // stop quickly at edges
        if (nx - bx).abs() < 0.01 && (bx <= 0.0 || bx >= max_x) {
            *self.vel_x.borrow_mut() = 0.0;
        }
        if (ny - by).abs() < 0.01 && (by <= 0.0 || by >= max_y) {
            *self.vel_y.borrow_mut() = 0.0;
        }

        let decay_per_60hz = 0.95f32;
        let decay = decay_per_60hz.powf(dt * 60.0);
        *self.vel_x.borrow_mut() *= decay;
        *self.vel_y.borrow_mut() *= decay;

        *self.animating.borrow_mut() =
            self.vel_x.borrow().abs() > 5.0 || self.vel_y.borrow().abs() > 5.0;

        if *self.animating.borrow() {
            request_frame();
            return true;
        }
        false
    }
}

/// Remembered ScrollState (requires unique key).
pub fn remember_scroll_state(key: impl Into<String>) -> Rc<ScrollState> {
    repose_core::remember_with_key(key.into(), ScrollState::new)
}

pub fn remember_horizontal_scroll_state(key: impl Into<String>) -> Rc<HorizontalScrollState> {
    repose_core::remember_with_key(key.into(), HorizontalScrollState::new)
}
pub fn remember_scroll_state_xy(key: impl Into<String>) -> Rc<ScrollStateXY> {
    repose_core::remember_with_key(key.into(), ScrollStateXY::new)
}

/// Scroll container with inertia, like verticalScroll.
pub fn ScrollArea(modifier: Modifier, state: Rc<ScrollState>, content: View) -> View {
    let st_clone = state.clone();
    let on_scroll = {
        Rc::new(move |d: Vec2| -> Vec2 {
            Vec2 {
                x: d.x,
                y: st_clone.scroll_immediate(d.y),
            }
        })
    };
    let set_viewport = {
        let st = state.clone();
        Rc::new(move |h: f32| st.set_viewport_height(h))
    };
    let set_content = {
        let st = state.clone();
        Rc::new(move |h: f32| st.set_content_height(h))
    };
    let get_scroll = {
        let st = state.clone();
        Rc::new(move || {
            st.tick();
            st.get()
        })
    };
    let set_scroll = {
        let st = state.clone();
        Rc::new(move |off: f32| st.set_offset(off))
    };
    View::new(
        0,
        ViewKind::ScrollV {
            on_scroll: Some(on_scroll),
            set_viewport_height: Some(set_viewport),
            set_content_height: Some(set_content),
            get_scroll_offset: Some(get_scroll),
            set_scroll_offset: Some(set_scroll),
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}

pub fn HorizontalScrollArea(
    modifier: Modifier,
    state: Rc<HorizontalScrollState>,
    content: View,
) -> View {
    let st_clone = state.clone();
    let on_scroll = {
        Rc::new(move |d: Vec2| -> Vec2 {
            // Most mice only generate vertical wheel. If dx is zero, treat dy as horizontal scroll.
            // Do also consume that vertical delta so parent vertical scrollers don't steal it.
            let use_dx = if d.x.abs() > 0.001 { d.x } else { d.y };
            let leftover_x = st_clone.scroll_immediate(use_dx);
            Vec2 {
                x: leftover_x,
                y: if d.x.abs() > 0.001 { d.y } else { 0.0 },
            }
        })
    };
    let set_viewport_w = {
        let st = state.clone();
        Rc::new(move |w: f32| st.set_viewport_width(w))
    };
    let set_content_w = {
        let st = state.clone();
        Rc::new(move |w: f32| st.set_content_width(w))
    };
    let get_scroll_xy = {
        let st = state.clone();
        Rc::new(move || {
            st.tick();
            (st.get(), 0.0)
        })
    };
    let set_xy = {
        let st = state.clone();
        Rc::new(move |x: f32, _y: f32| st.set_offset(x))
    };
    View::new(
        0,
        ViewKind::ScrollXY {
            on_scroll: Some(on_scroll),
            set_viewport_width: Some(set_viewport_w),
            set_viewport_height: None,
            set_content_width: Some(set_content_w),
            set_content_height: None,
            get_scroll_offset_xy: Some(get_scroll_xy),
            set_scroll_offset_xy: Some(set_xy),
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}

pub fn ScrollAreaXY(modifier: Modifier, state: Rc<ScrollStateXY>, content: View) -> View {
    let on_scroll = {
        let st = state.clone();
        Rc::new(move |d: Vec2| -> Vec2 { st.scroll_immediate(d) })
    };
    let set_vw = {
        let st = state.clone();
        Rc::new(move |w: f32| st.set_viewport(w, st.vp_h.get()))
    };
    let set_vh = {
        let st = state.clone();
        Rc::new(move |h: f32| st.set_viewport(st.vp_w.get(), h))
    };
    let set_cw = {
        let st = state.clone();
        Rc::new(move |w: f32| {
            st.set_content(w, st.c_h.get());
        })
    };
    let set_ch = {
        let st = state.clone();
        Rc::new(move |h: f32| {
            st.set_content(st.c_w.get(), h);
        })
    };
    let get_xy = {
        let st = state.clone();
        Rc::new(move || {
            st.tick();
            st.get()
        })
    };
    let set_xy = {
        let st = state.clone();
        Rc::new(move |x: f32, y: f32| st.set_offset_xy(x, y))
    };

    View::new(
        0,
        ViewKind::ScrollXY {
            on_scroll: Some(on_scroll),
            set_viewport_width: Some(set_vw),
            set_viewport_height: Some(set_vh),
            set_content_width: Some(set_cw),
            set_content_height: Some(set_ch),
            get_scroll_offset_xy: Some(get_xy),
            set_scroll_offset_xy: Some(set_xy),
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}