move stuff out

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

View file

@ -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()
}
}

View file

@ -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()

View file

@ -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)
}