forked from steverusso/mdedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
336 lines (307 loc) · 6.88 KB
/
session.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package mdedit
import (
"errors"
"image"
"image/color"
"io/fs"
"log"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"gioui.org/app"
"gioui.org/io/key"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/widget"
"gioui.org/widget/material"
)
type FS interface {
HomeDir() string
WorkingDir() string
ReadDir(fpath string) ([]fs.FileInfo, error)
ReadFile(fpath string) ([]byte, error)
WriteFile(fpath string, data []byte) error
}
type Session struct {
fsys FS
win *app.Window
tabs []tab
tabList layout.List
activeTab int
}
type tab struct {
btn widget.Clickable
content tabContent
}
func NewSession(fsys FS, win *app.Window) Session {
return Session{
fsys: fsys,
win: win,
tabList: layout.List{Axis: layout.Vertical},
}
}
func (s *Session) Layout(gtx C, th *material.Theme) D {
paint.Fill(gtx.Ops, th.Bg)
if len(s.tabs) == 0 {
return layout.Center.Layout(gtx, func(gtx C) D {
return material.Body1(th, "Use Ctrl-O to open a file!").Layout(gtx)
})
}
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = 220
gtx.Constraints.Max.X = 220
return s.tabList.Layout(gtx, len(s.tabs), func(gtx C, i int) D {
t := &s.tabs[i]
if t.btn.Clicked() {
s.SelectTab(i)
op.InvalidateOp{}.Add(gtx.Ops)
}
// If this is the active tab, emphasize the text and invert the bg & fg.
lbl := material.Body1(th, t.content.title())
lbl.Font.Variant = "Mono"
bg := th.Bg
if i == s.activeTab {
lbl.Font.Weight = text.Bold
lbl.Color = bg
bg = th.ContrastBg
}
// Record the layout in order to get the size for filling the background.
m := op.Record(gtx.Ops)
dims := t.btn.Layout(gtx, func(gtx C) D {
return layout.UniformInset(5).Layout(gtx, lbl.Layout)
})
call := m.Stop()
// Fill the background and draw the tab button.
rect := clip.Rect{Max: dims.Size}
paint.FillShape(gtx.Ops, bg, rect.Op())
call.Add(gtx.Ops)
return dims
})
}),
layout.Rigid(func(gtx C) D {
size := image.Point{1, gtx.Constraints.Max.Y}
rect := clip.Rect{Max: size}.Op()
paint.FillShape(gtx.Ops, th.Fg, rect)
return D{Size: size}
}),
layout.Flexed(1, func(gtx C) D {
return s.layTabContent(gtx, th, s.tabs[s.activeTab].content)
}),
)
}
func (s *Session) HandleKeyEvent(e key.Event) {
if e.State != key.Press {
return
}
switch e.Modifiers {
case key.ModCtrl:
switch e.Name {
case "O":
s.OpenFileExplorerTab()
case "W":
s.CloseActiveTab()
case key.NameTab:
s.NextTab()
}
case key.ModCtrl | key.ModShift:
switch e.Name {
case key.NamePageUp:
s.SwapTabUp()
case key.NamePageDown:
s.SwapTabDown()
case key.NameTab:
s.PrevTab()
}
case key.ModAlt:
if strings.Contains("123456789", e.Name) {
if n, err := strconv.Atoi(e.Name); err == nil {
s.SelectTab(n - 1)
}
}
}
}
func (s *Session) layTabContent(gtx C, th *material.Theme, t tabContent) D {
switch t := t.(type) {
case *markdownTab:
return s.layMarkdownTab(gtx, th, t)
case *explorerTab:
return s.layExplorerTab(gtx, th, t)
default:
return D{}
}
}
func (s *Session) layMarkdownTab(gtx C, th *material.Theme, t *markdownTab) D {
if t.view.Editor.SaveRequested() {
go s.writeFile(t.name, t.view.Editor.Text())
}
return ViewStyle{
Theme: th,
EditorFont: text.Font{Variant: "Mono"},
Palette: Palette{
Fg: th.Fg,
Bg: th.Bg,
LineNumber: color.NRGBA{200, 180, 4, 125},
Heading: color.NRGBA{200, 193, 255, 255},
ListMarker: color.NRGBA{10, 190, 240, 255},
BlockQuote: color.NRGBA{165, 165, 165, 230},
CodeBlock: color.NRGBA{162, 120, 70, 255},
},
View: &t.view,
}.Layout(gtx)
}
func (s *Session) layExplorerTab(gtx C, th *material.Theme, t *explorerTab) D {
for _, e := range t.expl.Events() {
switch e := e.(type) {
case DirChosenEvent:
go s.openExplorerDir(t, e.Path)
case FilesChosenEvent:
go func() {
for i, fpath := range e.Paths {
s.OpenFile(fpath)
if i == 0 {
s.CloseActiveTab()
s.win.Invalidate()
}
}
}()
}
}
return t.expl.Layout(gtx, th)
}
func (s *Session) OpenFileExplorerTab() {
s.tabs = append(s.tabs, tab{content: &explorerTab{
expl: NewExplorer(s.fsys.HomeDir(), s.fsys.WorkingDir()),
}})
s.NextTab()
}
func (s *Session) OpenFile(fpath string) {
if fpath == "" {
log.Println("open file: empty file path")
return
}
data, err := s.fsys.ReadFile(fpath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
log.Printf("reading '%s': %v\n", fpath, err)
return
}
name := fpath
if fpath[0] == '/' {
rel, err := filepath.Rel(s.fsys.WorkingDir(), fpath)
if err != nil {
log.Printf("getting relative path '%s': %v\n", fpath, err)
return
}
name = rel
}
md := &markdownTab{name: name}
md.view.Editor.SetText(data)
md.view.SplitRatio = 0.5
s.tabs = append(s.tabs, tab{content: md})
s.win.Invalidate()
}
func (s *Session) openExplorerDir(t *explorerTab, fpath string) {
if fpath == "" {
log.Println("open dir: empty file path")
return
}
fpath = path.Clean(fpath)
files, err := s.fsys.ReadDir(fpath)
if err != nil {
log.Println(err)
return
}
sort.SliceStable(files, func(i, j int) bool {
a, b := files[i], files[j]
if a.IsDir() == b.IsDir() {
return a.Name() < b.Name()
}
return a.IsDir()
})
t.expl.Populate(fpath, files)
s.win.Invalidate()
}
func (s *Session) writeFile(fpath string, data []byte) {
if err := s.fsys.WriteFile(fpath, data); err != nil {
log.Println(err)
}
}
func (s *Session) CloseActiveTab() {
s.tabs = append(s.tabs[:s.activeTab], s.tabs[s.activeTab+1:]...)
n := len(s.tabs)
if s.activeTab > 0 && s.activeTab >= n {
s.activeTab = n - 1
}
if n > 0 {
s.tabs[s.activeTab].content.focus()
}
}
func (s *Session) FocusActiveTab() {
if i := s.activeTab; i >= 0 && i < len(s.tabs) {
s.tabs[i].content.focus()
}
}
func (s *Session) NextTab() {
if s.activeTab < len(s.tabs)-1 {
s.activeTab++
}
}
func (s *Session) PrevTab() {
if s.activeTab > 0 {
s.activeTab--
}
}
func (s *Session) SwapTabUp() {
if s.activeTab == 0 {
return
}
i := s.activeTab
j := i - 1
s.tabs[i], s.tabs[j] = s.tabs[j], s.tabs[i]
s.activeTab--
}
func (s *Session) SwapTabDown() {
if s.activeTab == len(s.tabs)-1 {
return
}
i := s.activeTab
j := i + 1
s.tabs[i], s.tabs[j] = s.tabs[j], s.tabs[i]
s.activeTab++
}
func (s *Session) SelectTab(n int) {
if len(s.tabs) == 0 || n < 0 {
return
}
if n >= len(s.tabs) {
n = len(s.tabs) - 1
}
s.activeTab = n
s.tabs[s.activeTab].content.focus()
}
type tabContent interface {
title() string
focus()
}
type markdownTab struct {
name string
view View
}
func (t *markdownTab) title() string {
return t.name
}
func (t *markdownTab) focus() {
t.view.Editor.Focus()
}
type explorerTab struct {
expl Explorer
}
func (t *explorerTab) title() string {
return "[Choose Files]"
}
func (t *explorerTab) focus() {}