From 02afbde7b70d35c995def0de51ba64c3d19fd5c6 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 11 Aug 2023 17:31:59 +0800 Subject: [PATCH 1/4] Update accordion to take available space Fixes #4126 --- CHANGELOG.md | 1 + cmd/fyne_demo/tutorials/widget.go | 1 + widget/accordion.go | 27 +++- widget/accordion_test.go | 120 +++++++++++++++++- ..._expanded_multiple_open_multiple_items.xml | 27 ++++ ...ed_multiple_open_multiple_items_opened.xml | 33 +++++ ...layout_expanded_multiple_open_one_item.xml | 16 +++ ...expanded_multiple_open_one_item_opened.xml | 19 +++ ...ut_expanded_single_open_multiple_items.xml | 27 ++++ ...nded_single_open_multiple_items_opened.xml | 30 +++++ .../layout_expanded_single_open_one_item.xml | 16 +++ ...t_expanded_single_open_one_item_opened.xml | 19 +++ .../layout_multiple_open_multiple_items.xml | 6 +- ...ut_multiple_open_multiple_items_opened.xml | 6 +- .../layout_multiple_open_one_item.xml | 6 +- .../layout_multiple_open_one_item_opened.xml | 6 +- .../layout_single_open_multiple_items.xml | 6 +- ...yout_single_open_multiple_items_opened.xml | 6 +- .../accordion/layout_single_open_one_item.xml | 6 +- .../layout_single_open_one_item_opened.xml | 6 +- 20 files changed, 355 insertions(+), 29 deletions(-) create mode 100644 widget/testdata/accordion/layout_expanded_multiple_open_multiple_items.xml create mode 100644 widget/testdata/accordion/layout_expanded_multiple_open_multiple_items_opened.xml create mode 100644 widget/testdata/accordion/layout_expanded_multiple_open_one_item.xml create mode 100644 widget/testdata/accordion/layout_expanded_multiple_open_one_item_opened.xml create mode 100644 widget/testdata/accordion/layout_expanded_single_open_multiple_items.xml create mode 100644 widget/testdata/accordion/layout_expanded_single_open_multiple_items_opened.xml create mode 100644 widget/testdata/accordion/layout_expanded_single_open_one_item.xml create mode 100644 widget/testdata/accordion/layout_expanded_single_open_one_item_opened.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 27411b5945..44249851f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ More detailed release notes can be found on the [releases page](https://github.c * Go 1.17 or later is now required. * Icons for macOS bundles are now padded and rounded, disable with "-use-raw-icon" * Focus handling for List/Tree/Table are now at the parent widget not child elements +* Accordion widget now fills available space - put it inside a `VBox` container for old behavior ## 2.3.5 - 6 June 2023 diff --git a/cmd/fyne_demo/tutorials/widget.go b/cmd/fyne_demo/tutorials/widget.go index 80c0cf6738..f1b91bf42a 100644 --- a/cmd/fyne_demo/tutorials/widget.go +++ b/cmd/fyne_demo/tutorials/widget.go @@ -50,6 +50,7 @@ func makeAccordionTab(_ fyne.Window) fyne.CanvasObject { Detail: widget.NewLabel("Three"), }, ) + ac.MultiOpen = true ac.Append(widget.NewAccordionItem("D", &widget.Entry{Text: "Four"})) return ac } diff --git a/widget/accordion.go b/widget/accordion.go index 10fb9ae1e1..fed3d422f6 100644 --- a/widget/accordion.go +++ b/widget/accordion.go @@ -121,10 +121,29 @@ func (r *accordionRenderer) Layout(size fyne.Size) { dividerOff := (theme.Padding() + theme.SeparatorThicknessSize()) / 2 x := float32(0) y := float32(0) + hasOpen := 0 + for i, ai := range r.container.Items { + h := r.headers[i] + min := h.MinSize().Height + y += min + + if ai.Open { + y += theme.Padding() + hasOpen++ + } + if i < len(r.container.Items)-1 { + y += theme.Padding() + } + } + + openSize := (size.Height - y) / float32(hasOpen) + y = 0 for i, ai := range r.container.Items { if i != 0 { div := r.dividers[i-1] - div.Move(fyne.NewPos(x, y-dividerOff)) + if i > 0 { + div.Move(fyne.NewPos(x, y-dividerOff)) + } div.Resize(fyne.NewSize(size.Width, theme.SeparatorThicknessSize())) } @@ -133,12 +152,12 @@ func (r *accordionRenderer) Layout(size fyne.Size) { min := h.MinSize().Height h.Resize(fyne.NewSize(size.Width, min)) y += min + if ai.Open { d := ai.Detail d.Move(fyne.NewPos(x, y)) - min := d.MinSize().Height - d.Resize(fyne.NewSize(size.Width, min)) - y += min + d.Resize(fyne.NewSize(size.Width, openSize)) + y += openSize } if i < len(r.container.Items)-1 { y += theme.Padding() diff --git a/widget/accordion_test.go b/widget/accordion_test.go index ab56836103..e685b931e5 100644 --- a/widget/accordion_test.go +++ b/widget/accordion_test.go @@ -208,7 +208,125 @@ func TestAccordion_Layout(t *testing.T) { } window := test.NewWindow(&fyne.Container{Layout: layout.NewCenterLayout(), Objects: []fyne.CanvasObject{accordion}}) - window.Resize(accordion.MinSize().Max(fyne.NewSize(150, 200))) + window.Resize(accordion.MinSize().Add(fyne.NewSize(theme.Padding()*2, theme.Padding()*2))) + + test.AssertRendersToMarkup(t, "accordion/layout_"+name+".xml", window.Canvas()) + + window.Close() + }) + } +} + +func TestAccordion_Layout_Expanded(t *testing.T) { + test.NewApp() + + for name, tt := range map[string]struct { + multiOpen bool + items []*widget.AccordionItem + opened []int + }{ + "expanded_single_open_one_item": { + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + }, + }, + "expanded_single_open_one_item_opened": { + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + }, + opened: []int{0}, + }, + "expanded_single_open_multiple_items": { + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + { + Title: "B", + Detail: widget.NewLabel("2222222222"), + }, + }, + }, + "expanded_single_open_multiple_items_opened": { + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + { + Title: "B", + Detail: widget.NewLabel("2222222222"), + }, + }, + opened: []int{0, 1}, + }, + "expanded_multiple_open_one_item": { + multiOpen: true, + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + }, + }, + "expanded_multiple_open_one_item_opened": { + multiOpen: true, + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + }, + opened: []int{0}, + }, + "expanded_multiple_open_multiple_items": { + multiOpen: true, + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + { + Title: "B", + Detail: widget.NewLabel("2222222222"), + }, + }, + }, + "expanded_multiple_open_multiple_items_opened": { + multiOpen: true, + items: []*widget.AccordionItem{ + { + Title: "A", + Detail: widget.NewLabel("11111"), + }, + { + Title: "B", + Detail: widget.NewLabel("2222222222"), + }, + }, + opened: []int{0, 1}, + }, + } { + t.Run(name, func(t *testing.T) { + accordion := &widget.Accordion{ + MultiOpen: tt.multiOpen, + } + for _, ai := range tt.items { + accordion.Append(ai) + } + for _, o := range tt.opened { + accordion.Open(o) + } + + window := test.NewWindow(&fyne.Container{Layout: layout.NewCenterLayout(), Objects: []fyne.CanvasObject{accordion}}) + window.Resize(accordion.MinSize().Max(fyne.NewSize(150, 280))) test.AssertRendersToMarkup(t, "accordion/layout_"+name+".xml", window.Canvas()) diff --git a/widget/testdata/accordion/layout_expanded_multiple_open_multiple_items.xml b/widget/testdata/accordion/layout_expanded_multiple_open_multiple_items.xml new file mode 100644 index 0000000000..9a5dc5e3c7 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_multiple_open_multiple_items.xml @@ -0,0 +1,27 @@ + + + + + + + + + A + + + + + + + + B + + + + + + + + + + diff --git a/widget/testdata/accordion/layout_expanded_multiple_open_multiple_items_opened.xml b/widget/testdata/accordion/layout_expanded_multiple_open_multiple_items_opened.xml new file mode 100644 index 0000000000..3c1d9517cd --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_multiple_open_multiple_items_opened.xml @@ -0,0 +1,33 @@ + + + + + + + + + A + + + + + + + + B + + + + + 11111 + + + 2222222222 + + + + + + + + diff --git a/widget/testdata/accordion/layout_expanded_multiple_open_one_item.xml b/widget/testdata/accordion/layout_expanded_multiple_open_one_item.xml new file mode 100644 index 0000000000..4e1c44b7a6 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_multiple_open_one_item.xml @@ -0,0 +1,16 @@ + + + + + + + + + A + + + + + + + diff --git a/widget/testdata/accordion/layout_expanded_multiple_open_one_item_opened.xml b/widget/testdata/accordion/layout_expanded_multiple_open_one_item_opened.xml new file mode 100644 index 0000000000..a3c2e487f3 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_multiple_open_one_item_opened.xml @@ -0,0 +1,19 @@ + + + + + + + + + A + + + + + 11111 + + + + + diff --git a/widget/testdata/accordion/layout_expanded_single_open_multiple_items.xml b/widget/testdata/accordion/layout_expanded_single_open_multiple_items.xml new file mode 100644 index 0000000000..9a5dc5e3c7 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_single_open_multiple_items.xml @@ -0,0 +1,27 @@ + + + + + + + + + A + + + + + + + + B + + + + + + + + + + diff --git a/widget/testdata/accordion/layout_expanded_single_open_multiple_items_opened.xml b/widget/testdata/accordion/layout_expanded_single_open_multiple_items_opened.xml new file mode 100644 index 0000000000..f9ac150210 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_single_open_multiple_items_opened.xml @@ -0,0 +1,30 @@ + + + + + + + + + A + + + + + + + + B + + + + + 2222222222 + + + + + + + + diff --git a/widget/testdata/accordion/layout_expanded_single_open_one_item.xml b/widget/testdata/accordion/layout_expanded_single_open_one_item.xml new file mode 100644 index 0000000000..4e1c44b7a6 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_single_open_one_item.xml @@ -0,0 +1,16 @@ + + + + + + + + + A + + + + + + + diff --git a/widget/testdata/accordion/layout_expanded_single_open_one_item_opened.xml b/widget/testdata/accordion/layout_expanded_single_open_one_item_opened.xml new file mode 100644 index 0000000000..a3c2e487f3 --- /dev/null +++ b/widget/testdata/accordion/layout_expanded_single_open_one_item_opened.xml @@ -0,0 +1,19 @@ + + + + + + + + + A + + + + + 11111 + + + + + diff --git a/widget/testdata/accordion/layout_multiple_open_multiple_items.xml b/widget/testdata/accordion/layout_multiple_open_multiple_items.xml index e56e8faf15..bb1c7b7d8f 100644 --- a/widget/testdata/accordion/layout_multiple_open_multiple_items.xml +++ b/widget/testdata/accordion/layout_multiple_open_multiple_items.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_multiple_open_multiple_items_opened.xml b/widget/testdata/accordion/layout_multiple_open_multiple_items_opened.xml index 34b9faa2d6..9c0e2adf6a 100644 --- a/widget/testdata/accordion/layout_multiple_open_multiple_items_opened.xml +++ b/widget/testdata/accordion/layout_multiple_open_multiple_items_opened.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_multiple_open_one_item.xml b/widget/testdata/accordion/layout_multiple_open_one_item.xml index bfbc48235a..17017f30bb 100644 --- a/widget/testdata/accordion/layout_multiple_open_one_item.xml +++ b/widget/testdata/accordion/layout_multiple_open_one_item.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_multiple_open_one_item_opened.xml b/widget/testdata/accordion/layout_multiple_open_one_item_opened.xml index 9326fb8f15..217136f83e 100644 --- a/widget/testdata/accordion/layout_multiple_open_one_item_opened.xml +++ b/widget/testdata/accordion/layout_multiple_open_one_item_opened.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_single_open_multiple_items.xml b/widget/testdata/accordion/layout_single_open_multiple_items.xml index e56e8faf15..bb1c7b7d8f 100644 --- a/widget/testdata/accordion/layout_single_open_multiple_items.xml +++ b/widget/testdata/accordion/layout_single_open_multiple_items.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_single_open_multiple_items_opened.xml b/widget/testdata/accordion/layout_single_open_multiple_items_opened.xml index 0c7bf69e07..13f2bb1166 100644 --- a/widget/testdata/accordion/layout_single_open_multiple_items_opened.xml +++ b/widget/testdata/accordion/layout_single_open_multiple_items_opened.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_single_open_one_item.xml b/widget/testdata/accordion/layout_single_open_one_item.xml index bfbc48235a..17017f30bb 100644 --- a/widget/testdata/accordion/layout_single_open_one_item.xml +++ b/widget/testdata/accordion/layout_single_open_one_item.xml @@ -1,7 +1,7 @@ - + - - + + diff --git a/widget/testdata/accordion/layout_single_open_one_item_opened.xml b/widget/testdata/accordion/layout_single_open_one_item_opened.xml index 9326fb8f15..217136f83e 100644 --- a/widget/testdata/accordion/layout_single_open_one_item_opened.xml +++ b/widget/testdata/accordion/layout_single_open_one_item_opened.xml @@ -1,7 +1,7 @@ - + - - + + From 3cd4d07dd107b5868444cf1543ff2fcb23c469fb Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 11 Aug 2023 23:45:07 +0800 Subject: [PATCH 2/4] Update widget/accordion_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jacob Alzén --- widget/accordion_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/accordion_test.go b/widget/accordion_test.go index e685b931e5..dd3c9be9fb 100644 --- a/widget/accordion_test.go +++ b/widget/accordion_test.go @@ -208,7 +208,7 @@ func TestAccordion_Layout(t *testing.T) { } window := test.NewWindow(&fyne.Container{Layout: layout.NewCenterLayout(), Objects: []fyne.CanvasObject{accordion}}) - window.Resize(accordion.MinSize().Add(fyne.NewSize(theme.Padding()*2, theme.Padding()*2))) + window.Resize(accordion.MinSize().Add(fyne.NewSquareSize(theme.Padding()*2))) test.AssertRendersToMarkup(t, "accordion/layout_"+name+".xml", window.Canvas()) From 09ffd388851ddafbcab4d2c700ac3e9f6cdbac0e Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 11 Aug 2023 23:47:38 +0800 Subject: [PATCH 3/4] Only read pad once in each function --- widget/accordion.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/widget/accordion.go b/widget/accordion.go index fed3d422f6..20efba06ba 100644 --- a/widget/accordion.go +++ b/widget/accordion.go @@ -118,7 +118,8 @@ type accordionRenderer struct { } func (r *accordionRenderer) Layout(size fyne.Size) { - dividerOff := (theme.Padding() + theme.SeparatorThicknessSize()) / 2 + pad := theme.Padding() + dividerOff := (pad + theme.SeparatorThicknessSize()) / 2 x := float32(0) y := float32(0) hasOpen := 0 @@ -128,11 +129,11 @@ func (r *accordionRenderer) Layout(size fyne.Size) { y += min if ai.Open { - y += theme.Padding() + y += pad hasOpen++ } if i < len(r.container.Items)-1 { - y += theme.Padding() + y += pad } } @@ -160,15 +161,16 @@ func (r *accordionRenderer) Layout(size fyne.Size) { y += openSize } if i < len(r.container.Items)-1 { - y += theme.Padding() + y += pad } } } func (r *accordionRenderer) MinSize() (size fyne.Size) { + pad := theme.Padding() for i, ai := range r.container.Items { if i != 0 { - size.Height += theme.Padding() + size.Height += pad } min := r.headers[i].MinSize() size.Width = fyne.Max(size.Width, min.Width) @@ -177,7 +179,7 @@ func (r *accordionRenderer) MinSize() (size fyne.Size) { size.Width = fyne.Max(size.Width, min.Width) if ai.Open { size.Height += min.Height - size.Height += theme.Padding() + size.Height += pad } } return From 4a7f0466527a7c89622315c24207df179731ce44 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 11 Aug 2023 23:52:00 +0800 Subject: [PATCH 4/4] Fix missed goimports check --- widget/accordion_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/accordion_test.go b/widget/accordion_test.go index dd3c9be9fb..68704071c3 100644 --- a/widget/accordion_test.go +++ b/widget/accordion_test.go @@ -208,7 +208,7 @@ func TestAccordion_Layout(t *testing.T) { } window := test.NewWindow(&fyne.Container{Layout: layout.NewCenterLayout(), Objects: []fyne.CanvasObject{accordion}}) - window.Resize(accordion.MinSize().Add(fyne.NewSquareSize(theme.Padding()*2))) + window.Resize(accordion.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2))) test.AssertRendersToMarkup(t, "accordion/layout_"+name+".xml", window.Canvas())