Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions crates/bevy_ui/src/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_math::Vec2;
use bevy_reflect::Reflect;
use std::fmt::Formatter;
pub use taffy::style::AvailableSpace;
use taffy::{node::MeasureFunc, prelude::Size as TaffySize};

impl std::fmt::Debug for ContentSize {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down Expand Up @@ -50,33 +51,27 @@ impl Measure for FixedMeasure {
pub struct ContentSize {
/// The `Measure` used to compute the intrinsic size
#[reflect(ignore)]
pub(crate) measure_func: Option<taffy::node::MeasureFunc>,
pub(crate) measure_func: Option<MeasureFunc>,
}

impl ContentSize {
/// Set a `Measure` for this function
pub fn set(&mut self, measure: impl Measure) {
let measure_func =
move |size: taffy::prelude::Size<Option<f32>>,
available: taffy::prelude::Size<AvailableSpace>| {
let size =
measure.measure(size.width, size.height, available.width, available.height);
taffy::prelude::Size {
width: size.x,
height: size.y,
}
};
self.measure_func = Some(taffy::node::MeasureFunc::Boxed(Box::new(measure_func)));
let measure_func = move |size: TaffySize<_>, available: TaffySize<_>| {
let size = measure.measure(size.width, size.height, available.width, available.height);
TaffySize {
width: size.x,
height: size.y,
}
};
self.measure_func = Some(MeasureFunc::Boxed(Box::new(measure_func)));
Copy link
Contributor

@ickshonpe ickshonpe Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much cleaner. We should be using type aliases for taffy types in the layout module too maybe.

}
}

#[allow(clippy::derivable_impls)]
impl Default for ContentSize {
fn default() -> Self {
Self {
measure_func: Some(taffy::node::MeasureFunc::Raw(|_, _| {
taffy::prelude::Size::ZERO
})),
measure_func: Some(MeasureFunc::Raw(|_, _| TaffySize::ZERO)),
}
}
}
30 changes: 8 additions & 22 deletions crates/bevy_ui/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
};
use bevy_asset::{Assets, Handle};

#[cfg(feature = "bevy_text")]
use bevy_ecs::query::Without;
use bevy_ecs::{
prelude::Component,
Expand All @@ -15,8 +14,6 @@ use bevy_math::Vec2;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::texture::Image;
use bevy_sprite::TextureAtlas;
#[cfg(feature = "bevy_text")]
use bevy_text::Text;
use bevy_window::{PrimaryWindow, Window};

/// The size of the image's texture
Expand Down Expand Up @@ -73,20 +70,18 @@ impl Measure for ImageMeasure {
}
}

#[cfg(feature = "bevy_text")]
type UpdateImageFilter = (With<Node>, Without<bevy_text::Text>);
#[cfg(not(feature = "bevy_text"))]
type UpdateImageFilter = With<Node>;

/// Updates content size of the node based on the image provided
pub fn update_image_content_size_system(
mut previous_combined_scale_factor: Local<f64>,
windows: Query<&Window, With<PrimaryWindow>>,
ui_scale: Res<UiScale>,
textures: Res<Assets<Image>>,
#[cfg(feature = "bevy_text")] mut query: Query<
(&mut ContentSize, &UiImage, &mut UiImageSize),
(With<Node>, Without<Text>),
>,
#[cfg(not(feature = "bevy_text"))] mut query: Query<
(&mut ContentSize, &UiImage, &mut UiImageSize),
With<Node>,
>,
mut query: Query<(&mut ContentSize, &UiImage, &mut UiImageSize), UpdateImageFilter>,
) {
let combined_scale_factor = windows
.get_single()
Expand Down Expand Up @@ -120,23 +115,14 @@ pub fn update_atlas_content_size_system(
windows: Query<&Window, With<PrimaryWindow>>,
ui_scale: Res<UiScale>,
atlases: Res<Assets<TextureAtlas>>,
#[cfg(feature = "bevy_text")] mut atlas_query: Query<
(
&mut ContentSize,
&Handle<TextureAtlas>,
&UiTextureAtlasImage,
&mut UiImageSize,
),
(With<Node>, Without<Text>, Without<UiImage>),
>,
#[cfg(not(feature = "bevy_text"))] mut atlas_query: Query<
mut atlas_query: Query<
(
&mut ContentSize,
&Handle<TextureAtlas>,
&UiTextureAtlasImage,
&mut UiImageSize,
),
(With<Node>, Without<UiImage>),
(UpdateImageFilter, Without<UiImage>),
>,
) {
let combined_scale_factor = windows
Expand Down