Skip to content

Commit

Permalink
Change the default for the measure_func field of ContentSize to N…
Browse files Browse the repository at this point in the history
…one. (#9346)

# Objective

The default for `ContentSize` should have the `measure_func` field set
to `None`, instead of a fixed size of zero. This means that until a
measure func is set the size of the UI node will be determined by its
`Style` constraints. This is preferable as it allows users to specify
the space the Node should take up in the layout while waiting for
content to load.

## Solution

Derive `Default` for `ContentSize`.

The PR also adds a `fixed_size` helper function to make it a bit easier
to access the old behaviour.

## Changelog
* Derived `Default` for `ContentSize`
* Added a `fixed_size` helper function to `ContentSize` that creates a
new `ContentSize` with a `MeasureFunc` that always returns the same
value, regardless of layout constraints.

## Migration Guide
The default for `ContentSize` now sets its `measure_func` to `None`,
instead of a fixed size measure that returns `Vec2::ZERO`.
The helper function `fixed_size` can be called with
`ContentSize::fixed_size(Vec2::ZERO)` to get the previous behaviour.
  • Loading branch information
ickshonpe committed Aug 7, 2023
1 parent 84f6b87 commit fe37ba5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions crates/bevy_ui/src/measurement.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_ecs::prelude::Component;
use bevy_ecs::reflect::ReflectComponent;
use bevy_math::Vec2;
use bevy_reflect::Reflect;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use std::fmt::Formatter;
pub use taffy::style::AvailableSpace;
use taffy::{node::MeasureFunc, prelude::Size as TaffySize};
Expand Down Expand Up @@ -46,8 +46,8 @@ impl Measure for FixedMeasure {

/// A node with a `ContentSize` component is a node where its size
/// is based on its content.
#[derive(Component, Reflect)]
#[reflect(Component)]
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default)]
pub struct ContentSize {
/// The `Measure` used to compute the intrinsic size
#[reflect(ignore)]
Expand All @@ -66,12 +66,11 @@ impl ContentSize {
};
self.measure_func = Some(MeasureFunc::Boxed(Box::new(measure_func)));
}
}

impl Default for ContentSize {
fn default() -> Self {
Self {
measure_func: Some(MeasureFunc::Raw(|_, _| TaffySize::ZERO)),
}
/// Creates a `ContentSize` with a `Measure` that always returns given `size` argument, regardless of the UI layout's constraints.
pub fn fixed_size(size: Vec2) -> ContentSize {
let mut content_size = Self::default();
content_size.set(FixedMeasure { size });
content_size
}
}

0 comments on commit fe37ba5

Please sign in to comment.