-
Notifications
You must be signed in to change notification settings - Fork 570
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
Align: try to keep baseline offset #2078
Conversation
Thanks for the PR, and sorry for the slow response! I do have one minor concern, which is that this will also trigger for vertical aligns that just happen to tightly fit their contents. Is this the desired behavior? I suspect not, because it would cause the baseline to suddenly change if the layout changes by just one pixel. |
You are right, this is not correct. Unfortunately I have no idea how to fix it correctly. |
It looks like (I don't understand why the current code is like this, and I'd be happy to accept a simplification.) |
maybe because of inspiration from flutter? |
Good point, although flutter allows changing the factor and we don't. That brings up a question: what would be the desired behavior of the baseline if |
No idea. I guess a similar problem arises if we add padding to a widget? Anyway, here is a small program to show the problem: |
Yes, the same problem occurs with padding. My example just uses .padding(0.) to show the effect. |
Probably the desired behavior is that the baseline is propagated through the padding, right? (I haven't done much text layout in druid so I'm not really sure.) In that case, I think the right behavior for align is to preserve the baseline whenever |
Yes. I just run a few tests with flutter. and flutter handles this pretty well. They have a special method computeDistanceToActualBaseline() in there box render model: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/rendering/box.dart Please note that this should works with all kind of boxes (Align, Padding, Container, ...) |
I am also not sure the current flex.rs layout is correct. For example, any_use_baseline seems wrong to me. I would use:
|
The solution for padding widget seems to be trivial:
|
Just tested with different height_factor. It works if height_factor >= 1.0. I am unsure what a height_factor < 1.0 is supposed to do?
|
I can also make it work for Container (see code below). But how is it supposed to work with SizedBox and AspectRatioBox? Any ideas?
|
Keep the baseline offset for horizontal alignments. This avoids alignment problems when used inside a horizontal Flex box. Signed-off-by: Dietmar Maurer <[email protected]>
Signed-off-by: Dietmar Maurer <[email protected]>
druid/src/widget/flex.rs
Outdated
@@ -674,7 +674,7 @@ impl<T: Data> Widget<T> for Flex<T> { | |||
for child in &mut self.children { | |||
match child { | |||
Child::Fixed { widget, alignment } => { | |||
any_use_baseline &= *alignment == Some(CrossAxisAlignment::Baseline); | |||
any_use_baseline |= *alignment == Some(CrossAxisAlignment::Baseline); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense (although I'm not super familiar with this code). But I'm confused by:
- why is
any_use_baseline
not updated in the "measure flex children" loop below? - is there any way to even set alignment for a fixed child? I only see it in
FlexParams
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- why is
any_use_baseline
not updated in the "measure flex children" loop below?
just updated the code for that. It now defaults to "false", and it is only set when a child uses baseline alignment.
- is there any way to even set alignment for a fixed child? I only see it in
FlexParams
.
I guess not. But we can add this later if someone needs it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, makes sense. I was just confused that we were only checking alignment for those children that couldn't have it set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me now, thanks again for the PR!
But, you will need to mollify CI before I can merge it...
any_use_baseline needs to be set when any child uses baseline alignment (as opposed to all children). Signed-off-by: Dietmar Maurer <[email protected]>
Signed-off-by: Dietmar Maurer <[email protected]>
Keep the baseline offset when height does not change (horizontal align).
This avoids alignment problems when used inside a horizontal Flex box.
Signed-off-by: Dietmar Maurer [email protected]