vizia_core/text/
scrolling.rs

1use crate::layout::BoundingBox;
2
3pub(crate) fn enforce_text_bounds(
4    text_bounds: &BoundingBox,
5    bounds: &BoundingBox,
6    transform: (f32, f32),
7) -> (f32, f32) {
8    let (mut tx, mut ty) = transform;
9    let text_box = BoundingBox {
10        x: text_bounds.x + tx,
11        y: text_bounds.y + ty,
12        w: text_bounds.w,
13        h: text_bounds.h,
14    };
15
16    if text_box.right() < bounds.right() {
17        tx += bounds.right() - text_box.right();
18    }
19    if text_box.left() > bounds.left() {
20        tx -= text_box.left() - bounds.left();
21    }
22    if text_box.width() < bounds.width() {
23        tx = 0.0;
24    }
25    if text_box.bottom() < bounds.bottom() {
26        ty += bounds.bottom() - text_box.bottom();
27    }
28    if text_box.top() > bounds.top() {
29        ty -= text_box.top() - bounds.top();
30    }
31    if text_box.height() < bounds.height() {
32        ty = 0.0;
33    }
34    (tx, ty)
35}
36
37pub(crate) fn ensure_visible(
38    text_bounds: &BoundingBox,
39    bounds: &BoundingBox,
40    transform: (f32, f32),
41) -> (f32, f32) {
42    let (mut tx, mut ty) = transform;
43    let caret_box = BoundingBox {
44        x: text_bounds.x + tx,
45        y: text_bounds.y + ty,
46        w: text_bounds.w,
47        h: text_bounds.h,
48    };
49    if caret_box.left() < bounds.left() {
50        tx += bounds.left() - caret_box.left();
51    }
52    if caret_box.right() > bounds.right() {
53        tx -= caret_box.right() - bounds.right();
54    }
55    if caret_box.top() < bounds.top() {
56        ty += bounds.top() - caret_box.top();
57    }
58    if caret_box.bottom() > bounds.bottom() {
59        ty -= caret_box.bottom() - bounds.bottom();
60    }
61    (tx, ty)
62}