Skip to content

Commit

Permalink
Add macro for generating builders and add builders to most widgets
Browse files Browse the repository at this point in the history
Closes #133.
  • Loading branch information
LPGhatguy committed Jan 25, 2025
1 parent a092b6f commit 68fc2dd
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 38 deletions.
3 changes: 2 additions & 1 deletion crates/yakui-widgets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

mod ignore_debug;

pub mod util;

pub mod colors;
pub mod font;
pub mod shapes;
pub mod shorthand;
pub mod style;
pub mod text_renderer;
pub mod util;
pub mod widgets;

pub use self::shorthand::*;
Expand Down
19 changes: 19 additions & 0 deletions crates/yakui-widgets/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ where
let dom = context::dom();
dom.do_widget::<T>(props)
}

#[macro_export]
macro_rules! auto_builders {
(
$struct:ident {
$( $name:ident: $type:ty, )*
}
) => {
impl $struct {
$(
pub fn $name(self, $name: $type) -> Self {
Self { $name, ..self }
}
)*
}
};
}

pub use auto_builders;
12 changes: 11 additions & 1 deletion crates/yakui-widgets/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use yakui_core::input::MouseButton;
use yakui_core::widget::{EventContext, Widget};
use yakui_core::{Alignment, Response};

use crate::colors;
use crate::style::{TextAlignment, TextStyle};
use crate::util::widget;
use crate::widgets::Pad;
use crate::{auto_builders, colors};

use super::{RenderText, RoundRect};

Expand Down Expand Up @@ -38,6 +38,16 @@ pub struct Button {
pub down_style: DynamicButtonStyle,
}

auto_builders!(Button {
text: Cow<'static, str>,
alignment: Alignment,
padding: Pad,
border_radius: f32,
style: DynamicButtonStyle,
hover_style: DynamicButtonStyle,
down_style: DynamicButtonStyle,
});

/// Contains styles that can vary based on the state of the button.
#[derive(Debug, Clone)]
pub struct DynamicButtonStyle {
Expand Down
7 changes: 6 additions & 1 deletion crates/yakui-widgets/src/widgets/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use yakui_core::geometry::{Color, Constraints, Vec2};
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::Response;

use crate::shapes;
use crate::util::{widget, widget_children};
use crate::{auto_builders, shapes};

/**
A colored circle that can contain children.
Expand All @@ -17,6 +17,11 @@ pub struct Circle {
pub min_radius: f32,
}

auto_builders!(Circle {
color: Color,
min_radius: f32,
});

impl Circle {
pub fn new() -> Self {
Self {
Expand Down
6 changes: 6 additions & 0 deletions crates/yakui-widgets/src/widgets/colored_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use yakui_core::paint::PaintRect;
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::Response;

use crate::auto_builders;
use crate::util::{widget, widget_children};

/**
Expand All @@ -17,6 +18,11 @@ pub struct ColoredBox {
pub min_size: Vec2,
}

auto_builders!(ColoredBox {
color: Color,
min_size: Vec2,
});

impl ColoredBox {
pub fn empty() -> Self {
Self {
Expand Down
30 changes: 10 additions & 20 deletions crates/yakui-widgets/src/widgets/count_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use yakui_core::{
CrossAxisAlignment, Direction, MainAxisAlignItems, MainAxisAlignment, MainAxisSize, Response,
};

use crate::auto_builders;
use crate::util::widget_children;

/**
Expand Down Expand Up @@ -37,6 +38,15 @@ pub struct CountGrid {
pub cross_axis_alignment: CrossAxisAlignment,
}

auto_builders!(CountGrid {
direction: Direction,
cross_axis_count: usize,
main_axis_alignment: MainAxisAlignment,
main_axis_size: MainAxisSize,
main_axis_align_items: MainAxisAlignItems,
cross_axis_alignment: CrossAxisAlignment,
});

impl CountGrid {
/// The children will be laid out in a grid with the given number of columns.
/// They should be provided in row-major order.
Expand Down Expand Up @@ -64,26 +74,6 @@ impl CountGrid {
}
}

pub fn cross_axis_aligment(mut self, alignment: CrossAxisAlignment) -> Self {
self.cross_axis_alignment = alignment;
self
}

pub fn main_axis_aligment(mut self, alignment: MainAxisAlignment) -> Self {
self.main_axis_alignment = alignment;
self
}

pub fn main_axis_size(mut self, size: MainAxisSize) -> Self {
self.main_axis_size = size;
self
}

pub fn main_axis_align_items(mut self, items: MainAxisAlignItems) -> Self {
self.main_axis_align_items = items;
self
}

/// The children will be laid out in a grid with the given number of columns/rows.
/// They should be provided in cross-axis-major order.
/// For example, if you want a 2x3 column-based grid, you should provide the children in this order:
Expand Down
8 changes: 8 additions & 0 deletions crates/yakui-widgets/src/widgets/cutout.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::auto_builders;
use crate::shapes::RoundedRectangle;
use yakui_core::geometry::{Color, Constraints, Rect, Vec2};
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
Expand All @@ -21,6 +22,13 @@ pub struct CutOut {
pub radius: f32,
}

auto_builders!(CutOut {
image_color: Color,
overlay_color: Color,
min_size: Vec2,
radius: f32,
});

impl CutOut {
pub fn new<I>(image: I, overlay_color: Color) -> Self
where
Expand Down
10 changes: 10 additions & 0 deletions crates/yakui-widgets/src/widgets/divider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use yakui_core::paint::PaintRect;
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::Response;

use crate::auto_builders;

/// A horizontal divider line. Will take up the whole width of the parent.
///
/// Responds with [DividerResponse].
Expand All @@ -22,6 +24,14 @@ pub struct Divider {
pub end_indent: f32,
}

auto_builders!(Divider {
color: Color,
thickness: f32,
height: f32,
indent: f32,
end_indent: f32,
});

impl Divider {
pub fn new(color: Color, height: f32, thickness: f32) -> Self {
Self {
Expand Down
15 changes: 7 additions & 8 deletions crates/yakui-widgets/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use yakui_core::paint::PaintRect;
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::{Response, TextureId};

use crate::auto_builders;
use crate::util::widget;

/**
Expand All @@ -19,6 +20,12 @@ pub struct Image {
pub fit_mode: ImageFit,
}

auto_builders!(Image {
size: Vec2,
color: Color,
fit_mode: ImageFit,
});

#[derive(Debug, Clone, Copy)]
pub enum ImageFit {
Stretch,
Expand All @@ -41,14 +48,6 @@ impl Image {
pub fn show(self) -> Response<ImageResponse> {
widget::<ImageWidget>(self)
}

pub fn color(self, color: Color) -> Self {
Self { color, ..self }
}

pub fn fit_mode(self, fit_mode: ImageFit) -> Self {
Self { fit_mode, ..self }
}
}

#[derive(Debug)]
Expand Down
8 changes: 8 additions & 0 deletions crates/yakui-widgets/src/widgets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use yakui_core::geometry::{Constraints, FlexFit, Vec2};
use yakui_core::widget::{LayoutContext, Widget};
use yakui_core::{CrossAxisAlignment, Direction, Flow, MainAxisAlignment, MainAxisSize, Response};

use crate::auto_builders;
use crate::util::widget_children;

/**
Expand Down Expand Up @@ -36,6 +37,13 @@ pub struct List {
pub cross_axis_alignment: CrossAxisAlignment,
}

auto_builders!(List {
item_spacing: f32,
main_axis_size: MainAxisSize,
main_axis_alignment: MainAxisAlignment,
cross_axis_alignment: CrossAxisAlignment,
});

impl List {
pub fn new(direction: Direction) -> Self {
Self {
Expand Down
14 changes: 11 additions & 3 deletions crates/yakui-widgets/src/widgets/nineslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ use yakui_core::{
ManagedTextureId, Response,
};

use crate::{shorthand::pad, util::widget_children, widgets::pad::Pad};
use crate::{auto_builders, shorthand::pad, util::widget_children, widgets::pad::Pad};

#[derive(Debug)]
#[must_use = "yakui widgets do nothing if you don't `show` them"]
pub struct NineSlice {
texture: ManagedTextureId,
pub texture: ManagedTextureId,

/// Texture margins in pixels around the central NineSlice region, before
/// scaling.
pub margins: Pad,

pub scale: f32,
}

auto_builders!(NineSlice {
texture: ManagedTextureId,
margins: Pad,
scale: f32,
}
});

impl NineSlice {
pub fn new(texture: ManagedTextureId, margins: Pad, scale: f32) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion crates/yakui-widgets/src/widgets/round_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use yakui_core::geometry::{Color, Constraints, Vec2};
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::Response;

use crate::shapes;
use crate::util::{widget, widget_children};
use crate::{auto_builders, shapes};

/**
A colored box with rounded corners that can contain children.
Expand All @@ -18,6 +18,12 @@ pub struct RoundRect {
pub min_size: Vec2,
}

auto_builders!(RoundRect {
radius: f32,
color: Color,
min_size: Vec2,
});

impl RoundRect {
pub fn new(radius: f32) -> Self {
Self {
Expand Down
9 changes: 8 additions & 1 deletion crates/yakui-widgets/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use yakui_core::geometry::{Color, Constraints, Rect, Vec2};
use yakui_core::widget::{LayoutContext, PaintContext, Widget};
use yakui_core::Response;

use crate::{colored_circle, colors, draggable, util};
use crate::{auto_builders, colored_circle, colors, draggable, util};

use crate::colored_box;

Expand All @@ -25,6 +25,13 @@ pub struct Slider {
pub step: Option<f64>,
}

auto_builders!(Slider {
value: f64,
min: f64,
max: f64,
step: Option<f64>,
});

impl Slider {
pub fn new(value: f64, min: f64, max: f64) -> Self {
Slider {
Expand Down
7 changes: 6 additions & 1 deletion crates/yakui-widgets/src/widgets/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::borrow::Cow;
use yakui_core::widget::Widget;
use yakui_core::Response;

use crate::pad;
use crate::style::TextStyle;
use crate::util::widget;
use crate::{auto_builders, pad};

use super::{Pad, RenderText};

Expand Down Expand Up @@ -35,6 +35,11 @@ pub struct Text {
pub padding: Pad,
}

auto_builders!(Text {
style: TextStyle,
padding: Pad,
});

impl Text {
pub fn new<S: Into<Cow<'static, str>>>(font_size: f32, text: S) -> Self {
let mut style = TextStyle::label();
Expand Down
15 changes: 14 additions & 1 deletion crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::font::Fonts;
use crate::shapes::{self, RoundedRectangle};
use crate::style::{TextAlignment, TextStyle};
use crate::util::widget;
use crate::{colors, pad};
use crate::{auto_builders, colors, pad};

use super::{Pad, RenderText};

Expand Down Expand Up @@ -44,6 +44,19 @@ pub struct TextBox {
pub placeholder: String,
}

auto_builders!(TextBox {
style: TextStyle,
padding: Pad,
fill: Option<Color>,
radius: f32,
inline_edit: bool,
multiline: bool,
selection_halo_color: Color,
selected_bg_color: Color,
cursor_color: Color,
placeholder: String,
});

impl TextBox {
pub fn new<S: Into<String>>(text: S) -> Self {
let mut style = TextStyle::label();
Expand Down

0 comments on commit 68fc2dd

Please sign in to comment.