Skip to content

Commit

Permalink
check for text constraints given in percent
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Feb 21, 2021
1 parent 3319195 commit 239619c
Showing 1 changed file with 91 additions and 7 deletions.
98 changes: 91 additions & 7 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,26 @@ fn scale_value(value: f32, factor: f64) -> f32 {

/// Defines how min_size, size, and max_size affects the bounds of a text
/// block.
pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f64) -> f32 {
// Needs support for percentages
pub fn text_constraint(
min_size: Val,
size: Val,
max_size: Val,
scale_factor: f64,
constraint: f32,
) -> f32 {
match (min_size, size, max_size) {
(_, _, Val::Px(max)) => scale_value(max, scale_factor),
(_, _, Val::Percent(max)) => scale_value((max / 100.) * constraint, scale_factor),
(Val::Px(min), _, _) => scale_value(min, scale_factor),
(Val::Percent(min), _, _) => scale_value((min / 100.) * constraint, scale_factor),
(Val::Undefined, Val::Px(size), Val::Undefined) => scale_value(size, scale_factor),
(Val::Auto, Val::Px(size), Val::Auto) => scale_value(size, scale_factor),
(Val::Undefined, Val::Percent(size), Val::Undefined) => {
scale_value((size / 100.) * constraint, scale_factor)
}
(Val::Auto, Val::Percent(size), Val::Auto) => {
scale_value((size / 100.) * constraint, scale_factor)
}
_ => f32::MAX,
}
}
Expand All @@ -54,11 +67,12 @@ pub fn text_system(
Query<(&Text, &Style, &mut CalculatedSize)>,
)>,
) {
let scale_factor = if let Some(window) = windows.get_primary() {
window.scale_factor()
} else {
1.
};
let (scale_factor, width_constraint, height_constraint) =
if let Some(window) = windows.get_primary() {
(window.scale_factor(), window.width(), window.height())
} else {
(1., f32::MAX, f32::MAX)
};

let inv_scale_factor = 1. / scale_factor;

Expand All @@ -82,12 +96,14 @@ pub fn text_system(
style.size.width,
style.max_size.width,
scale_factor,
width_constraint,
),
text_constraint(
style.min_size.height,
style.size.height,
style.max_size.height,
scale_factor,
height_constraint,
),
);

Expand Down Expand Up @@ -166,3 +182,71 @@ pub fn draw_text_system(
}
}
}

#[cfg(test)]
mod tests {
use super::text_constraint;
use crate::Val;

#[test]
fn should_constrain_based_on_pixel_values() {
assert_eq!(
text_constraint(Val::Px(100.), Val::Undefined, Val::Undefined, 1., 1.),
100.
);
assert_eq!(
text_constraint(Val::Undefined, Val::Undefined, Val::Px(100.), 1., 1.),
100.
);
assert_eq!(
text_constraint(Val::Undefined, Val::Px(100.), Val::Undefined, 1., 1.),
100.
);
}

#[test]
fn should_constrain_based_on_percent_values() {
assert_eq!(
text_constraint(Val::Percent(33.), Val::Undefined, Val::Undefined, 1., 1000.),
330.
);
assert_eq!(
text_constraint(Val::Undefined, Val::Undefined, Val::Percent(33.), 1., 1000.),
330.
);
assert_eq!(
text_constraint(Val::Undefined, Val::Percent(33.), Val::Undefined, 1., 1000.),
330.
);
}

#[test]
fn should_ignore_min_if_max_is_given() {
assert_eq!(
text_constraint(
Val::Percent(33.),
Val::Undefined,
Val::Percent(50.),
1.,
1000.
),
500.,
"min in percent and max in percent"
);
assert_eq!(
text_constraint(Val::Px(33.), Val::Undefined, Val::Px(50.), 1., 1000.),
50.,
"min in px and max in px"
);
assert_eq!(
text_constraint(Val::Px(33.), Val::Undefined, Val::Percent(50.), 1., 1000.),
500.,
"min in px and max in percent"
);
assert_eq!(
text_constraint(Val::Percent(33.), Val::Undefined, Val::Px(50.), 1., 1000.),
50.,
"min in percent and max in px"
);
}
}

0 comments on commit 239619c

Please sign in to comment.