Make naming scheme more consistent
Constructor functions that set/overwrite a value are called with_*, functions that add a value are called and_*.
This commit is contained in:
parent
0f21c3701e
commit
638a449343
7 changed files with 144 additions and 170 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -864,6 +864,12 @@ dependencies = [
|
||||||
"windows-targets 0.48.5",
|
"windows-targets 0.48.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "paste"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.3.1"
|
version = "2.3.1"
|
||||||
|
|
@ -1167,6 +1173,7 @@ dependencies = [
|
||||||
"cosmic-text",
|
"cosmic-text",
|
||||||
"image",
|
"image",
|
||||||
"palette",
|
"palette",
|
||||||
|
"paste",
|
||||||
"showbits-assets",
|
"showbits-assets",
|
||||||
"taffy",
|
"taffy",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ anyhow.workspace = true
|
||||||
cosmic-text.workspace = true
|
cosmic-text.workspace = true
|
||||||
image.workspace = true
|
image.workspace = true
|
||||||
palette.workspace = true
|
palette.workspace = true
|
||||||
|
paste = "1.0.14"
|
||||||
showbits-assets.workspace = true
|
showbits-assets.workspace = true
|
||||||
taffy.workspace = true
|
taffy.workspace = true
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use paste::paste;
|
||||||
use taffy::{
|
use taffy::{
|
||||||
AlignContent, AlignItems, AlignSelf, Dimension, Display, FlexDirection, FlexWrap, GridAutoFlow,
|
AlignContent, AlignItems, AlignSelf, Dimension, Display, FlexDirection, FlexWrap, GridAutoFlow,
|
||||||
GridPlacement, JustifyContent, LengthPercentage, LengthPercentageAuto, Line, NodeId,
|
GridPlacement, JustifyContent, LengthPercentage, LengthPercentageAuto, Line, NodeId,
|
||||||
|
|
@ -22,12 +23,12 @@ impl<C> Node<C> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn child(mut self, node: NodeId) -> Self {
|
pub fn and_child(mut self, node: NodeId) -> Self {
|
||||||
self.children.push(node);
|
self.children.push(node);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn widget<W: Widget<C> + 'static>(mut self, widget: W) -> Self {
|
pub fn with_widget<W: Widget<C> + 'static>(mut self, widget: W) -> Self {
|
||||||
self.widget = Some(Box::new(widget));
|
self.widget = Some(Box::new(widget));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
@ -44,212 +45,177 @@ impl<C> Node<C> {
|
||||||
|
|
||||||
macro_rules! layout_setter {
|
macro_rules! layout_setter {
|
||||||
( $name:ident : $type:ty ) => {
|
( $name:ident : $type:ty ) => {
|
||||||
pub fn $name(mut self, $name: $type) -> Self {
|
paste! {
|
||||||
self.layout.$name = $name;
|
pub fn [<with_ $name>](mut self, $name: $type) -> Self {
|
||||||
self
|
self.layout.$name = $name;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! layout_setter_point {
|
macro_rules! layout_setter_point {
|
||||||
(
|
( $name:ident : Point<$type:ty> ) => {
|
||||||
$name:ident : Point<$type:ty>
|
paste! {
|
||||||
as $name_x:ident, $name_y:ident
|
pub fn [<with_ $name>](mut self, $name: Point<$type>) -> Self {
|
||||||
as $name_all:ident
|
self.layout.$name = $name;
|
||||||
) => {
|
self
|
||||||
pub fn $name_x(mut self, x: $type) -> Self {
|
}
|
||||||
self.layout.$name.x = x;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn $name_y(mut self, y: $type) -> Self {
|
pub fn [<with_ $name _x>](mut self, x: $type) -> Self {
|
||||||
self.layout.$name.y = y;
|
self.layout.$name.x = x;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_all(mut self, all: $type) -> Self {
|
pub fn [<with_ $name _y>](mut self, y: $type) -> Self {
|
||||||
self.layout.$name.x = all;
|
self.layout.$name.y = y;
|
||||||
self.layout.$name.y = all;
|
self
|
||||||
self
|
}
|
||||||
|
|
||||||
|
pub fn [<with_ $name _all>](mut self, all: $type) -> Self {
|
||||||
|
self.layout.$name.x = all;
|
||||||
|
self.layout.$name.y = all;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! layout_setter_size {
|
macro_rules! layout_setter_size {
|
||||||
(
|
( $name:ident : Size<$type:ty> ) => {
|
||||||
$name:ident : Size<$type:ty>
|
paste! {
|
||||||
as $name_width:ident, $name_height:ident
|
pub fn [<with_ $name>](mut self, $name: Size<$type>) -> Self {
|
||||||
as $name_all:ident
|
self.layout.$name = $name;
|
||||||
) => {
|
self
|
||||||
pub fn $name_width(mut self, width: $type) -> Self {
|
}
|
||||||
self.layout.$name.width = width;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn $name_height(mut self, height: $type) -> Self {
|
pub fn [<with_ $name _width>](mut self, width: $type) -> Self {
|
||||||
self.layout.$name.height = height;
|
self.layout.$name.width = width;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_all(mut self, all: $type) -> Self {
|
pub fn [<with_ $name _height>](mut self, height: $type) -> Self {
|
||||||
self.layout.$name.width = all;
|
self.layout.$name.height = height;
|
||||||
self.layout.$name.height = all;
|
self
|
||||||
self
|
}
|
||||||
|
|
||||||
|
pub fn [<with_ $name _all>](mut self, all: $type) -> Self {
|
||||||
|
self.layout.$name.width = all;
|
||||||
|
self.layout.$name.height = all;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! layout_setter_line {
|
macro_rules! layout_setter_line {
|
||||||
(
|
( $name:ident : Line<$type:ty> ) => {
|
||||||
$name:ident : Line<$type:ty>
|
paste! {
|
||||||
as $name_start:ident, $name_end:ident
|
pub fn [<with_ $name>](mut self, $name: Line<$type>) -> Self {
|
||||||
as $name_all:ident
|
self.layout.$name = $name;
|
||||||
) => {
|
self
|
||||||
pub fn $name_start(mut self, start: $type) -> Self {
|
}
|
||||||
self.layout.$name.start = start;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn $name_end(mut self, end: $type) -> Self {
|
pub fn [<with_ $name _start>](mut self, start: $type) -> Self {
|
||||||
self.layout.$name.end = end;
|
self.layout.$name.start = start;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_all(mut self, all: $type) -> Self {
|
pub fn [<with_ $name _end>](mut self, end: $type) -> Self {
|
||||||
self.layout.$name.start = all;
|
self.layout.$name.end = end;
|
||||||
self.layout.$name.end = all;
|
self
|
||||||
self
|
}
|
||||||
|
|
||||||
|
pub fn [<with_ $name _all>](mut self, all: $type) -> Self {
|
||||||
|
self.layout.$name.start = all;
|
||||||
|
self.layout.$name.end = all;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! layout_setter_rect {
|
macro_rules! layout_setter_rect {
|
||||||
(
|
( $name:ident : Rect<$type:ty> ) => {
|
||||||
$name:ident : Rect<$type:ty>
|
paste! {
|
||||||
as $name_left:ident, $name_right:ident, $name_top:ident, $name_bottom:ident
|
pub fn [<with_ $name>](mut self, $name: Rect<$type>) -> Self {
|
||||||
as $name_horiz:ident, $name_vert:ident, $name_all:ident
|
self.layout.$name = $name;
|
||||||
) => {
|
self
|
||||||
pub fn $name_left(mut self, left: $type) -> Self {
|
}
|
||||||
self.layout.$name.left = left;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn $name_right(mut self, right: $type) -> Self {
|
pub fn [<with_ $name _left>](mut self, left: $type) -> Self {
|
||||||
self.layout.$name.right = right;
|
self.layout.$name.left = left;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_top(mut self, top: $type) -> Self {
|
pub fn [<with_ $name _right>](mut self, right: $type) -> Self {
|
||||||
self.layout.$name.top = top;
|
self.layout.$name.right = right;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_bottom(mut self, left: $type) -> Self {
|
pub fn [<with_ $name _top>](mut self, top: $type) -> Self {
|
||||||
self.layout.$name.bottom = left;
|
self.layout.$name.top = top;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_horiz(mut self, horizontal: $type) -> Self {
|
pub fn [<with_ $name _bottom>](mut self, left: $type) -> Self {
|
||||||
self.layout.$name.left = horizontal;
|
self.layout.$name.bottom = left;
|
||||||
self.layout.$name.right = horizontal;
|
self
|
||||||
self
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn $name_vert(mut self, vertical: $type) -> Self {
|
pub fn [<with_ $name _horiz>](mut self, horizontal: $type) -> Self {
|
||||||
self.layout.$name.top = vertical;
|
self.layout.$name.left = horizontal;
|
||||||
self.layout.$name.bottom = vertical;
|
self.layout.$name.right = horizontal;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn $name_all(mut self, all: $type) -> Self {
|
pub fn [<with_ $name _vert>](mut self, vertical: $type) -> Self {
|
||||||
self.layout.$name.left = all;
|
self.layout.$name.top = vertical;
|
||||||
self.layout.$name.right = all;
|
self.layout.$name.bottom = vertical;
|
||||||
self.layout.$name.top = all;
|
self
|
||||||
self.layout.$name.bottom = all;
|
}
|
||||||
self
|
|
||||||
|
pub fn [<with_ $name _all>](mut self, all: $type) -> Self {
|
||||||
|
self.layout.$name.left = all;
|
||||||
|
self.layout.$name.right = all;
|
||||||
|
self.layout.$name.top = all;
|
||||||
|
self.layout.$name.bottom = all;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C> Node<C> {
|
impl<C> Node<C> {
|
||||||
layout_setter!(display: Display);
|
layout_setter!(display: Display);
|
||||||
|
layout_setter_point!(overflow: Point<Overflow>);
|
||||||
layout_setter!(overflow: Point<Overflow>);
|
|
||||||
layout_setter_point!(overflow: Point<Overflow>
|
|
||||||
as overflow_x, overflow_y
|
|
||||||
as overflow_all);
|
|
||||||
|
|
||||||
layout_setter!(scrollbar_width: f32);
|
layout_setter!(scrollbar_width: f32);
|
||||||
layout_setter!(position: Position);
|
layout_setter!(position: Position);
|
||||||
|
layout_setter_rect!(inset: Rect<LengthPercentageAuto>);
|
||||||
layout_setter!(inset: Rect<LengthPercentageAuto>);
|
layout_setter_size!(size: Size<Dimension>);
|
||||||
layout_setter_rect!(inset: Rect<LengthPercentageAuto>
|
layout_setter_size!(min_size: Size<Dimension>);
|
||||||
as inset_left, inset_right, inset_top, inset_bottom
|
layout_setter_size!(max_size: Size<Dimension>);
|
||||||
as inset_horiz, inset_vert, inset_all);
|
|
||||||
|
|
||||||
layout_setter!(size: Size<Dimension>);
|
|
||||||
layout_setter_size!(size: Size<Dimension>
|
|
||||||
as size_width, size_height
|
|
||||||
as size_all);
|
|
||||||
|
|
||||||
layout_setter!(min_size: Size<Dimension>);
|
|
||||||
layout_setter_size!(min_size: Size<Dimension>
|
|
||||||
as min_size_width, min_size_height
|
|
||||||
as min_size_all);
|
|
||||||
|
|
||||||
layout_setter!(max_size: Size<Dimension>);
|
|
||||||
layout_setter_size!(max_size: Size<Dimension>
|
|
||||||
as max_size_width, max_size_height
|
|
||||||
as max_size_all);
|
|
||||||
|
|
||||||
layout_setter!(aspect_ratio: Option<f32>);
|
layout_setter!(aspect_ratio: Option<f32>);
|
||||||
|
layout_setter_rect!(margin: Rect<LengthPercentageAuto>);
|
||||||
layout_setter!(margin: Rect<LengthPercentageAuto>);
|
layout_setter_rect!(padding: Rect<LengthPercentage>);
|
||||||
layout_setter_rect!(margin: Rect<LengthPercentageAuto>
|
layout_setter_rect!(border: Rect<LengthPercentage>);
|
||||||
as margin_left, margin_right, margin_top, margin_bottom
|
|
||||||
as margin_horiz, margin_vert, margin_all);
|
|
||||||
|
|
||||||
layout_setter!(padding: Rect<LengthPercentage>);
|
|
||||||
layout_setter_rect!(padding: Rect<LengthPercentage>
|
|
||||||
as padding_left, padding_right, padding_top, padding_bottom
|
|
||||||
as padding_horiz, padding_vert, padding_all);
|
|
||||||
|
|
||||||
layout_setter!(border: Rect<LengthPercentage>);
|
|
||||||
layout_setter_rect!(border: Rect<LengthPercentage>
|
|
||||||
as border_left, border_right, border_top, border_bottom
|
|
||||||
as border_horiz, border_vert, border_all);
|
|
||||||
|
|
||||||
layout_setter!(align_items: Option<AlignItems>);
|
layout_setter!(align_items: Option<AlignItems>);
|
||||||
layout_setter!(align_self: Option<AlignSelf>);
|
layout_setter!(align_self: Option<AlignSelf>);
|
||||||
layout_setter!(justify_items: Option<AlignItems>);
|
layout_setter!(justify_items: Option<AlignItems>);
|
||||||
layout_setter!(justify_self: Option<AlignSelf>);
|
layout_setter!(justify_self: Option<AlignSelf>);
|
||||||
layout_setter!(align_content: Option<AlignContent>);
|
layout_setter!(align_content: Option<AlignContent>);
|
||||||
layout_setter!(justify_content: Option<JustifyContent>);
|
layout_setter!(justify_content: Option<JustifyContent>);
|
||||||
|
layout_setter_size!(gap: Size<LengthPercentage>);
|
||||||
layout_setter!(gap: Size<LengthPercentage>);
|
|
||||||
layout_setter_size!(gap: Size<LengthPercentage>
|
|
||||||
as gap_width, gap_height
|
|
||||||
as gap_all);
|
|
||||||
|
|
||||||
layout_setter!(flex_direction: FlexDirection);
|
layout_setter!(flex_direction: FlexDirection);
|
||||||
layout_setter!(flex_wrap: FlexWrap);
|
layout_setter!(flex_wrap: FlexWrap);
|
||||||
layout_setter!(flex_basis: Dimension);
|
layout_setter!(flex_basis: Dimension);
|
||||||
layout_setter!(flex_grow: f32);
|
layout_setter!(flex_grow: f32);
|
||||||
layout_setter!(flex_shrink: f32);
|
layout_setter!(flex_shrink: f32);
|
||||||
|
|
||||||
layout_setter!(grid_template_rows: Vec<TrackSizingFunction>);
|
layout_setter!(grid_template_rows: Vec<TrackSizingFunction>);
|
||||||
layout_setter!(grid_template_columns: Vec<TrackSizingFunction>);
|
layout_setter!(grid_template_columns: Vec<TrackSizingFunction>);
|
||||||
layout_setter!(grid_auto_rows: Vec<NonRepeatedTrackSizingFunction>);
|
layout_setter!(grid_auto_rows: Vec<NonRepeatedTrackSizingFunction>);
|
||||||
layout_setter!(grid_auto_columns: Vec<NonRepeatedTrackSizingFunction>);
|
layout_setter!(grid_auto_columns: Vec<NonRepeatedTrackSizingFunction>);
|
||||||
layout_setter!(grid_auto_flow: GridAutoFlow);
|
layout_setter!(grid_auto_flow: GridAutoFlow);
|
||||||
|
layout_setter_line!(grid_row: Line<GridPlacement>);
|
||||||
layout_setter!(grid_row: Line<GridPlacement>);
|
layout_setter_line!(grid_column: Line<GridPlacement>);
|
||||||
layout_setter_line!(grid_row: Line<GridPlacement>
|
|
||||||
as grid_row_start, grid_row_end
|
|
||||||
as grid_row_all);
|
|
||||||
|
|
||||||
layout_setter!(grid_column: Line<GridPlacement>);
|
|
||||||
layout_setter_line!(grid_column: Line<GridPlacement>
|
|
||||||
as grid_column_start, grid_column_end
|
|
||||||
as grid_column_all);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,6 @@ pub trait WidgetExt<C> {
|
||||||
|
|
||||||
impl<C, W: Widget<C> + 'static> WidgetExt<C> for W {
|
impl<C, W: Widget<C> + 'static> WidgetExt<C> for W {
|
||||||
fn node(self) -> Node<C> {
|
fn node(self) -> Node<C> {
|
||||||
Node::empty().widget(self)
|
Node::empty().with_widget(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,12 @@ impl Block {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn border(mut self, color: Srgba) -> Self {
|
pub fn with_border(mut self, color: Srgba) -> Self {
|
||||||
self.border = color;
|
self.border = color;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn background(mut self, color: Srgba) -> Self {
|
pub fn with_background(mut self, color: Srgba) -> Self {
|
||||||
self.background = color;
|
self.background = color;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -96,19 +96,19 @@ impl Text {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn chunk_plain<S: ToString>(mut self, text: S) -> Self {
|
pub fn and_plain<S: ToString>(mut self, text: S) -> Self {
|
||||||
let chunk = (self.default_attrs.clone(), text.to_string());
|
let chunk = (self.default_attrs.clone(), text.to_string());
|
||||||
self.chunks.push(chunk);
|
self.chunks.push(chunk);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn chunk_rich<S: ToString>(mut self, attrs: Attrs<'_>, text: S) -> Self {
|
pub fn and_rich<S: ToString>(mut self, attrs: Attrs<'_>, text: S) -> Self {
|
||||||
let chunk = (AttrsOwned::new(attrs), text.to_string());
|
let chunk = (AttrsOwned::new(attrs), text.to_string());
|
||||||
self.chunks.push(chunk);
|
self.chunks.push(chunk);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn chunks<I>(mut self, chunks: I) -> Self
|
pub fn and_chunks<I>(mut self, chunks: I) -> Self
|
||||||
where
|
where
|
||||||
I: IntoIterator<Item = (AttrsOwned, String)>,
|
I: IntoIterator<Item = (AttrsOwned, String)>,
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -69,28 +69,28 @@ impl Drawer {
|
||||||
let mut tree = Tree::<Context>::new(Srgba::new(1.0, 1.0, 1.0, 1.0));
|
let mut tree = Tree::<Context>::new(Srgba::new(1.0, 1.0, 1.0, 1.0));
|
||||||
|
|
||||||
let text = Text::new()
|
let text = Text::new()
|
||||||
.chunk_plain("Hello\nworld!")
|
|
||||||
.with_metrics(Text::default_metrics().scale(2.0))
|
.with_metrics(Text::default_metrics().scale(2.0))
|
||||||
|
.and_plain("Hello\nworld!")
|
||||||
.widget(&mut self.ctx.font_stuff)
|
.widget(&mut self.ctx.font_stuff)
|
||||||
.node()
|
.node()
|
||||||
.margin_horiz(length(8.0))
|
.with_margin_horiz(length(8.0))
|
||||||
.margin_vert(length(2.0))
|
.with_margin_vert(length(2.0))
|
||||||
.register(&mut tree)?;
|
.register(&mut tree)?;
|
||||||
|
|
||||||
let wrap = Block::new()
|
let wrap = Block::new()
|
||||||
.border(color::BLACK)
|
.with_border(color::BLACK)
|
||||||
.node()
|
.node()
|
||||||
.border_all(length(2.0))
|
.with_border_all(length(2.0))
|
||||||
.child(text)
|
.and_child(text)
|
||||||
.register(&mut tree)?;
|
.register(&mut tree)?;
|
||||||
|
|
||||||
let root = Block::new()
|
let root = Block::new()
|
||||||
.border(color::BLACK)
|
.with_border(color::BLACK)
|
||||||
.node()
|
.node()
|
||||||
.size_width(percent(1.0))
|
.with_size_width(percent(1.0))
|
||||||
.border_all(length(2.0))
|
.with_border_all(length(2.0))
|
||||||
.padding_all(length(10.0))
|
.with_padding_all(length(10.0))
|
||||||
.child(wrap)
|
.and_child(wrap)
|
||||||
.register(&mut tree)?;
|
.register(&mut tree)?;
|
||||||
|
|
||||||
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
|
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue