/
view.go
76 lines (60 loc) · 1.33 KB
/
view.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
package view
import (
"github.com/darkhz/invidtui/ui/app"
"github.com/darkhz/invidtui/ui/theme"
"github.com/darkhz/tview"
"github.com/gdamore/tcell/v2"
)
// View describes a view.
type View interface {
Name() string
Tabs() app.Tab
Init() bool
Exit() bool
Keybindings(event *tcell.EventKey) *tcell.EventKey
Primitive() tview.Primitive
ThemeProperty() theme.ThemeProperty
}
var views []View
// SetView sets the current view.
func SetView(viewIface View, noappend ...struct{}) {
if !viewIface.Init() {
return
}
app.SetTab(viewIface.Tabs(), viewIface.ThemeProperty().Context)
app.UI.Pages.AddAndSwitchToPage(viewIface.Name(), viewIface.Primitive(), true)
app.SetPrimaryFocus()
for _, iface := range views {
if iface == viewIface && noappend == nil {
return
}
}
if noappend != nil {
return
}
views = append(views, viewIface)
}
// CloseView closes the current view.
func CloseView() {
vlen := len(views)
if !views[vlen-1].Exit() {
return
}
if vlen > 1 {
vlen--
views = views[:vlen]
}
SetView(views[vlen-1], struct{}{})
app.SetPrimaryFocus()
}
// PreviousView returns the view before the one currently displayed.
func PreviousView() View {
if len(views) < 2 {
return nil
}
return views[len(views)-2]
}
// GetCurrentView returns the current view.
func GetCurrentView() View {
return views[len(views)-1]
}