How to teardown a zserge webview window

Dear gophers,

In a utility I am working on, I use the brilliant webview code by zserge
( https://github.com/zserge/webview/blob/master/examples/page-load-go/main.go )
to create window notifications. In my utility, I’ve used runDataURL() to open the windows.

It all works very well, but now I want to make go auto-close the window
after x seconds, rather than expecting/waiting for the end-user to close it interactively.
How can this be achieved?
I’ve tried playing around using goroutines and channels, but so far without success.

I’d be very grateful for any guidance on the matter.

From the example you shared and the docs for the package it seems all you have to do to close the window is call the Exit() function. So instead of deferring it you could try to block for however long with a wait or something and then call the Exit().

Thanks for the suggestion, but I
cannot get it to work. The window
stays open until main exits. Here’s what I do:

package main

import (
	"fmt"
	"time"
	"net/url"
	"github.com/zserge/webview"
)

func main() {
	go runDataURL(400, 400)
	fmt.Println("Immediately back in main, hopefully window will be closed in 2 seconds")
	fmt.Println("Meanwhile, sleeping for 10 secs ....")
	time.Sleep(10 * time.Second)
} 

func runDataURL(windowWidth int, windowHeight int) {
	msg := "Teardown in 2 sec"

	w := webview.New(webview.Settings{
		Width:     windowWidth,
		Height:    windowHeight,
		Title: "Some window title",
		URL:   "data:text/html," + url.PathEscape(msg),
	})
	fmt.Println("In func runDataURL, sleeping for 2 seconds ...")
	w.Run()
	time.Sleep(2 * time.Second)
	w.Exit()
	fmt.Println("Did I manage to close?")	
}

I got the idea to write it as a command and use exec.Command to run custom windows whenever you want. Here’s the repo if you would like to check it out:

Hi again, Curtis - and thanks a lot for your effort to put this together.
Although I was hoping to manage this without calling any external
component, I’ll give it a go and check if it meets the utility requirements.

Thanks again

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.