tui: call handleResize on states before transitioning

This commit is contained in:
Matt Low 2024-04-01 17:05:36 +00:00
parent 9e6d41a3ff
commit 1404cae6a7
3 changed files with 24 additions and 11 deletions

View File

@ -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) { func (m chatModel) Update(msg tea.Msg) (chatModel, tea.Cmd) {
var cmds []tea.Cmd var cmds []tea.Cmd
switch msg := msg.(type) { 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)) cmds = append(cmds, m.loadConversation(m.opts.convShortname))
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.width = msg.Width m.handleResize(msg.Width, msg.Height)
m.height = msg.Height
m.content.Width = msg.Width
m.input.SetWidth(msg.Width - m.input.FocusedStyle.Base.GetHorizontalBorderSize())
m.rebuildMessageCache()
m.updateContent()
case msgTempfileEditorClosed: case msgTempfileEditorClosed:
contents := string(msg) contents := string(msg)
switch m.editorTarget { switch m.editorTarget {

View File

@ -66,13 +66,18 @@ func (m conversationsModel) Init() tea.Cmd {
return nil 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) { func (m conversationsModel) Update(msg tea.Msg) (conversationsModel, tea.Cmd) {
var cmds []tea.Cmd var cmds []tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case msgChangeState: case msgChangeState:
cmds = append(cmds, m.loadConversations()) cmds = append(cmds, m.loadConversations())
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.content.Width = msg.Width m.handleResize(msg.Width, msg.Height)
case msgConversationsLoaded: case msgConversationsLoaded:
m.conversations = msg m.conversations = msg
m.content.SetContent(m.renderConversationList()) m.content.SetContent(m.renderConversationList())

View File

@ -125,12 +125,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd return m, cmd
} }
case msgChangeState: 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) m.state = state(msg)
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
w, h := msg.Width, msg.Height m.width, m.height = 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
case msgError: case msgError:
m.err = msg m.err = msg
} }