TUI view management and input handling cleanup
This commit is contained in:
@@ -25,8 +25,9 @@ type (
|
||||
// sent when a conversation is deleted
|
||||
msgConversationDeleted struct{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
shared.Shared
|
||||
*shared.ViewState
|
||||
shared.Sections
|
||||
|
||||
App *model.AppModel
|
||||
@@ -38,21 +39,25 @@ type Model struct {
|
||||
confirmPrompt bubbles.ConfirmPrompt
|
||||
}
|
||||
|
||||
func Conversations(app *model.AppModel, shared shared.Shared) Model {
|
||||
func Conversations(app *model.AppModel, shared shared.ViewState) Model {
|
||||
m := Model{
|
||||
App: app,
|
||||
Shared: shared,
|
||||
content: viewport.New(0, 0),
|
||||
App: app,
|
||||
ViewState: &shared,
|
||||
content: viewport.New(0, 0),
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Model) HandleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
func (m Model) Initialized() bool {
|
||||
return m.ViewState.Initialized
|
||||
}
|
||||
|
||||
func (m *Model) handleInput(msg tea.KeyMsg) tea.Cmd {
|
||||
if m.confirmPrompt.Focused() {
|
||||
var cmd tea.Cmd
|
||||
m.confirmPrompt, cmd = m.confirmPrompt.Update(msg)
|
||||
if cmd != nil {
|
||||
return true, cmd
|
||||
return cmd
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,8 +66,8 @@ func (m *Model) HandleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
if len(m.App.Conversations) > 0 && m.cursor < len(m.App.Conversations) {
|
||||
m.App.Conversation = &m.App.Conversations[m.cursor].Conv
|
||||
m.App.Messages = []api.Message{}
|
||||
return true, func() tea.Msg {
|
||||
return shared.MsgViewChange(shared.StateChat)
|
||||
return func() tea.Msg {
|
||||
return shared.MsgViewChange(shared.ViewChat)
|
||||
}
|
||||
}
|
||||
case "j", "down":
|
||||
@@ -81,7 +86,7 @@ func (m *Model) HandleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
m.cursor = len(m.App.Conversations) - 1
|
||||
m.content.GotoBottom()
|
||||
}
|
||||
return true, nil
|
||||
return shared.KeyHandled(msg)
|
||||
case "k", "up":
|
||||
if m.cursor > 0 {
|
||||
m.cursor--
|
||||
@@ -95,7 +100,7 @@ func (m *Model) HandleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
m.cursor = 0
|
||||
m.content.GotoTop()
|
||||
}
|
||||
return true, nil
|
||||
return shared.KeyHandled(msg)
|
||||
case "n":
|
||||
// new conversation
|
||||
case "d":
|
||||
@@ -111,7 +116,7 @@ func (m *Model) HandleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
m.confirmPrompt.Style = lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color("3"))
|
||||
return true, nil
|
||||
return shared.KeyHandled(msg)
|
||||
}
|
||||
case "c":
|
||||
// copy/clone conversation
|
||||
@@ -120,16 +125,27 @@ func (m *Model) HandleInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
case "shift+r":
|
||||
// show prompt to generate name for conversation
|
||||
}
|
||||
return false, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
m.ViewState.Initialized = true
|
||||
return m.loadConversations()
|
||||
}
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
func (m Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
|
||||
isInput := false
|
||||
inputHandled := false
|
||||
|
||||
var cmds []tea.Cmd
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
isInput = true
|
||||
cmd := m.handleInput(msg)
|
||||
if cmd != nil {
|
||||
inputHandled = true
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
case shared.MsgViewEnter:
|
||||
cmds = append(cmds, m.loadConversations())
|
||||
m.content.SetContent(m.renderConversationList())
|
||||
@@ -153,10 +169,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
cmds = append(cmds, m.loadConversations())
|
||||
}
|
||||
|
||||
var cmd tea.Cmd
|
||||
m.content, cmd = m.content.Update(msg)
|
||||
if cmd != nil {
|
||||
cmds = append(cmds, cmd)
|
||||
if !isInput || !inputHandled {
|
||||
var cmd tea.Cmd
|
||||
m.content, cmd = m.content.Update(msg)
|
||||
if cmd != nil {
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
if m.Width > 0 {
|
||||
@@ -171,7 +189,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
m.content.Height = m.Height - fixedHeight
|
||||
m.Content = m.content.View()
|
||||
}
|
||||
return m, tea.Batch(cmds...)
|
||||
|
||||
if len(cmds) > 0 {
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Model) loadConversations() tea.Cmd {
|
||||
@@ -289,7 +312,7 @@ func (m *Model) renderConversationList() string {
|
||||
sb.WriteRune('\n')
|
||||
}
|
||||
|
||||
tStyle := titleStyle.Copy()
|
||||
tStyle := titleStyle
|
||||
if c.Conv.Title == "" {
|
||||
tStyle = tStyle.Inherit(untitledStyle).SetString("(untitled)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user