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
//! # Align cursive views
//!
//! This crate provides an `AlignedView` for
//! [gyscos/cursive](https://github.com/gyscos/cursive) views which makes it
//! possible to align the child view (center, left, right, top, bottom). The
//! `AlignedView` uses the `required_size` reported by the child view and fills
//! the rest of the available space with the views background color.
//!
//! ## Aligning a child view
//!
//! The easiest way to align a view is via the `Alignable` trait:
//!
//! ```rust
//! use cursive::Cursive;
//! use cursive::view::Boxable;
//! use cursive::views::{Panel, DummyView};
//! use cursive_aligned_view::Alignable;
//!
//! fn main() {
//!     let mut siv = Cursive::default();
//!
//!     let panel = Panel::new(DummyView)
//!         .title("Hello, world!")
//!         .fixed_width(20)
//!         .align_center();
//!
//!     siv.add_fullscreen_layer(panel);
//!     // siv.run()
//! }
//! ```
//!
//! This is the preferred way as it is *chainable* and consistent with cursive's
//! `Boxable` and `Identifiable` traits.
//!
//! As an alternative you can use the `AlignedView` constructors directly:
//!
//! ```rust
//! use cursive::Cursive;
//! use cursive::view::Boxable;
//! use cursive::views::{Panel, DummyView};
//! use cursive_aligned_view::AlignedView;
//!
//! fn main() {
//!     let mut siv = Cursive::default();
//!
//!     let panel = Panel::new(DummyView)
//!         .title("Hello, world!")
//!         .fixed_width(20);
//!     let aligned = AlignedView::with_center(panel);
//!
//!     siv.add_fullscreen_layer(aligned);
//!     // siv.run()
//! }
//! ```
//!
//! ## Supported Alignments
//!
//! | Alignment     | Construction method   |
//! |---------------|-----------------------|
//! | top left      | `align_top_left`      |
//! | top center    | `align_top_center`    |
//! | top right     | `align_top_right`     |
//! | center left   | `align_center_left`   |
//! | center        | `align_center`        |
//! | center right  | `align_center_right`  |
//! | bottom left   | `align_bottom_left`   |
//! | bottom center | `align_bottom_center` |
//! | bottom right  | `align_bottom_right`  |

use cursive::view::{View, Selector};
use cursive::event::{AnyCb, Event, EventResult};
use cursive::direction::Direction;
use cursive::align::{Align, HAlign, VAlign};
use cursive::{Printer, Vec2, Rect};

/// Use this trait to extend all `cursive::view::View` instances to support
/// the `align_...` methods.
///
/// This trait provides *chainable* constructors for the `AlignedView`.
///
/// # Usage Example
///
/// ```rust
/// use cursive::Cursive;
/// use cursive::view::Boxable;
/// use cursive::views::{Panel, DummyView};
/// use cursive_aligned_view::Alignable;
///
/// fn main() {
///     let mut siv = Cursive::default();
///
///     let panel = Panel::new(DummyView)
///         .title("Hello, world!")
///         .fixed_width(20)
///         .align_top_center(); // constructing `AlignedView`
///
///     siv.add_fullscreen_layer(panel);
///     // siv.run()
/// }
/// ```
pub trait Alignable: View + Sized {
    /// Align a child view at the top-left of the parent.
    fn align_top_left(self) -> AlignedView<Self> {
        AlignedView::with_top_left(self)
    }

    /// Align a child view at the top-center of the parent.
    fn align_top_center(self) -> AlignedView<Self> {
        AlignedView::with_top_center(self)
    }

    /// Align a child view at the top-right of the parent.
    fn align_top_right(self) -> AlignedView<Self> {
        AlignedView::with_top_right(self)
    }

    /// Align a child view at the center-left of the parent.
    fn align_center_left(self) -> AlignedView<Self> {
        AlignedView::with_center_left(self)
    }

    /// Align a child view at the center of the parent.
    fn align_center(self) -> AlignedView<Self> {
        AlignedView::with_center(self)
    }

    /// Align a child view at the center-right of the parent.
    fn align_center_right(self) -> AlignedView<Self> {
        AlignedView::with_center_right(self)
    }

    /// Align a child view at the bottom-left of the parent.
    fn align_bottom_left(self) -> AlignedView<Self> {
        AlignedView::with_bottom_left(self)
    }

    /// Align a child view at the bottom-center of the parent.
    fn align_bottom_center(self) -> AlignedView<Self> {
        AlignedView::with_bottom_center(self)
    }

    /// Align a child view at the bottom-right of the parent.
    fn align_bottom_right(self) -> AlignedView<Self> {
        AlignedView::with_bottom_right(self)
    }
}

impl<T: View> Alignable for T {}

/// This struct aligns a child view with a given alignment.
///
/// The child view will have its minimum allowed size. Additionally, the child
/// may get cropped if it is larger than the available size.
///
/// The padded space around the child view is filled with the `View` color from
/// cursive's color palette.
///
/// # Usage
///
/// The `AlignedView` may be used in 2 different ways:
///
/// 1. Via the `Alignable` composition trait
/// 2. Via normal constructors
///
/// ## Using Alignable
///
/// ```rust
/// use cursive::Cursive;
/// use cursive::view::Boxable;
/// use cursive::views::{Panel, DummyView};
/// use cursive_aligned_view::Alignable;
///
/// fn main() {
///     let mut siv = Cursive::default();
///
///     let panel = Panel::new(DummyView)
///         .title("Hello, world!")
///         .fixed_width(20)
///         .align_top_center(); // `align_...` methods from `Alignable`
///
///     siv.add_fullscreen_layer(panel);
///     // siv.run()
/// }
/// ```
///
/// ## Constructors
///
/// ```rust
/// use cursive::Cursive;
/// use cursive::view::Boxable;
/// use cursive::views::{Panel, DummyView};
/// use cursive_aligned_view::AlignedView;
///
/// fn main() {
///     let mut siv = Cursive::default();
///
///     let panel = Panel::new(DummyView)
///         .title("Hello, world!")
///         .fixed_width(20);
///     let aligned = AlignedView::with_bottom_center(panel); // constructor
///
///     siv.add_fullscreen_layer(aligned);
///     // siv.run()
/// }
/// ```
pub struct AlignedView<T> {
    view: T,
    alignment: Align,
    last_size: Vec2,
    offset: Vec2,
    needs_relayout: bool,
}

impl<T: View> AlignedView<T> {
    fn new(view: T, alignment: Align) -> Self {
        Self {
            view,
            alignment,
            last_size: Vec2::new(0, 0),
            offset: Vec2::new(0, 0),
            needs_relayout: false,
        }
    }

    /// Wrap a child view and align it at the top-left of the parent.
    pub fn with_top_left(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Left, VAlign::Top))
    }

    /// Wrap a child view and align it at the top-center of the parent.
    pub fn with_top_center(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Center, VAlign::Top))
    }

    /// Wrap a child view and align it at the top-right of the parent.
    pub fn with_top_right(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Right, VAlign::Top))
    }

    /// Wrap a child view and align it at the center-left of the parent.
    pub fn with_center_left(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Left, VAlign::Center))
    }

    /// Wrap a child view and align it at the center of the parent.
    pub fn with_center(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Center, VAlign::Center))
    }

    /// Wrap a child view and align it at the center-right of the parent.
    pub fn with_center_right(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Right, VAlign::Center))
    }

    /// Wrap a child view and align it at the bottom-left of the parent.
    pub fn with_bottom_left(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Left, VAlign::Bottom))
    }

    /// Wrap a child view and align it at the bottom-center of the parent.
    pub fn with_bottom_center(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Center, VAlign::Bottom))
    }

    /// Wrap a child view and align it at the bottom-right of the parent.
    pub fn with_bottom_right(view: T) -> Self {
        Self::new(view, Align::new(HAlign::Right, VAlign::Bottom))
    }

    /// Set the alignment of this view to top-left.
    pub fn set_top_left(&mut self) {
        self.alignment = Align::new(HAlign::Left, VAlign::Top);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to top-center.
    pub fn set_top_center(&mut self) {
        self.alignment = Align::new(HAlign::Center, VAlign::Top);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to top-right.
    pub fn set_top_right(&mut self) {
        self.alignment = Align::new(HAlign::Right, VAlign::Top);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to center-left.
    pub fn set_center_left(&mut self) {
        self.alignment = Align::new(HAlign::Left, VAlign::Center);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to center.
    pub fn set_center(&mut self) {
        self.alignment = Align::new(HAlign::Center, VAlign::Center);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to center-right.
    pub fn set_center_right(&mut self) {
        self.alignment = Align::new(HAlign::Right, VAlign::Center);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to bottom-left.
    pub fn set_bottom_left(&mut self) {
        self.alignment = Align::new(HAlign::Left, VAlign::Bottom);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to bottom-center.
    pub fn set_bottom_center(&mut self) {
        self.alignment = Align::new(HAlign::Center, VAlign::Bottom);
        self.needs_relayout = true;
    }

    /// Set the alignment of this view to bottom-right.
    pub fn set_bottom_right(&mut self) {
        self.alignment = Align::new(HAlign::Right, VAlign::Bottom);
        self.needs_relayout = true;
    }

    /// Get the current alignment of this view.
    pub fn alignment(&self) -> &Align {
        &self.alignment
    }
}

impl<T: View> View for AlignedView<T> {
    fn draw(&self, printer: &Printer) {
        let offset_printer = printer.offset(self.offset).cropped(self.last_size);
        self.view.draw(&offset_printer);
    }

    fn layout(&mut self, size: Vec2) {
        self.offset = Vec2::new(
            self.alignment.h.get_offset(self.last_size.x, size.x),
            self.alignment.v.get_offset(self.last_size.y, size.y),
        );

        let x = std::cmp::min(size.x, self.last_size.x);
        let y = std::cmp::min(size.y, self.last_size.y);

        self.view.layout(Vec2::new(x, y));

        self.needs_relayout = false;
    }

    fn needs_relayout(&self) -> bool {
        self.needs_relayout || self.view.needs_relayout()
    }

    fn required_size(&mut self, constraint: Vec2) -> Vec2 {
        self.last_size = self.view.required_size(constraint);

        constraint
    }

    fn on_event(&mut self, ev: Event) -> EventResult {
        self.view.on_event(ev.relativized(self.offset))
    }

    fn call_on_any<'a>(&mut self, sel: &Selector, cb: AnyCb<'a>) {
        self.view.call_on_any(sel, cb);
    }

    fn focus_view(&mut self, sel: &Selector) -> Result<(), ()> {
        self.view.focus_view(sel)
    }

    fn take_focus(&mut self, source: Direction) -> bool {
        self.view.take_focus(source)
    }

    fn important_area(&self, view_size: Vec2) -> Rect {
        self.view.important_area(view_size)
    }
}