mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-03 13:30:44 +02:00
230 lines
7.5 KiB
Rust
230 lines
7.5 KiB
Rust
use crate::*;
|
|
|
|
pub use self::spatial::*; mod spatial {
|
|
use crate::*;
|
|
|
|
/// A widget that tracks its rendered width and height.
|
|
///
|
|
/// ```
|
|
/// let measure = tengri::Measure::<u16>::default();
|
|
/// ```
|
|
#[derive(Default)] pub struct Measure<N: Coord> {
|
|
pub x: Arc<AtomicUsize>,
|
|
pub y: Arc<AtomicUsize>,
|
|
pub __: PhantomData<N>,
|
|
}
|
|
|
|
/// Increment X and/or Y coordinate.
|
|
///
|
|
/// ```
|
|
/// let pushed = tengri::Push::XY(2, 2, "Hello");
|
|
/// ```
|
|
pub enum Push<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
|
|
/// Decrement X and/or Y coordinate.
|
|
///
|
|
/// ```
|
|
/// let pulled = tengri::Pull::XY(2, 2, "Hello");
|
|
/// ```
|
|
pub enum Pull<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
|
|
/// Set the content to fill the container.
|
|
///
|
|
/// ```
|
|
/// let filled = tengri::Fill::XY("Hello");
|
|
/// ```
|
|
pub enum Fill<A> { X(A), Y(A), XY(A) }
|
|
|
|
/// Set fixed size for content.
|
|
///
|
|
/// ```
|
|
/// let fixed = tengri::Fixed::XY(3, 5, "Hello"); // 3x5
|
|
/// ```
|
|
pub enum Fixed<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
|
|
/// Decrease the width and/or height of the content.
|
|
///
|
|
/// ```
|
|
/// let shrunk = tengri::Shrink::XY(2, 0, "Hello"); // 1x1
|
|
/// ```
|
|
pub enum Shrink<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
|
|
/// Increaase the width and/or height of the content.
|
|
///
|
|
/// ```
|
|
/// let expanded = tengri::Expand::XY(5, 3, "HELLO"); // 15x3
|
|
/// ```
|
|
pub enum Expand<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
|
|
/// Align position of inner area to middle, side, or corner of outer area.
|
|
///
|
|
///
|
|
/// ```
|
|
/// use ::tengri::*;
|
|
/// let area = XYWH(10u16, 10, 20, 20);
|
|
/// fn test (area: XYWH<u16>, item: &impl Draw<Tui>, expected: [u16;4]) {
|
|
/// //assert_eq!(Lay::layout(item, area), expected);
|
|
/// //assert_eq!(Draw::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]);
|
|
/// ```
|
|
pub struct Align<T>(pub Alignment, pub T);
|
|
|
|
// TODO DOCUMENTME
|
|
pub enum Pad<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
|
|
// TODO DOCUMENTME
|
|
pub struct Bordered<S, W>(pub bool, pub S, pub W);
|
|
|
|
// TODO DOCUMENTME
|
|
pub struct Border<S>(pub bool, pub S);
|
|
|
|
/// TODO DOCUMENTME
|
|
///
|
|
/// ```
|
|
/// use tengri::{Bounded, XYWH};
|
|
/// let bounded: Bounded<tengri::Tui, _> = Bounded(0, 0 ,0 ,0 ,"");
|
|
/// ```
|
|
pub struct Bounded<N: Coord, D>(N, N, N, N, pub D);
|
|
|
|
/// Draws items from an iterator.
|
|
///
|
|
/// ```
|
|
/// // FIXME let map = tengri::Map(||[].iter(), |_|{});
|
|
/// ```
|
|
pub struct Map<O, A, B, I, F, G>
|
|
where
|
|
I: Iterator<Item = A> + Send + Sync,
|
|
F: Fn() -> I + Send + Sync,
|
|
{
|
|
/// Function that returns iterator over stacked components
|
|
pub get_iter: F,
|
|
/// Function that returns each stacked component
|
|
pub get_item: G,
|
|
|
|
pub __: PhantomData<(O, B)>,
|
|
}
|
|
/// A color in OKHSL and RGB representations.
|
|
#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct ItemColor {
|
|
pub okhsl: Okhsl<f32>,
|
|
#[cfg(feature = "tui")] pub rgb: Color,
|
|
}
|
|
/// A color in OKHSL and RGB with lighter and darker variants.
|
|
#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct ItemTheme {
|
|
pub base: ItemColor,
|
|
pub light: ItemColor,
|
|
pub lighter: ItemColor,
|
|
pub lightest: ItemColor,
|
|
pub dark: ItemColor,
|
|
pub darker: ItemColor,
|
|
pub darkest: ItemColor,
|
|
}
|
|
// TODO DOCUMENTME
|
|
pub struct FieldH<Theme, Label, Value>(pub Theme, pub Label, pub Value);
|
|
// TODO DOCUMENTME
|
|
pub struct FieldV<Theme, Label, Value>(pub Theme, pub Label, pub Value);
|
|
// TODO:
|
|
pub struct Field<C, T, U> {
|
|
pub direction: Direction,
|
|
pub label: Option<T>,
|
|
pub label_fg: Option<C>,
|
|
pub label_bg: Option<C>,
|
|
pub label_align: Option<Direction>,
|
|
pub value: Option<U>,
|
|
pub value_fg: Option<C>,
|
|
pub value_bg: Option<C>,
|
|
pub value_align: Option<Direction>,
|
|
}
|
|
}
|
|
:wqa
|
|
#[cfg(feature = "tui")] pub use self::terminal::*;
|
|
#[cfg(feature = "tui")] mod terminal {
|
|
use crate::*;
|
|
/// TUI input loop event.
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)]
|
|
pub struct TuiEvent(pub Event);
|
|
/// TUI key spec.
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)]
|
|
pub struct TuiKey(pub Option<KeyCode>, pub KeyModifiers);
|
|
/// TUI buffer sized by `usize` instead of `u16`.
|
|
#[derive(Default)] pub struct BigBuffer {
|
|
pub width: usize,
|
|
pub height: usize,
|
|
pub content: Vec<Cell>
|
|
}
|
|
pub struct Foreground<Color, Item>(pub Color, pub Item);
|
|
pub struct Background<Color, Item>(pub Color, pub Item);
|
|
pub struct Modify<T>(pub bool, pub Modifier, pub T);
|
|
pub struct Styled<T>(pub Option<Style>, pub T);
|
|
/// A cell that takes up 3 rows on its own,
|
|
/// but stacks, giving (N+1)*2 rows per N cells.
|
|
pub struct Phat<T> { pub width: u16, pub height: u16, pub content: T, pub colors: [Color;4], }
|
|
/// A three-column layout.
|
|
pub struct Tryptich<A, B, C> {
|
|
pub top: bool,
|
|
pub h: u16,
|
|
pub left: (u16, A),
|
|
pub middle: (u16, B),
|
|
pub right: (u16, C),
|
|
}
|
|
/// Repeat a string, e.g. for background fill
|
|
pub enum Repeat<'a> { X(&'a str), Y(&'a str), XY(&'a str) }
|
|
/// Scroll indicator
|
|
pub enum Scrollbar {
|
|
/// Horizontal scrollbar
|
|
X { offset: usize, length: usize, total: usize, },
|
|
/// Vertical scrollbar
|
|
Y { offset: usize, length: usize, total: usize, }
|
|
}
|
|
}
|
|
|
|
pub use self::textual::*;
|
|
mod textual {
|
|
/// Displays an owned [str]-like with fixed maximum width.
|
|
///
|
|
/// Width is computed using [unicode_width].
|
|
pub struct TrimString<T: AsRef<str>>(pub u16, pub T);
|
|
/// Displays a borrowed [str]-like with fixed maximum width
|
|
///
|
|
/// Width is computed using [unicode_width].
|
|
pub struct TrimStringRef<'a, T: AsRef<str>>(pub u16, pub &'a T);
|
|
}
|
|
|
|
#[cfg(feature = "jack")] pub use self::aural::*;
|
|
#[cfg(feature = "jack")] mod aural {
|
|
}
|
|
|
|
pub use self::temporal::*; mod temporal {
|
|
use crate::*;
|
|
|
|
/// Performance counter
|
|
#[derive(Debug)]
|
|
pub struct PerfModel {
|
|
pub clock: quanta::Clock,
|
|
/// Measurement has a small cost. Disable it here.
|
|
pub enabled: bool,
|
|
// In nanoseconds. Time used by last iteration.
|
|
pub used: AtomicF64,
|
|
// In microseconds. Max prescribed time for iteration (frame, chunk...).
|
|
pub window: AtomicF64,
|
|
}
|
|
}
|