Refactor TUI rendering handling and general cleanup
Improves render handling by moving the responsibility of laying out the whole UI from each view and into the main `tui` model. Our `ViewModel` interface has now diverged from bubbletea's `Model` and introduces individual `Header`, `Content`, and `Footer` methods for rendering those UI elements. Also moved away from using value receivers on our Update and View functions (as is common across Bubbletea) to pointer receivers, which cleaned up some of the weirder aspects of the code (e.g. before we essentially had no choice but to do our rendering in `Update` in order to calculate and update the final height of the main content's `viewport`).
This commit is contained in:
@@ -9,15 +9,12 @@ import (
|
||||
type ViewModel interface {
|
||||
Init() tea.Cmd
|
||||
Update(tea.Msg) (ViewModel, tea.Cmd)
|
||||
View() string
|
||||
Initialized() bool // Return whether this view is initialized
|
||||
}
|
||||
|
||||
type ViewState struct {
|
||||
Initialized bool
|
||||
Width int
|
||||
Height int
|
||||
Err error
|
||||
// View methods
|
||||
Header(width int) string
|
||||
// Render the view's main content into a container of the given dimensions
|
||||
Content(width, height int) string
|
||||
Footer(width int) string
|
||||
}
|
||||
|
||||
type View int
|
||||
@@ -29,22 +26,12 @@ const (
|
||||
//StateHelp
|
||||
)
|
||||
|
||||
// a convenience struct for holding rendered content for indiviudal UI
|
||||
// elements
|
||||
type Sections struct {
|
||||
Header string
|
||||
Content string
|
||||
Error string
|
||||
Input string
|
||||
Footer string
|
||||
}
|
||||
|
||||
type (
|
||||
// send to change the current state
|
||||
MsgViewChange View
|
||||
// sent to a state when it is entered
|
||||
MsgViewEnter struct{}
|
||||
// sent when an error occurs
|
||||
// sent when a recoverable error occurs (displayed to user)
|
||||
MsgError error
|
||||
// sent when the view has handled a key input
|
||||
MsgKeyHandled tea.KeyMsg
|
||||
|
||||
Reference in New Issue
Block a user