Question about architecture #4793
-
Hi everyone, I'm currently learning Fyne and I find it nice but have some issue with the way to organize my code. Of course I want to avoid the "all" in the main function. Separating my code into multiples files. Actually, my question is pretty simple : How to pass window ref around ? Explanation : func NewContent() *fyne.Container {
btTest := widget.NewButton("Test", btTestClicked)
content := container.Center(btTest)
return content
}
func btTestClicked() {
mainWindow.SetContent(SomeOtherContent)
} In this exemple "mainWindow" is a global var defined in my package, so I can access it from anywhere. But I don't really like this solution. I'm a bit new to Golang and I don't pretend to know what's good or not good habits with this language. But from my experience global context is Thanks for reading and thanks in advance for your help. Kind regards. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This really is more of a Go question than a Fyne architecture question. However there are many ways to work with this, you could use a wrapper function: func NewContent() *fyne.Container {
a := app.New()
mainWindow := a.NewWindow("Thing")
btTest := widget.NewButton("Test", func() {
btTestClicked(mainWindow)
})
content := container.NewStack(btTest)
return content
}
func btTestClicked(win fyne.Window) {
win.SetContent(SomeOtherContent)
} Or attach your functionality to a custom type: type myApp struct {
myWindow fyne.Window
}
func NewContent() *fyne.Container {
a := &myApp{}
btTest := widget.NewButton("Test", a.btTestClicked)
content := container.NewStack(btTest)
return content
}
func (m *myApp) btTestClicked() {
m.myWindow.SetContent(SomeOtherContent)
} Both of these are shown through the examples and longer form tutorial videos. |
Beta Was this translation helpful? Give feedback.
This really is more of a Go question than a Fyne architecture question.
We don't have extra parameters to pass into button callbacks because it would cause all callbacks to need to mirror this and it's hard to know if you'd want the button or window etc etc.
However there are many ways to work with this, you could use a wrapper function:
Or attach your functionality to a custom type: