-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
81 lines (69 loc) · 1.59 KB
/
router.go
File metadata and controls
81 lines (69 loc) · 1.59 KB
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
package giorouter
import (
"gioui.org/layout"
"gioui.org/widget/material"
)
// Route
type Route interface {
Layout(gtx layout.Context) layout.Dimensions
}
// Routes
type Routes map[string]Route
// Router holds the precious information of the router
type Router struct {
C chan byte
Routes Routes
Stack []string
Th *material.Theme
}
// NewRouter returns an instance of a Router
func NewRouter(th *material.Theme) Router {
return Router{
C: make(chan byte, 1),
Routes: make(Routes),
Th: th,
}
}
// SetRoutes links the provided paths to the Route
func (r *Router) SetRoutes(routes Routes, initialRoute string) {
r.Routes = routes
r.Push(initialRoute)
}
// Push adds a route to the stack and ask for a redraw
func (r *Router) Push(route string) {
if _, ok := r.Routes[route]; ok {
r.Stack = append(r.Stack, route)
r.Redraw()
}
}
// Top returns the current route
func (r Router) Top() string {
l := len(r.Stack)
if l == 0 {
return ""
}
return r.Stack[l-1]
}
// Pop pops a route out of the Routes stack and ask for a redraw (basically go back)
func (r *Router) Pop() string {
if !r.CanPop() {
return ""
}
l := len(r.Stack)
top := r.Stack[l-1]
r.Stack = r.Stack[:l-1]
r.Redraw()
return top
}
// Redraw sends an event through the Router'c Channel to ask for an immediate redraw
func (r *Router) Redraw() {
r.C <- 0
}
// CanPop verifies if a previous route is available
func (r *Router) CanPop() bool {
return len(r.Stack) > 1
}
// Layout draws the current route
func (r *Router) Layout(gtx layout.Context) layout.Dimensions {
return r.Routes[r.Top()].Layout(gtx)
}