fission-core 0.2.0

Core runtime, state, actions, effects, resources, input, and UI model for Fission
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
use crate::env::ScrollStateMap;
use fission_ir::{CoreIR, FlexDirection, LayoutOp, NodeId, Op};
use fission_layout::{LayoutPoint, LayoutRect, LayoutSnapshot};

pub const SCROLLBAR_INSET: f32 = 2.0;
pub const SCROLLBAR_THICKNESS: f32 = 6.0;
pub const SCROLLBAR_MIN_THUMB: f32 = 24.0;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollbarAxis {
    Horizontal,
    Vertical,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScrollbarGeometry {
    pub node_id: NodeId,
    pub axis: ScrollbarAxis,
    pub rail_rect: LayoutRect,
    pub thumb_rect: LayoutRect,
    pub offset: f32,
    pub max_offset: f32,
    pub track_travel: f32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollbarHitKind {
    Thumb,
    Rail,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScrollbarHit {
    pub geometry: ScrollbarGeometry,
    pub kind: ScrollbarHitKind,
    pub pointer_to_thumb_start: f32,
    pub layout_point: LayoutPoint,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScrollbarDragState {
    pub node_id: NodeId,
    pub pointer_to_thumb_start: f32,
}

pub fn scrollbar_geometry_for_node(
    ir: &CoreIR,
    layout: &LayoutSnapshot,
    scroll_map: &ScrollStateMap,
    node_id: NodeId,
) -> Option<ScrollbarGeometry> {
    let node = ir.nodes.get(&node_id)?;
    let Op::Layout(LayoutOp::Scroll {
        direction,
        show_scrollbar,
        ..
    }) = &node.op
    else {
        return None;
    };
    if !show_scrollbar {
        return None;
    }

    let geom = layout.get_node_geometry(node_id)?;
    let rect = geom.rect;
    let (axis, viewport_extent, content_extent, rail_rect) = match direction {
        FlexDirection::Column => {
            let rail_extent = (rect.size.height - SCROLLBAR_INSET * 2.0).max(0.0);
            (
                ScrollbarAxis::Vertical,
                rect.size.height,
                geom.content_size.height,
                LayoutRect::new(
                    rect.origin.x + rect.size.width - SCROLLBAR_THICKNESS - SCROLLBAR_INSET,
                    rect.origin.y + SCROLLBAR_INSET,
                    SCROLLBAR_THICKNESS,
                    rail_extent,
                ),
            )
        }
        FlexDirection::Row => {
            let rail_extent = (rect.size.width - SCROLLBAR_INSET * 2.0).max(0.0);
            (
                ScrollbarAxis::Horizontal,
                rect.size.width,
                geom.content_size.width,
                LayoutRect::new(
                    rect.origin.x + SCROLLBAR_INSET,
                    rect.origin.y + rect.size.height - SCROLLBAR_THICKNESS - SCROLLBAR_INSET,
                    rail_extent,
                    SCROLLBAR_THICKNESS,
                ),
            )
        }
    };

    if viewport_extent <= 0.0 || content_extent <= viewport_extent + 0.5 {
        return None;
    }

    let rail_extent = axis_extent(axis, rail_rect);
    if rail_extent <= 0.0 {
        return None;
    }

    let max_offset = (content_extent - viewport_extent).max(0.0);
    let offset = scroll_map.get_offset(node_id).clamp(0.0, max_offset);
    let min_thumb = SCROLLBAR_MIN_THUMB.min(rail_extent);
    let thumb_extent =
        ((viewport_extent / content_extent) * rail_extent).clamp(min_thumb, rail_extent);
    let track_travel = (rail_extent - thumb_extent).max(0.0);
    let thumb_start = axis_start(axis, rail_rect)
        + if max_offset > 0.0 && track_travel > 0.0 {
            (offset / max_offset) * track_travel
        } else {
            0.0
        };

    let thumb_rect = match axis {
        ScrollbarAxis::Vertical => LayoutRect::new(
            rail_rect.origin.x,
            thumb_start,
            SCROLLBAR_THICKNESS,
            thumb_extent,
        ),
        ScrollbarAxis::Horizontal => LayoutRect::new(
            thumb_start,
            rail_rect.origin.y,
            thumb_extent,
            SCROLLBAR_THICKNESS,
        ),
    };

    Some(ScrollbarGeometry {
        node_id,
        axis,
        rail_rect,
        thumb_rect,
        offset,
        max_offset,
        track_travel,
    })
}

pub fn scrollbar_hit_test(
    ir: &CoreIR,
    layout: &LayoutSnapshot,
    scroll_map: &ScrollStateMap,
    point: LayoutPoint,
) -> Option<ScrollbarHit> {
    let root = ir.root?;
    scrollbar_hit_test_recursive(root, ir, layout, scroll_map, point)
}

pub fn scrollbar_drag_offset(geometry: ScrollbarGeometry, point: LayoutPoint) -> f32 {
    scrollbar_drag_offset_with_grab(geometry, point, geometry.thumb_extent() * 0.5)
}

pub fn scrollbar_point_for_node(
    ir: &CoreIR,
    scroll_map: &ScrollStateMap,
    node_id: NodeId,
    mut point: LayoutPoint,
) -> LayoutPoint {
    let mut current = ir.nodes.get(&node_id).and_then(|node| node.parent);
    while let Some(parent_id) = current {
        let Some(parent) = ir.nodes.get(&parent_id) else {
            break;
        };
        if let Op::Layout(LayoutOp::Scroll { direction, .. }) = &parent.op {
            let offset = scroll_map.get_offset(parent_id);
            match direction {
                FlexDirection::Column => point.y += offset,
                FlexDirection::Row => point.x += offset,
            }
        }
        current = parent.parent;
    }
    point
}

pub fn scrollbar_drag_offset_with_grab(
    geometry: ScrollbarGeometry,
    point: LayoutPoint,
    pointer_to_thumb_start: f32,
) -> f32 {
    if geometry.track_travel <= 0.0 || geometry.max_offset <= 0.0 {
        return 0.0;
    }
    let rail_start = axis_start(geometry.axis, geometry.rail_rect);
    let pointer_axis = point_axis(geometry.axis, point);
    let requested_thumb_start = pointer_axis - pointer_to_thumb_start;
    let normalized = ((requested_thumb_start - rail_start) / geometry.track_travel).clamp(0.0, 1.0);
    normalized * geometry.max_offset
}

impl ScrollbarGeometry {
    pub fn thumb_extent(self) -> f32 {
        axis_extent(self.axis, self.thumb_rect)
    }
}

fn scrollbar_hit_test_recursive(
    node_id: NodeId,
    ir: &CoreIR,
    layout: &LayoutSnapshot,
    scroll_map: &ScrollStateMap,
    point: LayoutPoint,
) -> Option<ScrollbarHit> {
    let node = ir.nodes.get(&node_id)?;
    let geom = layout.get_node_geometry(node_id)?;
    let is_clip_container = matches!(
        node.op,
        Op::Layout(LayoutOp::Clip { .. }) | Op::Layout(LayoutOp::Scroll { .. })
    );
    if is_clip_container && !geom.rect.contains(point) {
        return None;
    }

    if let Some(geometry) = scrollbar_geometry_for_node(ir, layout, scroll_map, node_id) {
        if geometry.thumb_rect.contains(point) {
            return Some(ScrollbarHit {
                geometry,
                kind: ScrollbarHitKind::Thumb,
                pointer_to_thumb_start: point_axis(geometry.axis, point)
                    - axis_start(geometry.axis, geometry.thumb_rect),
                layout_point: point,
            });
        }
        if geometry.rail_rect.contains(point) {
            return Some(ScrollbarHit {
                geometry,
                kind: ScrollbarHitKind::Rail,
                pointer_to_thumb_start: geometry.thumb_extent() * 0.5,
                layout_point: point,
            });
        }
    }

    let mut child_point = point;
    if let Op::Layout(LayoutOp::Scroll { direction, .. }) = &node.op {
        let offset = scroll_map.get_offset(node_id);
        match direction {
            FlexDirection::Column => child_point.y += offset,
            FlexDirection::Row => child_point.x += offset,
        }
    }

    for child_id in node.children.iter().rev() {
        if let Some(hit) =
            scrollbar_hit_test_recursive(*child_id, ir, layout, scroll_map, child_point)
        {
            return Some(hit);
        }
    }

    None
}

fn axis_start(axis: ScrollbarAxis, rect: LayoutRect) -> f32 {
    match axis {
        ScrollbarAxis::Horizontal => rect.origin.x,
        ScrollbarAxis::Vertical => rect.origin.y,
    }
}

fn axis_extent(axis: ScrollbarAxis, rect: LayoutRect) -> f32 {
    match axis {
        ScrollbarAxis::Horizontal => rect.size.width,
        ScrollbarAxis::Vertical => rect.size.height,
    }
}

fn point_axis(axis: ScrollbarAxis, point: LayoutPoint) -> f32 {
    match axis {
        ScrollbarAxis::Horizontal => point.x,
        ScrollbarAxis::Vertical => point.y,
    }
}

#[cfg(test)]
mod tests {
    use super::{
        scrollbar_drag_offset_with_grab, scrollbar_geometry_for_node, scrollbar_hit_test,
        scrollbar_point_for_node, ScrollbarAxis, ScrollbarHitKind,
    };
    use crate::env::ScrollStateMap;
    use fission_ir::{CompositeStyle, CoreIR, CoreNode, FlexDirection, LayoutOp, NodeId, Op};
    use fission_layout::{LayoutNodeGeometry, LayoutPoint, LayoutRect, LayoutSize, LayoutSnapshot};

    #[test]
    fn vertical_scrollbar_geometry_tracks_offset_inside_viewport() {
        let (ir, mut layout, scroll) = scroll_tree();
        layout.nodes.insert(
            scroll,
            LayoutNodeGeometry {
                rect: LayoutRect::new(10.0, 20.0, 100.0, 200.0),
                content_size: LayoutSize::new(100.0, 600.0),
            },
        );
        let mut scroll_map = ScrollStateMap::default();
        scroll_map.set_offset(scroll, 200.0);

        let geometry =
            scrollbar_geometry_for_node(&ir, &layout, &scroll_map, scroll).expect("scrollbar");

        assert_eq!(geometry.axis, ScrollbarAxis::Vertical);
        assert_eq!(geometry.rail_rect.origin.x, 102.0);
        assert_eq!(geometry.rail_rect.origin.y, 22.0);
        assert!(geometry.thumb_rect.origin.y > geometry.rail_rect.origin.y);
        assert!(geometry.thumb_rect.bottom() <= geometry.rail_rect.bottom());
    }

    #[test]
    fn scrollbar_hit_test_prioritizes_thumb_chrome() {
        let (ir, mut layout, scroll) = scroll_tree();
        layout.nodes.insert(
            scroll,
            LayoutNodeGeometry {
                rect: LayoutRect::new(0.0, 0.0, 100.0, 200.0),
                content_size: LayoutSize::new(100.0, 600.0),
            },
        );

        let hit = scrollbar_hit_test(
            &ir,
            &layout,
            &ScrollStateMap::default(),
            LayoutPoint::new(97.0, 8.0),
        )
        .expect("scrollbar hit");

        assert_eq!(hit.kind, ScrollbarHitKind::Thumb);
        assert_eq!(hit.geometry.node_id, scroll);
    }

    #[test]
    fn scrollbar_drag_maps_thumb_position_to_offset() {
        let (ir, mut layout, scroll) = scroll_tree();
        layout.nodes.insert(
            scroll,
            LayoutNodeGeometry {
                rect: LayoutRect::new(0.0, 0.0, 100.0, 200.0),
                content_size: LayoutSize::new(100.0, 600.0),
            },
        );
        let geometry =
            scrollbar_geometry_for_node(&ir, &layout, &ScrollStateMap::default(), scroll).unwrap();

        let offset = scrollbar_drag_offset_with_grab(geometry, LayoutPoint::new(97.0, 198.0), 0.0);

        assert!((offset - geometry.max_offset).abs() <= 0.01);
    }

    #[test]
    fn nested_scrollbar_hit_uses_target_layout_coordinates() {
        let parent = NodeId::derived(71, &[0]);
        let child = NodeId::derived(71, &[1]);
        let mut ir = CoreIR::new();
        ir.add_node(
            child,
            Op::Layout(LayoutOp::Scroll {
                direction: FlexDirection::Row,
                show_scrollbar: true,
                width: Some(100.0),
                height: Some(50.0),
                min_width: None,
                max_width: None,
                min_height: None,
                max_height: None,
                padding: [0.0; 4],
                flex_grow: 0.0,
                flex_shrink: 0.0,
            }),
            vec![],
        );
        ir.add_node(
            parent,
            Op::Layout(LayoutOp::Scroll {
                direction: FlexDirection::Column,
                show_scrollbar: true,
                width: Some(120.0),
                height: Some(120.0),
                min_width: None,
                max_width: None,
                min_height: None,
                max_height: None,
                padding: [0.0; 4],
                flex_grow: 0.0,
                flex_shrink: 0.0,
            }),
            vec![child],
        );
        ir.set_root(parent);

        let mut layout = LayoutSnapshot::new(LayoutSize::new(120.0, 120.0));
        layout.nodes.insert(
            parent,
            LayoutNodeGeometry {
                rect: LayoutRect::new(0.0, 0.0, 120.0, 120.0),
                content_size: LayoutSize::new(120.0, 320.0),
            },
        );
        layout.nodes.insert(
            child,
            LayoutNodeGeometry {
                rect: LayoutRect::new(0.0, 160.0, 100.0, 50.0),
                content_size: LayoutSize::new(300.0, 50.0),
            },
        );
        let mut scroll_map = ScrollStateMap::default();
        scroll_map.set_offset(parent, 100.0);

        let visual_rail_point = LayoutPoint::new(50.0, 104.0);
        let hit =
            scrollbar_hit_test(&ir, &layout, &scroll_map, visual_rail_point).expect("child rail");

        assert_eq!(hit.geometry.node_id, child);
        assert_eq!(hit.kind, ScrollbarHitKind::Rail);
        assert_eq!(
            hit.layout_point,
            scrollbar_point_for_node(&ir, &scroll_map, child, visual_rail_point)
        );
        assert!(
            hit.geometry.rail_rect.contains(hit.layout_point),
            "hit point must be in the target scrollbar's layout coordinate space"
        );
    }

    fn scroll_tree() -> (CoreIR, LayoutSnapshot, NodeId) {
        let scroll = NodeId::derived(70, &[1]);
        let mut ir = CoreIR::default();
        ir.nodes.insert(
            scroll,
            CoreNode {
                id: scroll,
                parent: None,
                children: Vec::new(),
                op: Op::Layout(LayoutOp::Scroll {
                    direction: FlexDirection::Column,
                    show_scrollbar: true,
                    width: Some(100.0),
                    height: Some(200.0),
                    min_width: None,
                    max_width: None,
                    min_height: None,
                    max_height: None,
                    padding: [0.0; 4],
                    flex_grow: 0.0,
                    flex_shrink: 0.0,
                }),
                composite: CompositeStyle::default(),
                hash: 0,
            },
        );
        ir.set_root(scroll);
        (
            ir,
            LayoutSnapshot::new(LayoutSize::new(100.0, 200.0)),
            scroll,
        )
    }
}