-
Notifications
You must be signed in to change notification settings - Fork 5
/
layout.go
148 lines (127 loc) · 3.34 KB
/
layout.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
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const (
pageBackground = "BACKGROUND"
pageCover = "COVER"
pageMain = "MAIN"
)
type popupWindow struct {
name string
primitive tview.Primitive
}
var (
// structure of safebox windows
// layoutRoot
// |______layoutCover
// |______layoutMain
// |______layoutFooter
// |_______layoutShortCuts
// |______layoutBody
// |______layoutMainBody
// | |______layoutMainBodyTable
// |______layoutInfo
layoutRoot *tview.Pages
layoutCover *tview.Flex
layoutBody *tview.Flex
layoutMain *tview.Flex
layoutMainBody *tview.Flex
layoutMainBodyTable *tview.Table
layoutInfo *tview.Flex
layoutFooter *tview.Flex
layoutShortcuts *tview.TextView
)
var (
keyNames = map[tcell.Key]string{
tcell.KeyF1: "F1",
tcell.KeyF2: "F2",
tcell.KeyF3: "F3",
tcell.KeyF4: "F4",
tcell.KeyEsc: "ESC",
tcell.KeyCtrlG: "F1|Ctrl-G",
tcell.KeyCtrlL: "F2|Ctrl-L",
tcell.KeyCtrlP: "F3|Ctrl-P",
tcell.KeyCtrlC: "Ctrl-C",
}
shortCuts = map[tcell.Key]string{
tcell.KeyCtrlG: "Generate master key",
tcell.KeyCtrlL: "Load master key",
tcell.KeyCtrlP: "Change password",
tcell.KeyEsc: "Back",
tcell.KeyCtrlC: "Quit",
}
)
// global function keys handling
func globalInputCapture(event *tcell.EventKey) *tcell.EventKey {
// check cover page
name, _ := layoutRoot.GetFrontPage()
if name == pageCover {
layoutRoot.SwitchToPage(pageMain)
return nil
}
// non cover page
switch event.Key() {
case tcell.KeyF1, tcell.KeyCtrlG:
if name, _ := layoutRoot.GetFrontPage(); name == pageMain {
showKeyEntropyInputWindow()
layoutShortcuts.Highlight(fmt.Sprint(tcell.KeyF1))
}
return nil
case tcell.KeyF2, tcell.KeyCtrlL:
if name, _ := layoutRoot.GetFrontPage(); name == pageMain {
showLoadKeyWindow()
layoutShortcuts.Highlight(fmt.Sprint(tcell.KeyF2))
}
return nil
case tcell.KeyF3, tcell.KeyCtrlP:
if name, _ := layoutRoot.GetFrontPage(); name == pageMain {
showChangePasswordWindow()
layoutShortcuts.Highlight(fmt.Sprint(tcell.KeyF3))
}
return nil
case tcell.KeyEsc:
if name, _ := layoutRoot.GetFrontPage(); name != pageMain {
layoutRoot.RemovePage(name)
if name, _ := layoutRoot.GetFrontPage(); name == pageMain {
layoutShortcuts.Highlight(layoutShortcuts.GetHighlights()...)
}
}
return nil
case tcell.KeyCtrlC:
app.Stop()
return nil
default:
return event
}
}
// init pages
func initLayouts() {
// Main frame
layoutMainBody = mainFrameWindow()
layoutCover = coverPage()
layoutInfo = infoWindow()
layoutFooter = footerWindow()
layoutBody = tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(layoutInfo, 0, 20, false).
AddItem(layoutMainBody, 0, 80, true)
// Create the layout.
layoutMain = tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(layoutBody, 0, 1, true).
AddItem(layoutFooter, 1, 1, false)
layoutRoot = tview.NewPages().
AddPage(pageMain, layoutMain, true, false).
AddPage(pageCover, layoutCover, true, true)
layoutRoot.SwitchToPage(pageCover)
// Input capture
app.SetInputCapture(globalInputCapture)
}
func refresh() {
refreshFooter()
refreshInfo()
refreshMainFrame()
}