-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add RowWrap layout #5612
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
base: develop
Are you sure you want to change the base?
Add RowWrap layout #5612
Conversation
ba044e6
to
669edf7
Compare
4c13496
to
e678464
Compare
// For a RowWrapLayout this is the width of the widest child | ||
// and the height of the first child multiplied by the number of children, | ||
// with appropriate padding between them. | ||
func (l *rowWrapLayout) MinSize(objects []fyne.CanvasObject) fyne.Size { |
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.
For something with wrapping like this, (and Label/RichText etc with wrapping), the behavior needed IIRC is for the MinSize returned to depend on the current size of the object. If the object hasn't been Resized (IE its size is 0, 0), then it returns the MinSize as you currently calculate it. But if it has been resized to a width >= the min width, the MinSize height is based on how many rows it needs to lay things out at the current width. Otherwise, placing it as the child of another layout will always make it take up more height than needed.
Not sure how to do this with a layout though, given that they are stateless by design. I suppose in Layout you could remeber the size in the rowWrapLayout
struct, and then reference it in MinSize
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.
Layouts don't have to be stateless (see Border), the thing they lack is any connection to the container they are laying out.
// NewRowWrapLayout returns a layout that dynamically arranges objects | ||
// with the same height in rows and wraps them as necessary. | ||
// | ||
// Object visibility is supported. |
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.
"Object visibility is supported" probably isn't needed, since all first-party layouts support object visibility, and as such it can just be assumed.
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 really like the idea of this. Just left a few minor suggestions inline before I noticed it was a draft PR but I'm attaching them anyway :)
return &rowWrapLayout{ | ||
horizontalPadding: theme.Padding(), | ||
verticalPadding: theme.Padding(), | ||
} |
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.
Fewer theme lookups is usually better:
return &rowWrapLayout{ | |
horizontalPadding: theme.Padding(), | |
verticalPadding: theme.Padding(), | |
} | |
padding := theme.Padding() | |
return &rowWrapLayout{ | |
horizontalPadding: padding, | |
verticalPadding: padding, | |
} |
s := fyne.NewSize(width, rowHeight*float32(rows)+l.verticalPadding*float32(rows-1)) | ||
return s |
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.
Cleaner to just return directly:
s := fyne.NewSize(width, rowHeight*float32(rows)+l.verticalPadding*float32(rows-1)) | |
return s | |
return fyne.NewSize(width, rowHeight*float32(rows)+l.verticalPadding*float32(rows-1)) |
if size.Width > width { | ||
width = size.Width | ||
} |
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 there is a fyne.Max()
function you can use here instead.
} | ||
rowHeight := objects[0].MinSize().Height | ||
var width float32 | ||
for _, o := range objects { |
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 will add sizes of invisible objects as well. Is that expected?
Description:
Adds a new layout for dynamically arranging objects with same height in rows with wrapping.
In my opinion this layout solves a common layout problem and would be a good addition the Fyne toolkit. For example it would allow to dynamically arranging a set of filter chips in multiple rows:
I went with "RowWrap", because it arranges objects in rows and wraps them. This layout is similar to the existing GridWrap layout, so a similar name makes sense (i.e. it arranges in rows instead of a grid). But I am of course open to other suggestions.
Demo
Here is an example render:
Screencast.from.17.03.2025.15.39.08.webm
Code to reproduce the example:
Checklist:
Where applicable: