Debian Codegraph @

lib/ui/borders.go

raw
1 package ui
2
3 import (
4 "git.sr.ht/~rjarry/aerc/config"
5 "go.rockorager.dev/vaxis"
6 )
7
8 const (
9 BORDER_LEFT = 1 << iota
10 BORDER_TOP = 1 << iota
11 BORDER_RIGHT = 1 << iota
12 BORDER_BOTTOM = 1 << iota
13 )
14
15 type Bordered struct {
16 borders uint
17 content Drawable
18 uiConfig *config.UIConfig
19 }
20
21 func NewBordered(
22 content Drawable, borders uint, uiConfig *config.UIConfig,
23 ) *Bordered {
24 b := &Bordered{
25 borders: borders,
26 content: content,
27 uiConfig: uiConfig,
28 }
29 return b
30 }
31
32 func (bordered *Bordered) Children() []Drawable {
33 return []Drawable{bordered.content}
34 }
35
36 func (bordered *Bordered) Invalidate() {
37 Invalidate()
38 }
39
40 func (bordered *Bordered) Draw(ctx *Context) {
41 x := 0
42 y := 0
43 width := ctx.Width()
44 height := ctx.Height()
45 style := bordered.uiConfig.GetStyle(config.STYLE_BORDER)
46 verticalChar := bordered.uiConfig.BorderCharVertical
47 horizontalChar := bordered.uiConfig.BorderCharHorizontal
48
49 if bordered.borders&BORDER_LEFT != 0 {
50 ctx.Fill(0, 0, 1, ctx.Height(), verticalChar, style)
51 x += 1
52 width -= 1
53 }
54 if bordered.borders&BORDER_TOP != 0 {
55 ctx.Fill(0, 0, ctx.Width(), 1, horizontalChar, style)
56 y += 1
57 height -= 1
58 }
59 if bordered.borders&BORDER_RIGHT != 0 {
60 ctx.Fill(ctx.Width()-1, 0, 1, ctx.Height(), verticalChar, style)
61 width -= 1
62 }
63 if bordered.borders&BORDER_BOTTOM != 0 {
64 ctx.Fill(0, ctx.Height()-1, ctx.Width(), 1, horizontalChar, style)
65 height -= 1
66 }
67 subctx := ctx.Subcontext(x, y, width, height)
68 bordered.content.Draw(subctx)
69 }
70
71 func (bordered *Bordered) MouseEvent(localX int, localY int, event vaxis.Event) {
72 if content, ok := bordered.content.(Mouseable); ok {
73 content.MouseEvent(localX, localY, event)
74 }
75 }