aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/ui/settings.go
blob: e90c69159975e59e99504e5a0a8440bdb595175e (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package ui

import (
	"github.com/diamondburned/gotk4/pkg/gtk/v4"

	"krino/gui/internal/model"
	"krino/internal/xdg"
)

// settingsWindow is Settings: krino's defaults, which every directory
// inherits, and how this window behaves. The first are written to
// krino.conf with the care a rules file is written with - checked first,
// the previous text kept as krino.conf.bak - and the second to gui.json,
// which krino itself never reads (his request, 2026-09-16).
type settingsWindow struct {
	w    *Window
	win  *gtk.Window
	main *model.MainConf

	rows   []*settingRow
	check  *gtk.Label
	save   *gtk.Button
	toSave bool
}

// showSettings opens it, one at a time.
func (w *Window) showSettings() {
	main, err := model.OpenMain(w.engine)
	if err != nil {
		w.setStatus("settings: %v", err)
		return
	}
	s := &settingsWindow{w: w, main: main}
	s.win = gtk.NewWindow()
	s.win.SetTitle("krino settings")
	s.win.SetTransientFor(&w.win.Window)
	s.win.SetModal(true)
	s.win.SetDefaultSize(560, 640)

	box := gtk.NewBox(gtk.OrientationVertical, 8)
	box.SetMarginStart(12)
	box.SetMarginEnd(12)
	box.SetMarginTop(12)
	box.SetMarginBottom(12)

	box.Append(heading("Defaults for every directory - " + xdg.Abbrev(main.File)))
	note := gtk.NewLabel("An empty field is one krino.conf does not set, and krino's own default applies. A directory's file overrides these.")
	note.SetXAlign(0)
	note.SetWrap(true)
	note.AddCSSClass("dim-label")
	box.Append(note)
	for _, head := range append(append([]string{}, model.MainSettings...), "log") {
		args, _, err := main.Setting(head)
		if err != nil {
			w.setStatus("settings: %v", err)
			return
		}
		row := newSettingRow(head, args, s.armSave)
		s.rows = append(s.rows, row)
		box.Append(row.root)
	}
	s.check = gtk.NewLabel("")
	s.check.SetXAlign(0)
	s.check.SetWrap(true)
	box.Append(s.check)

	box.Append(heading("This window"))
	prefs := w.prefs
	colours := gtk.NewCheckButtonWithLabel("colour the configuration in the Text tab")
	colours.SetActive(prefs.Colours)
	colours.SetTooltipText("paint comments, strings, form heads and actions in the Text sub-tab")
	preview := gtk.NewCheckButtonWithLabel("show the file behind the selected row")
	preview.SetActive(prefs.Preview)
	preview.SetTooltipText("show the selected file under its explanation: a picture, a PDF's first page, or the first lines of text")
	selectAll := gtk.NewDropDownFromStrings([]string{"every file checked", "nothing checked"})
	if !prefs.SelectAll {
		selectAll.SetSelected(1)
	}
	selectAll.SetTooltipText("what a freshly scanned plan starts as: every file that can be acted on checked, as the terminal review does, or nothing checked so you pick")

	layout := gtk.NewDropDownFromStrings([]string{
		"file list beside the explanation",
		"file list on top, preview beside the explanation",
	})
	if prefs.Layout == model.LayoutStacked {
		layout.SetSelected(1)
	}
	layout.SetTooltipText("how the Plan tab is arranged")
	previewHeight := gtk.NewSpinButtonWithRange(80, 2000, 20)
	previewHeight.SetValue(float64(prefs.PreviewHeight))
	previewHeight.SetTooltipText("how tall the preview under an explanation is, in pixels; dragging the divider above it does the same")
	box.Append(field("layout", layout))
	box.Append(field("preview height", previewHeight))
	box.Append(field("after a scan", selectAll))
	box.Append(colours)
	box.Append(preview)

	apply := func() {
		which := model.LayoutSide
		if layout.Selected() == 1 {
			which = model.LayoutStacked
		}
		p := model.Prefs{
			Colours:       colours.Active(),
			Preview:       preview.Active(),
			SelectAll:     selectAll.Selected() == 0,
			PreviewHeight: int(previewHeight.Value()),
			Layout:        which,
		}
		w.applyPrefs(p)
		if err := p.Save(); err != nil {
			w.setStatus("settings: %v", err)
		}
	}
	colours.ConnectToggled(apply)
	preview.ConnectToggled(apply)
	selectAll.Connect("notify::selected", func() { apply() })
	layout.Connect("notify::selected", func() { apply() })

	s.save = gtk.NewButtonWithLabel("Save krino.conf")
	s.save.AddCSSClass("suggested-action")
	s.save.SetSensitive(false)
	s.save.ConnectClicked(s.onSave)
	closeBtn := gtk.NewButtonWithLabel("Close")
	closeBtn.ConnectClicked(func() { s.win.Close() })
	// The buttons sit outside the scrolled area: on a short screen they
	// would otherwise be below the fold, which is where he found them.
	buttons := gtk.NewBox(gtk.OrientationHorizontal, 6)
	buttons.SetHAlign(gtk.AlignEnd)
	buttons.SetMarginStart(12)
	buttons.SetMarginEnd(12)
	buttons.SetMarginTop(8)
	buttons.SetMarginBottom(12)
	buttons.Append(closeBtn)
	buttons.Append(s.save)

	scroll := gtk.NewScrolledWindow()
	scroll.SetChild(box)
	scroll.SetVExpand(true)
	outer := gtk.NewBox(gtk.OrientationVertical, 0)
	outer.Append(scroll)
	outer.Append(gtk.NewSeparator(gtk.OrientationHorizontal))
	outer.Append(buttons)
	s.win.SetChild(outer)
	s.win.Show()
}

// armSave writes what the fields hold into the text and checks it, without
// touching the file: Save is what writes.
func (s *settingsWindow) armSave() {
	for _, row := range s.rows {
		value := row.value()
		was, _, err := s.main.Setting(row.head)
		if err != nil {
			s.check.SetText(escape(err.Error()))
			return
		}
		if value == was {
			continue
		}
		if err := s.main.SetSetting(row.head, value); err != nil {
			s.check.SetText(escape(err.Error()))
			row.set(was)
			continue
		}
		s.toSave = true
	}
	diags := s.main.Check()
	switch {
	case len(diags) == 0:
		s.check.SetText("")
		s.save.SetSensitive(s.toSave && s.main.Modified())
	default:
		s.check.SetText(escape(diags[0].Error()))
		s.check.AddCSSClass("error")
		s.save.SetSensitive(false)
	}
}

// onSave writes krino.conf and makes the new defaults the ones every tab
// works from.
func (s *settingsWindow) onSave() {
	if err := s.main.Save(); err != nil {
		s.check.SetText(escape(err.Error()))
		return
	}
	s.check.RemoveCSSClass("error")
	s.check.SetText("saved; the previous text is beside it as krino.conf.bak")
	s.save.SetSensitive(false)
	if err := s.w.reloadEngine(); err != nil {
		s.w.setStatus("saved, but the new configuration does not load: %v", err)
		return
	}
	s.w.setStatus("saved %s", escape(xdg.Abbrev(s.main.File)))
}

// settingHintsMain is the example shown in an empty krino.conf field, for
// the settings the directory form does not already describe.
var settingHintsMain = map[string]string{
	"log": `"~/.local/state/krino/krino.log"`,
}

// mainHint is the example for a krino.conf setting.
func mainHint(head string) string {
	if h, ok := settingHintsMain[head]; ok {
		return h
	}
	return settingHints[head]
}