iced_aw/widget/
common.rs

1//! Common types for reuse.
2//!
3
4use iced_core::{Padding, Rectangle};
5
6/// Methods for creating inner bounds
7#[allow(missing_debug_implementations)]
8pub enum InnerBounds {
9    /// Create inner bounds ratio to the outer bounds
10    Ratio(f32, f32),
11    /// Create inner bounds by padding the outer bounds
12    Padding(Padding),
13    /// Create square inner bounds
14    Square(f32),
15    /// Create inner bounds with a custom function
16    Custom(Box<dyn Fn(Rectangle) -> Rectangle>),
17}
18impl InnerBounds {
19    /// Gets the inner bounds of the Set type.
20    #[must_use]
21    pub fn get_bounds(&self, outer_bounds: Rectangle) -> Rectangle {
22        use InnerBounds::{Custom, Padding, Ratio, Square};
23        match self {
24            Ratio(w, h) => {
25                let width = w * outer_bounds.width;
26                let height = h * outer_bounds.height;
27                let x = outer_bounds.x + (outer_bounds.width - width) * 0.5;
28                let y = outer_bounds.y + (outer_bounds.height - height) * 0.5;
29                Rectangle {
30                    x,
31                    y,
32                    width,
33                    height,
34                }
35            }
36            Padding(p) => {
37                let x = outer_bounds.x + p.left;
38                let y = outer_bounds.y + p.top;
39                let width = outer_bounds.width - p.x();
40                let height = outer_bounds.width - p.y();
41                Rectangle {
42                    x,
43                    y,
44                    width,
45                    height,
46                }
47            }
48            Square(l) => {
49                let width = *l;
50                let height = *l;
51                let x = outer_bounds.x + (outer_bounds.width - width) * 0.5;
52                let y = outer_bounds.y + (outer_bounds.height - height) * 0.5;
53                Rectangle {
54                    x,
55                    y,
56                    width,
57                    height,
58                }
59            }
60            Custom(f) => f(outer_bounds),
61        }
62    }
63}