mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
80 lines
3.3 KiB
Rust
80 lines
3.3 KiB
Rust
//! ```
|
|
//! use ::tengri::{output::*, tui::*};
|
|
//! let area: [u16;4] = [10, 10, 20, 20];
|
|
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
|
//! assert_eq!(Content::layout(item, area), expected);
|
|
//! assert_eq!(Render::layout(item, area), expected);
|
|
//! };
|
|
//!
|
|
//! let four = ||Fixed::xy(4, 4, "");
|
|
//! test(area, &Align::nw(four()), [10, 10, 4, 4]);
|
|
//! test(area, &Align::n(four()), [18, 10, 4, 4]);
|
|
//! test(area, &Align::ne(four()), [26, 10, 4, 4]);
|
|
//! test(area, &Align::e(four()), [26, 18, 4, 4]);
|
|
//! test(area, &Align::se(four()), [26, 26, 4, 4]);
|
|
//! test(area, &Align::s(four()), [18, 26, 4, 4]);
|
|
//! test(area, &Align::sw(four()), [10, 26, 4, 4]);
|
|
//! test(area, &Align::w(four()), [10, 18, 4, 4]);
|
|
//!
|
|
//! let two_by_four = ||Fixed::xy(4, 2, "");
|
|
//! test(area, &Align::nw(two_by_four()), [10, 10, 4, 2]);
|
|
//! test(area, &Align::n(two_by_four()), [18, 10, 4, 2]);
|
|
//! test(area, &Align::ne(two_by_four()), [26, 10, 4, 2]);
|
|
//! test(area, &Align::e(two_by_four()), [26, 19, 4, 2]);
|
|
//! test(area, &Align::se(two_by_four()), [26, 28, 4, 2]);
|
|
//! test(area, &Align::s(two_by_four()), [18, 28, 4, 2]);
|
|
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
|
|
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
|
|
//! ```
|
|
use crate::*;
|
|
|
|
/// 9th of area to place.
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
|
|
|
pub struct Align<A>(Alignment, A);
|
|
|
|
impl<A> Align<A> {
|
|
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
|
#[inline] pub const fn x (a: A) -> Self { Self(Alignment::X, a) }
|
|
#[inline] pub const fn y (a: A) -> Self { Self(Alignment::Y, a) }
|
|
#[inline] pub const fn n (a: A) -> Self { Self(Alignment::N, a) }
|
|
#[inline] pub const fn s (a: A) -> Self { Self(Alignment::S, a) }
|
|
#[inline] pub const fn e (a: A) -> Self { Self(Alignment::E, a) }
|
|
#[inline] pub const fn w (a: A) -> Self { Self(Alignment::W, a) }
|
|
#[inline] pub const fn nw (a: A) -> Self { Self(Alignment::NW, a) }
|
|
#[inline] pub const fn sw (a: A) -> Self { Self(Alignment::SW, a) }
|
|
#[inline] pub const fn ne (a: A) -> Self { Self(Alignment::NE, a) }
|
|
#[inline] pub const fn se (a: A) -> Self { Self(Alignment::SE, a) }
|
|
}
|
|
|
|
impl<E: Output, A: Content<E>> Content<E> for Align<A> {
|
|
fn content (&self) -> impl Render<E> + '_ {
|
|
&self.1
|
|
}
|
|
fn layout (&self, on: E::Area) -> E::Area {
|
|
use Alignment::*;
|
|
let it = Render::layout(&self.content(), on).xywh();
|
|
let cx = on.x()+(on.w().minus(it.w())/2.into());
|
|
let cy = on.y()+(on.h().minus(it.h())/2.into());
|
|
let fx = (on.x()+on.w()).minus(it.w());
|
|
let fy = (on.y()+on.h()).minus(it.h());
|
|
let [x, y] = match self.0 {
|
|
Center => [cx, cy],
|
|
X => [cx, it.y()],
|
|
Y => [it.x(), cy],
|
|
NW => [on.x(), on.y()],
|
|
N => [cx, on.y()],
|
|
NE => [fx, on.y()],
|
|
W => [on.x(), cy],
|
|
E => [fx, cy],
|
|
SW => [on.x(), fy],
|
|
S => [cx, fy],
|
|
SE => [fx, fy],
|
|
}.into();
|
|
[x, y, it.w(), it.h()].into()
|
|
}
|
|
fn render (&self, to: &mut E) {
|
|
to.place(Content::layout(self, to.area()), &self.content())
|
|
}
|
|
}
|