forked from pion/obs-wormhole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
138 lines (112 loc) · 2.84 KB
/
router.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
type Navigator interface {
Push(path string, ctx interface{}) error
Pop() error
Reset()
}
type Page interface {
fyne.Widget
BeforeDestroy()
}
type routerRenderer struct {
router *Router
}
func (r *routerRenderer) Layout(size fyne.Size) {
r.router.currentPage.Resize(size)
}
func (r *routerRenderer) MinSize() fyne.Size {
return r.router.currentPage.MinSize()
}
func (r *routerRenderer) BackgroundColor() color.Color {
return theme.BackgroundColor()
}
func (r *routerRenderer) Objects() []fyne.CanvasObject {
return []fyne.CanvasObject{r.router.currentPage}
}
func (r *routerRenderer) Destroy() {}
func (r *routerRenderer) Refresh() {
r.router.currentPage.Refresh()
}
type PageBuilder func(navigator Navigator, ctx interface{}) (Page, error)
type RouterConfig struct {
routes map[string]PageBuilder
initialPath string
}
func (cfg *RouterConfig) Route(path string, pageBuilder PageBuilder) *RouterConfig {
if cfg.routes == nil {
cfg.routes = make(map[string]PageBuilder)
}
cfg.routes[path] = pageBuilder
// by default if initialPath is not defined, used whatever path that was initialized first
if cfg.initialPath == "" {
cfg.initialPath = path
}
return cfg
}
func (cfg *RouterConfig) InitialPath(path string) *RouterConfig {
cfg.initialPath = path
return cfg
}
func (cfg *RouterConfig) Build() (*Router, error) {
if len(cfg.routes) == 0 {
return nil, fmt.Errorf("routes can't be empty")
}
buildInitialPage, ok := cfg.routes[cfg.initialPath]
if !ok {
return nil, fmt.Errorf("%s doesn't exist in routes: %v", cfg.initialPath, cfg.routes)
}
router := Router{
RouterConfig: cfg,
}
initialPage, err := buildInitialPage(&router, nil)
if err != nil {
return nil, err
}
router.currentPage = initialPage
router.ExtendBaseWidget(&router)
return &router, nil
}
type Router struct {
widget.BaseWidget
*RouterConfig
history []Page
currentPage Page
}
func (router *Router) CreateRenderer() fyne.WidgetRenderer {
return &routerRenderer{router: router}
}
func (router *Router) Push(path string, ctx interface{}) error {
buildNextPage, ok := router.routes[path]
if !ok {
return fmt.Errorf("%s doesn't exist in routes: %v", path, router.routes)
}
router.history = append(router.history, router.currentPage)
nextPage, err := buildNextPage(router, ctx)
if err != nil {
return err
}
router.currentPage = nextPage
router.Refresh()
return nil
}
func (router *Router) Pop() error {
if len(router.history) == 0 {
return fmt.Errorf("there's no more pages in history")
}
router.currentPage.BeforeDestroy()
router.currentPage = router.history[len(router.history)-1]
router.history = router.history[:len(router.history)-1]
router.Refresh()
return nil
}
func (router *Router) Reset() {
for router.Pop() == nil {
}
}