From 8729e8f345ab60f9688e2ad7d39e9afd4d2087cd Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 7 Mar 2024 22:46:01 +0100 Subject: [PATCH] move stuff out --- hui/src/element/builtin/image.rs | 31 +++++++------------------ hui/src/element/builtin/progress_bar.rs | 18 ++++---------- hui/src/layout.rs | 18 +++++++++++++- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/hui/src/element/builtin/image.rs b/hui/src/element/builtin/image.rs index 1470134..47f5945 100644 --- a/hui/src/element/builtin/image.rs +++ b/hui/src/element/builtin/image.rs @@ -4,7 +4,7 @@ use crate::{ background::BackgroundColor, draw::{ImageHandle, RoundedCorners, UiDrawCommand}, element::{MeasureContext, ProcessContext, UiElement}, - layout::{Size, Size2d}, + layout::{compute_size, Size, Size2d}, measure::Response, rectangle::Corners, }; @@ -57,31 +57,18 @@ impl UiElement for Image { fn measure(&self, ctx: MeasureContext) -> Response { let dim = ctx.images.get_size(self.image).expect("invalid image handle"); + let pre_size = compute_size(ctx.layout, self.size, dim.as_vec2()); Response { - size: vec2( - match self.size.width { - Size::Auto => { - match self.size.height { - Size::Auto => dim.x as f32, - Size::Fraction(f) => ((f * ctx.layout.max_size.y) / dim.y as f32) * dim.x as f32, - Size::Static(pixels) => (pixels / dim.y as f32) * dim.x as f32, - } - }, - Size::Fraction(percentage) => ctx.layout.max_size.x * percentage, - Size::Static(pixels) => pixels, + size: compute_size(ctx.layout, self.size, vec2( + match self.size.height { + Size::Auto => dim.x as f32, + _ => (pre_size.y / dim.y as f32) * dim.x as f32, }, match self.size.height { - Size::Auto => { - match self.size.width { - Size::Auto => dim.y as f32, - Size::Fraction(f) => ((f * ctx.layout.max_size.x) / dim.x as f32) * dim.y as f32, - Size::Static(pixels) => (pixels / dim.x as f32) * dim.y as f32, - } - }, - Size::Fraction(percentage) => ctx.layout.max_size.y * percentage, - Size::Static(pixels) => pixels, + Size::Auto => dim.x as f32, + _ => (pre_size.y / dim.y as f32) * dim.x as f32, }, - ), + )), ..Default::default() } } diff --git a/hui/src/element/builtin/progress_bar.rs b/hui/src/element/builtin/progress_bar.rs index 57c9f12..a077f29 100644 --- a/hui/src/element/builtin/progress_bar.rs +++ b/hui/src/element/builtin/progress_bar.rs @@ -4,7 +4,7 @@ use crate::{ background::BackgroundColor, draw::{RoundedCorners, UiDrawCommand}, element::{MeasureContext, ProcessContext, UiElement}, - layout::{Size, Size2d}, + layout::{compute_size, Size, Size2d}, measure::Response, rectangle::Corners }; @@ -55,18 +55,10 @@ impl UiElement for ProgressBar { fn measure(&self, ctx: MeasureContext) -> Response { Response { - size: vec2( - match self.size.width { - Size::Auto => ctx.layout.max_size.x.max(300.), - Size::Fraction(p) => ctx.layout.max_size.x * p, - Size::Static(p) => p, - }, - match self.size.height { - Size::Auto => Self::DEFAULT_HEIGHT, - Size::Fraction(p) => ctx.layout.max_size.y * p, - Size::Static(p) => p, - } - ), + size: compute_size(ctx.layout, self.size, vec2( + ctx.layout.max_size.x.max(300.), + Self::DEFAULT_HEIGHT, + )), hints: Default::default(), user_data: None, ..Default::default() diff --git a/hui/src/layout.rs b/hui/src/layout.rs index ac6658e..e7963b4 100644 --- a/hui/src/layout.rs +++ b/hui/src/layout.rs @@ -1,6 +1,6 @@ //! element layout, alignment and sizing -use glam::Vec2; +use glam::{vec2, Vec2}; /// Alignment along a single axis #[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)] @@ -160,3 +160,19 @@ pub struct LayoutInfo { /// (Usually matches direction of the parent container) pub direction: Direction, } + +/// Helper function to calculate the size of an element based on its layout and size information\ +/// Used to help reduce code duplication in the `measure` method of UI elements +pub fn compute_size(layout: &LayoutInfo, size: Size2d, comfy_size: Vec2) -> Vec2 { + let width = match size.width { + Size::Auto => comfy_size.x, + Size::Fraction(fraction) => layout.max_size.x * fraction, + Size::Static(size) => size, + }; + let height = match size.height { + Size::Auto => comfy_size.y, + Size::Fraction(fraction) => layout.max_size.y * fraction, + Size::Static(size) => size, + }; + vec2(width, height) +}