Repository navigation
Expand file tree
/
Copy pathcomponents.go
More file actions
455 lines (419 loc) · 15.3 KB
/
Copy pathcomponents.go
File metadata and controls
455 lines (419 loc) · 15.3 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package frostui
import (
"hash/fnv"
"strings"
"unicode"
"unicode/utf8"
"github.com/egoist/mygo/ui"
)
// Card is a panel: a column with padding and a hairline border, on the
// panel gradient. Chain Children to fill it.
func Card(c *ui.Context) ui.Element {
t := c.Theme()
return panelFace(ui.Column(c).Padding(18).Gap(SpaceM).Radius(RadiusCard).
Border(1, t.Border).AlignItems(ui.Stretch), t)
}
// CardHeader is a card's title row: a heading, an optional subtitle under
// it, and trailing controls at the end.
func CardHeader(c *ui.Context, title, subtitle string, trailing func()) {
ui.Row(c).Gap(SpaceM).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).Gap(2).Children(func() {
Heading(c, title)
if subtitle != "" {
Muted(c, subtitle)
}
})
if trailing != nil {
trailing()
}
})
}
// PageHeader is the top of a page: its title, a subtitle that says the
// page's state in one line, and the page's main actions at the end.
func PageHeader(c *ui.Context, title, subtitle string, actions func()) {
ui.Row(c).Gap(SpaceM).AlignItems(ui.Center).Children(func() {
ui.Column(c).Grow(1).Gap(2).Children(func() {
Title(c, title).Role(ui.RoleHeading)
if subtitle != "" {
Muted(c, subtitle).FontSize(13.5)
}
})
if actions != nil {
ui.Row(c).Gap(SpaceS).AlignItems(ui.Center).Children(actions)
}
})
}
// Pill is a small tag in a tone: a 1px outline in the tone's line around
// its text in capitals, in the tone's color (the tag text for Neutral).
func Pill(c *ui.Context, text string, tone Tone) ui.Element {
t := c.Theme()
return outlinedPill(c, text, pillColor(t, tone), ToneLine(t, tone), nil)
}
// IconPill is a pill that starts with an icon.
func IconPill(c *ui.Context, icon, text string, tone Tone) ui.Element {
t := c.Theme()
color := pillColor(t, tone)
return outlinedPill(c, text, color, ToneLine(t, tone), func() {
Icon(c, icon).TextColor(color).FontSize(TextSize(c, 11))
})
}
// ColorPill is a pill in a color of the app's own rather than a tone, as
// one color per kind of thing. Pass the color from the page's theme: under
// SoftSelection the accent of c's theme is a tint.
func ColorPill(c *ui.Context, text string, color ui.Color) ui.Element {
return outlinedPill(c, text, color, color.Alpha(0.4), nil)
}
// DotPill is a pill that starts with a dot in its color, as a state that
// waits for the user: "Update".
func DotPill(c *ui.Context, text string, color ui.Color) ui.Element {
return outlinedPill(c, text, color, color.Alpha(0.4), func() {
ui.Box(c).Size(6, 6).Radius(3).Background(color)
})
}
// SquareAvatar is a square with the initials of name on a color
// the name picks, the same color for the same name in every frame: an app
// or a package without an icon of its own. size is the square's side.
func SquareAvatar(c *ui.Context, name string, size float32) ui.Element {
h := fnv.New32a()
_, _ = h.Write([]byte(strings.ToLower(name)))
hue := float32(h.Sum32() % 360)
bg, fg := ui.Oklch(0.92, 0.05, hue), ui.Oklch(0.42, 0.12, hue)
if c.Theme().Dark {
bg, fg = ui.Oklch(0.36, 0.07, hue), ui.Oklch(0.88, 0.09, hue)
}
return ui.Box(c).Size(size, size).Radius(min(4, size/4)).Background(bg).Center().Shrink(0).Children(func() {
ui.Text(c, Initials(name)).FontSize(size * 0.36).Bold().TextColor(fg).SingleLine()
})
}
// Initials are the first letters of name's first two words, upper case:
// "Mission Center" is "MC", "fzf" is "F", and a name without a letter or
// digit is "?".
func Initials(name string) string {
words := strings.FieldsFunc(name, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) })
var initials []rune
for _, w := range words {
if len(initials) == 2 {
break
}
r, _ := utf8.DecodeRuneInString(w)
initials = append(initials, unicode.ToUpper(r))
}
if len(initials) == 0 {
return "?"
}
return string(initials)
}
// Tab is one tab of PillTabs.
type Tab struct {
Title, Icon string
}
// PillTabs draws tabs as a row of pills, each an icon and a title, the
// chosen one filled with the surface color. *selected is the chosen index;
// a click writes it. It returns the tab list.
func PillTabs(c *ui.Context, selected *int, tabs []Tab) ui.Element {
t := c.Theme()
parts := ui.TabsBase(c, selected, len(tabs))
return parts.List.Gap(SpaceXS).Children(func() {
for i, tab := range tabs {
el := parts.Tab(i).Row().Gap(6).Padding(6, 12).Radius(RadiusChip).AlignItems(ui.Center).Cursor(ui.CursorPointer)
switch {
case i == *selected:
el.Background(t.Surface)
case el.Hovered():
el.Background(t.SurfaceHover)
}
el.Children(func() {
Icon(c, tab.Icon)
ui.Text(c, tab.Title).FontSize(13.5).SingleLine()
})
}
})
}
// SoftSelection is t with the accent of chosen rows softened to a tint of
// it on a card, and the text on them left as it is, so the pills on a
// chosen row of a table still show. Draw the table under it with
// ui.Themed, and take the cells' colors from t.
func SoftSelection(t *ui.Theme) *ui.Theme {
tt := *t
tint := Panel(t).Mix(t.Accent, 0.2)
if !t.Dark {
tint = Panel(t).Mix(t.Accent, 0.14)
}
if k, ok := frostyardColors(t); ok {
tint = k.SelectedFill.Over(k.Panel)
}
tt.Accent, tt.InactiveAccent, tt.AccentText = tint, tint, t.Text
return &tt
}
// IconTile is an icon on a square, as rows and cards lead with. size is
// the square's side.
func IconTile(c *ui.Context, icon string, tone Tone, size float32) ui.Element {
t := c.Theme()
return ui.Box(c).Size(size, size).Radius(min(4, size/4)).Background(ToneFill(t, tone)).Center().Children(func() {
Icon(c, icon).TextColor(ToneColor(t, tone)).FontSize(size * 0.5)
})
}
// IconButton is a flat button showing only an icon. Callers must pass a
// nonempty label for the accessible name and tooltip. An empty label is
// passed through unchanged, without validation or a fallback.
// It is a ghost: a hairline and no face, its icon in the hover text while
// the pointer is over it, and the accent face while it is Selected.
func IconButton(c *ui.Context, icon, label string) ui.Element {
return ghost(c, ui.Button(c, "")).Label(label).Tooltip(label).Children(func() {
Icon(c, icon)
})
}
// QuietIconButton is an icon-only button with no resting face or border.
// As with [IconButton], callers must pass a nonempty label for the accessible
// name and tooltip. An empty label is passed through unchanged, without
// validation or a fallback.
// Hover and press show the theme's surface treatments; MyGo retains focus,
// activation and disabled behavior. Dimensions chain as on IconButton.
func QuietIconButton(c *ui.Context, icon, label string) ui.Element {
b := ui.Button(c, "").Background(ui.Transparent).BorderWidth(0)
// MyGo skips its state faces for a custom background. Paint our faces
// after styling, when chained Disabled and inherited state are known.
b.Draw(func(p *ui.Painter, r ui.Rect) {
t := p.Theme()
switch {
case b.Pressed():
p.Fill(r, t.SurfacePressed, t.Radius)
case b.Hovered():
p.Fill(r, t.SurfaceHover, t.Radius)
}
})
return b.Label(label).Tooltip(label).Children(func() {
Icon(c, icon)
})
}
// PrimaryButton is the main action as a button with a text label: MyGo's
// PrimaryButton on the brand gradient, with the on-gradient text, the face
// of LabeledButton's primary button. Disabled, AutoFocus, Tooltip, Clicked
// and OnClick chain on it as on MyGo's; disabled, it shows at half its
// opacity, face and text, as MyGo dims every disabled element.
func PrimaryButton(c *ui.Context, label string) ui.Element {
return primaryFace(ui.PrimaryButton(c, label), c.Theme())
}
// PrimaryIconButton is an icon-only button on the same brand gradient as
// PrimaryButton, with the on-gradient icon color. As with [IconButton],
// callers must pass a nonempty label for the accessible name and tooltip.
// An empty label is passed through unchanged, without validation or a fallback.
// Dimensions, activation, focus and Disabled chain as on MyGo's PrimaryButton.
func PrimaryIconButton(c *ui.Context, icon, label string) ui.Element {
return primaryFace(ui.PrimaryButton(c, ""), c.Theme()).Label(label).Tooltip(label).Children(func() {
Icon(c, icon)
})
}
// LabeledButton is a button with an icon before its label. primary makes
// it the primary button, on the brand gradient, as PrimaryButton;
// otherwise it is a ghost, as IconButton.
func LabeledButton(c *ui.Context, icon, label string, primary bool) ui.Element {
var b ui.Element
if primary {
b = primaryFace(ui.PrimaryButton(c, ""), c.Theme())
} else {
b = ghost(c, ui.Button(c, ""))
}
return b.Label(label).Children(func() {
ui.Row(c).Gap(6).AlignItems(ui.Center).Children(func() {
Icon(c, icon)
ui.Text(c, label).SingleLine()
})
})
}
// Banner is a full-width notice in a tone, with an optional action and a
// close button. It reports a click on the action and on close.
func Banner(c *ui.Context, tone Tone, icon, text, action string, closable bool) (acted, closed bool) {
closeLabel := ""
if closable {
closeLabel = "Dismiss"
}
return BannerClose(c, tone, icon, text, action, closeLabel)
}
// BannerClose is Banner with its close button named closeLabel, for
// assistive technology and its tooltip; "" draws no close button.
func BannerClose(c *ui.Context, tone Tone, icon, text, action, closeLabel string) (acted, closed bool) {
t := c.Theme()
color := ToneColor(t, tone)
ui.Row(c).Gap(SpaceM).Padding(SpaceM, SpaceL).Radius(RadiusChip).
Background(ToneFill(t, tone)).Border(1, ToneLine(t, tone)).AlignItems(ui.Center).Role(ui.RoleAlert).
Children(func() {
Icon(c, icon).TextColor(color)
ui.Text(c, text).FontSize(TextSize(c, 13)).Grow(1).Wrap()
if action != "" && ghost(c, ui.Button(c, action)).Clicked() {
acted = true
}
if closeLabel != "" && IconButton(c, "window-close", closeLabel).Clicked() {
closed = true
}
})
return acted, closed
}
// Empty is the placeholder of a list with nothing in it.
func Empty(c *ui.Context, icon, title, body string) ui.Element {
t := c.Theme()
return ui.Column(c).Padding(SpaceXL).Gap(SpaceS).AlignItems(ui.Center).Children(func() {
Icon(c, icon).FontSize(28).TextColor(t.TextMuted)
Strong(c, title)
if body != "" {
Muted(c, body).Wrap()
}
})
}
// Loading is a spinner with a line saying what loads.
func Loading(c *ui.Context, text string) ui.Element {
return ui.Row(c).Gap(SpaceS).Padding(SpaceM, 0).AlignItems(ui.Center).Children(func() {
ui.Spinner(c).Label(text)
Muted(c, text)
})
}
// Problem is an inline error line in a card.
func Problem(c *ui.Context, text string) ui.Element {
t := c.Theme()
return ui.Row(c).Gap(SpaceS).AlignItems(ui.Start).Children(func() {
Icon(c, "dialog-warning").TextColor(t.Danger)
ui.Text(c, text).FontSize(12.5).TextColor(t.Danger).Wrap().Grow(1).Selectable()
})
}
// KeyValue is one line of a KeyValues list.
type KeyValue struct {
Key, Value string
// Mono shows the value in the monospace font.
Mono bool
}
// KeyValuesStyle fits a KeyValues list to its place; its zero value is
// the list of a dialog. A narrow side panel takes a narrower names column
// and smaller values, as an inspector beside a table: KeyWidth 72, Gap 10,
// FontSize 12.5.
type KeyValuesStyle struct {
// KeyWidth is the width of the names' column: 120 when zero.
KeyWidth float32
// Gap is the space between the lines: 6 when zero.
Gap float32
// FontSize is the size of the values: 13 when zero. A monospaced value
// is half a point smaller.
FontSize float32
}
// KeyValues is a two-column list of names and values. An optional style
// sets its metrics.
func KeyValues(c *ui.Context, rows []KeyValue, style ...KeyValuesStyle) ui.Element {
s := KeyValuesStyle{KeyWidth: 120, Gap: 6, FontSize: 13}
if len(style) > 0 {
if st := style[0]; st.KeyWidth > 0 {
s.KeyWidth = st.KeyWidth
}
if st := style[0]; st.Gap > 0 {
s.Gap = st.Gap
}
if st := style[0]; st.FontSize > 0 {
s.FontSize = st.FontSize
}
}
return ui.Column(c).Gap(s.Gap).AlignItems(ui.Stretch).Children(func() {
for i, kv := range rows {
ui.Row(c.Key(i)).Gap(SpaceM).AlignItems(ui.Start).Children(func() {
Muted(c, kv.Key).Width(s.KeyWidth).Shrink(0)
v := ui.Text(c, kv.Value).FontSize(s.FontSize).Grow(1).MinWidth(0).Wrap().Selectable()
if kv.Mono {
v.Font("monospace").FontSize(s.FontSize - 0.5)
}
})
}
})
}
// Figure is one figure of a Figures strip.
type Figure struct {
Label, Value, Note string
// Tone colors the note.
Tone Tone
// OnClick makes the figure a button, as one that opens its page.
OnClick func()
}
// Figures is one strip of figures side by side, divided by hairlines.
func Figures(c *ui.Context, figs []Figure) ui.Element {
t := c.Theme()
return panelFace(ui.Row(c).Padding(14, 0).Radius(RadiusCard).Border(1, t.Border).AlignItems(ui.Stretch), t).Children(func() {
for i, f := range figs {
if i > 0 {
ui.Box(c.Key(i * 2)).Width(1).Background(t.Border)
}
box := ui.Column(c.Key(i*2+1)).Grow(1).Basis(0).Padding(4, 20).Gap(4)
if f.OnClick != nil {
box.Role(ui.RoleButton).Focusable().Cursor(ui.CursorPointer).Label(f.Label + ": " + f.Value)
if box.Clicked() {
f.OnClick()
}
}
box.Children(func() {
Muted(c, f.Label)
size := TextSize(c, 23)
ui.Text(c, f.Value).FontSize(size).Bold().LetterSpacing(-0.05 * size).SingleLine()
if f.Note != "" {
ui.Text(c, f.Note).FontSize(12).TextColor(ToneColor(t, f.Tone)).SingleLine()
}
})
}
})
}
// Section is a labelled group inside a page: an icon, a title and a muted
// note on one line (SectionHeader), then fn's content.
func Section(c *ui.Context, icon, title, note string, fn func()) {
ui.Column(c).Gap(SpaceS).AlignItems(ui.Stretch).Children(func() {
SectionHeader(c, SectionHead{Icon: icon, Title: title, Note: note})
fn()
})
}
// SectionHead is what heads a group of a page.
type SectionHead struct {
Icon, Title string
// Note follows the title, muted, on one line.
Note string
// Aside is a caption at the end of the line: counts, a status.
Aside string
// Quiet sets the title in the muted color too, for a group whose items
// carry the weight.
Quiet bool
}
// SectionHeader is the line that heads a group of a page: an icon, a bold
// title, a note after it and an aside at the end. Section draws one over
// its content; a page that lays its group out itself draws it alone.
func SectionHeader(c *ui.Context, h SectionHead) ui.Element {
t := c.Theme()
return ui.Row(c).Gap(SpaceS).AlignItems(ui.Center).Children(func() {
if h.Icon != "" {
Icon(c, h.Icon).TextColor(t.TextMuted)
}
title := ui.Text(c, h.Title).FontSize(13.5).Bold()
if h.Quiet {
title.TextColor(t.TextMuted)
}
switch {
case h.Note != "":
Muted(c, h.Note).Grow(1).SingleLine()
case h.Aside != "":
ui.Spacer(c)
}
if h.Aside != "" {
Caption(c, h.Aside).SingleLine()
}
})
}
// RowDivider is the hairline between rows of a card.
func RowDivider(c *ui.Context) ui.Element {
return ui.Box(c).Height(1).Background(c.Theme().Border)
}
// Kbd draws a keyboard key, as in "Ctrl K".
func Kbd(c *ui.Context, key string) ui.Element {
t := c.Theme()
return ui.Box(c).Padding(1, 6).Radius(RadiusChip).Border(1, t.Border).Background(t.Background).Children(func() {
ui.Text(c, key).FontSize(TextSize(c, 11)).TextColor(t.TextMuted).SingleLine()
})
}
// Shortcut draws a key combination, as "Ctrl" "K".
func Shortcut(c *ui.Context, keys ...string) ui.Element {
return ui.Row(c).Gap(3).AlignItems(ui.Center).Children(func() {
for i, k := range keys {
Kbd(c.Key(i), k)
}
})
}