efficient canvas refresh with fyne golang - go

I have the code below, It shows a window, generate an image as raster and then update the window content with it.
However, the setContent method is slow (with it I have 100% of 1 cpu core and almost 0 without).
I wonder if there is anything to do to do what I do here efficiently (modifiying underlaying raster, use gpu in anyway...) . I would like to be able to generate an image with raster and then display it at ~60 fps efficiently.
Any suggestion or other tools to do it better would be appreciated.
package main
import (
"image/color"
"math/rand"
"time"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Raster")
go func() {
for {
time.Sleep(time.Millisecond * 500)
raster := canvas.NewRasterWithPixels(
func(_, _, w, h int) color.Color {
return color.RGBA{uint8(rand.Intn(255)),
uint8(rand.Intn(255)),
uint8(rand.Intn(255)), 0xff}
})
w.SetContent(raster)
}
}()
w.SetFullScreen(true)
// w.Resize(fyne.NewSize(120, 100))
w.ShowAndRun()
}

Setting the window content is a very expensive call as it has to re-layout all the content and check sizes etc.
Just call raster.Refresh() instead.
You could also cache the raster pixels in your widget so you don’t have to create a new image for every frame to refresh.

Related

Create a verbose console inside Golang app using Fyne

Comming from Python with PyQt gui, I was used to add kind of console in my programm. The purpose was to indicate to the user information on the processes in progress, on the execution errors encountered, etc.
In Python/PyQt, I was using QLineEdit to do that. It was pretty easy to use. Just create and insert the widget in my gui and add a row for each information by calling appen().
For example, the console could say "esedb loading" when loading an esedb file, then "esedb file loaded" when finished, then "esedb parsing" for the next step, etc...
Now, I'm learning Golang with Fyne and I'm looking for a way to do something similar.
I found widget.NewTextGrid() but it doesn't work as I expect.
I can't just append new line. If I understand well, I have to store text in a string variable
Could you advice me about the way to do that ?
Thanks!
package main
import (
//"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func main() {
myapp := app.New()
myappGui := myapp.NewWindow("Example")
myappGui.Resize(fyne.NewSize(400, 600))
textConsole := widget.NewTextGrid()
TextGrid is a complex component designed for managing character specific font styles in a monospace arrangement (like a terminal etc).
For performance I would recommend a VBox in a Scroll widget where each line is another appended Label (you can set them to monospace text style as well). If you want the text to be interactive then as other answers have said the NewMultiLineEntry is likely for you.
Text is complex and we are working hard to optimise more of the complex usages and large file handling, so it will get smoother in later releases…
widget.TextGrid does not have a method to append a line, but it does support querying its current content using TextGrid.Text(). So what you may do is set a new text that is its current content and the new line concatenated, e.g.:
textConsole.SetText(textConsole.Text() + "\n" + line)
But know that widget.TextGrid does not support scrolling: its size will be dictated by its string content. You can make it scrollable of course by using a container.Scroll.
For example:
func main() {
myapp := app.New()
w := myapp.NewWindow("Example")
w.Resize(fyne.NewSize(500, 300))
textConsole := widget.NewTextGrid()
scrollPane := container.NewScroll(textConsole)
w.SetContent(scrollPane)
go func() {
for {
textConsole.SetText(textConsole.Text() + time.Now().String() + "\n")
scrollPane.ScrollToBottom()
time.Sleep(time.Second)
}
}()
w.ShowAndRun()
}
Alternatively you may use a multiline widget.Entry. It also supports selecting any part of it, and by default it's also editable. You may disable editing of course. It supports scrolling by default.
See this example:
func main() {
myapp := app.New()
w := myapp.NewWindow("Example")
w.Resize(fyne.NewSize(500, 300))
textConsole := widget.NewMultiLineEntry()
textConsole.Disable() // Disable editing
w.SetContent(textConsole)
go func() {
for {
textConsole.SetText(textConsole.Text + time.Now().String() + "\n")
time.Sleep(time.Second)
}
}()
w.ShowAndRun()
}

How to make a grid / Fyne / Golang

I'm making an application with Fyne.
I need to create a grid where the left column will be fixed and the right column will stretch. In general, there will be a menu on the left, and the main block on the right (a screenshot of the expected one below).
I read the documentation https://developer.fyne.io/container/grid but still don't understand how to do it. Help me please.
Grid
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
)
func main() {
application := app.New()
window := application.NewWindow("title")
window.Resize(fyne.NewSize(1920, 1080))
window.ShowAndRun()
}
you can check the demo application of Fyne.
$ go get fyne.io/fyne/v2/cmd/fyne_demo
$ fyne_demo
from your description here is an example with menu on the left, and the main block on the right:
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
w := a.NewWindow("Fyne Demo")
w.SetMaster()
content := container.NewMax()
title := widget.NewLabel("Component name")
intro := widget.NewLabel("An introduction would probably go\nhere, as well as a")
intro.Wrapping = fyne.TextWrapWord
tutorial := container.NewBorder(
container.NewVBox(title, widget.NewSeparator(), intro), nil, nil, nil, content)
split := container.NewHSplit(makeNav(), tutorial)
split.Offset = 0
w.SetContent(split)
w.Resize(fyne.NewSize(640, 460))
w.ShowAndRun()
}
func makeNav() fyne.CanvasObject {
tree := widget.NewTreeWithStrings(menuItems)
return container.NewBorder(nil, nil, nil, nil, tree)
}
var menuItems = map[string][]string{
"": {"welcome", "collections", "advanced"},
"collections": {"list", "table"},
}
output :
if you explore their demo on your go path source you can see the full function of (makeNav) that will make things clickable.
and to make (the left column will be fixed and the right column will stretch):
split.Offset = 0
It sounds like you are making a Border layout (something attached to one edge). If so then you can set the left to be minimum size and the content stretch to fill by doing:
container.NewBorder(nil, nil, left, nil, content)
(the parameters are top, bottom, left, right, middle).
If you want the user to control the split then do as suggested elsewhere and user container.NewHSplit(left, right).

How to resize window to dialog size in Fyne

I am using the Fyne package for Go. All I want to do is display a dialog, and have the window automatically resize to the maximum size of the dialog. I've gone over the documentation and looked at examples but I can't find it anywhere! The window is always 0x0 pixels. If I resize the window out I can tell that the dialog has a maximum height and width but I can't seem to feed that back into the size of the window. Here is my code:
import (
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/dialog"
)
var myApp fyne.App
func main() {
myApp = app.New()
myWindow := myApp.NewWindow("test")
cnfm := dialog.NewConfirm("Test Dialog!", "Are you sure you want to interact with this test dialog?", loseCallback, myWindow)
cnfm.Show()
cnfm.Resize(cnfm.MaxSize())
myWindow.Show()
// uncomment to have a window greater than 0x0
//myWindow.Resize(fyne.NewSize(375, 180))
myApp.Run()
}
func loseCallback(yes bool) {
myApp.Quit()
}
This seems really basic but I just can't find any way to achieve this! I've tried playing with layouts, containers, etc but nothing makes a difference, the window is always 0x0. I would really appreciate some help with this!
Fyne doesn't really have a concept of MaxSize. There is MinSize() (what a component should never be smaller than) and it's Size() which is the current size on screen.
Dialogs appear over the current content so are not constrained, or expaneded, by the content layout etc.
As a dialog is not designed to be the main content of a while I wonder if changing your content to be inside the window would be more suitable (as the dialog is really used when something in the app is happening and you want to interrupt it).
For example:
import (
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/container"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
)
var myApp fyne.App
func main() {
myApp = app.New()
myWindow := myApp.NewWindow("test")
yes := widget.NewButton("Yes", closeCallback)
yes.Importance = widget.HighImportance
myWindow.SetContent(container.NewVBox(
widget.NewLabel("Are you sure you want to interact with this test dialog?"),
container.NewHBox(layout.NewSpacer(),
widget.NewButton("No", closeCallback), yes,
layout.NewSpacer())))
myWindow.ShowAndRun()
}
func closeCallback() {
myApp.Quit()
}

how do I make wxWidgets (using golang) repaint window on MacOS?

Here's some sample code that puts a Gauge on the screen and make the progress bar increase 1 value every second. On MacOS I don't see the progress bar update unless I drag the window around or resize it manually with the mouse. Any idea how to force the whole thing to repaint? I'm calling f.Refresh() and f.Update()
package main
import "github.com/dontpanic92/wxGo/wx"
import "time"
var g wx.Gauge
type MyFrame struct {
wx.Frame
}
func (f *MyFrame) startUpload() {
for {
time.Sleep(time.Second)
g.SetValue(g.GetValue() + 1)
f.Refresh()
f.Update()
}
}
func NewMyFrame() MyFrame {
f := MyFrame{}
f.Frame = wx.NewFrame(wx.NullWindow, -1, "Test Thread")
mainSizer := wx.NewBoxSizer(wx.HORIZONTAL)
g = wx.NewGauge(f, wx.ID_ANY, 100, wx.DefaultPosition, wx.NewSize(600, 40), 0)
f.SetSizer(mainSizer)
mainSizer.Add(g, 100, wx.ALL|wx.EXPAND, 50)
f.Layout()
go f.startUpload()
return f
}
func main() {
wx1 := wx.NewApp()
f := NewMyFrame()
f.Show()
wx1.MainLoop()
return
}
Update: I've been reading http://docs.wxwidgets.org/trunk/overview_thread.html and I'm trying code like:
b := wx.NewPaintEvent()
f.GetEventHandler().QueueEvent(b)
instead of calling Refresh and Update but my wx.NewPaintEvent doesn't do anything. Maybe I'm making the wx.NewPaintEvent wrong? Or I'm adding it to the wrong EventHandler?
Generally speaking, doing anything with GUI objects from the non-main (i.e. not the one that initialized the library) thread is not supported by wxWidgets.
The usual workaround is to post an event to the main thread asking it to update the widget instead of doing it directly in the worker thread. In C++ this can be done easily and elegantly using CallAfter(), but I don't know enough about Go and wxGo to show you an example of doing it in this language.
author of wxGo fixed it:
https://github.com/dontpanic92/wxGo/issues/10
wx.Bind(f, wx.EVT_THREAD, func(e wx.Event) {
threadEvent := wx.ToThreadEvent(e)
the_gauge.SetValue(threadEvent.GetInt())
}, UPLOAD_WORKER_ID)
then in the thread:
threadEvent := wx.NewThreadEvent(wx.EVT_THREAD, UPLOAD_WORKER_ID)
threadEvent.SetInt(50)
f.QueueEvent(threadEvent)

Golang Iris Web - Serve 1x1 pixel

I'm new to Golang and trying to use a framework called iris.
My problem is how to serve 1x1 gif pixel, not using c.HTML context, but the way the browser title becomes 1x1 image gif. The image will be used for tracking.
Any help will be deeply appreciated.
Attention: I'm not really familiar with iris so the solution maybe not idiomatic.
package main
import (
"github.com/kataras/iris"
"image"
"image/color"
"image/gif"
)
func main() {
iris.Get("/", func(ctx *iris.Context) {
img := image.NewRGBA(image.Rect(0, 0, 1, 1)) //We create a new image of size 1x1 pixels
img.Set(0, 0, color.RGBA{255, 0, 0, 255}) //set the first and only pixel to some color, in this case red
err := gif.Encode(ctx.Response.BodyWriter(), img, nil) //encode the rgba image to gif, using gif.encode and write it to the response
if err != nil {
panic(err) //if we encounter some problems panic
}
ctx.SetContentType("image/gif") //set the content type so the browser can identify that our response is actually an gif image
})
iris.Listen(":8080")
}
Links for better understanding:
https://golang.org/pkg/image/
https://golang.org/pkg/image/gif
https://golang.org/pkg/image/color
https://golang.org/pkg/image/
https://godoc.org/github.com/valyala/fasthttp#Response

Resources