1use crate::{Error, Rect};
2
3#[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}
37#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
39pub enum AnchorArrowPosition {
40 #[default]
42 Center,
43 Side,
45}
46
47#[derive(Clone, Copy, Debug, PartialEq)]
49pub struct AnchorArrow {
50 pub size: f64,
51 pub radius: f64,
52 pub offset: f64,
53 pub position: AnchorArrowPosition,
54}
55impl Default for AnchorArrow {
56 fn default() -> Self {
57 Self {
58 size: 8.0,
59 radius: 1.0,
60 offset: 10.0,
61 position: AnchorArrowPosition::Center,
62 }
63 }
64}
65impl AnchorArrow {
66 pub fn validate(self) -> Result<(), Error> {
67 if [self.size, self.radius, self.offset]
68 .iter()
69 .any(|value| !value.is_finite() || !(0.0..=4096.0).contains(value))
70 || self.size == 0.0
71 || self.radius > self.size / 2.0
72 {
73 return Err(Error::InvalidProps(
74 "anchor arrow size, radius and offset must be finite; size must be positive and radius no greater than half the size",
75 ));
76 }
77 Ok(())
78 }
79}
80impl AnchorAlign {
81 pub fn as_str(self) -> &'static str {
82 match self {
83 Self::Start => "start",
84 Self::Center => "center",
85 Self::End => "end",
86 }
87 }
88}
89#[derive(Clone, Copy, Debug, PartialEq)]
93pub struct AnchorPlacement {
94 pub side: AnchorSide,
95 pub align: AnchorAlign,
96 pub gap: f64,
97 pub viewport_padding: f64,
98 pub flip: bool,
99 pub shift: bool,
100 pub match_width: bool,
102 pub arrow: Option<AnchorArrow>,
104}
105impl Default for AnchorPlacement {
106 fn default() -> Self {
107 Self::new(AnchorSide::Bottom, AnchorAlign::Start)
108 }
109}
110impl AnchorPlacement {
111 pub const fn new(side: AnchorSide, align: AnchorAlign) -> Self {
112 Self {
113 side,
114 align,
115 gap: 6.0,
116 viewport_padding: 8.0,
117 flip: true,
118 shift: true,
119 match_width: false,
120 arrow: None,
121 }
122 }
123 pub const fn gap(mut self, value: f64) -> Self {
124 self.gap = value;
125 self
126 }
127 pub fn validate(self) -> Result<(), Error> {
128 if [self.gap, self.viewport_padding]
129 .iter()
130 .any(|v| !v.is_finite() || !(0.0..=4096.0).contains(v))
131 {
132 return Err(Error::InvalidProps(
133 "anchor gap and viewport padding must be finite and within 4096 CSS pixels",
134 ));
135 }
136 if let Some(arrow) = self.arrow {
137 arrow.validate()?;
138 }
139 Ok(())
140 }
141 pub fn arrow_cross(self, anchor: Rect, panel: Rect, resolved: AnchorPosition) -> Option<f64> {
143 let arrow = self.arrow?;
144 let (extent, centered) = match resolved.side {
145 AnchorSide::Top | AnchorSide::Bottom => {
146 (panel.width, anchor.x + anchor.width / 2.0 - resolved.x)
147 }
148 AnchorSide::Left | AnchorSide::Right => {
149 (panel.height, anchor.y + anchor.height / 2.0 - resolved.y)
150 }
151 };
152 let requested = match (arrow.position, self.align) {
153 (AnchorArrowPosition::Side, AnchorAlign::Start) => arrow.offset + arrow.size / 2.0,
154 (AnchorArrowPosition::Side, AnchorAlign::End) => {
155 extent - arrow.offset - arrow.size / 2.0
156 }
157 _ => centered,
158 };
159 Some(requested.clamp(arrow.size, (extent - arrow.size).max(arrow.size)))
160 }
161 pub fn resolve(
164 self,
165 anchor: Rect,
166 panel: Rect,
167 viewport: Rect,
168 ) -> Result<AnchorPosition, Error> {
169 self.validate()?;
170 for rect in [anchor, panel, viewport] {
171 if ![
172 rect.x,
173 rect.y,
174 rect.width,
175 rect.height,
176 rect.x + rect.width,
177 rect.y + rect.height,
178 ]
179 .iter()
180 .all(|v| v.is_finite())
181 || rect.width < 0.0
182 || rect.height < 0.0
183 {
184 return Err(Error::InvalidProps(
185 "anchor geometry must be finite with nonnegative dimensions",
186 ));
187 }
188 }
189 let candidate = |side| {
190 let vertical = matches!(side, AnchorSide::Top | AnchorSide::Bottom);
191 let (start, extent, size) = if vertical {
192 (anchor.x, anchor.width, panel.width)
193 } else {
194 (anchor.y, anchor.height, panel.height)
195 };
196 let cross = match self.align {
197 AnchorAlign::Start => start,
198 AnchorAlign::Center => start + (extent - size) / 2.0,
199 AnchorAlign::End => start + extent - size,
200 };
201 let (x, y) = match side {
202 AnchorSide::Top => (cross, anchor.y - panel.height - self.gap),
203 AnchorSide::Bottom => (cross, anchor.y + anchor.height + self.gap),
204 AnchorSide::Left => (anchor.x - panel.width - self.gap, cross),
205 AnchorSide::Right => (anchor.x + anchor.width + self.gap, cross),
206 };
207 AnchorPosition { x, y, side }
208 };
209 let min_x = viewport.x + self.viewport_padding;
210 let min_y = viewport.y + self.viewport_padding;
211 let max_x = viewport.x + viewport.width - self.viewport_padding;
212 let max_y = viewport.y + viewport.height - self.viewport_padding;
213 let overflow = |p: AnchorPosition| {
214 (min_x - p.x).max(0.0)
215 + (p.x + panel.width - max_x).max(0.0)
216 + (min_y - p.y).max(0.0)
217 + (p.y + panel.height - max_y).max(0.0)
218 };
219 let mut result = candidate(self.side);
220 let opposite = candidate(self.side.opposite());
221 if ![
222 result.x,
223 result.y,
224 opposite.x,
225 opposite.y,
226 min_x,
227 min_y,
228 max_x,
229 max_y,
230 overflow(result),
231 overflow(opposite),
232 ]
233 .iter()
234 .all(|v| v.is_finite())
235 {
236 return Err(Error::InvalidProps("anchor geometry arithmetic overflow"));
237 }
238 if self.flip && overflow(opposite) < overflow(result) {
239 result = opposite;
240 }
241 if self.shift {
242 result.x = result.x.clamp(min_x, (max_x - panel.width).max(min_x));
243 result.y = result.y.clamp(min_y, (max_y - panel.height).max(min_y));
244 }
245 Ok(result)
246 }
247}
248#[derive(Clone, Copy, Debug, PartialEq)]
249pub struct AnchorPosition {
250 pub x: f64,
251 pub y: f64,
252 pub side: AnchorSide,
253}