forked from loov/lensm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exe_ui.go
280 lines (241 loc) · 5.84 KB
/
exe_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"image"
"os"
"time"
"gioui.org/app"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/widget"
"gioui.org/widget/material"
)
type ExeUIConfig struct {
Exe string
Watch bool
Context int
}
type ExeUI struct {
Windows *Windows
Theme *material.Theme
Config ExeUIConfig
LoadError error
// Currently loaded executable.
Obj Obj
Symbols *SymbolSelectionList
// Active code view.
Code CodeUI
// Other ExeUI elements.
OpenInNew widget.Clickable
}
func NewExeUI(windows *Windows, theme *material.Theme) *ExeUI {
ui := &ExeUI{}
ui.Windows = windows
ui.Theme = theme
ui.Symbols = NewSymbolList(theme)
return ui
}
func (ui *ExeUI) Run(w *app.Window) error {
var ops op.Ops
exited := make(chan struct{})
defer close(exited)
exeLoaded := make(chan *GoObj, 1)
exeLoadError := make(chan error, 1)
loadFinished := func(exe *GoObj, err error) {
if err == nil {
select {
case <-exeLoaded:
default:
}
exeLoaded <- exe
} else {
select {
case <-exeLoadError:
default:
}
exeLoadError <- err
}
}
go func() {
var lastModTime time.Time
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
func() {
stat, err := os.Stat(ui.Config.Exe)
if err != nil {
loadFinished(nil, err)
return
}
if stat.ModTime().Equal(lastModTime) {
return
}
lastModTime = stat.ModTime()
exe, err := LoadExe(ui.Config.Exe)
loadFinished(exe, err)
}()
if !ui.Config.Watch {
break
}
select {
case <-tick.C:
case <-exited:
return
}
}
}()
for {
select {
case err := <-exeLoadError:
ui.LoadError = err
w.Invalidate()
case exe := <-exeLoaded:
ui.SetExe(exe)
w.Invalidate()
case e := <-w.Events():
switch e := e.(type) {
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
ui.Layout(gtx)
e.Frame(gtx.Ops)
case system.DestroyEvent:
return e.Err
}
}
}
}
func (ui *ExeUI) SetExe(exe Obj) {
if ui.Obj != nil {
_ = ui.Obj.Close()
}
ui.Obj = exe
ui.Symbols.SetSymbols(exe.Symbols())
if ui.Symbols.Selected != "" {
for _, sym := range exe.Symbols() {
if sym.Name() == ui.Symbols.Selected {
ui.Code.Code = sym.Load(ui.loadOptions())
}
}
}
}
func (ui *ExeUI) loadOptions() Options {
return Options{Context: ui.Config.Context}
}
func (ui *ExeUI) Layout(gtx layout.Context) {
for ui.OpenInNew.Clicked() {
ui.openInNew(gtx)
}
if ui.Symbols.Selected == "" {
ui.Symbols.SelectIndex(0)
}
if !ui.Code.Loaded() || ui.Code.Name != ui.Symbols.Selected {
selected := ui.Symbols.SelectedSymbol
if selected != nil {
ui.Code.Code = selected.Load(ui.loadOptions())
}
}
layout.Flex{
Axis: layout.Horizontal,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
gtx.Constraints = layout.Exact(image.Point{
X: gtx.Metric.Sp(10 * 20),
Y: gtx.Constraints.Max.Y,
})
return ui.Symbols.Layout(ui.Theme, gtx)
}),
layout.Rigid(VerticalLine{Width: 1, Color: splitterColor}.Layout),
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if ui.LoadError != nil {
return material.Body1(ui.Theme, ui.LoadError.Error()).Layout(gtx)
}
if !ui.Code.Loaded() {
return layout.Dimensions{}
}
txt := material.Body1(ui.Theme, ui.Code.Code.Name)
txt.TextSize *= 1.2
inset := layout.Inset{Top: 4, Left: 4, Right: 4, Bottom: 2}
return inset.Layout(gtx, txt.Layout)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if ui.LoadError != nil || !ui.Code.Loaded() {
return layout.Dimensions{}
}
txt := material.Body1(ui.Theme, "file: "+ui.Code.Code.File)
txt.Font.Style = text.Italic
inset := layout.Inset{Top: 2, Left: 4, Right: 4, Bottom: 4}
return inset.Layout(gtx, txt.Layout)
}),
layout.Rigid(HorizontalLine{Height: 1, Color: splitterColor}.Layout),
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
if ui.LoadError != nil {
return layout.Dimensions{}
}
gtx.Constraints = layout.Exact(gtx.Constraints.Max)
return layout.Stack{
Alignment: layout.SE,
}.Layout(gtx,
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
return CodeUIStyle{
CodeUI: &ui.Code,
TryOpen: ui.tryOpen,
Theme: ui.Theme,
TextHeight: ui.Theme.TextSize,
LineHeight: ui.Theme.TextSize * 1.2,
}.Layout(gtx)
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
button := material.IconButton(ui.Theme, &ui.OpenInNew, OpenInNewIcon, "Open in separate window")
button.Size = 16
button.Inset = layout.UniformInset(12)
return layout.UniformInset(2).Layout(gtx, button.Layout)
}),
)
}),
)
}),
)
}
func (ui *ExeUI) tryOpen(gtx layout.Context, call string) {
var sym Symbol
for _, target := range ui.Obj.Symbols() {
if target.Name() == call {
sym = target
break
}
}
if sym == nil {
return
}
load := sym.Load(ui.loadOptions())
ui.Symbols.Selected = load.Name
ui.Symbols.SelectedSymbol = sym
ui.Symbols.List.Selected = -1
for i, fil := range ui.Symbols.Filtered {
if fil == sym {
ui.Symbols.List.Selected = i
break
}
}
ui.Code.Code = load
if ui.Symbols.Selected == "" {
ui.Symbols.SelectIndex(0)
}
ui.Code.ResetScroll()
}
func (ui *ExeUI) openInNew(gtx layout.Context) {
state := ui.Code
style := CodeUIStyle{
Theme: ui.Theme,
CodeUI: &state,
TextHeight: ui.Theme.TextSize,
LineHeight: ui.Theme.TextSize * 14 / 12,
}
size := gtx.Constraints.Max
size.X = int(float32(size.X) / gtx.Metric.PxPerDp)
size.Y = int(float32(size.Y) / gtx.Metric.PxPerDp)
ui.Windows.Open(ui.Code.Name, size, WidgetWindow(style.Layout))
}