diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-02-17 20:21:33 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-02-17 20:21:33 -0500 |
commit | 05ec7357541bcc21dab041da01aec541f6c33cab (patch) | |
tree | 69ca6666e2958bd85c268deb4c33d09963701eff | |
parent | f0791d4ba720c4cfa1d3f90c99296584aa878cd9 (diff) |
Add text widget
-rw-r--r-- | cmd/aerc/main.go | 3 | ||||
-rw-r--r-- | ui/text.go | 71 |
2 files changed, 74 insertions, 0 deletions
diff --git a/cmd/aerc/main.go b/cmd/aerc/main.go index f666748..8541d08 100644 --- a/cmd/aerc/main.go +++ b/cmd/aerc/main.go @@ -62,6 +62,9 @@ func main() { }) // TODO: move sidebar into tab content, probably + grid.AddChild(ui.NewText("aerc"). + Strategy(ui.TEXT_CENTER). + Color(tb.ColorBlack, tb.ColorWhite)) // sidebar placeholder: grid.AddChild(ui.NewBordered( fill('.'), ui.BORDER_RIGHT)).At(1, 0).Span(2, 1) diff --git a/ui/text.go b/ui/text.go new file mode 100644 index 0000000..6164837 --- /dev/null +++ b/ui/text.go @@ -0,0 +1,71 @@ +package ui + +import ( + "github.com/mattn/go-runewidth" + tb "github.com/nsf/termbox-go" +) + +const ( + TEXT_LEFT = iota + TEXT_CENTER = iota + TEXT_RIGHT = iota +) + +type Text struct { + text string + strategy uint + fg tb.Attribute + bg tb.Attribute + onInvalidate func(d Drawable) +} + +func NewText(text string) *Text { + return &Text{text: text} +} + +func (t *Text) Text(text string) *Text { + t.text = text + t.Invalidate() + return t +} + +func (t *Text) Strategy(strategy uint) *Text { + t.strategy = strategy + t.Invalidate() + return t +} + +func (t *Text) Color(fg tb.Attribute, bg tb.Attribute) *Text { + t.fg = fg + t.bg = bg + t.Invalidate() + return t +} + +func (t *Text) Draw(ctx *Context) { + size := runewidth.StringWidth(t.text) + cell := tb.Cell{ + Ch: ' ', + Fg: t.fg, + Bg: t.bg, + } + x := 0 + if t.strategy == TEXT_CENTER { + x = (ctx.Width() - size) / 2 + } + if t.strategy == TEXT_RIGHT { + x = ctx.Width() - size + } + ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell) + ctx.Printf(x, 0, cell, "%s", t.text) +} + +func (t *Text) OnInvalidate(onInvalidate func(d Drawable)) { + t.onInvalidate = onInvalidate +} + +func (t *Text) Invalidate() { + if t.onInvalidate != nil { + t.onInvalidate(t) + } +} |