Split main model update function into an events file
This commit is contained in:
parent
123d1ca18a
commit
c3be59660d
2 changed files with 60 additions and 57 deletions
60
internal/ui_main_events.go
Normal file
60
internal/ui_main_events.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package internal
|
||||
|
||||
import tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
// Setup keybindings
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
// Exit when user presses Q or CTRL+C
|
||||
return m, tea.Quit
|
||||
|
||||
case "enter":
|
||||
if m.loaded {
|
||||
// Process the selected item
|
||||
m.processSelection()
|
||||
}
|
||||
case "ctrl+z", "backspace":
|
||||
// Go backwards in the model
|
||||
if m.focused > 0 {
|
||||
m.focused--
|
||||
return m, nil
|
||||
} else {
|
||||
// If we are at the beginning, just exit
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
case tea.WindowSizeMsg:
|
||||
if !m.loaded {
|
||||
// Initialize the static lists and make sure the content
|
||||
// does not extend past the screen
|
||||
m.initLists(msg.Width, msg.Height)
|
||||
|
||||
// Set model loaded to true
|
||||
m.loaded = true
|
||||
} else {
|
||||
// Else we are loaded and will update the sizing on the fly
|
||||
m.height = msg.Height
|
||||
m.width = msg.Width
|
||||
|
||||
// TODO: Find a better way to resize widgets when word wrapping happens
|
||||
// BUG: currently breaks the UI rendering if word wrapping happens in some cases...
|
||||
views := len(m.lists)
|
||||
if msg.Width > 83 {
|
||||
for i := 0; i < views; i++ {
|
||||
m.lists[i].SetSize(m.width-m.offsetx[i], m.height-m.offsety[i])
|
||||
// Update the styles with the correct width
|
||||
dialogStyle = dialogStyle.Width(m.width)
|
||||
listStyle = listStyle.Width(m.width)
|
||||
titleStyle = titleStyle.Width(m.width - 2)
|
||||
choiceStyle = choiceStyle.Width(m.width)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m.lists[m.focused], cmd = m.lists[m.focused].Update(msg)
|
||||
return m, cmd
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue