Skip to content

Commit 6d9511a

Browse files
authored
Implement From<Val> for Fontsize (#24076)
# Objective Implement `From<Val>` for `Fontsize` so that the `Val` `px`, `vh`, etc helpers to be used to set font sizes in `bsn! `. ## Solution Implement `From<Val>` for `Fontsize`, with `Val::Auto` mapped to `FontSize:Rem(1.)` and `Val::Percent(x)` mapped to `FontSize::Rem(x/100.)`.
1 parent 2f01f61 commit 6d9511a

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

crates/bevy_ui/src/geometry.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bevy_math::{MismatchedUnitsError, StableInterpolate as _, TryStableInterpolate, Vec2};
22
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
3+
use bevy_text::FontSize;
34
use bevy_utils::default;
45
use core::ops::{Div, DivAssign, Mul, MulAssign, Neg};
56
use thiserror::Error;
@@ -468,6 +469,20 @@ impl Val {
468469
}
469470
}
470471

472+
impl From<Val> for FontSize {
473+
fn from(value: Val) -> Self {
474+
match value {
475+
Val::Auto => FontSize::Rem(1.),
476+
Val::Px(px) => FontSize::Px(px),
477+
Val::Percent(percent) => FontSize::Rem(percent / 100.),
478+
Val::Vw(vw) => FontSize::Vw(vw),
479+
Val::Vh(vh) => FontSize::Vh(vh),
480+
Val::VMin(vmin) => FontSize::VMin(vmin),
481+
Val::VMax(vmax) => FontSize::VMax(vmax),
482+
}
483+
}
484+
}
485+
471486
impl TryStableInterpolate for Val {
472487
type Error = MismatchedUnitsError;
473488

examples/scene/bsn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn button(label: &str) -> impl Scene {
5252
Text(label)
5353
TextFont {
5454
font: FontSourceTemplate::Handle("fonts/FiraSans-Bold.ttf"),
55-
font_size: FontSize::Px(33.0),
55+
font_size: px(33.0),
5656
}
5757
TextColor(Color::srgb(0.9, 0.9, 0.9))
5858
TextShadow

0 commit comments

Comments
 (0)