FlexboxLayout: two fixes to the preferred-size sqrt heuristic#12180
Open
dfaure-kdab wants to merge 2 commits into
Open
FlexboxLayout: two fixes to the preferred-size sqrt heuristic#12180dfaure-kdab wants to merge 2 commits into
dfaure-kdab wants to merge 2 commits into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
(notice how the fix makes it more "square")
For the second commit:

(notice how it was completely broken when spacing was used)