From 4c26a1297ff5ef611ac724df0675d915c36f8b47 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 6 Mar 2024 17:19:35 +0100 Subject: [PATCH] add `should_wrap` flag --- hui/src/element.rs | 10 ++++++---- hui/src/element/builtin/container.rs | 6 +++++- hui/src/element/builtin/fill_rect.rs | 7 +++++-- hui/src/element/builtin/progress_bar.rs | 5 ++++- hui/src/element/builtin/spacer.rs | 7 +++++-- hui/src/element/builtin/text.rs | 7 +++++-- hui/src/element/builtin/transformer.rs | 4 ++++ hui/src/measure.rs | 11 +++++++++++ 8 files changed, 45 insertions(+), 12 deletions(-) diff --git a/hui/src/element.rs b/hui/src/element.rs index bb5b0a0..aa6017d 100644 --- a/hui/src/element.rs +++ b/hui/src/element.rs @@ -33,7 +33,7 @@ pub struct ProcessContext<'a> { pub trait UiElement { /// Get the name of the element, for example "Button" or "ProgressBar" - fn name(&self) -> &'static str { "UiElement" } + fn name(&self) -> &'static str; /// Get the unique id used for internal state management\ /// This value must be unique for each instance of the element @@ -41,11 +41,13 @@ pub trait UiElement { /// If the element is stateless, this function should return `None` fn state_id(&self) -> Option { None } - /// Check if the element has state + /// Check if the element has state.\ + /// Should not be overridden fn is_stateful(&self) -> bool { self.state_id().is_some() } - /// Check if the element has no state - fn is_stateless(&self) -> bool { self.state_id().is_none() } + /// Check if the element has no state\ + /// Should not be overridden + fn is_stateless(&self) -> bool { !self.is_stateful() } /// Initialize the state of the element\ /// This function should be called exactly once during the lifetime of the element, diff --git a/hui/src/element/builtin/container.rs b/hui/src/element/builtin/container.rs index 9da731e..932576c 100644 --- a/hui/src/element/builtin/container.rs +++ b/hui/src/element/builtin/container.rs @@ -103,6 +103,10 @@ impl Container { } impl UiElement for Container { + fn name(&self) -> &'static str { + "Container" + } + fn measure(&self, ctx: MeasureContext) -> Response { let mut size = Vec2::ZERO; let mut leftover_gap = Vec2::ZERO; @@ -156,7 +160,7 @@ impl UiElement for Container { inner_content_size, ..Default::default() }, - user_data: None + ..Default::default() } } diff --git a/hui/src/element/builtin/fill_rect.rs b/hui/src/element/builtin/fill_rect.rs index 623727b..6703736 100644 --- a/hui/src/element/builtin/fill_rect.rs +++ b/hui/src/element/builtin/fill_rect.rs @@ -40,6 +40,10 @@ impl Default for FillRect { } impl UiElement for FillRect { + fn name(&self) -> &'static str { + "FillRect" + } + fn measure(&self, ctx: MeasureContext) -> Response { Response { size: vec2( @@ -54,8 +58,7 @@ impl UiElement for FillRect { Size::Static(pixels) => pixels, }, ), - hints: Default::default(), - user_data: None + ..Default::default() } } diff --git a/hui/src/element/builtin/progress_bar.rs b/hui/src/element/builtin/progress_bar.rs index 5c72e17..eba1e49 100644 --- a/hui/src/element/builtin/progress_bar.rs +++ b/hui/src/element/builtin/progress_bar.rs @@ -49,7 +49,9 @@ impl Default for ProgressBar { } impl UiElement for ProgressBar { - fn name(&self) -> &'static str { "Progress bar" } + fn name(&self) -> &'static str { + "ProgressBar" + } fn measure(&self, ctx: MeasureContext) -> Response { Response { @@ -67,6 +69,7 @@ impl UiElement for ProgressBar { ), hints: Default::default(), user_data: None, + ..Default::default() } } diff --git a/hui/src/element/builtin/spacer.rs b/hui/src/element/builtin/spacer.rs index 4fb4b74..a9a6ae8 100644 --- a/hui/src/element/builtin/spacer.rs +++ b/hui/src/element/builtin/spacer.rs @@ -18,14 +18,17 @@ impl Default for Spacer { } impl UiElement for Spacer { + fn name(&self) -> &'static str { + "Spacer" + } + fn measure(&self, ctx: MeasureContext) -> Response { Response { size: match ctx.layout.direction { UiDirection::Horizontal => vec2(self.0, 0.), UiDirection::Vertical => vec2(0., self.0), }, - hints: Default::default(), - user_data: None + ..Default::default() } } diff --git a/hui/src/element/builtin/text.rs b/hui/src/element/builtin/text.rs index 51a6d69..ffb00b1 100644 --- a/hui/src/element/builtin/text.rs +++ b/hui/src/element/builtin/text.rs @@ -63,6 +63,10 @@ impl Text { } impl UiElement for Text { + fn name(&self) -> &'static str { + "Text" + } + fn measure(&self, ctx: MeasureContext) -> Response { let mut size = (0., 0.); if matches!(self.size.width, Size::Auto) || matches!(self.size.height, Size::Auto) { @@ -84,8 +88,7 @@ impl UiElement for Text { Size::Static(pixels) => pixels, }, ), - hints: Default::default(), - user_data: None + ..Default::default() } } diff --git a/hui/src/element/builtin/transformer.rs b/hui/src/element/builtin/transformer.rs index be27a5b..5389c2b 100644 --- a/hui/src/element/builtin/transformer.rs +++ b/hui/src/element/builtin/transformer.rs @@ -37,6 +37,10 @@ impl Transformer { } impl UiElement for Transformer { + fn name(&self) -> &'static str { + "Transformer" + } + fn measure(&self, ctx: MeasureContext) -> Response { self.element.measure(ctx) } diff --git a/hui/src/measure.rs b/hui/src/measure.rs index ebff258..e416314 100644 --- a/hui/src/measure.rs +++ b/hui/src/measure.rs @@ -11,7 +11,18 @@ pub struct Hints { #[derive(Default)] pub struct Response { + /// Computed size of the element pub size: Vec2, + + /// Hints for the layout system, can be used to optimize the layout engine.\ + /// These will never cause the UI to be rendered differently (assuming the values are correct) pub hints: Hints, + + /// Arbitrary user data, can be used to pass data (for example, cache) between measure and process stages pub user_data: Option>, + + /// If true, the element should always cause the content to wrap to the next line\ + /// (the element itself gets wrapped to the next line too) + /// You should almost never set this + pub should_wrap: bool, }