-
Notifications
You must be signed in to change notification settings - Fork 124
/
fileui.go
305 lines (262 loc) · 6.31 KB
/
fileui.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"image"
"os"
"time"
"gioui.org/app"
"gioui.org/font"
"gioui.org/io/event"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/widget"
"gioui.org/widget/material"
"loov.dev/lensm/internal/disasm"
"loov.dev/lensm/internal/goobj"
"loov.dev/lensm/internal/wasmobj"
)
var workInProgressWASM bool
type FileUIConfig struct {
Path string
Watch bool
Context int
}
type FileUI struct {
Windows *Windows
Theme *material.Theme
Config FileUIConfig
LoadError error
// Currently loaded executable.
File disasm.File
Funcs *FilterList[disasm.Func]
// Active code view.
Code CodeUI
// Other FileUI elements.
OpenInNew widget.Clickable
}
func NewExeUI(windows *Windows, theme *material.Theme) *FileUI {
ui := &FileUI{}
ui.Windows = windows
ui.Theme = theme
ui.Funcs = NewFilterList[disasm.Func](theme)
return ui
}
func (ui *FileUI) Run(w *app.Window) error {
var ops op.Ops
exited := make(chan struct{})
defer close(exited)
fileLoaded := make(chan disasm.File, 1)
fileLoadError := make(chan error, 1)
loadFinished := func(exe disasm.File, err error) {
if err == nil {
select {
case <-fileLoaded:
default:
}
fileLoaded <- exe
} else {
select {
case <-fileLoadError:
default:
}
fileLoadError <- err
}
}
go func() {
var lastModTime time.Time
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
func() {
stat, err := os.Stat(ui.Config.Path)
if err != nil {
loadFinished(nil, err)
return
}
if stat.ModTime().Equal(lastModTime) {
return
}
lastModTime = stat.ModTime()
if workInProgressWASM {
loadFinished(wasmobj.Load(ui.Config.Path))
} else {
loadFinished(goobj.Load(ui.Config.Path))
}
}()
if !ui.Config.Watch {
break
}
select {
case <-tick.C:
case <-exited:
return
}
}
}()
events := make(chan event.Event)
acks := make(chan struct{})
go func() {
for {
ev := w.Event()
events <- ev
<-acks
if _, ok := ev.(app.DestroyEvent); ok {
return
}
}
}()
for {
select {
case err := <-fileLoadError:
ui.LoadError = err
w.Invalidate()
case file := <-fileLoaded:
ui.SetFile(file)
w.Invalidate()
case e := <-events:
switch e := e.(type) {
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
ui.Layout(gtx)
e.Frame(gtx.Ops)
case app.DestroyEvent:
acks <- struct{}{}
return e.Err
}
acks <- struct{}{}
}
}
}
func (ui *FileUI) SetFile(file disasm.File) {
if ui.File != nil {
_ = ui.File.Close()
}
ui.File = file
ui.Funcs.SetItems(file.Funcs())
if ui.Funcs.Selected != "" {
for _, fn := range file.Funcs() {
if fn.Name() == ui.Funcs.Selected {
ui.Code.Code = fn.Load(ui.loadOptions())
}
}
}
}
func (ui *FileUI) loadOptions() disasm.Options {
return disasm.Options{Context: ui.Config.Context}
}
func (ui *FileUI) Layout(gtx layout.Context) {
for ui.OpenInNew.Clicked(gtx) {
ui.openInNew(gtx)
}
if ui.Funcs.Selected == "" {
ui.Funcs.SelectIndex(0)
}
if !ui.Code.Loaded() || ui.Code.Name != ui.Funcs.Selected {
selected := ui.Funcs.SelectedItem
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.Funcs.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 = font.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 *FileUI) tryOpen(gtx layout.Context, call string) {
var fn disasm.Func
for _, target := range ui.File.Funcs() {
if target.Name() == call {
fn = target
break
}
}
if fn == nil {
return
}
load := fn.Load(ui.loadOptions())
ui.Funcs.Selected = load.Name
ui.Funcs.SelectedItem = fn
ui.Funcs.List.Selected = -1
for i, fil := range ui.Funcs.Filtered {
if fil == fn {
ui.Funcs.List.Selected = i
break
}
}
ui.Code.Code = load
if ui.Funcs.Selected == "" {
ui.Funcs.SelectIndex(0)
}
ui.Code.ResetScroll()
}
func (ui *FileUI) 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))
}