Skip to content

Commit

Permalink
Merge pull request fyne-io#4848 from Jacalz/better-communication-with…
Browse files Browse the repository at this point in the history
…-progressbarinf-renderer
  • Loading branch information
Jacalz authored May 21, 2024
2 parents 92d5535 + 6ec160d commit bdb1061
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
4 changes: 4 additions & 0 deletions cmd/fyne_demo/tutorials/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ func startProgress() {
}

func stopProgress() {
if infProgress == nil {
return
}

if !infProgress.Running() {
return
}
Expand Down
76 changes: 51 additions & 25 deletions widget/progressbarinfinite.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type infProgressRenderer struct {
widget.BaseRenderer
background, bar canvas.Rectangle
animation fyne.Animation
running bool
wasRunning bool
progress *ProgressBarInfinite
}

Expand Down Expand Up @@ -64,8 +64,14 @@ func (p *infProgressRenderer) Layout(size fyne.Size) {

// Refresh updates the size and position of the horizontal scrolling infinite progress bar
func (p *infProgressRenderer) Refresh() {
if p.isRunning() {
running := p.progress.Running()
if running {
if !p.wasRunning {
p.start()
}
return // we refresh from the goroutine
} else if p.wasRunning {
p.stop()
}

th := p.progress.Theme()
Expand All @@ -82,27 +88,17 @@ func (p *infProgressRenderer) Refresh() {
canvas.Refresh(p.progress.super())
}

func (p *infProgressRenderer) isRunning() bool {
p.progress.propertyLock.RLock()
defer p.progress.propertyLock.RUnlock()

return p.running
}

// Start the infinite progress bar background thread to update it continuously
func (p *infProgressRenderer) start() {
if p.isRunning() {
return
}

p.progress.propertyLock.Lock()
defer p.progress.propertyLock.Unlock()

p.animation.Duration = time.Second * 3
p.animation.Tick = p.updateBar
p.animation.Curve = fyne.AnimationLinear
p.animation.RepeatCount = fyne.AnimationRepeatForever
p.running = true

p.wasRunning = true
p.animation.Start()
}

Expand All @@ -111,49 +107,76 @@ func (p *infProgressRenderer) stop() {
p.progress.propertyLock.Lock()
defer p.progress.propertyLock.Unlock()

p.running = false
p.wasRunning = false
p.animation.Stop()
}

func (p *infProgressRenderer) Destroy() {
p.progress.propertyLock.Lock()
p.progress.running = false
p.progress.propertyLock.Unlock()

p.stop()
}

// ProgressBarInfinite widget creates a horizontal panel that indicates waiting indefinitely
// An infinite progress bar loops 0% -> 100% repeatedly until Stop() is called
type ProgressBarInfinite struct {
BaseWidget
running bool
}

// Show this widget, if it was previously hidden
func (p *ProgressBarInfinite) Show() {
p.Start()
p.propertyLock.Lock()
p.running = true
p.propertyLock.Unlock()

p.BaseWidget.Show()
}

// Hide this widget, if it was previously visible
func (p *ProgressBarInfinite) Hide() {
p.Stop()
p.propertyLock.Lock()
p.running = false
p.propertyLock.Unlock()

p.BaseWidget.Hide()
}

// Start the infinite progress bar animation
func (p *ProgressBarInfinite) Start() {
cache.Renderer(p).(*infProgressRenderer).start()
p.propertyLock.Lock()
if p.running {
p.propertyLock.Unlock()
return
}

p.running = true
p.propertyLock.Unlock()

p.BaseWidget.Refresh()
}

// Stop the infinite progress bar animation
func (p *ProgressBarInfinite) Stop() {
cache.Renderer(p).(*infProgressRenderer).stop()
p.propertyLock.Lock()
if !p.running {
p.propertyLock.Unlock()
return
}

p.running = false
p.propertyLock.Unlock()

p.BaseWidget.Refresh()
}

// Running returns the current state of the infinite progress animation
func (p *ProgressBarInfinite) Running() bool {
if !cache.IsRendered(p) {
return false
}

return cache.Renderer(p).(*infProgressRenderer).isRunning()
p.propertyLock.RLock()
defer p.propertyLock.RUnlock()
return p.running
}

// MinSize returns the size that this widget should not shrink below
Expand Down Expand Up @@ -185,7 +208,10 @@ func (p *ProgressBarInfinite) CreateRenderer() fyne.WidgetRenderer {

render.SetObjects([]fyne.CanvasObject{&render.background, &render.bar})

render.start()
p.propertyLock.Lock()
p.running = true
p.propertyLock.Unlock()

return render
}

Expand Down

0 comments on commit bdb1061

Please sign in to comment.