Navigation best practice? #3400
-
What is the best way of doing navigation with fyne? At the moment - I am defining different screens of the type Edit I made a channel that I use to change the state of the application and then in a goroutine setting the current content of the window. package main
import (
"doorapp/ui/screens"
"doorapp/ui/viewmodel"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/dialog"
)
type Screen byte
const (
Login Screen = iota
Loading
Door
)
func main() {
vm := viewmodel.NewViewModel()
a := app.New()
w := a.NewWindow("Door")
navChan := make(chan Screen)
nav := Nav(w, navChan, vm)
go func() {
for dest := range navChan {
w.SetContent(nav[dest])
}
}()
navChan <- Login
w.ShowAndRun()
}
func Nav(w fyne.Window, navChan chan Screen, vm *viewmodel.ViewModel) map[Screen]*fyne.Container {
return map[Screen]*fyne.Container{
Login: screens.LoginScreen(func(username, password string) {
go func() {
if err := vm.Login(username, password); err != nil {
dialog.NewError(err, w).Show()
navChan <- Login
} else {
navChan <- Door
}
}()
navChan <- Loading
}),
Loading: screens.LoadingScreen(),
Door: screens.DoorScreen(vm.OpenDoor, vm.CloseDoor),
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It is generally a bad idea to call |
Beta Was this translation helpful? Give feedback.
-
I back up what Andy is saying but just want to point out that using goroutines and channels, as mentioned, is going be be very slow an inefficient in the long run. |
Beta Was this translation helpful? Give feedback.
-
Okay thank you - I will take a look at AppTabs |
Beta Was this translation helpful? Give feedback.
It is generally a bad idea to call
SetContent
many times - for one thing it is slow, and for another users don't get the context of why it switched and how to go back.Currently we have AppTabs container for managing different app areas, I don't know if this matches what you're looking to do?