mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 18:05:35 +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
|
redraw js.Func
|
||||||
clipboardCallback js.Func
|
clipboardCallback js.Func
|
||||||
requestAnimationFrame js.Value
|
requestAnimationFrame js.Value
|
||||||
|
browserHistory js.Value
|
||||||
cleanfuncs []func()
|
cleanfuncs []func()
|
||||||
touches []js.Value
|
touches []js.Value
|
||||||
composing bool
|
composing bool
|
||||||
@@ -51,6 +52,7 @@ func NewWindow(win Callbacks, opts *Options) error {
|
|||||||
clipboard: js.Global().Get("navigator").Get("clipboard"),
|
clipboard: js.Global().Get("navigator").Get("clipboard"),
|
||||||
}
|
}
|
||||||
w.requestAnimationFrame = w.window.Get("requestAnimationFrame")
|
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.redraw = w.funcOf(func(this js.Value, args []js.Value) interface{} {
|
||||||
w.animCallback()
|
w.animCallback()
|
||||||
return nil
|
return nil
|
||||||
@@ -61,6 +63,7 @@ func NewWindow(win Callbacks, opts *Options) error {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
w.addEventListeners()
|
w.addEventListeners()
|
||||||
|
w.addHistory()
|
||||||
w.w = win
|
w.w = win
|
||||||
go func() {
|
go func() {
|
||||||
w.w.SetDriver(w)
|
w.w.SetDriver(w)
|
||||||
@@ -125,6 +128,15 @@ func (w *window) addEventListeners() {
|
|||||||
args[0].Call("preventDefault")
|
args[0].Call("preventDefault")
|
||||||
return nil
|
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.addEventListener(w.cnv, "mousemove", func(this js.Value, args []js.Value) interface{} {
|
||||||
w.pointerEvent(pointer.Move, 0, 0, args[0])
|
w.pointerEvent(pointer.Move, 0, 0, args[0])
|
||||||
return nil
|
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() {
|
func (w *window) flushInput() {
|
||||||
val := w.tarea.Get("value").String()
|
val := w.tarea.Get("value").String()
|
||||||
w.tarea.Set("value", "")
|
w.tarea.Set("value", "")
|
||||||
|
|||||||
Reference in New Issue
Block a user