From 1404cae6a742157a41b67573526cf64ab3a4bc7f Mon Sep 17 00:00:00 2001 From: Matt Low Date: Mon, 1 Apr 2024 17:05:36 +0000 Subject: [PATCH] tui: call handleResize on states before transitioning --- pkg/tui/chat.go | 17 +++++++++++------ pkg/tui/conversations.go | 7 ++++++- pkg/tui/tui.go | 11 +++++++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/tui/chat.go b/pkg/tui/chat.go index 4219835..d76a633 100644 --- a/pkg/tui/chat.go +++ b/pkg/tui/chat.go @@ -214,6 +214,16 @@ func (m chatModel) Init() tea.Cmd { ) } +func (m *chatModel) handleResize(width, height int) { + m.width, m.height = width, height + m.content.Width = width + m.input.SetWidth(width - m.input.FocusedStyle.Base.GetHorizontalFrameSize()) + if len(m.messages) > 0 { + m.rebuildMessageCache() + m.updateContent() + } +} + func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) { var cmds []tea.Cmd switch msg := msg.(type) { @@ -222,12 +232,7 @@ func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) { cmds = append(cmds, m.loadConversation(m.opts.convShortname)) } case tea.WindowSizeMsg: - m.width = msg.Width - m.height = msg.Height - m.content.Width = msg.Width - m.input.SetWidth(msg.Width - m.input.FocusedStyle.Base.GetHorizontalBorderSize()) - m.rebuildMessageCache() - m.updateContent() + m.handleResize(msg.Width, msg.Height) case msgTempfileEditorClosed: contents := string(msg) switch m.editorTarget { diff --git a/pkg/tui/conversations.go b/pkg/tui/conversations.go index a16cd00..2df825f 100644 --- a/pkg/tui/conversations.go +++ b/pkg/tui/conversations.go @@ -66,13 +66,18 @@ func (m conversationsModel) Init() tea.Cmd { return nil } +func (m *conversationsModel) handleResize(width, height int) { + m.width, m.height = width, height + m.content.Width = width +} + func (m conversationsModel) Update(msg tea.Msg) (conversationsModel, tea.Cmd) { var cmds []tea.Cmd switch msg := msg.(type) { case msgChangeState: cmds = append(cmds, m.loadConversations()) case tea.WindowSizeMsg: - m.content.Width = msg.Width + m.handleResize(msg.Width, msg.Height) case msgConversationsLoaded: m.conversations = msg m.content.SetContent(m.renderConversationList()) diff --git a/pkg/tui/tui.go b/pkg/tui/tui.go index d12b32a..3f74013 100644 --- a/pkg/tui/tui.go +++ b/pkg/tui/tui.go @@ -125,12 +125,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, cmd } case msgChangeState: + switch msg { + case stateChat: + m.chat.handleResize(m.width, m.height) + case stateConversations: + m.conversations.handleResize(m.width, m.height) + } m.state = state(msg) case tea.WindowSizeMsg: - w, h := msg.Width, msg.Height - m.width, m.height = w, h - m.chat.width, m.chat.height = w, h - m.conversations.width, m.conversations.height = w, h + m.width, m.height = msg.Width, msg.Height case msgError: m.err = msg }