-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
app.go
94 lines (77 loc) · 2.02 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//go:generate go run gen/html.go
//go:generate go run gen/scripts.go
//go:generate go fmt
package app
import (
"net/url"
"github.com/maxence-charriere/go-app/v6/pkg/log"
)
var (
// LocalStorage is a storage that uses the browser local storage associated
// to the document origin. Data stored are encrypted and has no expiration
// time.
LocalStorage BrowserStorage
// SessionStorage is a storage that uses the browser session storage
// associated to the document origin. Data stored are encrypted and expire
// when the page session ends.
SessionStorage BrowserStorage
// NotFound is the ui element that is displayed when a request is not
// routed.
NotFound UI = ¬Found{}
routes = make(map[string]UI)
uiChan = make(chan func(), 256)
)
// EventHandler represents a function that can handle HTML events.
type EventHandler func(src Value, e Event)
// Route binds the requested path to the given UI node.
func Route(path string, n UI) {
routes[path] = n
}
// Run starts the wasm app and displays the UI node associated with the
// requested URL path.
//
// It panics if Go architecture is not wasm.
func Run() {
run()
}
// Navigate navigates to the given URL.
func Navigate(rawurl string) {
Dispatch(func() {
u, err := url.Parse(rawurl)
if err != nil {
log.Error("navigating to page failed").
T("url", rawurl).
T("error", err).
Panic()
}
if u.String() == Window().URL().String() {
return
}
if err = navigate(u, true); err != nil {
log.Error("navigating to page failed").
T("url", u).
T("error", err).
Panic()
}
})
}
// Reload reloads the current page.
func Reload() {
Dispatch(func() {
reload()
})
}
// Window returns the JavaScript "window" object.
func Window() BrowserWindow {
return window
}
// NewContextMenu displays a context menu filled with the given menu items.
func NewContextMenu(menuItems ...MenuItemNode) {
Dispatch(func() {
newContextMenu(menuItems...)
})
}
// Dispatch executes the given function on the UI goroutine.
func Dispatch(f func()) {
uiChan <- f
}