Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraints data should be stored on a per-axis basis #331

Open
alice-i-cecile opened this issue Jan 18, 2023 · 1 comment
Open

Constraints data should be stored on a per-axis basis #331

alice-i-cecile opened this issue Jan 18, 2023 · 1 comment
Labels
code quality Make the code cleaner or prettier. performance Layout go brr usability Make the library more comfortable to use

Comments

@alice-i-cecile
Copy link
Collaborator

          The way the constraints data is structured seems wrong to me.

In Flex you usually access the constraints one axis at a time but the current API makes that awkward and fragile.
For example, look at the text_constraint function in the widget/text.rs:

pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f64) -> f32 {
    match (min_size, size, max_size) {
       (_, _, Val::Px(max)) => scale_value(max, scale_factor),
       // .. 
    }
}

called with:

text_constraint(
      layout.size_constraints.min.width,
      layout.size_constraints.suggested.width,
      layout.size_constraints.max.width,
      scale_factor,
 )

If the constraints data is stored per-axis:

pub struct LengthConstraint {
    pub min: Val,
    pub max: Val,
    pub suggested: Val,
} 

it's much more natural:

pub fn text_constraint_2(length_constraint: LengthConstraint, scale_factor: f64) -> f32 {
    match length_constraint {
        LengthConstraint { max: Val::Px(max), .. } => scale_value(max, scale_factor),
        // .. 
    }
}
text_constraint(layout.size_constraints.width, scale_factor)

In Bevy that is the worst it gets at the moment, but in Taffy there are lots of tangled sections like:

 if constants.node_inner_size.main(constants.dir).is_none() && constants.is_row {
            child.target_size.set_main(
                constants.dir,
                child.size.main(constants.dir).unwrap_or(0.0).maybe_clamp(
                    child.resolved_minimum_size.main(constants.dir).into(),
                    child.max_size.main(constants.dir),
                ),
            );
        } else {
            child.target_size.set_main(constants.dir, child.hypothetical_inner_size.main(constants.dir));
        }

where most of the complexity would vanish with some sort of per-axis interface.

Originally posted by @ickshonpe in bevyengine/bevy#5513 (comment)

@alice-i-cecile alice-i-cecile added usability Make the library more comfortable to use code quality Make the code cleaner or prettier. performance Layout go brr labels Jan 18, 2023
@nicoburns
Copy link
Collaborator

nicoburns commented Jan 18, 2023

Hmm… there’s definitely a case to be made for this. I’ve reduced a lot of this noise by implementing methods directly on Size (which allows a kind of vectorised api that acts on both axis at once), but the are definitely still places where there is quite a bit of noise from the axis selection code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Make the code cleaner or prettier. performance Layout go brr usability Make the library more comfortable to use
Projects
None yet
Development

No branches or pull requests

2 participants