From 689b317de99c1288ceaa2195ca369616f4ddd2ac Mon Sep 17 00:00:00 2001 From: Inkeliz Date: Sat, 21 Nov 2020 13:34:44 +0000 Subject: [PATCH] app/internal/window: add support for system.CommandBack on wasm To get the `popstate` we need to create a new entry into the browser history. Then, Gio will handle the "back" and "forward" of the page. In some browsers (Chrome 87/Edge 87): The user must click inside the window/page at least one time. It will not work if the user leaves the page (clicking back button) without interaction with Gio. Signed-off-by: Inkeliz --- app/internal/window/os_js.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/internal/window/os_js.go b/app/internal/window/os_js.go index 60768b6a..e4fb17cc 100644 --- a/app/internal/window/os_js.go +++ b/app/internal/window/os_js.go @@ -27,6 +27,7 @@ type window struct { redraw js.Func clipboardCallback js.Func requestAnimationFrame js.Value + browserHistory js.Value cleanfuncs []func() touches []js.Value composing bool @@ -51,6 +52,7 @@ func NewWindow(win Callbacks, opts *Options) error { clipboard: js.Global().Get("navigator").Get("clipboard"), } w.requestAnimationFrame = w.window.Get("requestAnimationFrame") + w.browserHistory = w.window.Get("history") w.redraw = w.funcOf(func(this js.Value, args []js.Value) interface{} { w.animCallback() return nil @@ -61,6 +63,7 @@ func NewWindow(win Callbacks, opts *Options) error { return nil }) w.addEventListeners() + w.addHistory() w.w = win go func() { w.w.SetDriver(w) @@ -125,6 +128,15 @@ func (w *window) addEventListeners() { args[0].Call("preventDefault") return nil }) + w.addEventListener(w.window, "popstate", func(this js.Value, args []js.Value) interface{} { + ev := &system.CommandEvent{Type: system.CommandBack} + w.w.Event(ev) + if ev.Cancel { + return w.browserHistory.Call("forward") + } + + return w.browserHistory.Call("back") + }) w.addEventListener(w.cnv, "mousemove", func(this js.Value, args []js.Value) interface{} { w.pointerEvent(pointer.Move, 0, 0, args[0]) return nil @@ -218,6 +230,10 @@ func (w *window) addEventListeners() { }) } +func (w *window) addHistory() { + w.browserHistory.Call("pushState", nil, nil, w.window.Get("location").Get("href")) +} + func (w *window) flushInput() { val := w.tarea.Get("value").String() w.tarea.Set("value", "")