Skip to content

Commit

Permalink
Merge pull request #1670 from adrianre12/#1668-TabContainer-Panic-whe…
Browse files Browse the repository at this point in the history
…n-removing-tabs

#1668 tab container panic when removing tabs
  • Loading branch information
andydotxyz committed Dec 28, 2020
2 parents 4b579c7 + f604038 commit 6a6e7dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -3,6 +3,10 @@ cmd/fyne/fyne
cmd/fyne_demo/fyne_demo
cmd/fyne_settings/fyne_settings
cmd/hello/hello
cmd/fyne/fyne.exe
cmd/fyne_demo/fyne_demo.exe
cmd/fyne_settings/fyne_settings.exe
cmd/hello/hello.exe

### Tests
**/testdata/failed
Expand Down
6 changes: 5 additions & 1 deletion widget/tabcontainer.go
Expand Up @@ -128,6 +128,9 @@ func (c *TabContainer) RemoveIndex(index int) {
// SetItems sets the container’s items and refreshes.
func (c *TabContainer) SetItems(items []*TabItem) {
c.Items = items
if l := len(c.Items); c.current >= l {
c.current = l - 1
}
c.Refresh()
}

Expand All @@ -146,7 +149,6 @@ func (c *TabContainer) SelectTabIndex(index int) {
if index < 0 || index >= len(c.Items) || c.current == index {
return
}

c.current = index
c.Refresh()

Expand Down Expand Up @@ -360,6 +362,7 @@ func (r *tabContainerRenderer) buildTabBar(buttons []fyne.CanvasObject) *fyne.Co

func (r *tabContainerRenderer) moveSelection() {
if r.container.current < 0 {
r.underline.Hide()
return
}
selected := r.tabBar.Objects[r.container.current]
Expand All @@ -380,6 +383,7 @@ func (r *tabContainerRenderer) moveSelection() {
underlinePos = fyne.NewPos(r.tabBar.Position().X-theme.Padding(), selected.Position().Y)
underlineSize = fyne.NewSize(theme.Padding(), selected.Size().Height)
}
r.underline.Show()
r.underline.Resize(underlineSize)
r.underline.Move(underlinePos)
}
Expand Down
13 changes: 13 additions & 0 deletions widget/tabcontainer_test.go
Expand Up @@ -98,3 +98,16 @@ func TestTabContainer_SelectTabIndex(t *testing.T) {
assert.Equal(t, 1, tabs.CurrentTabIndex())
assert.Equal(t, tabs.Items[1], selectedTab)
}

func TestTabContainer_RemoveIndex(t *testing.T) {
tabs := widget.NewTabContainer(&widget.TabItem{Text: "Test1", Content: widget.NewLabel("Test1")},
&widget.TabItem{Text: "Test2", Content: widget.NewLabel("Test2")})

tabs.SelectTabIndex(1)
tabs.RemoveIndex(1)
assert.Equal(t, 0, tabs.CurrentTabIndex()) // check max item selection and no panic

tabs.SelectTabIndex(0)
tabs.RemoveIndex(0)
assert.Equal(t, -1, tabs.CurrentTabIndex()) // check deselection and no panic
}

0 comments on commit 6a6e7dd

Please sign in to comment.