1use iced_core::{Point, Size, layout};
4
5pub trait Position {
7 fn center_and_bounce(&mut self, position: Point, bounds: Size);
10}
11
12impl Position for layout::Node {
13 fn center_and_bounce(&mut self, position: Point, bounds: Size) {
14 let size = self.size();
15
16 self.move_to_mut(Point::new(
17 (position.x - (size.width / 2.0)).max(0.0),
18 (position.y - (size.height / 2.0)).max(0.0),
19 ));
20
21 let new_self_bounds = self.bounds();
22
23 self.move_to_mut(Point::new(
24 if new_self_bounds.x + new_self_bounds.width > bounds.width {
25 (new_self_bounds.x - (new_self_bounds.width - (bounds.width - new_self_bounds.x)))
26 .max(0.0)
27 } else {
28 new_self_bounds.x
29 },
30 if new_self_bounds.y + new_self_bounds.height > bounds.height {
31 (new_self_bounds.y - (new_self_bounds.height - (bounds.height - new_self_bounds.y)))
32 .max(0.0)
33 } else {
34 new_self_bounds.y
35 },
36 ));
37 }
38}