implement fixed Split

This commit is contained in:
🪞👃🪞 2024-09-28 19:51:10 +03:00
parent e555074bdf
commit cd0b8a6812
4 changed files with 50 additions and 15 deletions

View file

@ -62,7 +62,8 @@ pub trait Number: Send + Sync + Copy
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16>
+ From<u16> + Into<u16>
+ Into<f64>
{
fn minus (self, other: Self) -> Self {
if self >= other {
@ -81,5 +82,6 @@ impl<T> Number for T where
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16>
+ From<u16> + Into<u16>
+ Into<f64>
{}

View file

@ -92,6 +92,15 @@ pub trait Area<N: Number>: Copy {
#[inline] fn clip (&self, wh: impl Size<N>) -> [N;4] {
[self.x(), self.y(), wh.w(), wh.h()]
}
#[inline] fn split_fixed (&self, direction: Direction, a: N) -> ([N;4],[N;4]) {
match direction {
Direction::Down => (
[self.x(), self.y(), self.w(), a],
[self.x(), self.y() + a, self.w(), self.h() - a],
),
_ => todo!(),
}
}
}
impl<N: Number> Area<N> for (N, N, N, N) {
@ -829,3 +838,33 @@ where
#[macro_export] macro_rules! row {
($($expr:expr),* $(,)?) => { Stack::right(move|add|{ $(add(&$expr)?;)* Ok(()) }) }
}
/// A binary split with fixed proportion
pub struct Split<E: Engine, A: Widget<Engine = E>, B: Widget<Engine = E>>(
pub Direction, pub E::Unit, A, B, PhantomData<E>
);
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())
}
}
impl<E: Engine, A: Widget<Engine = E>, B: Widget<Engine = E>> Widget for Split<E, A, B> {
type Engine = E;
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
Ok(Some(to))
}
fn render (&self, to: &mut E::Output) -> Usually<()> {
let (a, b) = to.area().split_fixed(self.0, self.1);
to.render_in(a.into(), &self.2)?;
to.render_in(b.into(), &self.3)?;
Ok(())
}
}
/// A scrollable area.
pub struct Scroll<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
>(pub F, pub Direction, pub u64, PhantomData<E>);