Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back button killed webview #1

Open
diyism opened this issue Aug 15, 2021 · 0 comments
Open

Back button killed webview #1

diyism opened this issue Aug 15, 2021 · 0 comments

Comments

@diyism
Copy link
Owner

diyism commented Aug 15, 2021

package main

import
(
	"image/color"
	"log"
	"os"

	"gioui.org/app"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/text"
	"gioui.org/widget/material"
	"gioui.org/font/gofont"

	"github.com/inkeliz/gowebview"
)

func main()
{	go func()
	{	w := app.NewWindow()
		if err := loop(w); err != nil
		{	log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

func loop(w *app.Window) error
{	th := material.NewTheme(gofont.Collection())
	var ops op.Ops
	var wv  gowebview.WebView
	var has_back bool
	for
	{	e:=<-w.Events()
		switch e:=e.(type)
		{	case app.ViewEvent:
				if wv==nil
				{	go func()
					{	var err error
						wv,err = gowebview.New(&gowebview.Config{URL:"https://google.com/ncr", WindowConfig:&gowebview.WindowConfig{Title:"Hello World", Window:e.View, VM:app.JavaVM()}})
						if err!=nil {panic(err)}
						defer wv.Destroy()
						wv.Run()
					}()
				} else
				{	wv.SetVisibility(gowebview.VisibilityMaximized)
					log.Println("===================resume webview==============")
				}
			case *system.CommandEvent:
				if e.Type==system.CommandBack
				{	if has_back==false
					{	log.Println("===================back button==============", wv)
						e.Cancel = true
						wv.SetVisibility(gowebview.VisibilityMinimized)
						has_back=true
					} else
					{	log.Println("===================back button second time==============", wv)
						e.Cancel=false
						has_back=false
					}
				}
			case system.DestroyEvent:
				return e.Err
			case system.FrameEvent:
				gtx := layout.NewContext(&ops, e)
				l := material.H1(th, "Hello, gowebview")
				maroon := color.NRGBA{R: 127, G: 0, B: 0, A: 255}
				l.Color = maroon
				l.Alignment = text.Middle
				l.Layout(gtx)
				e.Frame(gtx.Ops)
		}
	}
}

javac -source 8 -target 8 -bootclasspath $ANDROID_SDK_ROOT/platforms/android-29/android.jar -d /tmp/gowebview/classes /opt/lib/go/src/github.com/inkeliz/gowebview/gowebview_android.java

jar cf /opt/lib/go/src/github.com/inkeliz/gowebview/gowebview_android.jar -C /tmp/gowebview/classes .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant