Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang/Fyne: (Re-)size How to detect

Tags:

go

fyne

Lately I started building a graphic app with Fyne whch requires drawing curved lines (such as a sinus wave). I managed to do that by using the NewWithoutLayout container.

However, unlike the other containers that is a container which does not resize when the parent container or window resizes. That's obvious because the container is used for hardcoded positions, so I have to redraw myself. But for that I need to know:

a) when the app or window is resized (in other words: a resize event) and

b) what the (new) size is of the canvas on which the lines in NewWithoutLayout are drawn.

The first one I haven't been able to find and the second one always gives {1,1}.

Is there no way to get these? Or must we conclude that Fyne is not for graphic apps like this one?

Tried Size() functions of just about everything that is hierarchically higer than the NewWithoutLayout contaner. They all give {1,1} and in one occassion {9,9}

Seached the internet for clues about a resize event, but could not find any.

like image 211
Ardjay Avatar asked Nov 16 '25 07:11

Ardjay


1 Answers

I searched with various search engines to find a specific solution but I didn't find one on the internet so I made up my own. I think I can help you with detecting a resize of the window. I would implement it like this:

package main

import (
        "fmt"
        "time"

        "fyne.io/fyne/v2/app"
        "fyne.io/fyne/v2/container"
        "fyne.io/fyne/v2/widget"
)

func main() {
        a := app.New()
        w := a.NewWindow("Hello")

        hello := widget.NewLabel("Hello Fyne!")
        w.SetContent(container.NewVBox(
                hello,
                widget.NewButton("Hi!", func() {
                        hello.SetText("Welcome :)")
                }),
        ))
        go func() {
                widthBefore := w.Content().Size().Width
                heightBefore := w.Content().Size().Height
                for {
                        width := w.Content().Size().Width
                        height := w.Content().Size().Height
                        if width != widthBefore || height != heightBefore {
                                // window was resized
                                fmt.Println("Window was resized!")
                        }
                        widthBefore = width
                        heightBefore = height
                        time.Sleep(20 * time.Millisecond) // you may want to change this
                }
        }()

        w.ShowAndRun()
}

I don't know if this is good enough, but I have to say that I didn't found anything better. This is the basic program from the Fyne GitHub to which I added a goroutine which checks every 20ms if the window got resized. I hope I could help you at least with something.

like image 182
mangosaftlama Avatar answered Nov 17 '25 20:11

mangosaftlama