Skip to main content

xrack_layout/
page_indicator.rs

1use crate::{Point, Rect, Size};
2use xrack_runtime::{
3    LayoutDirection, Node, Orientation, page_indicator_count, page_indicator_index,
4};
5
6const MAX_MARKERS: usize = 64;
7
8/// A logical page index and its marker in the current viewport.
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct PageMarker {
11    pub index: usize,
12    pub center: Point,
13    pub diameter: f32,
14    pub selected: bool,
15}
16
17/// A bounded window of markers. Painting and pointer selection use the same slots.
18pub struct PageIndicatorGeometry {
19    track: Rect,
20    orientation: Orientation,
21    mirrored: bool,
22    first: usize,
23    len: usize,
24    count: usize,
25    selected: Option<usize>,
26    step: f32,
27    diameter: f32,
28}
29
30impl PageIndicatorGeometry {
31    pub fn new(node: &Node, bounds: Rect) -> Option<Self> {
32        Self::with_layout_direction(node, bounds, node.layout_direction().unwrap_or_default())
33    }
34
35    pub fn with_layout_direction(
36        node: &Node,
37        bounds: Rect,
38        direction: LayoutDirection,
39    ) -> Option<Self> {
40        let count = page_indicator_count(node);
41        if count == 0
42            || !bounds.width.is_finite()
43            || !bounds.height.is_finite()
44            || bounds.width <= 0.0
45            || bounds.height <= 0.0
46        {
47            return None;
48        }
49        let orientation = node.orientation();
50        let (main, cross) = match orientation {
51            Orientation::Horizontal => (bounds.width, bounds.height),
52            Orientation::Vertical => (bounds.height, bounds.width),
53        };
54        let (preferred_step, preferred_cross, dot) = sizing(node);
55        let capacity = (main / preferred_step).floor().max(1.0) as usize;
56        let len = count.min(capacity).min(MAX_MARKERS);
57        let step = preferred_step.min(main / len as f32);
58        let diameter = dot.min(step).min(cross);
59        let selected = page_indicator_index(node);
60        let first = selected
61            .unwrap_or(0)
62            .saturating_sub(len / 2)
63            .min(count - len);
64        let main = step * len as f32;
65        let cross = cross.min(preferred_cross);
66        let (width, height) = match orientation {
67            Orientation::Horizontal => (main, cross),
68            Orientation::Vertical => (cross, main),
69        };
70        let track = Rect::new(
71            bounds.x + (bounds.width - width) * 0.5,
72            bounds.y + (bounds.height - height) * 0.5,
73            width,
74            height,
75        );
76        Some(Self {
77            track,
78            orientation,
79            mirrored: orientation == Orientation::Horizontal
80                && direction == LayoutDirection::RightToLeft,
81            first,
82            len,
83            count,
84            selected,
85            step,
86            diameter,
87        })
88    }
89
90    pub fn marker_at(&self, point: Point) -> Option<usize> {
91        if !self.track.contains(point) {
92            return None;
93        }
94        let distance = match self.orientation {
95            Orientation::Horizontal => point.x - self.track.x,
96            Orientation::Vertical => point.y - self.track.y,
97        };
98        let physical_slot = ((distance / self.step) as usize).min(self.len - 1);
99        Some(self.first + self.project_slot(physical_slot))
100    }
101
102    fn project_slot(&self, slot: usize) -> usize {
103        if self.mirrored {
104            self.len - 1 - slot
105        } else {
106            slot
107        }
108    }
109
110    pub fn markers(&self) -> impl Iterator<Item = PageMarker> + '_ {
111        (0..self.len).map(|slot| {
112            let index = self.first + slot;
113            let distance = self.step * (self.project_slot(slot) as f32 + 0.5);
114            let center = match self.orientation {
115                Orientation::Horizontal => Point {
116                    x: self.track.x + distance,
117                    y: self.track.y + self.track.height * 0.5,
118                },
119                Orientation::Vertical => Point {
120                    x: self.track.x + self.track.width * 0.5,
121                    y: self.track.y + distance,
122                },
123            };
124            let selected = self.selected == Some(index);
125            let edge =
126                (slot == 0 && self.first > 0) || (slot + 1 == self.len && index + 1 < self.count);
127            let diameter = if edge && !selected && self.len > 1 {
128                self.diameter * 0.65
129            } else {
130                self.diameter
131            };
132            PageMarker {
133                index,
134                center,
135                diameter,
136                selected,
137            }
138        })
139    }
140
141    pub(crate) fn preferred_size(node: &Node) -> Size {
142        let (step, cross, _) = sizing(node);
143        let main = step * page_indicator_count(node).min(12) as f32;
144        match node.orientation() {
145            Orientation::Horizontal => Size::new(main, cross),
146            Orientation::Vertical => Size::new(cross, main),
147        }
148    }
149}
150
151fn sizing(node: &Node) -> (f32, f32, f32) {
152    let dot = node.length("dot-size").unwrap_or(8.0).clamp(2.0, 32.0);
153    let spacing = node.length("spacing").unwrap_or(8.0).clamp(0.0, 48.0);
154    let (min_step, min_cross) = if node.boolean("interactive", false) {
155        (18.0, 22.0)
156    } else {
157        (6.0, 16.0)
158    };
159    (
160        (dot + spacing).max(min_step),
161        (dot + 6.0).max(min_cross),
162        dot,
163    )
164}