How to use Fyne just to render the GUI to an image ? #5544
-
Hello :) I am currently working on a project using eink display like this one I first started drawing raw image but as the layout gets more complex it's getting really painful so I was thinking to use fyne to render the GUI an an image. I can make a screenshot responding to a button for example like this func BuildWindow(app fyne.App) fyne.Window {
w := app.NewWindow("Dashink")
w.Resize(fyne.NewSize(WIDTH, HEIGHT))
w.SetFixedSize(true)
hello := widget.NewLabel("Hello Fyne!")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Hi!", func() {
hello.SetText("Welcome :)")
output.CaptureWindowToFile(w, "screenshot.png")
w.Close()
}),
))
return w
} func CaptureWindowToFile(w fyne.Window, fileName string) {
log.Println("Starting saving screenshot")
// Capture the current window's canvas content
img := w.Canvas().Capture()
// Create a file to save the image
f, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Encode the image as a PNG and write it to the file
err = png.Encode(f, img)
if err != nil {
log.Fatal(err)
}
log.Println("Screenshot saved to", fileName)
} But then now I am trying to make it completely headless like this func main() {
log.Println("Starting")
app := app.New()
window := gui.BuildWindow(app)
log.Println("Showing GUI")
window.ShowAndRun()
output.CaptureWindowToFile(window, "screenshot.png")
} but the How can I achieve this ? Is there a way to define something like a Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
To answer the question most directly you are looking for the App.Lifecycle() api which will run a callback when the app is foregrounded (window is completely shown). Alternatively you can run your capture code from a goroutine that is started before you run the app. However running the app headless using the default driver will still use the main display to show the window - is that what you want? There is also the software renderer like we use in unit testing which does not use the screen at all, and does not have the event loop to work around. Of course the trade-off is that you would have to handle events but I expect you'll have to do that anyway with your eink device. |
Beta Was this translation helpful? Give feedback.
Maybe https://github.com/fynelabs/snowflake/blob/ed3b7906ebdcfa5d258e913a88ec3309346793f9/snowflake.go#L143
In general the
driver/software
package provides a few great items - the snippet above pre-dates that but it's the same idea.Yes