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
|
package ui
import (
"strings"
"git.sr.ht/~rjarry/aerc/config"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
type Box struct {
content Drawable
title string
borders string
uiConfig *config.UIConfig
}
func NewBox(
content Drawable, title, borders string, uiConfig *config.UIConfig,
) *Box {
if borders == "" || len(borders) < 8 {
borders = "││┌─┐└─┘"
}
b := &Box{
content: content,
title: title,
borders: borders,
uiConfig: uiConfig,
}
return b
}
func (b *Box) Draw(ctx *Context) {
w := ctx.Width()
h := ctx.Height()
style := b.uiConfig.GetStyle(config.STYLE_BORDER)
box := []rune(b.borders)
ctx.Fill(0, 0, 1, h, box[0], style)
ctx.Fill(w-1, 0, 1, h, box[1], style)
ctx.Printf(0, 0, style, "%c%s%c", box[2], strings.Repeat(string(box[3]), w-2), box[4])
ctx.Printf(0, h-1, style, "%c%s%c", box[5], strings.Repeat(string(box[6]), w-2), box[7])
if b.title != "" && w > 4 {
title := runewidth.Truncate(b.title, w-4, "…")
ctx.Printf(2, 0, style, "%s", title)
}
subctx := ctx.Subcontext(1, 1, w-2, h-2)
b.content.Draw(subctx)
}
func (b *Box) Invalidate() {
b.content.Invalidate()
}
func (b *Box) MouseEvent(localX int, localY int, event tcell.Event) {
if content, ok := b.content.(Mouseable); ok {
content.MouseEvent(localX, localY, event)
}
}
func (b *Box) Event(e tcell.Event) bool {
if content, ok := b.content.(Interactive); ok {
return content.Event(e)
}
return false
}
func (b *Box) Focus(_ bool) {
}
|