Skip to main content

glory_render/
anchor.rs

1use crate::{Error, Rect};
2
3/// Physical viewport side; independent of document writing direction.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum AnchorSide {
6    Top,
7    #[default]
8    Bottom,
9    Left,
10    Right,
11}
12impl AnchorSide {
13    pub fn as_str(self) -> &'static str {
14        match self {
15            Self::Top => "top",
16            Self::Bottom => "bottom",
17            Self::Left => "left",
18            Self::Right => "right",
19        }
20    }
21    fn opposite(self) -> Self {
22        match self {
23            Self::Top => Self::Bottom,
24            Self::Bottom => Self::Top,
25            Self::Left => Self::Right,
26            Self::Right => Self::Left,
27        }
28    }
29}
30#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
31pub enum AnchorAlign {
32    #[default]
33    Start,
34    Center,
35    End,
36}
37impl AnchorAlign {
38    pub fn as_str(self) -> &'static str {
39        match self {
40            Self::Start => "start",
41            Self::Center => "center",
42            Self::End => "end",
43        }
44    }
45}
46/// Finite anchored-layer policy, in viewport CSS pixels. Flip compares the
47/// requested and opposite side; shift clamps both axes to viewport padding.
48/// Neither policy changes the declared side or the logical anchor relation.
49#[derive(Clone, Copy, Debug, PartialEq)]
50pub struct AnchorPlacement {
51    pub side: AnchorSide,
52    pub align: AnchorAlign,
53    pub gap: f64,
54    pub viewport_padding: f64,
55    pub flip: bool,
56    pub shift: bool,
57}
58impl Default for AnchorPlacement {
59    fn default() -> Self {
60        Self::new(AnchorSide::Bottom, AnchorAlign::Start)
61    }
62}
63impl AnchorPlacement {
64    pub const fn new(side: AnchorSide, align: AnchorAlign) -> Self {
65        Self {
66            side,
67            align,
68            gap: 6.0,
69            viewport_padding: 8.0,
70            flip: true,
71            shift: true,
72        }
73    }
74    pub const fn gap(mut self, value: f64) -> Self {
75        self.gap = value;
76        self
77    }
78    pub fn validate(self) -> Result<(), Error> {
79        if [self.gap, self.viewport_padding]
80            .iter()
81            .any(|v| !v.is_finite() || !(0.0..=4096.0).contains(v))
82        {
83            return Err(Error::InvalidProps(
84                "anchor gap and viewport padding must be finite and within 4096 CSS pixels",
85            ));
86        }
87        Ok(())
88    }
89    /// Pure finite geometry. `panel` contributes dimensions only. The host
90    /// supplies the current anchor, including any pending ancestor-layer delta.
91    pub fn resolve(
92        self,
93        anchor: Rect,
94        panel: Rect,
95        viewport: Rect,
96    ) -> Result<AnchorPosition, Error> {
97        self.validate()?;
98        for rect in [anchor, panel, viewport] {
99            if ![
100                rect.x,
101                rect.y,
102                rect.width,
103                rect.height,
104                rect.x + rect.width,
105                rect.y + rect.height,
106            ]
107            .iter()
108            .all(|v| v.is_finite())
109                || rect.width < 0.0
110                || rect.height < 0.0
111            {
112                return Err(Error::InvalidProps(
113                    "anchor geometry must be finite with nonnegative dimensions",
114                ));
115            }
116        }
117        let candidate = |side| {
118            let vertical = matches!(side, AnchorSide::Top | AnchorSide::Bottom);
119            let (start, extent, size) = if vertical {
120                (anchor.x, anchor.width, panel.width)
121            } else {
122                (anchor.y, anchor.height, panel.height)
123            };
124            let cross = match self.align {
125                AnchorAlign::Start => start,
126                AnchorAlign::Center => start + (extent - size) / 2.0,
127                AnchorAlign::End => start + extent - size,
128            };
129            let (x, y) = match side {
130                AnchorSide::Top => (cross, anchor.y - panel.height - self.gap),
131                AnchorSide::Bottom => (cross, anchor.y + anchor.height + self.gap),
132                AnchorSide::Left => (anchor.x - panel.width - self.gap, cross),
133                AnchorSide::Right => (anchor.x + anchor.width + self.gap, cross),
134            };
135            AnchorPosition { x, y, side }
136        };
137        let min_x = viewport.x + self.viewport_padding;
138        let min_y = viewport.y + self.viewport_padding;
139        let max_x = viewport.x + viewport.width - self.viewport_padding;
140        let max_y = viewport.y + viewport.height - self.viewport_padding;
141        let overflow = |p: AnchorPosition| {
142            (min_x - p.x).max(0.0)
143                + (p.x + panel.width - max_x).max(0.0)
144                + (min_y - p.y).max(0.0)
145                + (p.y + panel.height - max_y).max(0.0)
146        };
147        let mut result = candidate(self.side);
148        let opposite = candidate(self.side.opposite());
149        if ![
150            result.x,
151            result.y,
152            opposite.x,
153            opposite.y,
154            min_x,
155            min_y,
156            max_x,
157            max_y,
158            overflow(result),
159            overflow(opposite),
160        ]
161        .iter()
162        .all(|v| v.is_finite())
163        {
164            return Err(Error::InvalidProps("anchor geometry arithmetic overflow"));
165        }
166        if self.flip && overflow(opposite) < overflow(result) {
167            result = opposite;
168        }
169        if self.shift {
170            result.x = result.x.clamp(min_x, (max_x - panel.width).max(min_x));
171            result.y = result.y.clamp(min_y, (max_y - panel.height).max(min_y));
172        }
173        Ok(result)
174    }
175}
176#[derive(Clone, Copy, Debug, PartialEq)]
177pub struct AnchorPosition {
178    pub x: f64,
179    pub y: f64,
180    pub side: AnchorSide,
181}