wip: final simplify

This commit is contained in:
facile pop culture reference 2026-07-05 18:46:22 +03:00
parent 90ebae0f26
commit 0b9e9c0696
21 changed files with 607 additions and 544 deletions

13
src/layout/align.rs Normal file
View file

@ -0,0 +1,13 @@
use crate::*;
pub struct Align<T>(Option<Origin>, T);
/// ```
/// use tengri::*;
/// let _ = align(None, "unaligned");
/// let _ = align(Origin::SE, "southeast");
/// let _ = align(Some(Origin::SE), "southeast");
/// ```
pub fn align <S: Screen, T: Draw<S>, U: Into<Option<Origin>>> (origin: U, it: T) -> Align<T> {
Align(origin.into(), it)
}

15
src/layout/area.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::*;
pub struct Area<S: Screen, T: Draw<S>>(Option<XYWH<S::Unit>>, T);
/// ```
/// use tengri::*;
/// let _ = area(None, "unareaed");
/// let _ = area([1, 2, 3, 4], "southeast");
/// let _ = area(Some([1, 2, 3, 4]), "southeast");
/// ```
pub fn area <S: Screen, T: Draw<S>, U: Into<Option<XYWH<S::Unit>>>> (
origin: U, it: T
) -> Area<S, T> {
Area(origin.into(), it)
}

23
src/layout/origin.rs Normal file
View file

@ -0,0 +1,23 @@
use super::*;
/// Where is [0, 0] located?
///
/// ```
/// use tengri::draw::Origin;
/// let _ = Origin::NW.align(());
/// ```
#[cfg_attr(test, derive(Arbitrary))]
#[derive(Debug, Copy, Clone, Default)] pub enum Origin {
#[default] C, X, Y, NW, N, NE, E, SE, S, SW, W
}
/// Something that has `[0, 0]` at a particular point.
pub trait HasOrigin {
fn origin (&self) -> Origin;
}
impl<T: AsRef<Origin>> HasOrigin for T {
fn origin (&self) -> Origin {
*self.as_ref()
}
}

127
src/layout/split.rs Normal file
View file

@ -0,0 +1,127 @@
use super::*;
pub const fn east <T: Screen> (a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
Split::East.half(a, b)
}
pub const fn north <T: Screen> (a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
Split::North.half(a, b)
}
pub const fn west <T: Screen> (a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
Split::West.half(a, b)
}
pub const fn south <T: Screen> (a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
Split::South.half(a, b)
}
pub const fn above <T: Screen> (a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
Split::Above.half(a, b)
}
pub const fn below <T: Screen> (a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
Split::Below.half(a, b)
}
/// Split along an axis. Direction determines order.
#[cfg_attr(test, derive(Arbitrary))]
#[derive(Copy, Clone, PartialEq, Debug, Default)] pub enum Split {
North,
South,
East,
West,
Above,
#[default] Below
}
impl Split {
/// ```
/// use tengri::draw::Split::*;
/// let _ = Above.half("", "");
/// let _ = Below.half("", "");
/// let _ = North.half("", "");
/// let _ = South.half("", "");
/// let _ = East.half("", "");
/// let _ = West.half("", "");
/// ```
pub const fn half <T: Screen> (&self, a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
thunk(move|to: &mut T|{
let (area_a, area_b) = to.xywh().split_half(self);
let (origin_a, origin_b) = self.origins();
let a = align(origin_a, a);
let b = align(origin_b, b);
match self {
Self::Below => {
to.show(area(area_b, b))?;
to.show(area(area_b, a))?;
},
_ => {
to.show(area(area_a, a))?;
to.show(area(area_b, b))?;
}
}
Ok(to.xywh()) // FIXME: compute and return actually used area
})
}
/// Newly split areas begin at the center of the split
/// to maintain centeredness in the user's field of view.
///
/// Use [align] to override that and always start
/// at the top, bottom, etc.
///
/// ```
/// /*
///
/// Split east: Split south:
/// | | | | A |
/// | <-A|B-> | |---------|
/// | | | | B |
///
/// */
/// ```
const fn origins (&self) -> (Origin, Origin) {
use Origin::*;
match self {
Self::South => (S, N),
Self::East => (E, W),
Self::North => (N, S),
Self::West => (W, E),
Self::Above => (C, C),
Self::Below => (C, C),
}
}
}
#[macro_export] macro_rules! north {
($head:expr $(,)?) => { $head };
($head:expr, $($tail:expr),* $(,)?) => { north($head, north!($($tail,)*)) };
}
#[macro_export] macro_rules! south {
($head:expr $(,)?) => { $head };
($head:expr, $($tail:expr),* $(,)?) => { south($head, south!($($tail,)*)) };
}
#[macro_export] macro_rules! east {
($head:expr $(,)?) => { $head };
($head:expr, $($tail:expr),* $(,)?) => { east($head, east!($($tail,)*)) };
}
#[macro_export] macro_rules! west {
($head:expr $(,)?) => { $head };
($head:expr $(, $tail:expr)* $(,)?) => { west($head, west!($($tail,)*)) };
}
#[macro_export] macro_rules! above {
($head:expr $(,)?) => { $head };
($head:expr $(, $tail:expr)* $(,)?) => { above($head, above!($($tail,)*)) };
}
#[macro_export] macro_rules! below {
($head:expr $(,)?) => { $head };
($head:expr, $($tail:expr),* $(,)?) => { below($head, below!($($tail,)*)) };
}