layer_shika_domain/value_objects/
anchor_strategy.rs

1/// Strategy for calculating popup position relative to an anchor rectangle
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
3pub enum AnchorStrategy {
4    /// Center popup horizontally below the anchor
5    #[default]
6    CenterBottom,
7    /// Center popup horizontally above the anchor
8    CenterTop,
9    /// Position popup at anchor's bottom-left
10    LeftBottom,
11    /// Position popup at anchor's bottom-right
12    RightBottom,
13    /// Position popup at anchor's top-left
14    LeftTop,
15    /// Position popup at anchor's top-right
16    RightTop,
17    /// Position popup at cursor coordinates
18    Cursor,
19}
20
21impl AnchorStrategy {
22    #[must_use]
23    pub const fn calculate_position(
24        self,
25        anchor_x: f64,
26        anchor_y: f64,
27        anchor_w: f64,
28        anchor_h: f64,
29        popup_w: f64,
30        popup_h: f64,
31    ) -> (f64, f64) {
32        match self {
33            Self::CenterBottom => {
34                let center_x = anchor_x + (anchor_w / 2.0);
35                let x = center_x - (popup_w / 2.0);
36                let y = anchor_y + anchor_h;
37                (x, y)
38            }
39            Self::CenterTop => {
40                let center_x = anchor_x + (anchor_w / 2.0);
41                let x = center_x - (popup_w / 2.0);
42                let y = anchor_y - popup_h;
43                (x, y)
44            }
45            Self::LeftBottom => (anchor_x, anchor_y + anchor_h),
46            Self::RightBottom => (anchor_x + anchor_w - popup_w, anchor_y + anchor_h),
47            Self::LeftTop => (anchor_x, anchor_y - popup_h),
48            Self::RightTop => (anchor_x + anchor_w - popup_w, anchor_y - popup_h),
49            Self::Cursor => (anchor_x, anchor_y),
50        }
51    }
52}