frame_rect macro

This commit is contained in:
griffi-gh 2024-03-24 02:05:28 +01:00
parent 91668e575c
commit 9ba0a7e762
2 changed files with 84 additions and 5 deletions

View file

@ -1,6 +1,17 @@
use glam::vec4;
use hui::{
color, element::{container::Container, progress_bar::ProgressBar, text::Text, UiElementExt}, frame::FrameRect, layout::Alignment, rect::Corners, size, text::FontHandle
size, frame_rect,
color,
element::{
container::Container,
progress_bar::ProgressBar,
text::Text,
UiElementExt
},
frame::FrameRect,
layout::Alignment,
rect::Corners,
text::FontHandle
};
#[path = "../boilerplate.rs"]
@ -59,10 +70,10 @@ ui_main!(
.with_align((Alignment::Center, Alignment::Begin))
.with_padding(15.)
.with_gap(10.)
.with_background(
FrameRect::color((0., 0., 0., 0.5))
.with_corner_radius(8.)
)
.with_background(frame_rect! {
color: (0., 0., 0., 0.5),
corner_radius: 8.,
})
.with_children(|ui| {
Text::default()
.with_text("Did you know?")

View file

@ -60,3 +60,71 @@ macro_rules! size {
}
};
}
/// Helper macro for constructing a `FrameRect`
#[macro_export]
macro_rules! frame_rect {
() => {
$crate::frame::FrameRect::default()
};
($expr:expr) => {
{
let _frame_rect: $crate::frame::FrameRect = $crate::frame::FrameRect::from($expr);
_frame_rect
}
};
($image:expr, $color:expr) => {
$crate::frame::FrameRect::color_image($color, $image)
};
{$($ident:ident : $expr:expr),+$(,)?} => {
{
// ensure all identifiers are unique
#[allow(non_upper_case_globals)]
{$(const $ident: () = ();)+}
// construct the FrameRect
{
let mut frame_rect = $crate::frame::FrameRect::default();
$(
let $ident = ($expr).into();
frame_rect.$ident = $ident;
)+
frame_rect
}
}
};
// {$from:expr, $($ident:ident : $expr:expr),+$(,)?} => {
// {
// // ensure all identifiers are unique
// #[allow(non_upper_case_globals)]
// {
// $(
// const $ident: () = ();
// )+
// }
// // construct the FrameRect
// {
// let mut _frame_rect: $crate::frame::FrameRect = ($from).into();
// $(
// let $ident = ($expr).into();
// _frame_rect.$ident = $ident;
// )+
// _frame_rect
// }
// }
// };
}
// #[allow(unused)]
// fn test() {
// // let _ = frame_rect!(5, 6);
// let _ = frame_rect! {
// color: (0.2, 0.2, 0.3, 1.),
// corner_radius: 5.,
// };
// }