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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! The `Widget` abstraction and some related types.
use base::basic_types::*;
use base::{Cursor, Window, WrappingMode};
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);
}

/// An extension trait to Widget which access to convenience methods that alters the behavior of
/// the wrapped widgets.
pub trait WidgetExt: Widget + Sized {
    /// Center the widget according to the specified maximum demand within the supplied window.
    /// This is only useful if the widget has a defined maximum size and the window is larger than
    /// that.
    fn centered(self) -> Centered<Self> {
        Centered(self)
    }

    /// Alter the window before letting the widget draw itself in it.
    fn with_window<F: Fn(Window, RenderingHints) -> Window>(self, f: F) -> WithWindow<Self, F> {
        WithWindow(self, f)
    }

    /// Alter the reported demand of the widget. This can be useful, for example, to force a widget
    /// to assume all space in the window or to artificially restrict the size of a widget.
    fn with_demand<F: Fn(Demand2D) -> Demand2D>(self, f: F) -> WithDemand<Self, F> {
        WithDemand(self, f)
    }
}

impl<W: Widget + Sized> WidgetExt for W {}

/// Center the widget according to the specified maximum demand within the supplied window.
/// This is only useful if the widget has a defined maximum size and the window is larger than
/// that.
///
/// This wrapper can be created using `WidgetExt::centered`.
pub struct Centered<W>(W);

impl<W: Widget> Widget for Centered<W> {
    fn space_demand(&self) -> Demand2D {
        self.0.space_demand()
    }
    fn draw(&self, mut window: Window, hints: RenderingHints) {
        let demand = self.space_demand();

        let window_height = window.get_height();
        let window_width = window.get_width();

        let max_height = demand.height.max.unwrap_or(window.get_height());
        let max_width = demand.width.max.unwrap_or(window.get_width());

        let start_row = ((window_height - max_height) / 2)
            .from_origin()
            .positive_or_zero();
        let start_col = ((window_width - max_width) / 2)
            .from_origin()
            .positive_or_zero();
        let end_row = (start_row + max_height).min(window_height.from_origin());
        let end_col = (start_col + max_width).min(window_width.from_origin());

        let window = window.create_subwindow(start_col..end_col, start_row..end_row);
        self.0.draw(window, hints);
    }
}

/// Alter the window before letting the widget draw itself in it.
///
/// This wrapper can be created using `WidgetExt::centered`.
pub struct WithWindow<W, F>(W, F);

impl<W: Widget, F: Fn(Window, RenderingHints) -> Window> Widget for WithWindow<W, F> {
    fn space_demand(&self) -> Demand2D {
        self.0.space_demand()
    }
    fn draw(&self, window: Window, hints: RenderingHints) {
        self.0.draw(self.1(window, hints), hints);
    }
}

/// Alter the reported demand of the widget. This can be useful, for example, to force a widget
/// to assume all space in the window or to artificially restrict the size of a widget.
///
/// This wrapper can be created using `WidgetExt::centered`.
pub struct WithDemand<W, F>(W, F);

impl<W: Widget, F: Fn(Demand2D) -> Demand2D> Widget for WithDemand<W, F> {
    fn space_demand(&self) -> Demand2D {
        self.1(self.0.space_demand())
    }
    fn draw(&self, window: Window, hints: RenderingHints) {
        self.0.draw(window, hints);
    }
}

impl<S: std::convert::AsRef<str>> Widget for S {
    fn space_demand(&self) -> Demand2D {
        let mut width = 0;
        let mut height = 0;
        for line in self.as_ref().lines() {
            width = width.max(crate::widget::count_grapheme_clusters(line));
            height += 1;
        }
        Demand2D {
            width: Demand::exact(width),
            height: Demand::exact(height),
        }
    }
    fn draw(&self, mut window: Window, _hints: RenderingHints) {
        let mut cursor = Cursor::new(&mut window).wrapping_mode(WrappingMode::Wrap);
        cursor.write(self.as_ref());
    }
}

/// 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: (),
        }
    }
    /// Hint on whether the widget is active, i.e., most of the time: It receives input.
    pub fn active(self, val: bool) -> Self {
        RenderingHints {
            active: val,
            ..self
        }
    }

    /// Use this to implement blinking effects for your widget. Usually, Blink can be expected to
    /// alternate every second or so.
    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)]
#[allow(missing_docs)]
pub enum Blink {
    On,
    Off,
}

impl Blink {
    /// Get the alternate on/off value.
    pub fn toggled(self) -> Self {
        match self {
            Blink::On => Blink::Off,
            Blink::Off => Blink::On,
        }
    }

    /// Change to the alternate on/off value.
    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)]
#[allow(missing_docs)]
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);
    }
}

/// Horizontal Demand
pub type ColDemand = Demand<ColDimension>;
/// Vertical Demand
pub type RowDemand = Demand<RowDimension>;

/// A two dimensional (rectangular) Demand (composed of `Demand`s for columns and rows).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(missing_docs)]
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),
        }
    }
}