Skip to content
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

Add Option to CheckGroup to make Rows/Columns #5226

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

BieHDC
Copy link
Contributor

@BieHDC BieHDC commented Oct 26, 2024

Description:

This adds an option to CheckGroup to make Rows/Columns instead of one long list.
I made this for a program i am currently writing and it makes sense to upstream.
It is fully backwards compatible.

Video:
https://github.com/user-attachments/assets/79bf6be3-0d93-4e51-8f94-f94c1244da75

Example Code for testing:

package main

import (
	"fmt"
	"strconv"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
)

type gui struct {
	a fyne.App
	w fyne.Window
}

func main() {
	g := gui{}

	g.a = app.NewWithID("biehdc.fynedev.checkgroupcolumns")
	g.w = g.a.NewWindow("CheckGroup with Rows/Columns")
	g.w.CenterOnScreen()
	g.w.Resize(fyne.NewSize(800, 600))

	g.w.SetContent(g.content())
	g.w.ShowAndRun()
}

func (g *gui) content() fyne.CanvasObject {
	contenttainer := container.NewStack()
	setcontentainer := func(co fyne.CanvasObject) {
		contenttainer.Objects = []fyne.CanvasObject{co}
		contenttainer.Refresh()
	}

	numitems := widget.NewEntry()
	numitems.PlaceHolder = "Amount of items to generate"
	numcols := widget.NewEntry()
	numcols.PlaceHolder = "Amount of Rows/Columns"
	togglehorizonal := widget.NewCheck("Horizontal", nil)
	makeui := widget.NewButton("Make Example", func() {
		ni, err := strconv.Atoi(numitems.Text)
		if err != nil {
			dialog.ShowError(err, g.w)
			return
		}

		nr, err := strconv.Atoi(numcols.Text)
		if err != nil {
			dialog.ShowError(err, g.w)
			return
		}

		var items []string
		for i := range ni {
			items = append(items, fmt.Sprintf("Check Item %d", i))
		}

		checkgroup := widget.NewCheckGroup(items, nil)
		checkgroup.SetColumns(nr)
		checkgroup.Horizontal = togglehorizonal.Checked

		setcontentainer(checkgroup)
	})
	numitems.OnSubmitted = func(_ string) { makeui.OnTapped() }
	numcols.OnSubmitted = func(_ string) { makeui.OnTapped() }

	return container.NewBorder(
		container.NewHBox(
			NewMinSizer(numitems, fyne.NewSize(300, 0)),
			NewMinSizer(numcols, fyne.NewSize(300, 0)),
			togglehorizonal, makeui,
		), nil, nil, nil,
		container.NewScroll(contenttainer),
	)
}

type minSizer struct {
	widget.BaseWidget
	child   fyne.CanvasObject
	minsize fyne.Size
}

func NewMinSizer(child fyne.CanvasObject, minsize fyne.Size) *minSizer {
	ms := &minSizer{child: child, minsize: minsize}
	ms.ExtendBaseWidget(ms)
	return ms
}

func (ms *minSizer) MinSize() fyne.Size {
	return ms.child.MinSize().Max(ms.minsize)
}

func (ms *minSizer) CreateRenderer() fyne.WidgetRenderer {
	return widget.NewSimpleRenderer(ms.child)
}

Checklist:

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

@Jacalz
Copy link
Member

Jacalz commented Oct 27, 2024

It is fully backwards compatible.

Sorry but it actually isn’t. Your variable is indexed from one meaning that the default value of zero when creating a struct without using the constructor results in incorrect behaviour.

@BieHDC
Copy link
Contributor Author

BieHDC commented Oct 28, 2024

It is fully backwards compatible.

Sorry but it actually isn’t. Your variable is indexed from one meaning that the default value of zero when creating a struct without using the constructor results in incorrect behaviour.

True, but i can fix that. Right now i would more like to know if that feature was even wished in the main repo. Otherwise i would demote it to a new widget for fyne-x.

@Jacalz
Copy link
Member

Jacalz commented Oct 28, 2024

Absolutely, I meant it as a review comment. Sorry if that wasn't clear. I personally think it is a useful addition :)

This is fully backwards compatible and helps with very
long lists to be better organised.
@BieHDC
Copy link
Contributor Author

BieHDC commented Nov 2, 2024

I reworked the code on the basis of gridLayout which also fixed a rendering bug and the list now also has the natural flow. And of course fixed the backwards compatibility issue.

@coveralls
Copy link

Coverage Status

coverage: 59.971% (+0.01%) from 59.961%
when pulling d178c85 on BieHDC:develop
into de361f6 on fyne-io:develop.

Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a question about the layout. Perhaps folk have thoughts?

Thanks for adding this though1

widget/check_group.go Outdated Show resolved Hide resolved
widget/check_group.go Outdated Show resolved Hide resolved
Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the test failures it seems like perhaps the algorithm is now laying out items from a single row/column differently to before.
If that is part of what was agreed then the test golden files will need to be updated.

There should also be a test added for this new feature.

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.

4 participants