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

Clean up background processes when changing tutorial in fyne_demo #5450

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/fyne_demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ func makeNav(setTutorial func(tutorial tutorials.Tutorial), loadPrevious bool) f
},
OnSelected: func(uid string) {
if t, ok := tutorials.Tutorials[uid]; ok {
for _, f := range tutorials.OnChangeFuncs {
f(uid)
}
tutorials.OnChangeFuncs = nil // Loading a page registers a new cleanup.

a.Preferences().SetString(preferenceCurrentTutorial, uid)
setTutorial(t)
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/fyne_demo/tutorials/advanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ func advancedScreen(win fyne.Window) fyne.CanvasObject {
&widget.FormItem{Text: "Texture Scale", Widget: tex},
))

ticker := time.NewTicker(time.Second)

OnChangeFuncs = append(OnChangeFuncs, func(page string) {
if page != "advanced" {
ticker.Stop()
}
})

go func(canvas fyne.Canvas) {
for range time.NewTicker(time.Second).C {
for range ticker.C {
fyne.Do(func() {
scale.SetText(scaleToString(canvas))
tex.SetText(textureScaleToString(canvas))
Expand Down
16 changes: 16 additions & 0 deletions cmd/fyne_demo/tutorials/animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func makeAnimationCanvas() fyne.CanvasObject {
a2.Curve = fyne.AnimationLinear
a2.Start()

OnChangeFuncs = append(OnChangeFuncs, func(page string) {
if page != "animations" {
a.Stop()
a2.Stop()
}
})

running := true
var toggle *widget.Button
toggle = widget.NewButton("Stop", func() {
Expand All @@ -71,6 +78,15 @@ func makeAnimationCurves() fyne.CanvasObject {
label3, box3, a3 := makeAnimationCurveItem("EaseOut", fyne.AnimationEaseOut, 60+theme.Padding()*2)
label4, box4, a4 := makeAnimationCurveItem("Linear", fyne.AnimationLinear, 90+theme.Padding()*3)

OnChangeFuncs = append(OnChangeFuncs, func(page string) {
if page != "animations" {
a1.Stop()
a2.Stop()
a3.Stop()
a4.Stop()
}
})

start := widget.NewButton("Compare", func() {
a1.Start()
a2.Start()
Expand Down
10 changes: 9 additions & 1 deletion cmd/fyne_demo/tutorials/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ func rgbGradient(x, y, w, h int) color.Color {
// canvasScreen loads a graphics example panel for the demo app
func canvasScreen(_ fyne.Window) fyne.CanvasObject {
gradient := canvas.NewHorizontalGradient(color.NRGBA{0x80, 0, 0, 0xff}, color.NRGBA{0, 0x80, 0, 0xff})
ticker := time.NewTicker(time.Second)

OnChangeFuncs = append(OnChangeFuncs, func(page string) {
if page != "canvas" {
ticker.Stop()
}
})

go func() {
for range time.NewTicker(time.Second).C {
for range ticker.C {
fyne.Do(func() {
gradient.Angle += 45
if gradient.Angle >= 360 {
Expand Down
4 changes: 4 additions & 0 deletions cmd/fyne_demo/tutorials/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fyne.io/fyne/v2"
)

// OnChangeFuncs is a slice of functions that can be registered
// to run when the user switches tutorial.
var OnChangeFuncs []func(string)

// Tutorial defines the data structure for a tutorial
type Tutorial struct {
Title, Intro string
Expand Down
34 changes: 19 additions & 15 deletions cmd/fyne_demo/tutorials/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,25 @@ func makeProgressTab(_ fyne.Window) fyne.CanvasObject {
}

infProgress := widget.NewProgressBarInfinite()
startProgress(progress, fprogress)

progressAnimation := fyne.Animation{
Curve: fyne.AnimationLinear,
Duration: 10 * time.Second,
Tick: func(percentage float32) {
value := float64(percentage)
progress.SetValue(value)
fprogress.SetValue(value)
},
}

progressAnimation.Start()

OnChangeFuncs = append(OnChangeFuncs, func(page string) {
if page != "progress" {
progressAnimation.Stop()
infProgress.Stop()
}
})

return container.NewVBox(
widget.NewLabel("Percent"), progress,
Expand Down Expand Up @@ -463,20 +481,6 @@ func makeToolbarTab(_ fyne.Window) fyne.CanvasObject {
return container.NewBorder(t, nil, nil, nil)
}

func startProgress(progress, fprogress *widget.ProgressBar) {
progressAnimation := fyne.Animation{
Curve: fyne.AnimationLinear,
Duration: 10 * time.Second,
Tick: func(percentage float32) {
value := float64(percentage)
progress.SetValue(value)
fprogress.SetValue(value)
},
}

progressAnimation.Start()
}

// widgetScreen shows a panel containing widget demos
func widgetScreen(_ fyne.Window) fyne.CanvasObject {
content := container.NewVBox(
Expand Down
Loading