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

Fix the last remaining races in fyne_demo #5382

Merged
merged 5 commits into from
Jan 9, 2025
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
22 changes: 10 additions & 12 deletions cmd/fyne_demo/tutorials/advanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"fyne.io/fyne/v2/widget"
)

func scaleString(c fyne.Canvas) string {
func scaleToString(c fyne.Canvas) string {
return strconv.FormatFloat(float64(c.Scale()), 'f', 2, 32)
}

func texScaleString(c fyne.Canvas) string {
func textureScaleToString(c fyne.Canvas) string {
pixels, _ := c.PixelCoordinateForPosition(fyne.NewPos(1, 1))
texScale := float32(pixels) / c.Scale()
return strconv.FormatFloat(float64(texScale), 'f', 2, 32)
Expand All @@ -25,15 +25,6 @@ func prependTo(g *fyne.Container, s string) {
g.Refresh()
}

func setScaleText(scale, tex *widget.Label, win fyne.Window) {
for scale.Visible() {
scale.SetText(scaleString(win.Canvas()))
tex.SetText(texScaleString(win.Canvas()))

time.Sleep(time.Second)
}
}

// advancedScreen loads a panel that shows details and settings that are a bit
// more detailed than normally needed.
func advancedScreen(win fyne.Window) fyne.CanvasObject {
Expand All @@ -45,7 +36,14 @@ func advancedScreen(win fyne.Window) fyne.CanvasObject {
&widget.FormItem{Text: "Texture Scale", Widget: tex},
))

go setScaleText(scale, tex, win)
go func(canvas fyne.Canvas) {
for range time.NewTicker(time.Second).C {
fyne.CurrentApp().Driver().CallFromGoroutine(func() {
scale.SetText(scaleToString(canvas))
tex.SetText(textureScaleToString(canvas))
})
}
}(win.Canvas())

label := widget.NewLabel("Just type...")
generic := container.NewVBox()
Expand Down
4 changes: 1 addition & 3 deletions cmd/fyne_demo/tutorials/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func rgbGradient(x, y, w, h int) color.Color {
func canvasScreen(_ fyne.Window) fyne.CanvasObject {
gradient := canvas.NewHorizontalGradient(color.NRGBA{0x80, 0, 0, 0xff}, color.NRGBA{0, 0x80, 0, 0xff})
go func() {
for {
time.Sleep(time.Second)

for range time.NewTicker(time.Second).C {
fyne.CurrentApp().Driver().CallFromGoroutine(func() {
gradient.Angle += 45
if gradient.Angle >= 360 {
Expand Down
16 changes: 9 additions & 7 deletions cmd/fyne_demo/tutorials/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ func welcomeScreen(_ fyne.Window) fyne.CanvasObject {
fyne.CurrentApp().Settings().AddChangeListener(listen)
go func() {
for range listen {
bgColor = withAlpha(theme.Color(theme.ColorNameBackground), 0xe0)
bg.FillColor = bgColor
bg.Refresh()

shadowColor = withAlpha(theme.Color(theme.ColorNameBackground), 0x33)
footerBG.FillColor = bgColor
footer.Refresh()
fyne.CurrentApp().Driver().CallFromGoroutine(func() {
bgColor = withAlpha(theme.Color(theme.ColorNameBackground), 0xe0)
bg.FillColor = bgColor
bg.Refresh()

shadowColor = withAlpha(theme.Color(theme.ColorNameBackground), 0x33)
footerBG.FillColor = bgColor
footer.Refresh()
})
}
}()

Expand Down
90 changes: 24 additions & 66 deletions cmd/fyne_demo/tutorials/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ Mauris erat urna, fermentum et quam rhoncus, fringilla consequat ante. Vivamus c
Suspendisse id maximus felis. Sed mauris odio, mattis eget mi eu, consequat tempus purus.`
)

var (
progress *widget.ProgressBar
fprogress *widget.ProgressBar
infProgress *widget.ProgressBarInfinite
endProgress chan any
)

func makeAccordionTab(_ fyne.Window) fyne.CanvasObject {
link, err := url.Parse("https://fyne.io/")
if err != nil {
Expand Down Expand Up @@ -68,17 +61,16 @@ func makeActivityTab(win fyne.Window) fyne.CanvasObject {
a2.Start()
a2.Show()

defer func() {
go func() {
time.Sleep(time.Second * 10)
time.AfterFunc(10*time.Second, func() {
fyne.CurrentApp().Driver().CallFromGoroutine(func() {
a1.Stop()
a1.Hide()
a2.Stop()
a2.Hide()

button.Enable()
}()
}()
})
})
}

button = widget.NewButton("Animate", start)
Expand All @@ -97,11 +89,12 @@ func makeActivityTab(win fyne.Window) fyne.CanvasObject {
a3.Start()
d.Show()

go func() {
time.Sleep(time.Second * 5)
a3.Stop()
d.Hide()
}()
time.AfterFunc(5*time.Second, func() {
fyne.CurrentApp().Driver().CallFromGoroutine(func() {
a3.Stop()
d.Hide()
})
})
}))))
}

Expand Down Expand Up @@ -404,18 +397,15 @@ func makeInputTab(_ fyne.Window) fyne.CanvasObject {
}

func makeProgressTab(_ fyne.Window) fyne.CanvasObject {
stopProgress()

progress = widget.NewProgressBar()
progress := widget.NewProgressBar()

fprogress = widget.NewProgressBar()
fprogress := widget.NewProgressBar()
fprogress.TextFormatter = func() string {
return fmt.Sprintf("%.2f out of %.2f", fprogress.Value, fprogress.Max)
}

infProgress = widget.NewProgressBarInfinite()
endProgress = make(chan any, 1)
startProgress()
infProgress := widget.NewProgressBarInfinite()
startProgress(progress, fprogress)

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

func startProgress() {
progress.SetValue(0)
fprogress.SetValue(0)
select { // ignore stale end message
case <-endProgress:
default:
}

go func() {
end := endProgress
num := 0.0
for num < 1.0 {
time.Sleep(16 * time.Millisecond)
select {
case <-end:
return
default:
}

progress.SetValue(num)
fprogress.SetValue(num)
num += 0.002
}

progress.SetValue(1)
fprogress.SetValue(1)

// TODO make sure this resets when we hide etc...
stopProgress()
}()
infProgress.Start()
}

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

if !infProgress.Running() {
return
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)
},
}

infProgress.Stop()
endProgress <- struct{}{}
progressAnimation.Start()
}

// widgetScreen shows a panel containing widget demos
Expand Down
7 changes: 3 additions & 4 deletions cmd/fyne_demo/tutorials/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ func windowScreen(_ fyne.Window) fyne.CanvasObject {
fyne.TextAlignCenter, fyne.TextStyle{Bold: true}))
w.Show()

go func() {
time.Sleep(time.Second * 3)
w.Close()
}()
time.AfterFunc(3*time.Second, func() {
fyne.CurrentApp().Driver().CallFromGoroutine(w.Close)
})
}))
}

Expand Down
Loading