move stuff out

This commit is contained in:
griffi-gh 2024-03-07 22:46:01 +01:00
parent b348873632
commit 8729e8f345
3 changed files with 31 additions and 36 deletions

View file

@ -4,7 +4,7 @@ use crate::{
background::BackgroundColor, background::BackgroundColor,
draw::{ImageHandle, RoundedCorners, UiDrawCommand}, draw::{ImageHandle, RoundedCorners, UiDrawCommand},
element::{MeasureContext, ProcessContext, UiElement}, element::{MeasureContext, ProcessContext, UiElement},
layout::{Size, Size2d}, layout::{compute_size, Size, Size2d},
measure::Response, measure::Response,
rectangle::Corners, rectangle::Corners,
}; };
@ -57,31 +57,18 @@ impl UiElement for Image {
fn measure(&self, ctx: MeasureContext) -> Response { fn measure(&self, ctx: MeasureContext) -> Response {
let dim = ctx.images.get_size(self.image).expect("invalid image handle"); 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 { Response {
size: vec2( size: compute_size(ctx.layout, self.size, vec2(
match self.size.width {
Size::Auto => {
match self.size.height { match self.size.height {
Size::Auto => dim.x as f32, Size::Auto => dim.x as f32,
Size::Fraction(f) => ((f * ctx.layout.max_size.y) / dim.y as f32) * dim.x as f32, _ => (pre_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,
}, },
match self.size.height { match self.size.height {
Size::Auto => { Size::Auto => dim.x as f32,
match self.size.width { _ => (pre_size.y / dim.y as f32) * dim.x as f32,
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,
},
),
..Default::default() ..Default::default()
} }
} }

View file

@ -4,7 +4,7 @@ use crate::{
background::BackgroundColor, background::BackgroundColor,
draw::{RoundedCorners, UiDrawCommand}, draw::{RoundedCorners, UiDrawCommand},
element::{MeasureContext, ProcessContext, UiElement}, element::{MeasureContext, ProcessContext, UiElement},
layout::{Size, Size2d}, layout::{compute_size, Size, Size2d},
measure::Response, measure::Response,
rectangle::Corners rectangle::Corners
}; };
@ -55,18 +55,10 @@ impl UiElement for ProgressBar {
fn measure(&self, ctx: MeasureContext) -> Response { fn measure(&self, ctx: MeasureContext) -> Response {
Response { Response {
size: vec2( size: compute_size(ctx.layout, self.size, vec2(
match self.size.width { ctx.layout.max_size.x.max(300.),
Size::Auto => ctx.layout.max_size.x.max(300.), Self::DEFAULT_HEIGHT,
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,
}
),
hints: Default::default(), hints: Default::default(),
user_data: None, user_data: None,
..Default::default() ..Default::default()

View file

@ -1,6 +1,6 @@
//! element layout, alignment and sizing //! element layout, alignment and sizing
use glam::Vec2; use glam::{vec2, Vec2};
/// Alignment along a single axis /// Alignment along a single axis
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)] #[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)]
@ -160,3 +160,19 @@ pub struct LayoutInfo {
/// (Usually matches direction of the parent container) /// (Usually matches direction of the parent container)
pub direction: Direction, 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)
}