add widget dynamic helper and Split::up

This commit is contained in:
🪞👃🪞 2024-10-18 23:51:58 +03:00
parent 038451e2f4
commit 97af45b576
4 changed files with 47 additions and 17 deletions

View file

@ -42,6 +42,10 @@ pub trait Output<E: Engine> {
/// Render widget in area
fn render_in (&mut self, area: E::Area, widget: &dyn Widget<Engine = E>) -> Usually<()>;
}
/// Cast to dynamic pointer
pub fn widget <E: Engine, T: Widget<Engine = E>> (w: &T) -> &dyn Widget<Engine = E> {
w as &dyn Widget<Engine = E>
}
/// A renderable component
pub trait Widget: Send + Sync {
/// Engine for which this component is implemented

View file

@ -94,6 +94,10 @@ pub trait Area<N: Number>: Copy {
}
#[inline] fn split_fixed (&self, direction: Direction, a: N) -> ([N;4],[N;4]) {
match direction {
Direction::Up => (
[self.x(), self.y() + self.h() - a, self.w(), a],
[self.x(), self.y(), self.w(), self.h() - a],
),
Direction::Down => (
[self.x(), self.y(), self.w(), a],
[self.x(), self.y() + a, self.w(), self.h() - a],
@ -817,6 +821,18 @@ impl<E: Engine, A: Widget<Engine = E>, B: Widget<Engine = E>> Split<E, A, B> {
pub fn new (direction: Direction, proportion: E::Unit, a: A, b: B) -> Self {
Self(direction, proportion, a, b, Default::default())
}
pub fn up (proportion: E::Unit, a: A, b: B) -> Self {
Self(Direction::Up, proportion, a, b, Default::default())
}
pub fn down (proportion: E::Unit, a: A, b: B) -> Self {
Self(Direction::Down, proportion, a, b, Default::default())
}
pub fn left (proportion: E::Unit, a: A, b: B) -> Self {
Self(Direction::Left, proportion, a, b, Default::default())
}
pub fn right (proportion: E::Unit, a: A, b: B) -> Self {
Self(Direction::Right, proportion, a, b, Default::default())
}
}
impl<E: Engine, A: Widget<Engine = E>, B: Widget<Engine = E>> Widget for Split<E, A, B> {