Consistent baseline layout algorithm

Kevin Rushforth kevin.rushforth at oracle.com
Fri Mar 19 22:25:42 UTC 2021


I'd be interested to hear from app developers on this list.

Here are a few quick thoughts I have.

As you note, this is a long-standing problem with layout in FX. You 
mention in the performance considerations that for most cases this will 
iterate quickly. It would be interesting to know what some of the corner 
cases are, so we can see how bad the pathological case will be. I see 
that you propose to iterate up to 100 times. Maybe a lower threshold 
would be better? We already run layout passes 3 times in many cases. So 
also running 3 (or maybe 4 or 5) times seems reasonable, especially when 
your testing shows that most of the time it converges in <= 2 passes. So 
a smaller threshold than 100 would seem to make sense. If a control 
doesn't converge, you might consider logging it (in case an app 
developer wants to debug it) perhaps at a "fine" level so it isn't shown 
by default.

How do you propose to document this behavioral change -- not so much the 
fact that it will (usually) get the right answer now instead of the 
wrong one, but more about the multi-pass nature of it, and the fact that 
nodes with text will be "preferred". Related to that, how necessary is a 
new public API on Node?

-- Kevin


On 3/19/2021 3:02 PM, Michael Strauß wrote:
> Trying to use baseline alignment in JavaFX can be pretty hard to get
> right. For example: put some shape and a label into a layout
> container, and the container's baseline might not reflect what you
> would intuitively assume (which is the text baseline). In fact, most
> layout containers just take the first child that reports a baseline
> other than the special constant BASELINE_OFFSET_SAME_AS_HEIGHT and use
> that as the baseline for the entire container.
>
> Since there is no meaningful baseline if the first child is not a text
> node, the layout container won't neatly align with other kinds of text
> or text composites.
>
> Another issue is that the baseline offset of a resizable node depends
> on the height of the node, but the height of the node also depends on
> the baseline offset. This circular dependency is currently unaccounted
> for in controls such as Labeled (that's the reason for bugs like
> JDK-809261).
>
> I've prepared a PR that makes working with baseline alignments easier
> and more consistent, and also solves the circular dependency problem
> by introducing a multi-pass layout algorithm. The PR includes
> before-and-after images that help visualize the problem and the
> proposed solution.
>
> Here's the PR: https://github.com/openjdk/jfx/pull/433



More information about the openjfx-dev mailing list