iced_aw/core/
overlay.rs

1//! Helper functions for overlays
2
3use iced_core::{Point, Size, layout};
4
5/// Trait containing functions for positioning of nodes.
6pub trait Position {
7    /// Centers this node around the given position. If the node is over the
8    /// specified bounds it's bouncing back to be fully visible on screen.
9    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}