Skip to content

Commit 757f4a9

Browse files
authored
Merge pull request #5825 from Jacalz/speedup-wasm-device-handling
2 parents 3ff4700 + b7c022b commit 757f4a9

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

internal/driver/glfw/device_wasm.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,36 @@ import (
1111
)
1212

1313
var (
14-
isMobile = regexp.MustCompile("Android|iPhone|iPad|iPod").
15-
MatchString(js.Global().Get("navigator").Get("userAgent").String())
16-
isMacOS = strings.Contains(js.Global().Get("window").Get("navigator").Get("platform").String(), "Mac")
14+
isMobile bool
15+
isMacOS bool
16+
17+
setCursor func(name string)
18+
19+
blurDummyEntry func(...any) js.Value
20+
focusDummyEntry func(...any) js.Value
1721
)
1822

19-
var dummyEntry = js.Global().Get("document").Call("getElementById", "dummyEntry")
23+
func init() {
24+
navigator := js.Global().Get("navigator")
25+
isMobile = regexp.MustCompile("Android|iPhone|iPad|iPod").
26+
MatchString(navigator.Get("userAgent").String())
27+
isMacOS = strings.Contains(navigator.Get("platform").String(), "Mac")
28+
29+
document := js.Global().Get("document")
30+
style := document.Get("body").Get("style")
31+
setStyleProperty := style.Get("setProperty").Call("bind", style)
32+
setCursor = func(name string) {
33+
setStyleProperty.Invoke("cursor", name)
34+
}
35+
36+
dummyEntry := document.Call("getElementById", "dummyEntry")
37+
if dummyEntry.IsNull() {
38+
return
39+
}
40+
41+
blurDummyEntry = dummyEntry.Get("blur").Call("bind", dummyEntry).Invoke
42+
focusDummyEntry = dummyEntry.Get("focus").Call("bind", dummyEntry).Invoke
43+
}
2044

2145
func (*glDevice) IsMobile() bool {
2246
return isMobile
@@ -28,17 +52,17 @@ func (*glDevice) SystemScaleForWindow(w fyne.Window) float32 {
2852
}
2953

3054
func (*glDevice) hideVirtualKeyboard() {
31-
if dummyEntry.IsNull() {
55+
if blurDummyEntry == nil {
3256
return
3357
}
34-
dummyEntry.Call("blur")
58+
blurDummyEntry()
3559
}
3660

3761
func (*glDevice) showVirtualKeyboard() {
38-
if dummyEntry.IsNull() {
62+
if focusDummyEntry == nil {
3963
return
4064
}
41-
dummyEntry.Call("focus")
65+
focusDummyEntry()
4266
}
4367

4468
func connectKeyboard(c *glCanvas) {

internal/driver/glfw/window_wasm.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
_ "image/png" // for the icon
88
"runtime"
99
"sync"
10-
"syscall/js"
1110
"time"
1211

1312
"fyne.io/fyne/v2"
@@ -20,8 +19,6 @@ import (
2019
"github.com/fyne-io/glfw-js"
2120
)
2221

23-
var style = js.Global().Get("document").Get("body").Get("style")
24-
2522
type Cursor struct {
2623
JSName string
2724
}
@@ -217,7 +214,7 @@ func fyneToNativeCursor(cursor desktop.Cursor) (*Cursor, bool) {
217214
}
218215

219216
func (w *window) SetCursor(cursor *Cursor) {
220-
style.Call("setProperty", "cursor", cursor.JSName)
217+
setCursor(cursor.JSName)
221218
}
222219

223220
func (w *window) setCustomCursor(rawCursor *Cursor, isCustomCursor bool) {

0 commit comments

Comments
 (0)