commit 1418e1b9dc41d8f69bccb8de0fe0f1fb6835ce11
parent 661e3ec2a4dd97d4a8a8eab4f281b088770a6af2
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 26 Feb 2018 22:54:39 -0500
Split UI library and widgets
Diffstat:
13 files changed, 167 insertions(+), 158 deletions(-)
diff --git a/cmd/aerc/main.go b/cmd/aerc/main.go
@@ -11,12 +11,13 @@ import (
tb "github.com/nsf/termbox-go"
"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
-func (f fill) Draw(ctx *ui.Context) {
+func (f fill) Draw(ctx *libui.Context) {
for x := 0; x < ctx.Width(); x += 1 {
for y := 0; y < ctx.Height(); y += 1 {
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
}
@@ -48,38 +49,39 @@ func main() {
panic(err)
}
- tabs := ui.NewTabs()
+ tabs := libui.NewTabs()
tabs.Add(fill('★'), "白い星")
tabs.Add(fill('☆'), "empty stars")
- grid := ui.NewGrid().Rows([]ui.GridSpec{
- ui.GridSpec{ui.SIZE_EXACT, 1},
- ui.GridSpec{ui.SIZE_WEIGHT, 1},
- ui.GridSpec{ui.SIZE_EXACT, 1},
- }).Columns([]ui.GridSpec{
- ui.GridSpec{ui.SIZE_EXACT, 20},
- ui.GridSpec{ui.SIZE_WEIGHT, 1},
+ grid := libui.NewGrid().Rows([]libui.GridSpec{
+ libui.GridSpec{libui.SIZE_EXACT, 1},
+ libui.GridSpec{libui.SIZE_WEIGHT, 1},
+ libui.GridSpec{libui.SIZE_EXACT, 1},
+ }).Columns([]libui.GridSpec{
+ libui.GridSpec{libui.SIZE_EXACT, 20},
+ libui.GridSpec{libui.SIZE_WEIGHT, 1},
})
// TODO: move sidebar into tab content, probably
- grid.AddChild(ui.NewText("aerc").
- Strategy(ui.TEXT_CENTER).
+ grid.AddChild(libui.NewText("aerc").
+ Strategy(libui.TEXT_CENTER).
Color(tb.ColorBlack, tb.ColorWhite))
// sidebar placeholder:
- grid.AddChild(ui.NewBordered(
- fill('.'), ui.BORDER_RIGHT)).At(1, 0).Span(2, 1)
+ grid.AddChild(libui.NewBordered(
+ fill('.'), libui.BORDER_RIGHT)).At(1, 0).Span(2, 1)
grid.AddChild(tabs.TabStrip).At(0, 1)
grid.AddChild(tabs.TabContent).At(1, 1)
- exline := ui.NewExLine()
+ exline := widgets.NewExLine()
grid.AddChild(exline).At(2, 1)
- _ui, err := ui.Initialize(conf, grid)
+ ui, err := libui.Initialize(conf, grid)
if err != nil {
panic(err)
}
- defer _ui.Close()
+ defer ui.Close()
- _ui.AddInteractive(exline)
+ // TODO: this should be a stack
+ ui.AddInteractive(exline)
go (func() {
for {
@@ -88,8 +90,8 @@ func main() {
}
})()
- for !_ui.Exit {
- if !_ui.Tick() {
+ for !ui.Exit {
+ if !ui.Tick() {
// ~60 FPS
time.Sleep(16 * time.Millisecond)
}
diff --git a/ui/borders.go b/lib/ui/borders.go
diff --git a/ui/context.go b/lib/ui/context.go
diff --git a/ui/drawable.go b/lib/ui/drawable.go
diff --git a/ui/grid.go b/lib/ui/grid.go
diff --git a/lib/ui/interactive.go b/lib/ui/interactive.go
@@ -0,0 +1,15 @@
+package ui
+
+import (
+ tb "github.com/nsf/termbox-go"
+)
+
+type Interactive interface {
+ // Returns true if the event was handled by this component
+ Event(event tb.Event) bool
+}
+
+type Simulator interface {
+ // Queues up the given input events for simulation
+ Simulate(events []tb.Event)
+}
diff --git a/ui/tab.go b/lib/ui/tab.go
diff --git a/ui/text.go b/lib/ui/text.go
diff --git a/ui/ui.go b/lib/ui/ui.go
diff --git a/ui/exline.go b/ui/exline.go
@@ -1,127 +0,0 @@
-package ui
-
-import (
- tb "github.com/nsf/termbox-go"
-)
-
-// TODO: history
-// TODO: tab completion
-// TODO: commit
-// TODO: cancel (via esc/ctrl+c)
-// TODO: scrolling
-
-type ExLine struct {
- command *string
- commit func(cmd *string)
- index int
- scroll int
-
- onInvalidate func(d Drawable)
-}
-
-func NewExLine() *ExLine {
- cmd := ""
- return &ExLine{command: &cmd}
-}
-
-func (ex *ExLine) OnInvalidate(onInvalidate func(d Drawable)) {
- ex.onInvalidate = onInvalidate
-}
-
-func (ex *ExLine) Invalidate() {
- if ex.onInvalidate != nil {
- ex.onInvalidate(ex)
- }
-}
-
-func (ex *ExLine) Draw(ctx *Context) {
- cell := tb.Cell{
- Fg: tb.ColorDefault,
- Bg: tb.ColorDefault,
- Ch: ' ',
- }
- ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell)
- ctx.Printf(0, 0, cell, ":%s", *ex.command)
- tb.SetCursor(ctx.X()+ex.index-ex.scroll+1, ctx.Y())
-}
-
-func (ex *ExLine) insert(ch rune) {
- newCmd := (*ex.command)[:ex.index] + string(ch) + (*ex.command)[ex.index:]
- ex.command = &newCmd
- ex.index++
- ex.Invalidate()
-}
-
-func (ex *ExLine) deleteWord() {
- // TODO: Break on any of / " '
- if len(*ex.command) == 0 {
- return
- }
- i := ex.index - 1
- if (*ex.command)[i] == ' ' {
- i--
- }
- for ; i >= 0; i-- {
- if (*ex.command)[i] == ' ' {
- break
- }
- }
- newCmd := (*ex.command)[:i+1] + (*ex.command)[ex.index:]
- ex.command = &newCmd
- ex.index = i + 1
- ex.Invalidate()
-}
-
-func (ex *ExLine) deleteChar() {
- if len(*ex.command) > 0 && ex.index != len(*ex.command) {
- newCmd := (*ex.command)[:ex.index] + (*ex.command)[ex.index+1:]
- ex.command = &newCmd
- ex.Invalidate()
- }
-}
-
-func (ex *ExLine) backspace() {
- if len(*ex.command) > 0 && ex.index != 0 {
- newCmd := (*ex.command)[:ex.index-1] + (*ex.command)[ex.index:]
- ex.command = &newCmd
- ex.index--
- ex.Invalidate()
- }
-}
-
-func (ex *ExLine) Event(event tb.Event) bool {
- switch event.Type {
- case tb.EventKey:
- switch event.Key {
- case tb.KeySpace:
- ex.insert(' ')
- case tb.KeyBackspace, tb.KeyBackspace2:
- ex.backspace()
- case tb.KeyCtrlD, tb.KeyDelete:
- ex.deleteChar()
- case tb.KeyCtrlB, tb.KeyArrowLeft:
- if ex.index > 0 {
- ex.index--
- ex.Invalidate()
- }
- case tb.KeyCtrlF, tb.KeyArrowRight:
- if ex.index < len(*ex.command) {
- ex.index++
- ex.Invalidate()
- }
- case tb.KeyCtrlA, tb.KeyHome:
- ex.index = 0
- ex.Invalidate()
- case tb.KeyCtrlE, tb.KeyEnd:
- ex.index = len(*ex.command)
- ex.Invalidate()
- case tb.KeyCtrlW:
- ex.deleteWord()
- default:
- if event.Ch != 0 {
- ex.insert(event.Ch)
- }
- }
- }
- return true
-}
diff --git a/ui/interactive.go b/ui/interactive.go
@@ -1,10 +0,0 @@
-package ui
-
-import (
- tb "github.com/nsf/termbox-go"
-)
-
-type Interactive interface {
- // Returns true if the event was handled by this component
- Event(event tb.Event) bool
-}
diff --git a/ui/account.go.old b/widgets/account.go.old
diff --git a/widgets/exline.go b/widgets/exline.go
@@ -0,0 +1,129 @@
+package widgets
+
+import (
+ tb "github.com/nsf/termbox-go"
+
+ "git.sr.ht/~sircmpwn/aerc2/lib/ui"
+)
+
+// TODO: history
+// TODO: tab completion
+// TODO: commit
+// TODO: cancel (via esc/ctrl+c)
+// TODO: scrolling
+
+type ExLine struct {
+ command *string
+ commit func(cmd *string)
+ index int
+ scroll int
+
+ onInvalidate func(d ui.Drawable)
+}
+
+func NewExLine() *ExLine {
+ cmd := ""
+ return &ExLine{command: &cmd}
+}
+
+func (ex *ExLine) OnInvalidate(onInvalidate func(d ui.Drawable)) {
+ ex.onInvalidate = onInvalidate
+}
+
+func (ex *ExLine) Invalidate() {
+ if ex.onInvalidate != nil {
+ ex.onInvalidate(ex)
+ }
+}
+
+func (ex *ExLine) Draw(ctx *ui.Context) {
+ cell := tb.Cell{
+ Fg: tb.ColorDefault,
+ Bg: tb.ColorDefault,
+ Ch: ' ',
+ }
+ ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell)
+ ctx.Printf(0, 0, cell, ":%s", *ex.command)
+ tb.SetCursor(ctx.X()+ex.index-ex.scroll+1, ctx.Y())
+}
+
+func (ex *ExLine) insert(ch rune) {
+ newCmd := (*ex.command)[:ex.index] + string(ch) + (*ex.command)[ex.index:]
+ ex.command = &newCmd
+ ex.index++
+ ex.Invalidate()
+}
+
+func (ex *ExLine) deleteWord() {
+ // TODO: Break on any of / " '
+ if len(*ex.command) == 0 {
+ return
+ }
+ i := ex.index - 1
+ if (*ex.command)[i] == ' ' {
+ i--
+ }
+ for ; i >= 0; i-- {
+ if (*ex.command)[i] == ' ' {
+ break
+ }
+ }
+ newCmd := (*ex.command)[:i+1] + (*ex.command)[ex.index:]
+ ex.command = &newCmd
+ ex.index = i + 1
+ ex.Invalidate()
+}
+
+func (ex *ExLine) deleteChar() {
+ if len(*ex.command) > 0 && ex.index != len(*ex.command) {
+ newCmd := (*ex.command)[:ex.index] + (*ex.command)[ex.index+1:]
+ ex.command = &newCmd
+ ex.Invalidate()
+ }
+}
+
+func (ex *ExLine) backspace() {
+ if len(*ex.command) > 0 && ex.index != 0 {
+ newCmd := (*ex.command)[:ex.index-1] + (*ex.command)[ex.index:]
+ ex.command = &newCmd
+ ex.index--
+ ex.Invalidate()
+ }
+}
+
+func (ex *ExLine) Event(event tb.Event) bool {
+ switch event.Type {
+ case tb.EventKey:
+ switch event.Key {
+ case tb.KeySpace:
+ ex.insert(' ')
+ case tb.KeyBackspace, tb.KeyBackspace2:
+ ex.backspace()
+ case tb.KeyCtrlD, tb.KeyDelete:
+ ex.deleteChar()
+ case tb.KeyCtrlB, tb.KeyArrowLeft:
+ if ex.index > 0 {
+ ex.index--
+ ex.Invalidate()
+ }
+ case tb.KeyCtrlF, tb.KeyArrowRight:
+ if ex.index < len(*ex.command) {
+ ex.index++
+ ex.Invalidate()
+ }
+ case tb.KeyCtrlA, tb.KeyHome:
+ ex.index = 0
+ ex.Invalidate()
+ case tb.KeyCtrlE, tb.KeyEnd:
+ ex.index = len(*ex.command)
+ ex.Invalidate()
+ case tb.KeyCtrlW:
+ ex.deleteWord()
+ default:
+ if event.Ch != 0 {
+ ex.insert(event.Ch)
+ }
+ }
+ }
+ return true
+}