1use strata_primitives::{Point, Rect, Size};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct FocusTrap {
11 pub count: usize,
12 pub active_index: usize,
13 pub wrap_around: bool,
14}
15
16impl FocusTrap {
17 pub fn new(count: usize) -> Self {
18 Self {
19 count,
20 active_index: 0,
21 wrap_around: true,
22 }
23 }
24
25 #[allow(clippy::should_implement_trait)]
27 pub fn next(&mut self) -> usize {
28 if self.count == 0 {
29 return 0;
30 }
31 if self.active_index + 1 < self.count {
32 self.active_index += 1;
33 } else if self.wrap_around {
34 self.active_index = 0;
35 }
36 self.active_index
37 }
38
39 pub fn prev(&mut self) -> usize {
41 if self.count == 0 {
42 return 0;
43 }
44 if self.active_index > 0 {
45 self.active_index -= 1;
46 } else if self.wrap_around {
47 self.active_index = self.count - 1;
48 }
49 self.active_index
50 }
51
52 pub fn set(&mut self, idx: usize) {
53 if idx < self.count {
54 self.active_index = idx;
55 }
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
61pub enum Orientation {
62 #[default]
63 Horizontal,
64 Vertical,
65}
66
67#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct RovingIndex {
70 pub count: usize,
71 pub current: usize,
72 pub orientation: Orientation,
73 pub loop_around: bool,
74}
75
76impl RovingIndex {
77 pub fn new(count: usize, orientation: Orientation) -> Self {
78 Self {
79 count,
80 current: 0,
81 orientation,
82 loop_around: true,
83 }
84 }
85
86 pub fn handle_key(&mut self, key: &str) -> Option<usize> {
88 if self.count == 0 {
89 return None;
90 }
91 let old = self.current;
92 match (self.orientation, key) {
93 (Orientation::Horizontal, "ArrowRight") | (Orientation::Vertical, "ArrowDown") => {
94 if self.current + 1 < self.count {
95 self.current += 1;
96 } else if self.loop_around {
97 self.current = 0;
98 }
99 }
100 (Orientation::Horizontal, "ArrowLeft") | (Orientation::Vertical, "ArrowUp") => {
101 if self.current > 0 {
102 self.current -= 1;
103 } else if self.loop_around {
104 self.current = self.count - 1;
105 }
106 }
107 (_, "Home") => {
108 self.current = 0;
109 }
110 (_, "End") => {
111 self.current = self.count - 1;
112 }
113 _ => return None,
114 }
115
116 if self.current != old {
117 Some(self.current)
118 } else {
119 None
120 }
121 }
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
126pub enum Placement {
127 #[default]
128 Bottom,
129 BottomStart,
130 BottomEnd,
131 Top,
132 TopStart,
133 TopEnd,
134 Left,
135 LeftStart,
136 LeftEnd,
137 Right,
138 RightStart,
139 RightEnd,
140}
141
142pub struct FloatingAnchor;
144
145impl FloatingAnchor {
146 pub fn compute_position(
147 anchor: Rect,
148 floating_size: Size,
149 placement: Placement,
150 offset: f32,
151 ) -> Point {
152 let (ax, ay, aw, ah) = (
153 anchor.origin.x,
154 anchor.origin.y,
155 anchor.size.width,
156 anchor.size.height,
157 );
158 let (fw, fh) = (floating_size.width, floating_size.height);
159
160 match placement {
161 Placement::Bottom => Point::new(ax + (aw - fw) / 2.0, ay + ah + offset),
162 Placement::BottomStart => Point::new(ax, ay + ah + offset),
163 Placement::BottomEnd => Point::new(ax + aw - fw, ay + ah + offset),
164 Placement::Top => Point::new(ax + (aw - fw) / 2.0, ay - fh - offset),
165 Placement::TopStart => Point::new(ax, ay - fh - offset),
166 Placement::TopEnd => Point::new(ax + aw - fw, ay - fh - offset),
167 Placement::Left => Point::new(ax - fw - offset, ay + (ah - fh) / 2.0),
168 Placement::LeftStart => Point::new(ax - fw - offset, ay),
169 Placement::LeftEnd => Point::new(ax - fw - offset, ay + ah - fh),
170 Placement::Right => Point::new(ax + aw + offset, ay + (ah - fh) / 2.0),
171 Placement::RightStart => Point::new(ax + aw + offset, ay),
172 Placement::RightEnd => Point::new(ax + aw + offset, ay + ah - fh),
173 }
174 }
175
176 pub fn flip_if_overflow(pos: Point, floating_size: Size, viewport: Size) -> Point {
178 let mut x = pos.x;
179 let mut y = pos.y;
180
181 if x + floating_size.width > viewport.width {
182 x = (viewport.width - floating_size.width).max(0.0);
183 }
184 if x < 0.0 {
185 x = 0.0;
186 }
187
188 if y + floating_size.height > viewport.height {
189 y = (viewport.height - floating_size.height).max(0.0);
190 }
191 if y < 0.0 {
192 y = 0.0;
193 }
194
195 Point::new(x, y)
196 }
197}
198
199pub struct DismissableLayer;
201
202impl DismissableLayer {
203 pub fn should_dismiss_on_escape(key: &str) -> bool {
205 key == "Escape" || key == "Esc"
206 }
207
208 pub fn should_dismiss_on_outside_click(click: Point, layer_bounds: Rect) -> bool {
210 !layer_bounds.contains_point(click)
211 }
212}