abstracttui 0.6.0

A reactive, compositor-grade terminal UI engine: fine-grained signals, layered rendering with damage tracking, images (kitty/iTerm2/sixel/mosaic), software-rasterized 3D (GLB), themes and animation.
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
//! The flexbox-subset solver: pure geometry, integer cells, deterministic.
//!
//! Contract: `solve(tree, root, container)` assigns every node an absolute
//! `Rect`. Re-solving any subtree whose box did not change produces
//! identical output (purity is what makes incremental re-solve and damage
//! diffing trustworthy). When grow fills a container, children tile it
//! EXACTLY — rounding is distributed by largest remainder, never dropped.

use crate::base::{Point, Rect, Size};

use super::flex_math::{distribute, resolve_main_sizes, FlexItem};
use super::style::{Align, Dimension, Direction, Justify, Position};
use super::tree::{LayoutId, LayoutTree};

/// Solve the whole tree: `root` fills `container` (the viewport or the
/// parent-assigned box), children flow inside it.
pub fn solve(tree: &mut LayoutTree, root: LayoutId, container: Rect) {
    if !tree.is_alive(root) {
        return;
    }
    tree.assign_rect(root, container);
    resolve_subtree(tree, root);
}

/// Re-solve the children of `id` given its CURRENT rect. This is the
/// incremental entry point: a `Dyn` swap that cannot change its own box
/// only needs its subtree re-solved.
pub fn resolve_subtree(tree: &mut LayoutTree, id: LayoutId) {
    // Iterative: UI nesting depth must not be bounded by the native stack.
    let mut work = vec![id];
    while let Some(current) = work.pop() {
        layout_children_of(tree, current);
        let children: Vec<LayoutId> = tree.children(current).to_vec();
        work.extend(children);
    }
}

/// Resolve a `Dimension` against a parent extent.
pub(super) fn resolve_dim(dim: Dimension, parent_extent: i32) -> Option<i32> {
    match dim {
        Dimension::Auto => None,
        Dimension::Cells(c) => Some(c.max(0)),
        Dimension::Percent(p) => Some(((parent_extent as f32) * p.clamp(0.0, 1.0)).round() as i32),
    }
}

pub(super) fn clamp_axis(v: i32, min: Option<i32>, max: Option<i32>) -> i32 {
    let lo = min.unwrap_or(0).max(0);
    let hi = max.unwrap_or(i32::MAX).max(lo);
    v.clamp(lo, hi)
}

/// THE size query (backlog 0130): measure a subtree's intrinsic size
/// within `avail` without assigning rects — what a scroll container
/// asks to size its content wrapper when no `content_size` hint is
/// given. Explicit dimensions win; `Auto` asks the measure callback
/// (leaves) or aggregates children. Pure: no tree mutation.
///
/// Unconstrained axes pass a large `avail` extent (the solver's
/// absolute-placement path does exactly this through the same fold).
pub fn measure(tree: &LayoutTree, id: LayoutId, avail: Size) -> Size {
    intrinsic_size(tree, id, avail)
}

/// Content-driven size of a node within `avail`. Explicit dimensions win;
/// `Auto` asks the measure callback (leaves) or aggregates children
/// (containers): sum along the node's own main axis, max across.
///
/// Recursive over tree depth; fine for realistic UI nesting. Percent
/// resolves against `avail` (the parent content estimate) — good enough
/// for intrinsic purposes and documented as an approximation.
///
/// A child is measured inside the box it will actually be SOLVED to:
/// its own margins come off `avail` first, matching what the placement
/// pass does with `cross_avail`. Without that a wrapping leaf answers
/// the row count for a line length it is never drawn at. What this
/// CANNOT do is anticipate a flex share: a child sitting in a row
/// beside a fixed sibling is measured at the full content width,
/// because its share is not known until the basis it is being asked
/// for has been distributed.
///
/// That last limitation does NOT reach the solved rects, and the reason
/// is worth stating because it is easy to remove by accident: placement
/// re-measures the child's CROSS axis at the main size it was actually
/// given (see `layout_children_of`), so a row child's height is
/// recomputed at its distributed width. The stale estimate is therefore
/// visible through this query and through anything sizing itself from
/// it, but not in what gets drawn. Measured over a random-tree
/// population in `adv_layout`, which is the ONLY test in the crate that
/// goes red when that re-measure is weakened — verified by weakening it
/// and running everything. Simplifying the cross-axis re-measure away
/// will look free from every other suite.
pub(super) fn intrinsic_size(tree: &LayoutTree, id: LayoutId, avail: Size) -> Size {
    intrinsic(tree, id, avail, Want::Both)
}

/// ONE axis of the intrinsic size — for the call sites that read one
/// and discard the other.
///
/// WHY THIS EXISTS AND IT IS NOT A MICRO-OPTIMISATION. A measure
/// callback is not a pure arithmetic function in this engine: a text
/// leaf wraps, and a `Feed` typesets and CACHES at the width it was
/// asked about. So an intrinsic query whose answer is thrown away is
/// not free — it is a full wrap of the subtree, and if the caller asks
/// at a width the node will never be solved to, it also evicts the
/// cache the node had built at the right one.
///
/// That is not hypothetical. `commons#482`/`#490`: a `Feed` in a flex
/// child beside a fixed sibling was typeset TWICE per scroll notch, at
/// two different widths, against a perfectly persistent cache — 80% of
/// the notch. The first of those was this exact waste: the row asked
/// its `Auto`-width child for a BASIS (main axis = width only), the
/// child aggregated its children's widths, and the recursion consulted
/// the feed's measure callback — whose own width is `Percent(1.0)` and
/// therefore already known without asking anything at all. The callback
/// ran only to produce a HEIGHT that `axes.main(est)` then dropped on
/// the floor.
///
/// The axis passes to children UNCHANGED whatever the direction: a
/// node's width is built only from its children's widths (summed along
/// a row, maxed across a column) and its height only from theirs. So
/// asking for one axis never needs the other, at any depth.
///
/// The unrequested axis of the answer is NOT COMPUTED, so this returns
/// the scalar rather than a `Size` with a plausible-looking lie in the
/// other field.
pub(super) fn intrinsic_extent(tree: &LayoutTree, id: LayoutId, avail: Size, want: Want) -> i32 {
    debug_assert!(want != Want::Both, "intrinsic_extent answers ONE axis");
    let s = intrinsic(tree, id, avail, want);
    match want {
        Want::H => s.h,
        _ => s.w,
    }
}

/// Which axis of an intrinsic answer the caller will actually READ.
#[derive(Copy, Clone, PartialEq, Eq)]
pub(super) enum Want {
    Both,
    W,
    H,
}

impl Want {
    fn wants_w(self) -> bool {
        self != Want::H
    }
    fn wants_h(self) -> bool {
        self != Want::W
    }
}

fn intrinsic(tree: &LayoutTree, id: LayoutId, avail: Size, want: Want) -> Size {
    let Some(node) = tree.nodes.get(id.0) else {
        return Size::ZERO;
    };
    let style = &node.style;
    let explicit_w = resolve_dim(style.width, avail.w);
    let explicit_h = resolve_dim(style.height, avail.h);
    // Content is needed only for an axis the caller will READ and the
    // style does not already fix. An explicit dimension wins below
    // either way, so an axis nobody asked for buys nothing.
    let need_content =
        (want.wants_w() && explicit_w.is_none()) || (want.wants_h() && explicit_h.is_none());
    let content = if !need_content {
        Size::ZERO
    } else if let Some(measure) = &node.measure {
        let inner = Size::new(
            (avail.w - style.padding.horizontal()).max(0),
            (avail.h - style.padding.vertical()).max(0),
        );
        let m = measure(inner);
        Size::new(
            m.w + style.padding.horizontal(),
            m.h + style.padding.vertical(),
        )
    } else {
        let inner_avail = Size::new(
            (avail.w - style.padding.horizontal()).max(0),
            (avail.h - style.padding.vertical()).max(0),
        );
        let mut main = 0i32;
        let mut cross = 0i32;
        let mut flow_children = 0;
        for &child in &node.children {
            let Some(cnode) = tree.nodes.get(child.0) else {
                continue;
            };
            if cnode.style.position == Position::Absolute {
                continue; // out of flow: contributes nothing to content size
            }
            // The child's OWN margins are not space it can use: the
            // placement pass subtracts them from its cross extent
            // (`cross_avail`), so measuring at the full inner width
            // hands a content-sized child a width it will never be
            // solved to — and a text leaf then answers the row count
            // for the wrong wrap.
            let child_avail = Size::new(
                (inner_avail.w - cnode.style.margin.horizontal()).max(0),
                (inner_avail.h - cnode.style.margin.vertical()).max(0),
            );
            let cs = intrinsic(tree, child, child_avail, want);
            let (cm, cc) = match style.direction {
                Direction::Row => (
                    cs.w + cnode.style.margin.horizontal(),
                    cs.h + cnode.style.margin.vertical(),
                ),
                Direction::Column => (
                    cs.h + cnode.style.margin.vertical(),
                    cs.w + cnode.style.margin.horizontal(),
                ),
            };
            main += cm;
            cross = cross.max(cc);
            flow_children += 1;
        }
        if flow_children > 1 {
            main += style.gap.max(0) * (flow_children - 1);
        }
        match style.direction {
            Direction::Row => Size::new(
                main + style.padding.horizontal(),
                cross + style.padding.vertical(),
            ),
            Direction::Column => Size::new(
                cross + style.padding.horizontal(),
                main + style.padding.vertical(),
            ),
        }
    };
    // An axis nobody asked for is returned as ZERO rather than as the
    // clamp of an uncomputed content extent: `intrinsic_extent` never
    // reads it, and a hard zero is a louder wrong answer than a
    // min-width-inflated plausible one if anyone ever does.
    Size::new(
        if want.wants_w() {
            clamp_axis(
                explicit_w.unwrap_or(content.w),
                style.min_width,
                style.max_width,
            )
        } else {
            0
        },
        if want.wants_h() {
            clamp_axis(
                explicit_h.unwrap_or(content.h),
                style.min_height,
                style.max_height,
            )
        } else {
            0
        },
    )
}

/// Axis view helpers: the solver is written once, axis-agnostic.
#[derive(Copy, Clone)]
struct Axes {
    dir: Direction,
}

impl Axes {
    fn main(self, s: Size) -> i32 {
        match self.dir {
            Direction::Row => s.w,
            Direction::Column => s.h,
        }
    }
    fn size(self, main: i32, cross: i32) -> Size {
        match self.dir {
            Direction::Row => Size::new(main, cross),
            Direction::Column => Size::new(cross, main),
        }
    }
    /// The `Want` naming this axis view's main / cross axis, for the
    /// single-axis intrinsic query.
    fn want_main(self) -> Want {
        match self.dir {
            Direction::Row => Want::W,
            Direction::Column => Want::H,
        }
    }
    fn want_cross(self) -> Want {
        match self.dir {
            Direction::Row => Want::H,
            Direction::Column => Want::W,
        }
    }
}

fn layout_children_of(tree: &mut LayoutTree, id: LayoutId) {
    let (rect, style, children) = {
        let Some(node) = tree.nodes.get(id.0) else {
            return;
        };
        (node.rect, node.style.clone(), node.children.clone())
    };
    if children.is_empty() {
        return;
    }
    let content = Rect::new(
        rect.x + style.padding.left,
        rect.y + style.padding.top,
        (rect.w - style.padding.horizontal()).max(0),
        (rect.h - style.padding.vertical()).max(0),
    );
    let axes = Axes {
        dir: style.direction,
    };
    let content_main = axes.main(content.size());
    let mut flow: Vec<LayoutId> = Vec::new();
    let mut absolute: Vec<LayoutId> = Vec::new();
    for child in children {
        let Some(cnode) = tree.nodes.get(child.0) else {
            continue;
        };
        if cnode.style.position == Position::Absolute {
            absolute.push(child);
        } else {
            flow.push(child);
        }
    }

    // Non-flex algorithms take over the FLOW children; absolute
    // placement below is shared by every display mode.
    if let super::style::Display::Grid { .. } = &style.display {
        super::grid::layout_grid(tree, content, &style, &flow);
        place_absolute(tree, &absolute, content);
        return;
    }
    if style.wrap {
        super::wrap::layout_wrapped(tree, content, &style, &flow);
        place_absolute(tree, &absolute, content);
        return;
    }

    // ---- main axis ----------------------------------------------------
    let gap = style.gap.max(0);
    let gaps_total = if flow.len() > 1 {
        gap * (flow.len() as i32 - 1)
    } else {
        0
    };
    let mut items: Vec<FlexItem> = Vec::with_capacity(flow.len());
    let mut margins_main: Vec<(i32, i32)> = Vec::with_capacity(flow.len());
    // Zero-collapse watch (0240 follow-up #3): per child, the DECLARED
    // fixed main-axis extent (explicit `Cells` basis, else explicit
    // `Cells` size; 0 = not watched). An author-declared min — even
    // min 0, the documented opt-out — unwatches; percent/intrinsic
    // sizing is legitimately parent-relative and never watched.
    let mut declared_fixed: Vec<i32> = Vec::with_capacity(flow.len());
    for &child in &flow {
        let cstyle = tree
            .nodes
            .get(child.0)
            .expect("flow child alive")
            .style
            .clone();
        let (m_lead, m_trail, min, max, main_dim) = match style.direction {
            Direction::Row => (
                cstyle.margin.left,
                cstyle.margin.right,
                cstyle.min_width,
                cstyle.max_width,
                cstyle.width,
            ),
            Direction::Column => (
                cstyle.margin.top,
                cstyle.margin.bottom,
                cstyle.min_height,
                cstyle.max_height,
                cstyle.height,
            ),
        };
        declared_fixed.push(match (min, cstyle.basis, main_dim) {
            (Some(_), _, _) => 0, // explicit min = the author decided
            // Mirror basis precedence: an explicit basis (even the
            // deliberate `Cells(0)` of scroll containers) overrides
            // the fixed size, so only a POSITIVE Cells basis watches.
            (None, Dimension::Cells(n), _) => n.max(0),
            (None, Dimension::Auto, Dimension::Cells(n)) => n.max(0),
            _ => 0,
        });
        // flex-basis > explicit main size > intrinsic content.
        let basis = resolve_dim(cstyle.basis, content_main)
            .or_else(|| resolve_dim(main_dim, content_main))
            .unwrap_or_else(|| {
                // Same rule as the aggregation above: a child is
                // measured inside the box it will actually be SOLVED
                // to, which is the content box less its own margins.
                let avail = Size::new(
                    (content.w - cstyle.margin.horizontal()).max(0),
                    (content.h - cstyle.margin.vertical()).max(0),
                );
                // MAIN AXIS ONLY. The cross answer was never read here
                // (`axes.main` drops it), and computing it costs every
                // measure callback in the subtree a full run — at a
                // width the child may not be solved to. See
                // `intrinsic_extent`.
                intrinsic_extent(tree, child, avail, axes.want_main())
            });
        items.push(FlexItem {
            basis: basis.max(0),
            min: min.unwrap_or(0).max(0),
            max: max.unwrap_or(i32::MAX),
            grow: cstyle.grow.max(0.0) as f64,
            shrink: cstyle.shrink.max(0.0) as f64,
        });
        margins_main.push((m_lead, m_trail));
    }
    let margins_total: i32 = margins_main.iter().map(|(a, b)| a + b).sum();
    let available_main = (content_main - gaps_total - margins_total).max(0);
    let sizes = resolve_main_sizes(&items, available_main);

    // Debug diagnostic (0240 follow-up #3): a watched fixed-size child
    // crushed to zero names itself once instead of silently vanishing
    // (const-folded away in release builds).
    if cfg!(debug_assertions) {
        let axis = match style.direction {
            Direction::Row => "width",
            Direction::Column => "height",
        };
        for (i, &child) in flow.iter().enumerate() {
            if declared_fixed[i] > 0 && sizes[i] == 0 {
                tree.note_zero_collapse(child, declared_fixed[i], axis, content, i);
            }
        }
        // 1330: the other end of the same class. Zero-collapse catches a
        // child crushed OUT of existence; this catches one solved PAST
        // the parent's box, whose surplus rows a later sibling then
        // overpaints. Columns only, non-clipping parents only, and only
        // when the surplus is attributable to a child that could not
        // give — see `note_main_overflow`.
        let wanted: i32 = sizes.iter().sum();
        if matches!(style.direction, Direction::Column)
            && !style.clips_children()
            && wanted > available_main
        {
            if let Some(i) = (0..flow.len())
                .find(|&i| sizes[i] > 0 && (items[i].shrink <= 0.0 || sizes[i] <= items[i].min))
            {
                tree.note_main_overflow(content, wanted, available_main, i);
            }
        }
    }

    // Leftover space feeds `justify` (only exists when nothing grew).
    let used: i32 = sizes.iter().sum();
    let leftover = (available_main - used).max(0);
    let (lead_offset, between_extra) = match style.justify {
        Justify::Start => (0, vec![0; flow.len().saturating_sub(1)]),
        Justify::Center => (leftover / 2, vec![0; flow.len().saturating_sub(1)]),
        Justify::End => (leftover, vec![0; flow.len().saturating_sub(1)]),
        Justify::SpaceBetween => {
            let slots = flow.len().saturating_sub(1);
            if slots == 0 {
                // Single child: SpaceBetween degenerates to Start (CSS).
                (0, Vec::new())
            } else {
                // Integer leftover split across the inter-child gaps,
                // largest-remainder so the row still tiles exactly.
                (0, distribute(leftover, &vec![1.0; slots]))
            }
        }
    };

    // ---- place children ------------------------------------------------
    let content_cross_extent = match style.direction {
        Direction::Row => content.h,
        Direction::Column => content.w,
    };
    let mut cursor = match style.direction {
        Direction::Row => content.x,
        Direction::Column => content.y,
    } + lead_offset;
    for (i, &child) in flow.iter().enumerate() {
        let cstyle = tree
            .nodes
            .get(child.0)
            .expect("flow child alive")
            .style
            .clone();
        let main_size = sizes[i];
        let (m_lead, _m_trail) = margins_main[i];

        // Cross axis: explicit > stretch > intrinsic; then clamp + align.
        let (cross_dim, cross_min, cross_max, m_cross_lead, m_cross_total) = match style.direction {
            Direction::Row => (
                cstyle.height,
                cstyle.min_height,
                cstyle.max_height,
                cstyle.margin.top,
                cstyle.margin.vertical(),
            ),
            Direction::Column => (
                cstyle.width,
                cstyle.min_width,
                cstyle.max_width,
                cstyle.margin.left,
                cstyle.margin.horizontal(),
            ),
        };
        let cross_avail = (content_cross_extent - m_cross_total).max(0);
        let align = cstyle.align_self.unwrap_or(style.align_items);
        let cross_size = match resolve_dim(cross_dim, content_cross_extent) {
            Some(c) => c,
            None => match align {
                Align::Stretch => cross_avail,
                // CROSS AXIS ONLY — the main size is already decided
                // (`main_size`), so the main half of this answer was
                // computed and discarded.
                _ => intrinsic_extent(
                    tree,
                    child,
                    axes.size(main_size, cross_avail),
                    axes.want_cross(),
                ),
            },
        };
        let cross_size = clamp_axis(cross_size, cross_min, cross_max).min(cross_avail.max(0));
        let cross_offset = match align {
            Align::Start | Align::Stretch => 0,
            Align::Center => (cross_avail - cross_size) / 2,
            Align::End => cross_avail - cross_size,
        };

        cursor += m_lead;
        let (x, y, w, h) = match style.direction {
            Direction::Row => (
                cursor,
                content.y + m_cross_lead + cross_offset,
                main_size,
                cross_size,
            ),
            Direction::Column => (
                content.x + m_cross_lead + cross_offset,
                cursor,
                cross_size,
                main_size,
            ),
        };
        tree.assign_rect(child, Rect::new(x, y, w.max(0), h.max(0)));
        cursor += main_size + margins_main[i].1;
        if i + 1 < flow.len() {
            cursor += gap + between_extra.get(i).copied().unwrap_or(0);
        }
    }

    // ---- absolute children ----------------------------------------------
    place_absolute(tree, &absolute, content);
}

/// Out-of-flow placement against the parent's content box — shared by
/// flex, wrap and grid containers.
pub(super) fn place_absolute(tree: &mut LayoutTree, absolute: &[LayoutId], content: Rect) {
    for &child in absolute {
        let cstyle = tree
            .nodes
            .get(child.0)
            .expect("absolute child alive")
            .style
            .clone();
        let inset = cstyle.inset;
        let est = intrinsic_size(tree, child, content.size());
        // Horizontal: both insets + Auto width => width derived from insets.
        let w = match resolve_dim(cstyle.width, content.w) {
            Some(w) => w,
            None => match (inset.left, inset.right) {
                (Some(l), Some(r)) => (content.w - l - r).max(0),
                _ => est.w,
            },
        };
        let h = match resolve_dim(cstyle.height, content.h) {
            Some(h) => h,
            None => match (inset.top, inset.bottom) {
                (Some(t), Some(b)) => (content.h - t - b).max(0),
                _ => est.h,
            },
        };
        let w = clamp_axis(w, cstyle.min_width, cstyle.max_width);
        let h = clamp_axis(h, cstyle.min_height, cstyle.max_height);
        let x = match (inset.left, inset.right) {
            (Some(l), _) => content.x + l,
            (None, Some(r)) => content.right() - r - w,
            (None, None) => content.x,
        };
        let y = match (inset.top, inset.bottom) {
            (Some(t), _) => content.y + t,
            (None, Some(b)) => content.bottom() - b - h,
            (None, None) => content.y,
        };
        tree.assign_rect(child, Rect::new(x, y, w.max(0), h.max(0)));
    }
}

/// Convenience: place `p` relative to a solved rect. PRUNED from the
/// public surface (cycle-8 sweep: zero external consumers); widgets do
/// this arithmetic inline.
#[allow(dead_code)]
pub(crate) fn local_point(rect: Rect, p: Point) -> Point {
    Point::new(p.x - rect.x, p.y - rect.y)
}