From 90a9facc44621ee15f6b1e2b43f40a5f0df5d13d Mon Sep 17 00:00:00 2001 From: David Ashby Date: Wed, 28 Jul 2021 21:36:45 -0400 Subject: [PATCH] fully commit to event-driven arch --- cmd/manage/events.go | 89 +++++++++++++++++++++++++++++++++++++++++++ cmd/manage/main.go | 91 +++++++++++++++++++++++--------------------- 2 files changed, 136 insertions(+), 44 deletions(-) create mode 100644 cmd/manage/events.go diff --git a/cmd/manage/events.go b/cmd/manage/events.go new file mode 100644 index 0000000..f8eea27 --- /dev/null +++ b/cmd/manage/events.go @@ -0,0 +1,89 @@ +package main + +import ( + "git.yetaga.in/alazyreader/library/book" + "github.com/gdamore/tcell" +) + +// save change to book +type EventBookUpdate struct { + tcell.EventTime + book *book.Book +} + +func NewEventBookUpdate(b *book.Book) *EventBookUpdate { + e := &EventBookUpdate{book: b} + e.SetEventNow() + return e +} + +func (e *EventBookUpdate) Book() *book.Book { + return e.book +} + +// open new book in display +type EventLoadBook struct { + tcell.EventTime + ID int +} + +func NewEventLoadBook(id int) *EventLoadBook { + e := &EventLoadBook{ID: id} + e.SetEventNow() + return e +} + +// open new book in display +type EventEnterBook struct { + tcell.EventTime +} + +func NewEventEnterBook() *EventEnterBook { + e := &EventEnterBook{} + e.SetEventNow() + return e +} + +// switch back to menu control +type EventExitBook struct { + tcell.EventTime +} + +func NewEventExitBook() *EventExitBook { + e := &EventExitBook{} + e.SetEventNow() + return e +} + +// open import window +type EventOpenImport struct { + tcell.EventTime +} + +func NewEventOpenImport() *EventOpenImport { + e := &EventOpenImport{} + e.SetEventNow() + return e +} + +// close import window +type EventCloseImport struct { + tcell.EventTime +} + +func NewEventCloseImport() *EventCloseImport { + e := &EventCloseImport{} + e.SetEventNow() + return e +} + +// quit +type EventQuit struct { + tcell.EventTime +} + +func NewEventQuit() *EventQuit { + e := &EventQuit{} + e.SetEventNow() + return e +} diff --git a/cmd/manage/main.go b/cmd/manage/main.go index 5d9d5fe..2b4b624 100644 --- a/cmd/manage/main.go +++ b/cmd/manage/main.go @@ -46,37 +46,13 @@ func (s *State) Set(key string, value interface{}) { s.stateMap[key] = value } +// UI states const ( IN_MENU = iota IN_BOOK + IN_IMPORT ) -type EventBookUpdate struct { - tcell.EventTime - book *book.Book -} - -func NewEventBookUpdate(b *book.Book) *EventBookUpdate { - e := &EventBookUpdate{book: b} - e.SetEventNow() - return e -} - -func (e *EventBookUpdate) Book() *book.Book { - return e.book -} - -type EventLoadBook struct { - tcell.EventTime - ID int -} - -func NewEventLoadBook(id int) *EventLoadBook { - e := &EventLoadBook{ID: id} - e.SetEventNow() - return e -} - func main() { var c config.Config err := envconfig.Process("library", &c) @@ -131,6 +107,7 @@ func main() { } }() + // book list and options menu (left column) l := ui.NewList(Titles(state.Get("library").([]book.Book)), 0) menu := ui.NewBox( "library", @@ -145,6 +122,8 @@ func main() { book := ui.NewBookDetails(&book.Book{ Title: "test title", }) + + // book display (right column) activeBook := ui.NewBox( "book", nil, @@ -156,6 +135,10 @@ func main() { false, ) + // pop-up + popup := ui.NewBox("import", nil, nil, ui.StyleActive, false) + popup.SetVisible(false) + container := ui.NewContainer( ui.Contents{ {Container: menu, Offsets: ui.Offsets{Percent: 1}}, @@ -193,42 +176,62 @@ func main() { l.SetSelected(l.Selected() + 1) screen.PostEvent(NewEventLoadBook(l.SelectedID())) } - if v.Rune() == 'q' { - screen.Fini() - fmt.Printf("Thank you for playing Wing Commander!\n\n") - return - } if v.Key() == tcell.KeyEnter { - activeBook.SetStyle(ui.StyleActive) - menu.SetStyle(ui.StyleInactive) - state.Set("ui_state", IN_BOOK) + screen.PostEvent(NewEventEnterBook()) + } + if v.Rune() == 'i' { + screen.PostEvent(NewEventOpenImport()) + } + if v.Rune() == 'q' { + screen.PostEvent(NewEventQuit()) } } else if curr == IN_BOOK { if v.Key() == tcell.KeyEsc { - activeBook.SetStyle(ui.StyleInactive) - menu.SetStyle(ui.StyleActive) - state.Set("ui_state", IN_MENU) + screen.PostEvent(NewEventExitBook()) + } + } else if curr == IN_IMPORT { + if v.Key() == tcell.KeyEsc { + screen.PostEvent(NewEventCloseImport()) } } - screen.Clear() - container.Draw(screen) case *tcell.EventResize: // screen redraw w, h := screen.Size() container.SetSize(0, 0, h, w) - screen.Clear() - container.Draw(screen) case *EventBookUpdate: // TK + case *EventEnterBook: + activeBook.SetStyle(ui.StyleActive) + menu.SetStyle(ui.StyleInactive) + state.Set("ui_state", IN_BOOK) + case *EventExitBook: + state.Set("ui_state", IN_MENU) + activeBook.SetStyle(ui.StyleInactive) + menu.SetStyle(ui.StyleActive) case *EventLoadBook: book.SetBook(GetBookByID(v.ID, books)) - screen.Clear() - container.Draw(screen) + case *EventOpenImport: + state.Set("ui_state", IN_IMPORT) + menu.SetStyle(ui.StyleInactive) + popup.SetVisible(true) + popup.SetSize(6, 3, 10, 80) + case *EventCloseImport: + state.Set("ui_state", IN_MENU) + menu.SetStyle(ui.StyleActive) + popup.SetVisible(false) + case *EventQuit: + screen.Fini() + fmt.Printf("Thank you for playing Wing Commander!\n\n") + return case *tcell.EventInterrupt: case *tcell.EventMouse: case *tcell.EventTime: default: } - screen.Show() // repaint + // repaint + screen.Clear() + container.Draw(screen) + popup.Draw(screen) + screen.Show() } }