Skip to content

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

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

ErikKalkoken
Copy link
Contributor

@ErikKalkoken ErikKalkoken commented Mar 17, 2025

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:

image

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:

package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	w := a.NewWindow("RowWrap layout")
	c := container.New(
		layout.NewRowWrapLayout(),
		widget.NewButton("Alpha", nil),
		widget.NewButton("Bravo Charlie", nil),
		widget.NewButton("Delta", nil),
		widget.NewButton("Echo Foxtrott Golf Hotel", nil),
		widget.NewButton("India Juliet", nil),
		widget.NewButton("Kilo Limo Mike November", nil),
	)
	w.SetContent(c)
	w.Resize(fyne.NewSize(50, 50))
	w.ShowAndRun()
}

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

Where applicable:

  • Public APIs match existing style and have Since: line.

@coveralls
Copy link

coveralls commented Mar 17, 2025

Coverage Status

coverage: 62.303% (+0.04%) from 62.267%
when pulling 38a21aa on ErikKalkoken:add-rowwrap-layout
into a9d00e2 on fyne-io:develop.

@ErikKalkoken ErikKalkoken changed the title Add RowWrap layout WIP: Add RowWrap layout Mar 17, 2025
@ErikKalkoken ErikKalkoken marked this pull request as draft April 9, 2025 14:11
@ErikKalkoken ErikKalkoken changed the title WIP: Add RowWrap layout Add RowWrap layout Apr 9, 2025
// 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 {
Copy link
Contributor

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

Copy link
Member

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.
Copy link
Contributor

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.

Copy link
Member

@Jacalz Jacalz left a 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 :)

Comment on lines +21 to +24
return &rowWrapLayout{
horizontalPadding: theme.Padding(),
verticalPadding: theme.Padding(),
}
Copy link
Member

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:

Suggested change
return &rowWrapLayout{
horizontalPadding: theme.Padding(),
verticalPadding: theme.Padding(),
}
padding := theme.Padding()
return &rowWrapLayout{
horizontalPadding: padding,
verticalPadding: padding,
}

Comment on lines +60 to +61
s := fyne.NewSize(width, rowHeight*float32(rows)+l.verticalPadding*float32(rows-1))
return s
Copy link
Member

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:

Suggested change
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))

Comment on lines +56 to +58
if size.Width > width {
width = size.Width
}
Copy link
Member

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 {
Copy link
Member

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?

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.

5 participants