make it possible to switch between panels

This commit is contained in:
David 2021-07-04 14:49:36 -04:00
parent a6f958ccfb
commit 3bff06aad7
2 changed files with 64 additions and 14 deletions

View File

@ -10,6 +10,11 @@ import (
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
) )
const (
IN_MENU = iota
IN_BOOK
)
func main() { func main() {
var c config.Config var c config.Config
err := envconfig.Process("library", &c) err := envconfig.Process("library", &c)
@ -28,21 +33,28 @@ func main() {
l := ui.NewList([]string{"foo", "bar", "baz"}, 0) l := ui.NewList([]string{"foo", "bar", "baz"}, 0)
menu := ui.NewBox( menu := ui.NewBox(
"library", "library",
[]string{"(e)dit", "(q)uit"}, []string{"(q)uit"},
ui.Contents{{ ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2}, Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: l, Container: l,
}}, }},
) )
book := ui.NewBookDetails()
activeBook := ui.NewBox( activeBook := ui.NewBox(
"book", "book",
[]string{"test"}, []string{"test"},
ui.Contents{}, ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: book,
}},
) )
container := ui.NewContainer( container := ui.NewContainer(
ui.Contents{{Container: menu}, {Container: activeBook}}, ui.Contents{
ui.LayoutHorizontalEven, {Container: menu, Offsets: ui.Offsets{Percent: 1}},
{Container: activeBook, Offsets: ui.Offsets{Percent: 2}},
},
ui.LayoutHorizontalPercent,
) )
// init // init
@ -52,6 +64,8 @@ func main() {
container.Draw(screen) container.Draw(screen)
screen.Sync() screen.Sync()
state := IN_MENU
// UI loop // UI loop
for { for {
e := screen.PollEvent() e := screen.PollEvent()
@ -59,16 +73,25 @@ func main() {
case *tcell.EventError: case *tcell.EventError:
screen.Beep() screen.Beep()
case *tcell.EventKey: // input handling case *tcell.EventKey: // input handling
if v.Key() == tcell.KeyUp && l.Selected() > 0 { if state == IN_MENU {
l.SetSelected(l.Selected() - 1) if v.Key() == tcell.KeyUp && l.Selected() > 0 {
} l.SetSelected(l.Selected() - 1)
if v.Key() == tcell.KeyDown && l.Selected() < len(l.ListMembers())-1 { }
l.SetSelected(l.Selected() + 1) if v.Key() == tcell.KeyDown && l.Selected() < len(l.ListMembers())-1 {
} l.SetSelected(l.Selected() + 1)
if v.Rune() == 'q' { }
screen.Fini() if v.Rune() == 'q' {
fmt.Printf("Thank you for playing Wing Commander!\n\n") screen.Fini()
return fmt.Printf("Thank you for playing Wing Commander!\n\n")
return
}
if v.Key() == tcell.KeyRight {
state = IN_BOOK
}
} else if state == IN_BOOK {
if v.Key() == tcell.KeyLeft {
state = IN_MENU
}
} }
screen.Clear() screen.Clear()
container.Draw(screen) container.Draw(screen)

View File

@ -3,6 +3,7 @@ package ui
import ( import (
"strings" "strings"
"git.yetaga.in/alazyreader/library/book"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@ -266,6 +267,32 @@ func (l *List) ListMembers() []string {
return l.listItems return l.listItems
} }
// A List is a scrollable, pageable list with a selector token.
type BookDetails struct {
x, y int
h, w int
selected int
book book.Book
}
func NewBookDetails() *BookDetails {
return &BookDetails{}
}
func (l *BookDetails) SetSize(x, y, h, w int) {
l.x, l.y, l.h, l.w = x, y, h, w
}
func (l *BookDetails) Draw(s tcell.Screen) {
items := []string{"title", "authors", "isbn-10", "isbn-13"}
for i := range items {
for j, r := range items[i] {
s.SetContent(l.x+j, l.y+i, r, nil, tcell.StyleDefault)
}
s.SetContent(l.x+len(items[i]), l.y+i, ':', nil, tcell.StyleDefault)
}
}
// PaddedText outputs strings with a space on both sides. // PaddedText outputs strings with a space on both sides.
// Useful for generating headings, footers, etc. Used by Box. // Useful for generating headings, footers, etc. Used by Box.
type PaddedText struct { type PaddedText struct {