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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
// SPDX-License-Identifier: GPL-3.0-or-later
package ui
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotk4/pkg/pango"
"krino/gui/internal/model"
"krino/internal/xdg"
)
// checkDelay is how long the editor waits after a keystroke before it
// checks the text (GUI design §5.3).
const checkDelay = 300
// rulesView is the Rules tab: one directory's file as text, checked as it
// is typed, with Test on file and Save (GUI design §5.2, §5.3).
type rulesView struct {
w *Window
root *gtk.Box
dirs *gtk.DropDown
names []string
save *gtk.Button
revert *gtk.Button
reload *gtk.Button
view *gtk.TextView
buf *gtk.TextBuffer
check *gtk.Label
diags *gtk.ListBox
testPath *gtk.Entry
testGo *gtk.Button
out *gtk.TextView
rules *model.Rules
pending glib.SourceHandle
quiet bool // set while the view is being filled, so no check is armed
}
func newRulesView(w *Window) *rulesView {
r := &rulesView{w: w}
r.root = gtk.NewBox(gtk.OrientationVertical, 0)
r.names = model.RuleFiles(w.engine)
names := r.names
if len(names) == 0 {
names = []string{"none"}
}
r.dirs = gtk.NewDropDownFromStrings(names)
r.save = gtk.NewButtonWithLabel("Save")
r.save.AddCSSClass("suggested-action")
r.revert = gtk.NewButtonWithLabel("Revert")
r.reload = gtk.NewButtonWithLabel("Reload")
bar := gtk.NewBox(gtk.OrientationHorizontal, 6)
bar.SetMarginTop(6)
bar.SetMarginStart(6)
bar.SetMarginEnd(6)
bar.SetMarginBottom(6)
bar.Append(gtk.NewLabel("Rules for"))
bar.Append(r.dirs)
r.check = gtk.NewLabel("")
r.check.SetXAlign(0)
r.check.SetHExpand(true)
r.check.SetEllipsize(pango.EllipsizeEnd)
r.check.SetMaxWidthChars(20)
bar.Append(r.check)
bar.Append(r.reload)
bar.Append(r.revert)
bar.Append(r.save)
r.view = gtk.NewTextView()
r.view.SetMonospace(true)
r.view.SetLeftMargin(8)
r.view.SetRightMargin(8)
r.view.SetTopMargin(6)
r.buf = r.view.Buffer()
editScroll := gtk.NewScrolledWindow()
editScroll.SetChild(r.view)
editScroll.SetVExpand(true)
editScroll.SetHExpand(true)
r.diags = gtk.NewListBox()
r.diags.SetSelectionMode(gtk.SelectionSingle)
diagScroll := gtk.NewScrolledWindow()
diagScroll.SetChild(r.diags)
diagScroll.SetSizeRequest(-1, 110)
left := gtk.NewBox(gtk.OrientationVertical, 0)
left.Append(editScroll)
left.Append(gtk.NewSeparator(gtk.OrientationHorizontal))
left.Append(diagScroll)
// Test on file: any file under the directory, answered from the text in
// the editor rather than from what is saved.
r.testPath = gtk.NewEntry()
r.testPath.SetPlaceholderText("a file to test, e.g. ~/downloads/a.pdf")
r.testPath.SetHExpand(true)
r.testGo = gtk.NewButtonWithLabel("Test on file")
testBar := gtk.NewBox(gtk.OrientationHorizontal, 6)
testBar.SetMarginStart(6)
testBar.SetMarginEnd(6)
testBar.SetMarginTop(6)
testBar.SetMarginBottom(6)
testBar.Append(r.testPath)
testBar.Append(r.testGo)
r.out = gtk.NewTextView()
r.out.SetEditable(false)
r.out.SetMonospace(true)
r.out.SetWrapMode(gtk.WrapWordChar)
r.out.SetLeftMargin(8)
r.out.SetRightMargin(8)
r.out.SetTopMargin(6)
r.out.Buffer().SetText("Type a file's path and press Test on file to see what these rules would do to it.")
outScroll := gtk.NewScrolledWindow()
outScroll.SetChild(r.out)
outScroll.SetVExpand(true)
right := gtk.NewBox(gtk.OrientationVertical, 0)
right.Append(testBar)
right.Append(gtk.NewSeparator(gtk.OrientationHorizontal))
right.Append(outScroll)
panes := gtk.NewPaned(gtk.OrientationHorizontal)
panes.SetStartChild(left)
panes.SetEndChild(right)
panes.SetResizeStartChild(true)
panes.SetResizeEndChild(false)
panes.SetShrinkEndChild(false)
panes.SetPosition(620)
panes.SetVExpand(true)
r.root.Append(bar)
r.root.Append(gtk.NewSeparator(gtk.OrientationHorizontal))
r.root.Append(panes)
r.buf.ConnectChanged(r.onChanged)
r.diags.ConnectRowSelected(func(row *gtk.ListBoxRow) {
if row != nil {
r.goToLine(row.Name())
}
})
r.dirs.Connect("notify::selected", func() { r.open(r.selectedName()) })
r.save.ConnectClicked(r.onSave)
r.revert.ConnectClicked(func() {
if r.rules != nil {
r.rules.Revert()
r.fill()
}
})
r.reload.ConnectClicked(func() { r.reloadFromDisk() })
r.testGo.ConnectClicked(r.onTest)
r.testPath.ConnectActivate(r.onTest)
r.open(r.selectedName())
return r
}
// selectedName is the directory the picker names, "" when none is offered.
func (r *rulesView) selectedName() string {
i := int(r.dirs.Selected())
if i < 0 || i >= len(r.names) {
return ""
}
return r.names[i]
}
// open reads a directory's file into the editor.
func (r *rulesView) open(name string) {
if name == "" {
r.check.SetText("no directory has rules of its own yet; add one with: krino new NAME PATH")
r.setEditable(false)
return
}
rules, err := model.OpenRules(r.w.engine, name)
if err != nil {
r.check.SetText(escape(err.Error()))
r.setEditable(false)
return
}
r.rules = rules
r.setEditable(true)
if root := r.w.dirRoot(name); root != "" {
r.testPath.SetText(xdg.Abbrev(root) + "/")
}
r.fill()
}
// fill puts the model's text in the view and checks it.
func (r *rulesView) fill() {
r.quiet = true
r.buf.SetText(r.rules.Text)
r.quiet = false
r.runCheck()
}
// setEditable turns the editor on or off as a whole.
func (r *rulesView) setEditable(on bool) {
r.view.SetEditable(on)
r.save.SetSensitive(false)
r.revert.SetSensitive(on)
r.reload.SetSensitive(on)
r.testGo.SetSensitive(on)
}
// onChanged arms the check, one timer at a time: checking on every
// keystroke would load the whole configuration as the user types.
func (r *rulesView) onChanged() {
if r.quiet || r.rules == nil {
return
}
if r.pending != 0 {
glib.SourceRemove(r.pending)
}
r.pending = glib.TimeoutAdd(checkDelay, func() bool {
r.pending = 0
r.runCheck()
return false
})
}
// text is what the editor holds.
func (r *rulesView) text() string {
start, end := r.buf.Bounds()
return r.buf.Text(start, end, true)
}
// runCheck loads the configuration with the unsaved text and shows what is
// wrong with it.
func (r *rulesView) runCheck() {
if r.rules == nil {
return
}
r.rules.SetText(r.text())
diags := r.rules.Check()
clearList(r.diags)
switch {
case len(diags) == 0:
r.check.SetText("check: no errors")
case len(diags) == 1:
r.check.SetText("check: 1 problem")
default:
r.check.SetText(fmt.Sprintf("check: %d problems", len(diags)))
}
for _, d := range diags {
where := xdg.Abbrev(d.File)
if d.Pos.Line > 0 {
where = fmt.Sprintf("%s:%d:%d", where, d.Pos.Line, d.Pos.Col)
}
line := d.Pos.Line
row := gtk.NewListBoxRow()
label := gtk.NewLabel(escape(where + ": " + d.Msg))
label.SetXAlign(0)
label.SetMarginStart(6)
label.SetMarginEnd(6)
label.SetWrap(true)
label.AddCSSClass("error")
row.SetChild(label)
r.diags.Append(row)
if d.File == r.rules.File && line > 0 {
row.SetName(fmt.Sprintf("%d", line))
}
}
r.save.SetSensitive(len(diags) == 0 && r.rules.Modified())
r.revert.SetSensitive(r.rules.Modified())
}
// goToLine puts the cursor on the line a diagnostic names, so clicking one
// shows what it is about. A diagnostic in another file carries no line.
func (r *rulesView) goToLine(name string) {
line, err := strconv.Atoi(name)
if err != nil || line <= 0 {
return
}
iter, ok := r.buf.IterAtLine(line - 1)
if !ok {
return
}
r.buf.PlaceCursor(iter)
r.view.ScrollToIter(iter, 0, true, 0, 0.3)
r.view.GrabFocus()
}
// onSave writes the file, or asks what to do when someone else has.
func (r *rulesView) onSave() {
if r.rules == nil {
return
}
err := r.rules.Save()
switch {
case err == nil:
r.w.setStatus("saved %s; the previous text is beside it as %s.bak",
escape(xdg.Abbrev(r.rules.File)), escape(r.rules.Name+".conf"))
r.reloadEngine()
r.runCheck()
case errors.Is(err, model.ErrChangedOnDisk):
r.askAboutDiskChange()
default:
r.w.setStatus("save: %v", err)
}
}
// askAboutDiskChange offers Reload, Overwrite or Cancel when the file has
// changed since it was opened (GUI design §5.3).
func (r *rulesView) askAboutDiskChange() {
d := gtk.NewMessageDialog(&r.w.win.Window, gtk.DialogModal|gtk.DialogDestroyWithParent,
gtk.MessageWarning, gtk.ButtonsNone)
d.SetObjectProperty("text", "Something else changed "+escape(xdg.Abbrev(r.rules.File)))
d.SetObjectProperty("secondary-text",
"Reload takes what is on disk and drops your changes. Overwrite keeps yours and puts the other text in the .bak file.")
d.AddButton("Cancel", int(gtk.ResponseCancel))
d.AddButton("Reload", int(gtk.ResponseReject))
d.AddButton("Overwrite", int(gtk.ResponseAccept))
d.ConnectResponse(func(response int) {
d.Destroy()
switch response {
case int(gtk.ResponseReject):
r.reloadFromDisk()
case int(gtk.ResponseAccept):
if err := r.rules.SaveOverwriting(); err != nil {
r.w.setStatus("save: %v", err)
return
}
r.w.setStatus("saved over the other change; it is in the .bak file")
r.reloadEngine()
r.runCheck()
}
})
d.Show()
}
// reloadFromDisk drops unsaved text and re-reads the file.
func (r *rulesView) reloadFromDisk() {
if r.rules == nil {
return
}
if err := r.rules.Reload(); err != nil {
r.w.setStatus("reload: %v", err)
return
}
r.fill()
}
// reloadEngine makes the saved rules the ones the other tabs use.
func (r *rulesView) reloadEngine() {
if err := r.w.reloadEngine(); err != nil {
r.w.setStatus("saved, but the new rules do not load: %v", err)
}
}
// onTest explains one file against the unsaved text, off the main loop:
// content extraction can take seconds.
func (r *rulesView) onTest() {
if r.rules == nil {
return
}
path := strings.TrimSpace(r.testPath.Text())
if path == "" {
r.out.Buffer().SetText("Type the path of a file under the directory first.")
return
}
r.rules.SetText(r.text())
r.testGo.SetSensitive(false)
r.out.Buffer().SetText("testing " + escape(path) + "...")
var text string
runInBackground(func(ctx context.Context) error {
var err error
text, err = r.rules.Explain(ctx, path)
return err
}, func(err error) {
r.testGo.SetSensitive(true)
if err != nil {
r.out.Buffer().SetText(escapeText(err.Error()))
return
}
r.out.Buffer().SetText(escapeText(text))
})
}
|