mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-09 05:06:43 +01:00
Offset -> Plus; add Minus
This commit is contained in:
parent
dc03a664a4
commit
00da7de142
5 changed files with 176 additions and 77 deletions
|
|
@ -351,6 +351,10 @@ impl Direction {
|
|||
pub enum Align<L> {
|
||||
/// Draw at center of container
|
||||
Center(L),
|
||||
/// Draw at center of X axis
|
||||
X(L),
|
||||
/// Draw at center of Y axis
|
||||
Y(L),
|
||||
/// Draw at upper left corner of contaier
|
||||
NW(L),
|
||||
/// Draw at center of upper edge of container
|
||||
|
|
@ -373,6 +377,8 @@ impl<T> Align<T> {
|
|||
pub fn inner (&self) -> &T {
|
||||
match self {
|
||||
Self::Center(inner) => inner,
|
||||
Self::X(inner) => inner,
|
||||
Self::Y(inner) => inner,
|
||||
Self::NW(inner) => inner,
|
||||
Self::N(inner) => inner,
|
||||
Self::NE(inner) => inner,
|
||||
|
|
@ -396,6 +402,18 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Align<T> {
|
|||
let result = [outer_area.x() + offset_x, outer_area.y() + offset_y, w, h];
|
||||
result.into()
|
||||
}),
|
||||
Self::X(_) => self.inner().layout(outer_area)?.map(|inner_area|{
|
||||
let [_, y, w, h] = inner_area.xywh();
|
||||
let offset_x = (outer_area.w() - w) / 2.into();
|
||||
let result = [outer_area.x() + offset_x, y, w, h];
|
||||
result.into()
|
||||
}),
|
||||
Self::Y(_) => self.inner().layout(outer_area)?.map(|inner_area|{
|
||||
let [x, _, w, h] = inner_area.xywh();
|
||||
let offset_y = (outer_area.h() / 2.into()) - (h / 2.into());
|
||||
let result = [x, outer_area.y() + offset_y, w, h];
|
||||
result.into()
|
||||
}),
|
||||
Self::NW(_) => { todo!() },
|
||||
Self::N(_) => { todo!() },
|
||||
Self::NE(_) => { todo!() },
|
||||
|
|
@ -583,7 +601,7 @@ impl<N: Number, T: Widget> Outset<N, T> {
|
|||
}
|
||||
|
||||
/// Move origin point of drawing area
|
||||
pub enum Offset<N: Number, T: Widget> {
|
||||
pub enum Plus<N: Number, T: Widget> {
|
||||
/// Move origin to the right
|
||||
X(N, T),
|
||||
/// Move origin downwards
|
||||
|
|
@ -592,7 +610,7 @@ pub enum Offset<N: Number, T: Widget> {
|
|||
XY(N, N, T),
|
||||
}
|
||||
|
||||
impl<N: Number, T: Widget> Offset<N, T> {
|
||||
impl<N: Number, T: Widget> Plus<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
|
|
@ -604,7 +622,7 @@ impl<N: Number, T: Widget> Offset<N, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
|
||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Plus<E::Unit, T> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
Ok(self.inner().layout(to)?.map(|to|match *self {
|
||||
|
|
@ -618,6 +636,42 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Move origin point of drawing area
|
||||
pub enum Minus<N: Number, T: Widget> {
|
||||
/// Move origin to the right
|
||||
X(N, T),
|
||||
/// Move origin downwards
|
||||
Y(N, T),
|
||||
/// Move origin to the right and downwards
|
||||
XY(N, N, T),
|
||||
}
|
||||
|
||||
impl<N: Number, T: Widget> Minus<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
fn x (&self) -> N {
|
||||
match self { Self::X(x, _) => *x, Self::Y(_, _) => N::default(), Self::XY(x, _, _) => *x }
|
||||
}
|
||||
fn y (&self) -> N {
|
||||
match self { Self::X(_, _) => N::default(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y }
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Minus<E::Unit, T> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
Ok(self.inner().layout(to)?.map(|to|match *self {
|
||||
Self::X(x, _) => [to.x().minus(x), to.y(), to.w(), to.h()],
|
||||
Self::Y(y, _) => [to.x(), to.y().minus(y), to.w(), to.h()],
|
||||
Self::XY(x, y, _) => [to.x().minus(x), to.y().minus(y), to.w(), to.h()]
|
||||
}.into()))
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
/// A component that may contain [Focusable] components.
|
||||
pub trait Focus <const N: usize, E: Engine>: Widget<Engine = E> + Handle<E> {
|
||||
fn focus (&self) -> usize;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue