1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00
neonmodem/ui/windows/popuplist/popuplist.go

85 lines
1.8 KiB
Go
Raw Normal View History

2023-01-04 21:40:54 -05:00
package popuplist
import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
2023-01-06 19:46:41 -05:00
"github.com/mrusme/neonmodem/aggregator"
"github.com/mrusme/neonmodem/ui/ctx"
"github.com/mrusme/neonmodem/ui/toolkit"
2023-01-04 21:40:54 -05:00
)
var (
WIN_ID = "popuplist"
)
type Model struct {
ctx *ctx.Ctx
tk *toolkit.ToolKit
selectionID string
list list.Model
items []list.Item
a *aggregator.Aggregator
}
func (m Model) Init() tea.Cmd {
return nil
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
ctx: c,
tk: toolkit.New(
WIN_ID,
c.Theme,
c.Logger,
),
}
listDelegate := list.NewDefaultDelegate()
listDelegate.Styles.NormalTitle = m.ctx.Theme.PopupList.Item.Focused
listDelegate.Styles.DimmedTitle = m.ctx.Theme.PopupList.Item.Blurred
listDelegate.Styles.SelectedTitle = m.ctx.Theme.PopupList.Item.Selected
listDelegate.Styles.NormalDesc = m.ctx.Theme.PopupList.ItemDetail.Focused
listDelegate.Styles.DimmedDesc = m.ctx.Theme.PopupList.ItemDetail.Blurred
listDelegate.Styles.SelectedDesc = m.ctx.Theme.PopupList.ItemDetail.Selected
m.list = list.New(m.items, listDelegate, 0, 0)
m.list.SetShowTitle(false)
m.list.SetShowStatusBar(false)
m.tk.KeymapAdd("enter", "choose selection", "enter")
m.a, _ = aggregator.New(m.ctx)
m.tk.SetViewFunc(buildView)
m.tk.SetMsgHandling(toolkit.MsgHandling{
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
{
ID: "enter",
Handler: handleSelect,
},
},
OnViewResize: handleViewResize,
OnWinOpenCmd: handleWinOpenCmd,
})
return m
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
ret, cmds := m.tk.HandleMsg(&m, msg)
if ret {
return m, tea.Batch(cmds...)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}