-
Notifications
You must be signed in to change notification settings - Fork 1
/
columnui.go
197 lines (181 loc) · 4.2 KB
/
columnui.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
package main
import (
"bytes"
"image"
"log"
"os"
"9fans.net/go/draw"
"github.com/mjl-/duit"
)
type columnUI struct {
header *duit.Edit
headerBox *duit.Box
files *filesUI
duit.Box
}
func newColumnUI(paths []string) *columnUI {
tag := "New Delcol "
header, _ := duit.NewEdit(bytes.NewReader([]byte(tag)))
taglen := int64(len(tag))
header.SetCursor(duit.Cursor{Cur: taglen, Start: taglen})
header.Colors = tagColors
header.NoScrollbar = true
ui := &columnUI{
header: header,
}
header.Click = func(m draw.Mouse, offset int64) (e duit.Event) {
switch m.Buttons {
case duit.Button1:
case duit.Button2:
ui.execute("", expandText(header, offset), nil)
case duit.Button3:
ui.look("", expandText(header, offset))
}
return
}
header.Keys = func(k rune, m draw.Mouse) (r duit.Event) {
if k == control&'f' {
p, _ := os.Getwd()
topUI.complete(p, ui.header)
r.Consumed = true
}
return
}
square := &square{
dirty: false,
cleanColor: squareBorderColor,
borderColor: squareBorderColor,
dirtyColor: squareBorderColor,
b1: func() {
topUI.grow(ui)
},
b2: func() {
topUI.growAvailable(ui)
},
b3: func() {
topUI.growFull(ui)
},
lowdpiSize: image.Pt(duit.ScrollbarSize, tagHeight()),
}
headerSplit := &duit.Split{
Split: func(width int) []int {
w := dui.Scale(duit.ScrollbarSize)
return []int{w, width - w}
},
Kids: duit.NewKids(square, header),
}
ui.headerBox = &duit.Box{
Width: -1,
Height: tagHeight(),
Kids: duit.NewKids(headerSplit),
}
var files []*fileUI
for _, p := range paths {
files = append(files, newFileUI(ui, p))
}
ui.files = newFilesUI(ui, files)
ui.Box.Margin = image.Pt(0, 1)
ui.Box.Background = textColors.Fg
ui.Box.Width = -1
ui.Box.Height = -1
ui.Box.Kids = duit.NewKids(ui.headerBox, ui.files)
return ui
}
func (ui *columnUI) execute(filename, t string, edit *duit.Edit) {
switch t {
case "New":
ui.addFile("")
case "Delcol":
topUI.removeColumn(ui)
dui.MarkLayout(topUI)
default:
topUI.execute(filename, t, edit)
}
}
func (ui *columnUI) look(filename, t string) {
log.Printf("columnUI.look %q\n", t)
topUI.look(filename, t, true)
}
func (ui *columnUI) addFile(filename string) *fileUI {
f := newFileUI(ui, filename)
ui.files.add(f)
return f
}
func (ui *columnUI) removeFile(file *fileUI) {
ui.files.remove(file)
}
func (ui *columnUI) fileIndexMouse(m draw.Mouse) int {
y := ui.Kids[1].R.Min.Y
marginY := ui.files.marginY()
for i, h := range ui.files.heights {
if m.Y >= y && m.Y < y+h {
return i
}
y += h + marginY
}
return -1
}
func (ui *columnUI) fileIndex(file *fileUI) int {
for i, f := range ui.files.files {
if f == file {
return i
}
}
return -1
}
func (ui *columnUI) grow(file *fileUI) {
i := ui.fileIndex(file)
if i >= 0 {
ui.growIndex(i)
}
}
func (ui *columnUI) growAvailable(file *fileUI) {
i := ui.fileIndex(file)
if i >= 0 {
take := (ui.files.height() - len(ui.files.files)*tagHeight()) - ui.files.heights[i]
height := tagHeight()
grow(take, i, ui.files.heights, height)
setMinDims(ui.files.heights, height)
dui.MarkLayout(ui.files)
}
}
func (ui *columnUI) growFull(file *fileUI) {
i := ui.fileIndex(file)
if i >= 0 {
take := ui.files.height() - ui.files.heights[i]
grow(take, i, ui.files.heights, tagHeight())
dui.MarkLayout(ui.files)
}
}
func (ui *columnUI) growIndex(index int) {
height := tagHeight()
grow(dui.Scale(100), index, ui.files.heights, height)
setMinDims(ui.files.heights, height)
dui.MarkLayout(ui.files)
}
func (ui *columnUI) Key(dui *duit.DUI, self *duit.Kid, k rune, m draw.Mouse, orig image.Point) (r duit.Result) {
switch k {
case draw.KeyCmd + 'I':
i := ui.fileIndexMouse(m)
if i < 0 {
return
}
ui.growIndex(i)
case draw.KeyCmd + 'J':
i := (ui.fileIndexMouse(m) + 1) % len(ui.files.heights)
dui.Focus(ui.files.files[i])
case draw.KeyCmd + 'K':
i := (ui.fileIndexMouse(m) - 1 + len(ui.files.heights)) % len(ui.files.heights)
dui.Focus(ui.files.files[i])
case draw.KeyCmd + 'n':
ui.addFile("")
default:
return ui.Box.Key(dui, self, k, m, orig)
}
r.Consumed = true
return
}
func (ui *columnUI) Print(self *duit.Kid, indent int) {
duit.PrintUI("columnUI", self, indent)
duit.KidsPrint(ui.Kids, indent+1)
}