Use new with_* naming scheme

All builder-like functions are now named with_*. There is also now a way
to set each property imperatively with only a mutable reference.

The only widgets I haven't yet converted to this style are the Join*
widgets; they're a bit harder to figure out an appropriate API for.
This commit is contained in:
Joscha 2023-02-20 15:53:49 +01:00
parent cb483431cc
commit 607c11fea4
11 changed files with 125 additions and 90 deletions

View file

@ -4,7 +4,7 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget};
#[derive(Debug, Clone, Copy)]
pub struct Float<I> {
inner: I,
pub inner: I,
horizontal: Option<f32>,
vertical: Option<f32>,
}
@ -18,49 +18,68 @@ impl<I> Float<I> {
}
}
pub fn horizontal(mut self, position: f32) -> Self {
assert!((0.0..=1.0).contains(&position));
self.horizontal = Some(position);
pub fn horizontal(&self) -> Option<f32> {
self.horizontal
}
pub fn set_horizontal(&mut self, position: Option<f32>) {
if let Some(position) = position {
assert!((0.0..=1.0).contains(&position));
}
self.horizontal = position;
}
pub fn vertical(&self) -> Option<f32> {
self.vertical
}
pub fn set_vertical(&mut self, position: Option<f32>) {
if let Some(position) = position {
assert!((0.0..=1.0).contains(&position));
}
self.vertical = position;
}
pub fn with_horizontal(mut self, position: f32) -> Self {
self.set_horizontal(Some(position));
self
}
pub fn vertical(mut self, position: f32) -> Self {
assert!((0.0..=1.0).contains(&position));
self.vertical = Some(position);
pub fn with_vertical(mut self, position: f32) -> Self {
self.set_vertical(Some(position));
self
}
pub fn all(self, position: f32) -> Self {
assert!((0.0..=1.0).contains(&position));
self.horizontal(position).vertical(position)
pub fn with_all(self, position: f32) -> Self {
self.with_horizontal(position).with_vertical(position)
}
pub fn left(self) -> Self {
self.horizontal(0.0)
pub fn with_left(self) -> Self {
self.with_horizontal(0.0)
}
pub fn right(self) -> Self {
self.horizontal(1.0)
pub fn with_right(self) -> Self {
self.with_horizontal(1.0)
}
pub fn top(self) -> Self {
self.vertical(0.0)
pub fn with_top(self) -> Self {
self.with_vertical(0.0)
}
pub fn bottom(self) -> Self {
self.vertical(1.0)
pub fn with_bottom(self) -> Self {
self.with_vertical(1.0)
}
pub fn center_h(self) -> Self {
self.horizontal(0.5)
pub fn with_center_h(self) -> Self {
self.with_horizontal(0.5)
}
pub fn center_v(self) -> Self {
self.vertical(0.5)
pub fn with_center_v(self) -> Self {
self.with_vertical(0.5)
}
pub fn center(self) -> Self {
self.all(0.5)
pub fn with_center(self) -> Self {
self.with_all(0.5)
}
fn push_inner(&self, frame: &mut Frame, size: Size, mut inner_size: Size) {