Skip to content

Commit

Permalink
fix: allow apptabs to store state while transitioning tabs (#2192)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushJadhav committed Apr 24, 2021
1 parent 6b53bbc commit ba981f0
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions container/apptabs.go
Expand Up @@ -18,10 +18,11 @@ import (
type AppTabs struct {
widget.BaseWidget

Items []*TabItem
OnChanged func(tab *TabItem)
current int
tabLocation TabLocation
Items []*TabItem
OnChanged func(tab *TabItem)
current int
tabLocation TabLocation
isTransitioning bool
}

// TabItem represents a single view in a AppTabs.
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *AppTabs) CreateRenderer() fyne.WidgetRenderer {
c.BaseWidget.ExtendBaseWidget(c)
r := &appTabsRenderer{line: canvas.NewRectangle(theme.ShadowColor()),
underline: canvas.NewRectangle(theme.PrimaryColor()), container: c}
r.updateTabs()
r.updateTabs(false)
return r
}

Expand Down Expand Up @@ -158,8 +159,10 @@ func (c *AppTabs) SelectTabIndex(index int) {
if index < 0 || index >= len(c.Items) || c.current == index {
return
}
c.isTransitioning = true
c.current = index
c.Refresh()
c.isTransitioning = false

if c.OnChanged != nil {
c.OnChanged(c.Items[c.current])
Expand Down Expand Up @@ -277,7 +280,7 @@ func (r *appTabsRenderer) Layout(size fyne.Size) {
child.Move(childPos)
child.Resize(childSize)
}
r.moveSelection()
r.moveSelection(r.container.isTransitioning)
}

func (r *appTabsRenderer) MinSize() fyne.Size {
Expand Down Expand Up @@ -311,7 +314,7 @@ func (r *appTabsRenderer) Refresh() {
r.line.Refresh()
r.underline.FillColor = theme.PrimaryColor()

if r.updateTabs() {
if r.updateTabs(r.container.isTransitioning) {
r.Layout(r.container.Size())
} else {
current := r.container.current
Expand All @@ -335,7 +338,7 @@ func (r *appTabsRenderer) Refresh() {
button.Refresh()
}
}
r.moveSelection()
r.moveSelection(r.container.isTransitioning)
canvas.Refresh(r.container)
}

Expand Down Expand Up @@ -391,7 +394,7 @@ func (r *appTabsRenderer) buildTabBar(buttons []fyne.CanvasObject) *fyne.Contain
return fyne.NewContainerWithLayout(lay, buttons...)
}

func (r *appTabsRenderer) moveSelection() {
func (r *appTabsRenderer) moveSelection(withAnimation bool) {
if r.container.current < 0 {
r.underline.Hide()
return
Expand Down Expand Up @@ -422,6 +425,13 @@ func (r *appTabsRenderer) moveSelection() {
return
}

if !withAnimation && r.animation == nil {
r.underline.Move(underlinePos)
r.underline.Resize(underlineSize)
canvas.Refresh(r.underline)
return
}

if r.animation != nil {
r.animation.Stop()
}
Expand Down Expand Up @@ -468,7 +478,7 @@ func (r *appTabsRenderer) tabsInSync() bool {
return true
}

func (r *appTabsRenderer) updateTabs() bool {
func (r *appTabsRenderer) updateTabs(withAnimation bool) bool {
if r.tabsInSync() {
return false
}
Expand Down Expand Up @@ -497,7 +507,7 @@ func (r *appTabsRenderer) updateTabs() bool {
}
r.tabBar = r.buildTabBar(buttons)
r.objects = objects
r.moveSelection()
r.moveSelection(withAnimation)

return true
}
Expand Down

0 comments on commit ba981f0

Please sign in to comment.