burin 0.1.2

A Rust GUI framework built on the auralis reactive signal kernel
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
//! StickyHeader — stackable sticky headers driven by change-guarded frame
//! processing.
//!
//! Features:
//! - Single stick-to-top / stick-to-bottom (`StickyDirection`)
//! - Multi-stack (section headers accumulate below each other by REAL height)
//! - Push-up mode (`StickyMode::PushUp` / `.push_up()`): iOS-style — the
//!   next section header pushes the previous one up and out
//! - Teardown-safe: entries are reclaimed via the core teardown-hook protocol
//! - Change-guarded: when neither the parent scroll offset nor the layout
//!   changed, `process_all` performs zero writes and registers zero dirty —
//!   an idle app with sticky headers stays idle (one f32 compare per entry
//!   per driven frame; no frame is ever driven *by* this module).
//!
//! Coordinate model: `original_y`/`height` are captured from the element's
//! post-layout `screen_bounds` (layout space — paint subtracts the scroll
//! offset). `position_offset` is a paint-time translation, so it never feeds
//! back into the captured bounds. Captures refresh when the element's
//! `layout_generation` changes (one-frame lag after a relayout, same model as
//! portal anchors).

use std::cell::{Cell, RefCell};

use crate::core::context::MountContext;
use crate::core::dirty_registry;
use crate::core::element::{DirtyFlags, ElementArena, ElementId};
use crate::core::widget::Widget;
use crate::ecs::components;
use crate::style::styled::{StyleRefinement, Styled};
use crate::style::Vec2;

// ── Direction ──

/// Which edge the header sticks to during scroll.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum StickyDirection {
    Top,
    Bottom,
}

/// How consecutive stuck headers interact (Top direction only).
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum StickyMode {
    /// Headers accumulate below each other by their real height.
    #[default]
    Stack,
    /// iOS-style: only one header is pinned; the next approaching header
    /// pushes the previous one up and out of the viewport.
    PushUp,
}

// ── Registry (thread_local) ──

struct StickyEntry {
    eid: ElementId,
    top_offset: f32,
    direction: StickyDirection,
    mode: StickyMode,
    /// Resolved nearest scrollable ancestor (lazy; re-resolved if it dies).
    scroll_parent: Cell<Option<ElementId>>,
    /// Layout-space y captured post-layout. None until first valid capture.
    original_y: Cell<Option<f32>>,
    /// Real element height from screen_bounds (stacking accumulator input).
    height: Cell<f32>,
    /// Element layout_generation at capture time — recapture on change.
    layout_gen_seen: Cell<u64>,
    /// Scroll offset at the last processed frame (change guard).
    last_scroll_y: Cell<f32>,
    /// Last position_offset.y written (write/dirty suppression).
    last_applied_y: Cell<f32>,
}

/// Per-window sticky-header registry (audit 2026-07-18 multi-window pass):
/// window A's frame previously scanned (and could mis-resolve) window B's
/// entries. The `AppContext::extension` anymap keeps the widget-layer
/// `StickyEntry` type out of `core` — no reverse dependency. Lifecycle is
/// handled by the teardown hook below (audit 2026-07-17 round 3, A+B).
#[derive(Default)]
struct StickyDomain {
    entries: RefCell<Vec<StickyEntry>>,
}

fn sticky_domain() -> std::rc::Rc<StickyDomain> {
    crate::core::app_context::current_app().extension::<StickyDomain>()
}

pub fn register(eid: ElementId, top_offset: f32, direction: StickyDirection) {
    register_with_mode(eid, top_offset, direction, StickyMode::default());
}

pub fn register_with_mode(
    eid: ElementId,
    top_offset: f32,
    direction: StickyDirection,
    mode: StickyMode,
) {
    dirty_registry::register_teardown_hook(teardown_cleanup);
    dirty_registry::spatial_register_position_offset(eid);
    sticky_domain().entries.borrow_mut().push(StickyEntry {
        eid,
        top_offset,
        direction,
        mode,
        scroll_parent: Cell::new(None),
        original_y: Cell::new(None),
        height: Cell::new(0.0),
        layout_gen_seen: Cell::new(0),
        last_scroll_y: Cell::new(f32::NAN),
        last_applied_y: Cell::new(0.0),
    });
}

pub fn unregister(eid: ElementId) {
    sticky_domain()
        .entries
        .borrow_mut()
        .retain(|e| e.eid != eid);
}

fn teardown_cleanup(id: ElementId) {
    unregister(id);
}

/// Test-only introspection: registered sticky entry count.
#[doc(hidden)]
pub fn debug_entry_len() -> usize {
    sticky_domain().entries.borrow().len()
}

/// Test-only introspection: (eid, applied position_offset.y) per entry, in
/// registration order.
#[doc(hidden)]
pub fn debug_applied_offsets() -> Vec<(ElementId, f32)> {
    sticky_domain()
        .entries
        .borrow()
        .iter()
        .map(|e| (e.eid, e.last_applied_y.get()))
        .collect()
}

/// Test-only introspection: (eid, captured original_y, height) per entry.
#[doc(hidden)]
pub fn debug_captures() -> Vec<(ElementId, Option<f32>, f32)> {
    sticky_domain()
        .entries
        .borrow()
        .iter()
        .map(|e| (e.eid, e.original_y.get(), e.height.get()))
        .collect()
}

// ── Frame processing ──

/// Per-frame sticky pass (called from `run_pre_passes`). Change-guarded:
/// entries whose parent scroll offset and layout are unchanged are skipped
/// with a single f32/u64 compare — no writes, no dirty, no allocation.
pub fn process_all(arena: &ElementArena) {
    let dom = sticky_domain();
    let entries = dom.entries.borrow();
    if entries.is_empty() {
        return;
    }

    // Pass 1 — refresh captures + detect change. Indices of entries that
    // need a position recompute this frame.
    let mut active = false;
    for e in entries.iter() {
        let Some(el) = arena.get(e.eid) else { continue };

        // (Re)capture layout data when layout moved under us.
        let lgen = el.layout_generation.get();
        if e.original_y.get().is_none() || e.layout_gen_seen.get() != lgen {
            let b = el.screen_bounds;
            if b.height > 0.0 {
                e.original_y.set(Some(b.y));
                e.height.set(b.height);
                e.layout_gen_seen.set(lgen);
                // Force recompute after recapture.
                e.last_scroll_y.set(f32::NAN);
            }
        }

        // Resolve (and cache) the nearest scrollable ancestor.
        let parent = match e.scroll_parent.get() {
            Some(p) if arena.get(p).is_some() => Some(p),
            _ => {
                let p = find_scroll_parent(e.eid);
                e.scroll_parent.set(p);
                p
            }
        };
        let scroll_y = parent
            .and_then(|p| {
                crate::core::element::with_ct(|ct| {
                    ct.scroll.get(&p).map(|sc| sc.scroll_offset.get().y)
                })
            })
            .unwrap_or(0.0);

        let last = e.last_scroll_y.get();
        if e.original_y.get().is_some() && (last.is_nan() || (scroll_y - last).abs() > 0.01) {
            active = true;
        }
    }
    if !active {
        return;
    }

    // Pass 2 — group by scroll parent, sort by original_y, stack by real
    // height, and write only changed offsets. Entry counts are tiny
    // (headers per scroll area), so the sort is O(k log k) on scroll
    // frames only.
    let mut order: Vec<usize> = (0..entries.len())
        .filter(|&i| entries[i].original_y.get().is_some())
        .collect();
    order.sort_by(|&a, &b| {
        let ea = &entries[a];
        let eb = &entries[b];
        ea.scroll_parent.get().cmp(&eb.scroll_parent.get()).then(
            ea.original_y
                .get()
                .unwrap_or(0.0)
                .partial_cmp(&eb.original_y.get().unwrap_or(0.0))
                .unwrap_or(std::cmp::Ordering::Equal),
        )
    });

    let mut group_parent: Option<ElementId> = None;
    let mut accumulated = 0.0f32;
    for (k, &i) in order.iter().enumerate() {
        let e = &entries[i];
        if arena.get(e.eid).is_none() {
            continue;
        }
        let parent = e.scroll_parent.get();
        if parent != group_parent {
            group_parent = parent;
            accumulated = 0.0;
        }
        let (scroll_y, container_y, viewport_h) = match parent {
            Some(p) => {
                let sy = crate::core::element::with_ct(|ct| {
                    ct.scroll.get(&p).map(|sc| sc.scroll_offset.get().y)
                })
                .unwrap_or(0.0);
                let (cy, vh) = arena
                    .get(p)
                    .map(|pe| (pe.screen_bounds.y, pe.screen_bounds.height))
                    .unwrap_or((0.0, 0.0));
                (sy, cy, vh)
            }
            None => (0.0, 0.0, 0.0),
        };
        e.last_scroll_y.set(scroll_y);

        let original_y = e.original_y.get().unwrap_or(0.0);
        let height = e.height.get();
        // Content-space y relative to the scroll container's top.
        let content_y = original_y - container_y;
        // Where the element would render without our offset.
        let viewport_y = content_y - scroll_y;

        let new_y = match e.direction {
            StickyDirection::Top => match e.mode {
                StickyMode::Stack => {
                    let target = e.top_offset + accumulated;
                    if viewport_y < target {
                        accumulated += height;
                        target - viewport_y
                    } else {
                        0.0
                    }
                }
                StickyMode::PushUp => {
                    // iOS-style: pin at top_offset, but let the NEXT
                    // header of the same group push this one up as its
                    // natural position approaches the pinned bottom.
                    if viewport_y < e.top_offset {
                        let next_vy = order.get(k + 1).and_then(|&j| {
                            let n = &entries[j];
                            (n.scroll_parent.get() == parent).then(|| {
                                let n_cy = n.original_y.get().unwrap_or(0.0) - container_y;
                                n_cy - scroll_y
                            })
                        });
                        let pinned = match next_vy {
                            Some(nvy) => e.top_offset.min(nvy - height),
                            None => e.top_offset,
                        };
                        pinned - viewport_y
                    } else {
                        0.0
                    }
                }
            },
            StickyDirection::Bottom => {
                // Stick to the viewport bottom edge (offset from bottom).
                let target = viewport_h - e.top_offset - height;
                if viewport_y > target && viewport_h > 0.0 {
                    target - viewport_y
                } else {
                    0.0
                }
            }
        };

        if (new_y - e.last_applied_y.get()).abs() > 0.01 {
            e.last_applied_y.set(new_y);
            crate::core::element::with_ct_mut(|ct| {
                ct.xform
                    .entry(e.eid)
                    .or_default()
                    .position_offset
                    .set(Vec2::new(0.0, new_y));
            });
            dirty_registry::mark_dirty(e.eid, DirtyFlags::REPAINT);
            dirty_registry::register_dirty(e.eid, DirtyFlags::REPAINT);
        }
    }
}

fn find_scroll_parent(eid: ElementId) -> Option<ElementId> {
    let mut cur = eid;
    loop {
        {
            let pid = dirty_registry::parent_of(cur)?;
            let is_scroll = crate::core::element::with_ct(|ct| {
                ct.layout.get(&pid).map(|l| l.overflow)
                    == Some(crate::core::config::Overflow::Scroll)
            });
            if is_scroll {
                return Some(pid);
            }
            cur = pid;
        }
    }
}

// ── Widget ──

/// A header that pins to the top or bottom of its scroll container.
///
/// Use `.mode()` to pick between stacking (headers accumulate) and
/// push-up (iOS-style, only one visible at a time).
pub struct StickyHeader {
    child: Option<Box<dyn Widget>>,
    top_offset: f32,
    direction: StickyDirection,
    mode: StickyMode,
    style: StyleRefinement,
}

impl StickyHeader {
    pub fn new(child: impl Widget + 'static) -> Self {
        Self {
            child: Some(Box::new(child)),
            top_offset: 0.0,
            direction: StickyDirection::Top,
            mode: StickyMode::default(),
            style: StyleRefinement::default(),
        }
    }
    pub fn top(mut self, px: f32) -> Self {
        self.top_offset = px;
        self
    }
    pub fn bottom(mut self, px: f32) -> Self {
        self.top_offset = px;
        self.direction = StickyDirection::Bottom;
        self
    }
    /// iOS-style section headers: the next header pushes the previous one
    /// up and out instead of stacking below it (Top direction only).
    pub fn push_up(mut self) -> Self {
        self.mode = StickyMode::PushUp;
        self
    }
}

impl Styled for StickyHeader {
    fn style_refinement(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Widget for StickyHeader {
    fn component_mask(&self) -> u64 {
        components::STYLE | components::LAYOUT
    }

    fn mount_box(self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
        let id = ctx.arena.allocate();
        ctx.preallocate(id, self.component_mask());

        // Mount child — the wrapper stays in normal flex flow (component
        // mask excludes TRANSFORM) so taffy writes real screen_bounds.y
        // for the capture pass. Position_offset is wired manually below.
        if let Some(child) = self.child {
            let mut child_ctx = ctx.child_with_events(id);
            let child_id = Box::new(child).mount_box(&mut child_ctx);
            ctx.arena.add_child(id, child_id);
        }

        // Create the position_offset cell (normally done by preallocate
        // via the TRANSFORM component mask, which we omit intentionally —
        // with TRANSFORM the element is treated as a leaf by taffy, which
        // triggers absolute positioning (leaf_taffy_style z_index gate)
        // and pulls it out of flex flow, breaking screen_bounds.y capture).
        let pos_cell = std::rc::Rc::new(std::cell::Cell::new(Vec2::ZERO));
        crate::core::element::with_ct_mut(|ct| {
            ct.xform.entry(id).or_default().position_offset = pos_cell;
        });
        // Elevate the paint-floor so the stuck header renders above rows
        // that were allocated after it (same mechanism as drag elevation).
        // We do NOT touch z_index — taffy's leaf_taffy_style uses z_index > 0
        // as an absolute-positioning gate; z_index_floor is paint-only.
        if let Some(el) = ctx.arena.get_mut(id) {
            el.z_index_floor = Some(1);
        }
        register_with_mode(id, self.top_offset, self.direction, self.mode);

        id
    }
}