1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::num::Saturating;
use std::ops::{Add, AddAssign, Range, Sub};

mod draw;

mod align;
mod border;
mod expand;
mod layoutable;
mod linear;
mod padding;
mod placement;
mod scale;

pub mod prelude {
    pub use crate::{
        align::{center, east, north, south, west},
        border::{bordered, DashedLine, RoundedLine},
        expand::{expand, expand_horizontal, expand_vertical},
        layoutable::{owned_text, Layoutable},
        linear::{horizontal_layout, vertical_layout},
        padding::padding,
        placement::{callback_placement, optional_placement},
        scale::scale,
    };
}

///
/// Defined preferred size of a Layoutable
///
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct ComponentSize {
    width: ValueRange<Saturating<u32>>,
    height: ValueRange<Saturating<u32>>,
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct ValueRange<V> {
    preferred_value: V,
    min_value: V,
    max_value: V,
}

impl<V> Add<V> for ValueRange<V>
where
    V: Add<V, Output = V> + Clone,
{
    type Output = ValueRange<V>;

    fn add(self, rhs: V) -> Self::Output {
        ValueRange {
            preferred_value: self.preferred_value + rhs.clone(),
            min_value: self.min_value + rhs.clone(),
            max_value: self.max_value + rhs,
        }
    }
}
impl Add<i32> for ValueRange<Saturating<u32>> {
    type Output = ValueRange<Saturating<u32>>;

    fn add(self, rhs: i32) -> Self::Output {
        if rhs.is_negative() {
            let offset = Saturating(-rhs as u32);
            ValueRange {
                preferred_value: self.preferred_value - offset,
                min_value: self.min_value - offset,
                max_value: self.max_value - offset,
            }
        } else {
            let offset = Saturating(rhs as u32);
            ValueRange {
                preferred_value: self.preferred_value + offset,
                min_value: self.min_value + offset,
                max_value: self.max_value + offset,
            }
        }
    }
}
impl<V> Sub<V> for ValueRange<V>
where
    V: Sub<V, Output = V> + Clone,
{
    type Output = ValueRange<V>;

    fn sub(self, rhs: V) -> Self::Output {
        ValueRange {
            preferred_value: self.preferred_value - rhs.clone(),
            min_value: self.min_value - rhs.clone(),
            max_value: self.max_value - rhs,
        }
    }
}

impl<V: PartialOrd + Clone> ValueRange<V> {
    fn expand(&mut self, rhs: &Self) {
        if self.preferred_value < rhs.preferred_value {
            self.preferred_value = rhs.preferred_value.clone();
        }
        if self.min_value < rhs.min_value {
            self.min_value = rhs.min_value.clone()
        }
        if self.max_value < rhs.max_value {
            self.max_value = rhs.max_value.clone()
        }
    }
}

impl<V: AddAssign> AddAssign for ValueRange<V> {
    fn add_assign(&mut self, rhs: Self) {
        self.preferred_value += rhs.preferred_value;
        self.min_value += rhs.min_value;
        self.max_value += rhs.max_value;
    }
}

impl<V: Clone> ValueRange<Saturating<V>> {
    fn fixed(value: V) -> Self {
        Self {
            preferred_value: Saturating(value.clone()),
            min_value: Saturating(value.clone()),
            max_value: Saturating(value),
        }
    }
}

impl ValueRange<Saturating<u32>> {
    fn expand_max(&self) -> Self {
        Self {
            preferred_value: self.preferred_value,
            min_value: self.min_value,
            max_value: Saturating(u32::MAX),
        }
    }
}

impl ComponentSize {
    ///
    /// Create a fixed sized constraint
    ///
    /// # Arguments
    ///
    /// * `width`: fixed width
    /// * `height`: fixed height
    ///
    /// returns: ComponentSize
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_layout::ComponentSize;
    /// //defined  a 8x8 fixed size
    /// let fixed = ComponentSize::fixed_size(8,8);
    /// ```
    pub fn fixed_size(width: u32, height: u32) -> ComponentSize {
        ComponentSize {
            width: ValueRange::fixed(width),
            height: ValueRange::fixed(height),
        }
    }
    ///
    /// Defines a fully customizeable component size
    ///
    /// # Arguments
    ///
    /// * `preferred_width`: optimal width of the component
    /// * `preferred_height`: optimal height of the component
    /// * `width_range`: minimum and maximum width, allows the layout algorithm to arrange and place that element
    /// * `height_range`: minimum and maximum height, allows the layout algorithm to arrange and place that element
    ///
    /// returns: ComponentSize
    ///
    pub fn new(
        preferred_width: u32,
        preferred_height: u32,
        width_range: Range<u32>,
        height_range: Range<u32>,
    ) -> Self {
        Self {
            width: ValueRange {
                preferred_value: Saturating(preferred_width),
                min_value: Saturating(width_range.start),
                max_value: Saturating(width_range.end),
            },
            height: ValueRange {
                preferred_value: Saturating(preferred_height),
                min_value: Saturating(height_range.start),
                max_value: Saturating(height_range.end),
            },
        }
    }
}