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

View File

@ -3,6 +3,7 @@ package ui
import (
"strings"
"git.yetaga.in/alazyreader/library/book"
"github.com/gdamore/tcell"
)
@ -266,6 +267,32 @@ func (l *List) ListMembers() []string {
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.
// Useful for generating headings, footers, etc. Used by Box.
type PaddedText struct {