2018-02-18 01:11:58 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2020-11-30 22:07:03 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-07-27 08:03:55 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
2018-02-18 01:11:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BORDER_LEFT = 1 << iota
|
|
|
|
BORDER_TOP = 1 << iota
|
|
|
|
BORDER_RIGHT = 1 << iota
|
|
|
|
BORDER_BOTTOM = 1 << iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bordered struct {
|
2019-04-27 16:47:59 +00:00
|
|
|
Invalidatable
|
2018-02-18 01:11:58 +00:00
|
|
|
borders uint
|
|
|
|
content Drawable
|
|
|
|
onInvalidate func(d Drawable)
|
2020-07-27 08:03:55 +00:00
|
|
|
uiConfig config.UIConfig
|
2018-02-18 01:11:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 08:03:55 +00:00
|
|
|
func NewBordered(
|
|
|
|
content Drawable, borders uint, uiConfig config.UIConfig) *Bordered {
|
2018-02-18 01:11:58 +00:00
|
|
|
b := &Bordered{
|
2020-07-27 08:03:55 +00:00
|
|
|
borders: borders,
|
|
|
|
content: content,
|
|
|
|
uiConfig: uiConfig,
|
2018-02-18 01:11:58 +00:00
|
|
|
}
|
|
|
|
content.OnInvalidate(b.contentInvalidated)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bordered *Bordered) contentInvalidated(d Drawable) {
|
|
|
|
bordered.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-01-20 20:06:44 +00:00
|
|
|
func (bordered *Bordered) Children() []Drawable {
|
|
|
|
return []Drawable{bordered.content}
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:11:58 +00:00
|
|
|
func (bordered *Bordered) Invalidate() {
|
2019-04-27 16:47:59 +00:00
|
|
|
bordered.DoInvalidate(bordered)
|
2018-02-18 01:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bordered *Bordered) Draw(ctx *Context) {
|
|
|
|
x := 0
|
|
|
|
y := 0
|
|
|
|
width := ctx.Width()
|
|
|
|
height := ctx.Height()
|
2020-07-27 08:03:55 +00:00
|
|
|
style := bordered.uiConfig.GetStyle(config.STYLE_BORDER)
|
2021-11-29 22:48:35 +00:00
|
|
|
verticalChar := bordered.uiConfig.BorderCharVertical
|
|
|
|
horizontalChar := bordered.uiConfig.BorderCharHorizontal
|
|
|
|
|
2018-02-18 01:11:58 +00:00
|
|
|
if bordered.borders&BORDER_LEFT != 0 {
|
2021-11-29 22:48:35 +00:00
|
|
|
ctx.Fill(0, 0, 1, ctx.Height(), verticalChar, style)
|
2018-02-18 01:11:58 +00:00
|
|
|
x += 1
|
|
|
|
width -= 1
|
|
|
|
}
|
|
|
|
if bordered.borders&BORDER_TOP != 0 {
|
2021-11-29 22:48:35 +00:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), 1, horizontalChar, style)
|
2018-02-18 01:11:58 +00:00
|
|
|
y += 1
|
|
|
|
height -= 1
|
|
|
|
}
|
|
|
|
if bordered.borders&BORDER_RIGHT != 0 {
|
2021-11-29 22:48:35 +00:00
|
|
|
ctx.Fill(ctx.Width()-1, 0, 1, ctx.Height(), verticalChar, style)
|
2018-02-18 01:11:58 +00:00
|
|
|
width -= 1
|
|
|
|
}
|
|
|
|
if bordered.borders&BORDER_BOTTOM != 0 {
|
2021-11-29 22:48:35 +00:00
|
|
|
ctx.Fill(0, ctx.Height()-1, ctx.Width(), 1, horizontalChar, style)
|
2018-02-18 01:11:58 +00:00
|
|
|
height -= 1
|
|
|
|
}
|
|
|
|
subctx := ctx.Subcontext(x, y, width, height)
|
|
|
|
bordered.content.Draw(subctx)
|
|
|
|
}
|
2019-09-05 22:32:36 +00:00
|
|
|
|
|
|
|
func (bordered *Bordered) MouseEvent(localX int, localY int, event tcell.Event) {
|
|
|
|
switch content := bordered.content.(type) {
|
|
|
|
case Mouseable:
|
|
|
|
content.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
}
|