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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! The `Widget` abstraction and some related types.
use base::basic_types::*;
use base::Window;
use std::cmp::max;
use std::iter::Sum;
use std::marker::PhantomData;
use std::ops::{Add, AddAssign};

/// A widget is something that can be drawn to a window.
pub trait Widget {
    /// Return the current demand for (rectangular) screen estate.
    ///
    /// The callee may report different
    /// demands on subsequent calls.
    fn space_demand(&self) -> Demand2D;

    /// Draw the widget to the given window.
    ///
    /// There is no guarantee that the window is of the size
    /// requested in `space_demand`, it can be smaller than the minimum or larger than the maximum
    /// (if specified). However, in general, the layouting algorithm tries to honor the demand of
    /// the widget.
    ///
    /// The hints give the widget some useful information on how to render.
    fn draw(&self, window: Window, hints: RenderingHints);
}

/// Hints that can be used by applications to control how Widgets are rendered and used by Widgets
/// to deduce how to render to best show the current application state.
#[derive(Clone, Copy, Debug)]
pub struct RenderingHints {
    /// e.g., whether or not this Widget receives input
    pub active: bool,
    /// Periodic signal that can be used to e.g. let a cursor blink.
    pub blink: Blink,

    // Make users of the library unable to construct RenderingHints from members.
    // This way we can add members in a backwards compatible way in future versions.
    #[doc(hidden)]
    _do_not_construct: (),
}

impl Default for RenderingHints {
    fn default() -> Self {
        Self::new()
    }
}

impl RenderingHints {
    /// Construct a default hint object.
    pub fn new() -> Self {
        RenderingHints {
            active: true,
            blink: Blink::On,
            _do_not_construct: (),
        }
    }
    pub fn active(self, val: bool) -> Self {
        RenderingHints {
            active: val,
            ..self
        }
    }
    pub fn blink(self, val: Blink) -> Self {
        RenderingHints { blink: val, ..self }
    }
}

/// A value from a periodic boolean signal.
///
/// Think of it like the state of an LED or cursor (block).
#[derive(Clone, Copy, Debug)]
pub enum Blink {
    On,
    Off,
}

impl Blink {
    pub fn toggled(self) -> Self {
        match self {
            Blink::On => Blink::Off,
            Blink::Off => Blink::On,
        }
    }

    pub fn toggle(&mut self) {
        *self = self.toggled();
    }
}

/// A one dimensional description of spatial demand of a widget.
///
/// A Demand always has a minimum (although it may be zero) and may have a maximum. It is required
/// that the minimum is smaller or equal to the maximum (if present).
#[derive(Eq, PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Demand<T: AxisDimension> {
    pub min: PositiveAxisDiff<T>,
    pub max: Option<PositiveAxisDiff<T>>,
    _dim: PhantomData<T>,
}

impl<T: AxisDimension> Add<Demand<T>> for Demand<T> {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Demand {
            min: self.min + rhs.min,
            max: if let (Some(l), Some(r)) = (self.max, rhs.max) {
                Some(l + r)
            } else {
                None
            },
            _dim: Default::default(),
        }
    }
}
impl<T: AxisDimension> AddAssign for Demand<T> {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs
    }
}
impl<T: AxisDimension + PartialOrd + Ord> Sum for Demand<T> {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Self>,
    {
        iter.fold(Demand::exact(0), Demand::add)
    }
}
impl<'a, T: AxisDimension + PartialOrd + Ord> Sum<&'a Demand<T>> for Demand<T> {
    fn sum<I>(iter: I) -> Demand<T>
    where
        I: Iterator<Item = &'a Demand<T>>,
    {
        iter.fold(Demand::zero(), |d1: Demand<T>, d2: &Demand<T>| d1 + *d2)
    }
}

impl<T: AxisDimension + PartialOrd + Ord> Demand<T> {
    /// A minimum and maximum demand of exactly 0.
    pub fn zero() -> Self {
        Self::exact(0)
    }

    /// A minimum and maximum demand of exactly the specified amount.
    pub fn exact<I: Into<PositiveAxisDiff<T>> + Copy>(size: I) -> Self {
        Demand {
            min: size.into(),
            max: Some(size.into()),
            _dim: Default::default(),
        }
    }
    /// An specified minimum demand, but no defined maximum.
    pub fn at_least<I: Into<PositiveAxisDiff<T>> + Copy>(size: I) -> Self {
        Demand {
            min: size.into(),
            max: None,
            _dim: Default::default(),
        }
    }
    /// A specified range of acceptable values between minimum and maximum.
    pub fn from_to<I: Into<PositiveAxisDiff<T>> + Copy>(min: I, max: I) -> Self {
        assert!(min.into() <= max.into(), "Invalid min/max");
        Demand {
            min: min.into(),
            max: Some(max.into()),
            _dim: Default::default(),
        }
    }

    /// Compute the composed maximum of two Demands. This is especially useful when building tables
    /// for example.
    ///
    /// # Examples:
    /// ```
    /// use unsegen::widget::Demand;
    /// use unsegen::base::*;
    ///
    /// let d1 = Demand::<ColDimension>::exact(5);
    /// let d2 = Demand::<ColDimension>::at_least(0);
    ///
    /// assert_eq!(d1.max(d2), Demand::<ColDimension>::at_least(5));
    /// ```
    pub fn max(&self, other: Self) -> Self {
        Demand {
            min: max(self.min, other.min),
            max: if let (Some(l), Some(r)) = (self.max, other.max) {
                Some(max(l, r))
            } else {
                None
            },
            _dim: Default::default(),
        }
    }

    /// Replace self with the maximum of self and other (see `Demand::max`).
    pub fn max_assign(&mut self, other: Self) {
        *self = self.max(other);
    }
}

pub type ColDemand = Demand<ColDimension>;
pub type RowDemand = Demand<RowDimension>;

/// A two dimensional (rectangular) Demand (composed of `Demand`s for columns and rows).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Demand2D {
    pub width: ColDemand,
    pub height: RowDemand,
}

impl Demand2D {
    /// Combine two `Demand2D`s by accumulating the height and making the width accommodate both.
    ///
    /// This is useful two compute the combined  `Demand2D` of two widgets arranged on top of each
    /// other.
    ///
    /// # Examples:
    /// ```
    /// use unsegen::base::*;
    /// use unsegen::widget::*;
    ///
    /// let d1 = Demand2D {
    ///     width: ColDemand::exact(5),
    ///     height: RowDemand::exact(5),
    /// };
    /// let d2 = Demand2D {
    ///     width: ColDemand::at_least(2),
    ///     height: RowDemand::from_to(3, 5),
    /// };
    ///
    /// assert_eq!(
    ///     d1.add_vertical(d2),
    ///     Demand2D {
    ///         width: ColDemand::at_least(5),
    ///         height: RowDemand::from_to(8, 10),
    ///     }
    /// );
    /// ```
    pub fn add_vertical(self, other: Self) -> Self {
        Demand2D {
            width: self.width.max(other.width),
            height: self.height + other.height,
        }
    }

    /// Combine two `Demand2D`s by accumulating the width and making the height accommodate both.
    ///
    /// This is useful two compute the combined  `Demand2D` of two widgets arranged on top of each
    /// other.
    ///
    /// # Examples:
    /// ```
    /// use unsegen::base::*;
    /// use unsegen::widget::*;
    ///
    /// let d1 = Demand2D {
    ///     width: ColDemand::exact(5),
    ///     height: RowDemand::exact(5),
    /// };
    /// let d2 = Demand2D {
    ///     width: ColDemand::at_least(2),
    ///     height: RowDemand::from_to(3, 5),
    /// };
    ///
    /// assert_eq!(
    ///     d1.add_horizontal(d2),
    ///     Demand2D {
    ///         width: ColDemand::at_least(7),
    ///         height: RowDemand::exact(5),
    ///     }
    /// );
    /// ```
    pub fn add_horizontal(self, other: Self) -> Self {
        Demand2D {
            width: self.width + other.width,
            height: self.height.max(other.height),
        }
    }
}