mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
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 <inkeliz@inkeliz.com>
This commit is contained in:
@@ -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", "")
|
||||
|
||||
Reference in New Issue
Block a user