/
ui.go
211 lines (166 loc) · 4.79 KB
/
ui.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ui
import (
"strings"
"syscall"
"github.com/darkhz/bluetuith/bluez"
"github.com/darkhz/bluetuith/cmd"
"github.com/darkhz/bluetuith/network"
"github.com/darkhz/bluetuith/theme"
"github.com/darkhz/tview"
"github.com/gdamore/tcell/v2"
)
type Application struct {
// Pages holds the DeviceTable, along with
// any menu popups that will be added.
Pages *tview.Pages
// Layout holds the layout of the application.
Layout *tview.Flex
// Status holds the status bar.
Status Status
// Bluez holds the current bluez DBus connection.
Bluez *bluez.Bluez
// Obex holds the current bluez obex DBus connection.
Obex *bluez.Obex
// network holds the current network connection.
Network *network.Network
suspend bool
warn, page string
focus tview.Primitive
pageContext cmd.KeyContext
*tview.Application
}
var UI Application
// StartUI starts the UI.
func StartUI() {
UI.Application = tview.NewApplication()
UI.Pages = tview.NewPages()
box := tview.NewBox().
SetBackgroundColor(theme.GetColor(theme.ThemeMenuBar))
menuArea := tview.NewFlex().
AddItem(menuBar(), 0, 1, false).
AddItem(box, 1, 0, false).
AddItem(adapterStatusView(), 0, 1, false)
menuArea.SetBackgroundColor(theme.GetColor(theme.ThemeBackground))
flex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(menuArea, 1, 0, false).
AddItem(nil, 1, 0, false).
AddItem(deviceTable(), 0, 10, true)
flex.SetBackgroundColor(theme.GetColor(theme.ThemeBackground))
UI.focus = flex
UI.page = "main"
UI.pageContext = cmd.KeyContextApp
UI.Pages.AddPage("main", flex, true, true)
UI.Pages.SetBackgroundColor(theme.GetColor(theme.ThemeBackground))
UI.Pages.SetChangedFunc(func() {
page, _ := UI.Pages.GetFrontPage()
contexts := map[string]cmd.KeyContext{
"main": cmd.KeyContextDevice,
"filepicker": cmd.KeyContextFiles,
"progressview": cmd.KeyContextProgress,
}
switch page {
case "main", "filepicker", "progressview":
UI.page = page
UI.pageContext = contexts[page]
default:
UI.pageContext = cmd.KeyContextApp
}
showStatusHelp(page)
})
UI.Layout = tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(UI.Pages, 0, 10, true).
AddItem(statusBar(), UI.Status.itemCount, 0, false)
UI.Layout.SetBackgroundColor(theme.GetColor(theme.ThemeBackground))
UI.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
operation := cmd.KeyOperation(event)
if e, ok := cmd.NavigationKey(operation, event); ok {
focused := UI.GetFocus()
if focused != nil && focused.InputHandler() != nil {
focused.InputHandler()(e, nil)
return nil
}
}
switch operation {
case cmd.KeySuspend:
UI.suspend = true
case cmd.KeyCancel:
cancelOperation(true)
}
return tcell.NewEventKey(event.Key(), event.Rune(), event.Modifiers())
})
UI.SetMouseCapture(func(event *tcell.EventMouse, action tview.MouseAction) (*tcell.EventMouse, tview.MouseAction) {
return modalMouseHandler(event, action)
})
UI.SetBeforeDrawFunc(func(t tcell.Screen) bool {
ResizeModal()
suspendUI(t)
return false
})
UI.Pages.SwitchToPage("main")
statusHelpArea(true)
setupDevices()
displayWarning()
updateAdapterStatus(UI.Bluez.GetCurrentAdapter())
setAdapterStates()
connectDeviceByAddress()
InfoMessage("bluetuith is ready.", false)
if err := UI.SetRoot(UI.Layout, true).SetFocus(UI.focus).EnableMouse(true).Run(); err != nil {
cmd.PrintError("Cannot initialize application", err)
}
}
// StopUI stops the UI.
func StopUI() {
stopStatus()
UI.Stop()
}
// SetConnections sets the connections to bluez and networkmanager.
func SetConnections(b *bluez.Bluez, o *bluez.Obex, n *network.Network, warn string) {
UI.Bluez = b
UI.Obex = o
UI.Network = n
UI.warn = warn
}
// suspendUI suspends the application.
func suspendUI(t tcell.Screen) {
if !UI.suspend {
return
}
UI.suspend = false
if err := t.Suspend(); err != nil {
return
}
if err := syscall.Kill(syscall.Getpid(), syscall.SIGSTOP); err != nil {
return
}
if err := t.Resume(); err != nil {
return
}
}
func displayWarning() {
if UI.warn == "" || cmd.IsPropertyEnabled("no-warning") {
return
}
height := 10
if strings.Count(UI.warn, "\n") > 2 {
height += 4
}
UI.warn += "\n\n-- Press any key to close this dialog --\n\n"
warningTextView := tview.NewTextView()
warningTextView.SetText(UI.warn)
warningTextView.SetDynamicColors(true)
warningTextView.SetTextAlign(tview.AlignCenter)
warningTextView.SetTextColor(theme.GetColor(theme.ThemeText))
warningTextView.SetBackgroundColor(theme.GetColor(theme.ThemeBackground))
modal := NewModal("warning", "Warning", warningTextView, height, 60)
warningTextView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
modal.Exit(false)
return event
})
UI.focus = modal.Flex
modal.Show()
}
func confirmQuit() bool {
return SetInput("Quit (y/n)?") == "y"
}