Split UI library and widgets

This commit is contained in:
Drew DeVault 2018-02-26 22:54:39 -05:00
parent 661e3ec2a4
commit 1418e1b9dc
11 changed files with 34 additions and 25 deletions

View File

@ -11,12 +11,13 @@ import (
tb "github.com/nsf/termbox-go" tb "github.com/nsf/termbox-go"
"git.sr.ht/~sircmpwn/aerc2/config" "git.sr.ht/~sircmpwn/aerc2/config"
"git.sr.ht/~sircmpwn/aerc2/ui" libui "git.sr.ht/~sircmpwn/aerc2/lib/ui"
"git.sr.ht/~sircmpwn/aerc2/widgets"
) )
type fill rune type fill rune
func (f fill) Draw(ctx *ui.Context) { func (f fill) Draw(ctx *libui.Context) {
for x := 0; x < ctx.Width(); x += 1 { for x := 0; x < ctx.Width(); x += 1 {
for y := 0; y < ctx.Height(); y += 1 { for y := 0; y < ctx.Height(); y += 1 {
ctx.SetCell(x, y, rune(f), tb.ColorDefault, tb.ColorDefault) ctx.SetCell(x, y, rune(f), tb.ColorDefault, tb.ColorDefault)
@ -24,7 +25,7 @@ func (f fill) Draw(ctx *ui.Context) {
} }
} }
func (f fill) OnInvalidate(callback func(d ui.Drawable)) { func (f fill) OnInvalidate(callback func(d libui.Drawable)) {
// no-op // no-op
} }
@ -48,38 +49,39 @@ func main() {
panic(err) panic(err)
} }
tabs := ui.NewTabs() tabs := libui.NewTabs()
tabs.Add(fill('★'), "白い星") tabs.Add(fill('★'), "白い星")
tabs.Add(fill('☆'), "empty stars") tabs.Add(fill('☆'), "empty stars")
grid := ui.NewGrid().Rows([]ui.GridSpec{ grid := libui.NewGrid().Rows([]libui.GridSpec{
ui.GridSpec{ui.SIZE_EXACT, 1}, libui.GridSpec{libui.SIZE_EXACT, 1},
ui.GridSpec{ui.SIZE_WEIGHT, 1}, libui.GridSpec{libui.SIZE_WEIGHT, 1},
ui.GridSpec{ui.SIZE_EXACT, 1}, libui.GridSpec{libui.SIZE_EXACT, 1},
}).Columns([]ui.GridSpec{ }).Columns([]libui.GridSpec{
ui.GridSpec{ui.SIZE_EXACT, 20}, libui.GridSpec{libui.SIZE_EXACT, 20},
ui.GridSpec{ui.SIZE_WEIGHT, 1}, libui.GridSpec{libui.SIZE_WEIGHT, 1},
}) })
// TODO: move sidebar into tab content, probably // TODO: move sidebar into tab content, probably
grid.AddChild(ui.NewText("aerc"). grid.AddChild(libui.NewText("aerc").
Strategy(ui.TEXT_CENTER). Strategy(libui.TEXT_CENTER).
Color(tb.ColorBlack, tb.ColorWhite)) Color(tb.ColorBlack, tb.ColorWhite))
// sidebar placeholder: // sidebar placeholder:
grid.AddChild(ui.NewBordered( grid.AddChild(libui.NewBordered(
fill('.'), ui.BORDER_RIGHT)).At(1, 0).Span(2, 1) fill('.'), libui.BORDER_RIGHT)).At(1, 0).Span(2, 1)
grid.AddChild(tabs.TabStrip).At(0, 1) grid.AddChild(tabs.TabStrip).At(0, 1)
grid.AddChild(tabs.TabContent).At(1, 1) grid.AddChild(tabs.TabContent).At(1, 1)
exline := ui.NewExLine() exline := widgets.NewExLine()
grid.AddChild(exline).At(2, 1) grid.AddChild(exline).At(2, 1)
_ui, err := ui.Initialize(conf, grid) ui, err := libui.Initialize(conf, grid)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer _ui.Close() defer ui.Close()
_ui.AddInteractive(exline) // TODO: this should be a stack
ui.AddInteractive(exline)
go (func() { go (func() {
for { for {
@ -88,8 +90,8 @@ func main() {
} }
})() })()
for !_ui.Exit { for !ui.Exit {
if !_ui.Tick() { if !ui.Tick() {
// ~60 FPS // ~60 FPS
time.Sleep(16 * time.Millisecond) time.Sleep(16 * time.Millisecond)
} }

View File

@ -8,3 +8,8 @@ type Interactive interface {
// Returns true if the event was handled by this component // Returns true if the event was handled by this component
Event(event tb.Event) bool Event(event tb.Event) bool
} }
type Simulator interface {
// Queues up the given input events for simulation
Simulate(events []tb.Event)
}

View File

@ -1,7 +1,9 @@
package ui package widgets
import ( import (
tb "github.com/nsf/termbox-go" tb "github.com/nsf/termbox-go"
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
) )
// TODO: history // TODO: history
@ -16,7 +18,7 @@ type ExLine struct {
index int index int
scroll int scroll int
onInvalidate func(d Drawable) onInvalidate func(d ui.Drawable)
} }
func NewExLine() *ExLine { func NewExLine() *ExLine {
@ -24,7 +26,7 @@ func NewExLine() *ExLine {
return &ExLine{command: &cmd} return &ExLine{command: &cmd}
} }
func (ex *ExLine) OnInvalidate(onInvalidate func(d Drawable)) { func (ex *ExLine) OnInvalidate(onInvalidate func(d ui.Drawable)) {
ex.onInvalidate = onInvalidate ex.onInvalidate = onInvalidate
} }
@ -34,7 +36,7 @@ func (ex *ExLine) Invalidate() {
} }
} }
func (ex *ExLine) Draw(ctx *Context) { func (ex *ExLine) Draw(ctx *ui.Context) {
cell := tb.Cell{ cell := tb.Cell{
Fg: tb.ColorDefault, Fg: tb.ColorDefault,
Bg: tb.ColorDefault, Bg: tb.ColorDefault,