Skip to content

FlexboxLayout: two fixes to the preferred-size sqrt heuristic#12180

Open
dfaure-kdab wants to merge 2 commits into
slint-ui:masterfrom
dfaure-kdab:wip/dfaure/flex-sqrt-heuristic
Open

FlexboxLayout: two fixes to the preferred-size sqrt heuristic#12180
dfaure-kdab wants to merge 2 commits into
slint-ui:masterfrom
dfaure-kdab:wip/dfaure/flex-sqrt-heuristic

Conversation

@dfaure-kdab

Copy link
Copy Markdown
Collaborator
  • FlexboxLayout: snap the wrapping main-axis preferred size to whole items
  • FlexboxLayout: fold spacing into the wrapping square-side estimate
    See details in the respective commit logs.

The new interactive demo (flexbox-preferred-grid-demo.slint) shows before/after quite clearly.
For the first commit:
before-after-square-snap-fix
(notice how the fix makes it more "square")

For the second commit:
before-after-spacing-fix
(notice how it was completely broken when spacing was used)

A wrapping flex estimated its main-axis preferred size as the side of a
roughly-square pixel area, sqrt(sum of itemsize²). That value falls between
whole-item boundaries, so the solve wrapped mid-item and produced too many
lines: 3 equal items rendered as `A B C` instead of the intended grid-square
`A B / C`. It also added inter-item spacing for every item rather than just the
ones sharing a line, inflating the result.

Snap the square side up to a whole number of items (accumulating real sizes and
spacing), so a line holds a clean grid row. Updates two tests whose expected
preferred sizes encoded the old behavior.

See also the interactive testcase in the next commit
The wrapping main-axis preferred size aims for a roughly square pixel-area
arrangement: target = sqrt(sum of item areas), snapped up to a whole number
of items. The area summed item sizes only (size²) and ignored spacing, while
the accumulation that picks the line break does add spacing. So with large
spacing the spacing-blind target was reached after fewer items, skewing the
grid taller: e.g. 19 equal 50px items at 40px spacing wrapped to 3 columns /
7 rows instead of a square 5 columns / 4 rows.

Model each item as a (size + spacing) square in the area sum, so the gaps
count toward the target and the column count stays roughly square as spacing
grows. With zero or small spacing the column count is unchanged, so existing
tests keep their expected sizes.

Add an interactive demo (flexbox-preferred-grid-demo.slint).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant