| 1 |
package ui |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
|
| 6 |
"git.sr.ht/~rjarry/aerc/config" |
| 7 |
"github.com/mattn/go-runewidth" |
| 8 |
"go.rockorager.dev/vaxis" |
| 9 |
) |
| 10 |
|
| 11 |
type Box struct { |
| 12 |
content Drawable |
| 13 |
title string |
| 14 |
borders string |
| 15 |
uiConfig *config.UIConfig |
| 16 |
} |
| 17 |
|
| 18 |
func NewBox( |
| 19 |
content Drawable, title, borders string, uiConfig *config.UIConfig, |
| 20 |
) *Box { |
| 21 |
if borders == "" || len(borders) < 8 { |
| 22 |
borders = "││┌─┐└─┘" |
| 23 |
} |
| 24 |
|
| 25 |
b := &Box{ |
| 26 |
content: content, |
| 27 |
title: title, |
| 28 |
borders: borders, |
| 29 |
uiConfig: uiConfig, |
| 30 |
} |
| 31 |
return b |
| 32 |
} |
| 33 |
|
| 34 |
func (b *Box) Draw(ctx *Context) { |
| 35 |
w := ctx.Width() |
| 36 |
h := ctx.Height() |
| 37 |
|
| 38 |
style := b.uiConfig.GetStyle(config.STYLE_BORDER) |
| 39 |
|
| 40 |
box := []rune(b.borders) |
| 41 |
ctx.Fill(0, 0, 1, h, box[0], style) |
| 42 |
ctx.Fill(w-1, 0, 1, h, box[1], style) |
| 43 |
|
| 44 |
ctx.Printf(0, 0, style, "%c%s%c", box[2], strings.Repeat(string(box[3]), w-2), box[4]) |
| 45 |
ctx.Printf(0, h-1, style, "%c%s%c", box[5], strings.Repeat(string(box[6]), w-2), box[7]) |
| 46 |
|
| 47 |
if b.title != "" && w > 4 { |
| 48 |
style = b.uiConfig.GetStyle(config.STYLE_TITLE) |
| 49 |
title := runewidth.Truncate(b.title, w-4, "…") |
| 50 |
ctx.Printf(2, 0, style, "%s", title) |
| 51 |
} |
| 52 |
|
| 53 |
subctx := ctx.Subcontext(1, 1, w-2, h-2) |
| 54 |
b.content.Draw(subctx) |
| 55 |
} |
| 56 |
|
| 57 |
func (b *Box) Invalidate() { |
| 58 |
b.content.Invalidate() |
| 59 |
} |
| 60 |
|
| 61 |
func (b *Box) MouseEvent(localX int, localY int, event vaxis.Event) { |
| 62 |
if content, ok := b.content.(Mouseable); ok { |
| 63 |
content.MouseEvent(localX, localY, event) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func (b *Box) Event(e vaxis.Event) bool { |
| 68 |
if content, ok := b.content.(Interactive); ok { |
| 69 |
return content.Event(e) |
| 70 |
} |
| 71 |
return false |
| 72 |
} |
| 73 |
|
| 74 |
func (b *Box) Focus(_ bool) { |
| 75 |
} |