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:
@@ -27,29 +27,24 @@ type (
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
*shared.ViewState
|
||||
shared.Sections
|
||||
|
||||
App *model.AppModel
|
||||
cursor int
|
||||
App *model.AppModel
|
||||
width int
|
||||
height int
|
||||
|
||||
cursor int
|
||||
itemOffsets []int // conversation y offsets
|
||||
|
||||
content viewport.Model
|
||||
confirmPrompt bubbles.ConfirmPrompt
|
||||
}
|
||||
|
||||
func Conversations(app *model.AppModel, shared shared.ViewState) Model {
|
||||
func Conversations(app *model.AppModel) *Model {
|
||||
viewport.New(0, 0)
|
||||
m := Model{
|
||||
App: app,
|
||||
ViewState: &shared,
|
||||
content: viewport.New(0, 0),
|
||||
App: app,
|
||||
content: viewport.New(0, 0),
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m Model) Initialized() bool {
|
||||
return m.ViewState.Initialized
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Model) handleInput(msg tea.KeyMsg) tea.Cmd {
|
||||
@@ -129,12 +124,11 @@ func (m *Model) handleInput(msg tea.KeyMsg) tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
m.ViewState.Initialized = true
|
||||
return m.loadConversations()
|
||||
func (m *Model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
|
||||
func (m *Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
|
||||
isInput := false
|
||||
inputHandled := false
|
||||
|
||||
@@ -144,15 +138,14 @@ func (m Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
|
||||
isInput = true
|
||||
cmd := m.handleInput(msg)
|
||||
if cmd != nil {
|
||||
inputHandled = true
|
||||
cmds = append(cmds, cmd)
|
||||
inputHandled = true
|
||||
}
|
||||
case shared.MsgViewEnter:
|
||||
cmds = append(cmds, m.loadConversations())
|
||||
m.content.SetContent(m.renderConversationList())
|
||||
case tea.WindowSizeMsg:
|
||||
m.Width, m.Height = msg.Width, msg.Height
|
||||
m.content.Width = msg.Width
|
||||
m.width, m.height = msg.Width, msg.Height
|
||||
m.content.SetContent(m.renderConversationList())
|
||||
case msgConversationsLoaded:
|
||||
m.App.Conversations = msg
|
||||
@@ -171,26 +164,13 @@ func (m Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
|
||||
}
|
||||
|
||||
if !isInput || !inputHandled {
|
||||
var cmd tea.Cmd
|
||||
m.content, cmd = m.content.Update(msg)
|
||||
content, cmd := m.content.Update(msg)
|
||||
m.content = content
|
||||
if cmd != nil {
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
if m.Width > 0 {
|
||||
wrap := lipgloss.NewStyle().Width(m.Width)
|
||||
m.Header = m.headerView()
|
||||
m.Footer = "" // TODO: "Press ? for help"
|
||||
if m.confirmPrompt.Focused() {
|
||||
m.Footer = wrap.Render(m.confirmPrompt.View())
|
||||
}
|
||||
m.Error = tuiutil.ErrorBanner(m.Err, m.Width)
|
||||
fixedHeight := tuiutil.Height(m.Header) + tuiutil.Height(m.Error) + tuiutil.Height(m.Footer)
|
||||
m.content.Height = m.Height - fixedHeight
|
||||
m.Content = m.content.View()
|
||||
}
|
||||
|
||||
if len(cmds) > 0 {
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
@@ -218,32 +198,22 @@ func (m *Model) deleteConversation(conv api.Conversation) tea.Cmd {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
if m.Width == 0 {
|
||||
return ""
|
||||
}
|
||||
sections := make([]string, 0, 6)
|
||||
|
||||
if m.Header != "" {
|
||||
sections = append(sections, m.Header)
|
||||
}
|
||||
|
||||
sections = append(sections, m.Content)
|
||||
if m.Error != "" {
|
||||
sections = append(sections, m.Error)
|
||||
}
|
||||
|
||||
if m.Footer != "" {
|
||||
sections = append(sections, m.Footer)
|
||||
}
|
||||
|
||||
return lipgloss.JoinVertical(lipgloss.Left, sections...)
|
||||
}
|
||||
|
||||
func (m *Model) headerView() string {
|
||||
func (m *Model) Header(width int) string {
|
||||
titleStyle := lipgloss.NewStyle().Bold(true)
|
||||
header := titleStyle.Render("Conversations")
|
||||
return styles.Header.Width(m.Width).Render(header)
|
||||
return styles.Header.Width(width).Render(header)
|
||||
}
|
||||
|
||||
func (m *Model) Content(width int, height int) string {
|
||||
m.content.Width, m.content.Height = width, height
|
||||
return m.content.View()
|
||||
}
|
||||
|
||||
func (m *Model) Footer(width int) string {
|
||||
if m.confirmPrompt.Focused() {
|
||||
return lipgloss.NewStyle().Width(width).Render(m.confirmPrompt.View())
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Model) renderConversationList() string {
|
||||
@@ -321,7 +291,7 @@ func (m *Model) renderConversationList() string {
|
||||
tStyle = tStyle.Inherit(selectedStyle)
|
||||
}
|
||||
|
||||
title := tStyle.Width(m.Width - 3).PaddingLeft(2).Render(c.Conv.Title)
|
||||
title := tStyle.Width(m.width - 3).PaddingLeft(2).Render(c.Conv.Title)
|
||||
if i == m.cursor {
|
||||
title = ">" + title[1:]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user