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

346 lines
7.6 KiB
Go
Raw Normal View History

2022-12-28 22:22:36 -05:00
package ui
import (
// "fmt"
"strings"
2023-01-06 19:46:41 -05:00
"github.com/mrusme/neonmodem/aggregator"
"github.com/mrusme/neonmodem/models/forum"
"github.com/mrusme/neonmodem/system"
"github.com/mrusme/neonmodem/ui/cmd"
"github.com/mrusme/neonmodem/ui/ctx"
"github.com/mrusme/neonmodem/ui/header"
"github.com/mrusme/neonmodem/ui/views/posts"
"github.com/mrusme/neonmodem/ui/views/splash"
"github.com/mrusme/neonmodem/ui/windowmanager"
"github.com/mrusme/neonmodem/ui/windows/msgerror"
"github.com/mrusme/neonmodem/ui/windows/popuplist"
"github.com/mrusme/neonmodem/ui/windows/postcreate"
"github.com/mrusme/neonmodem/ui/windows/postshow"
2022-12-28 22:22:36 -05:00
2023-01-06 19:46:41 -05:00
"github.com/mrusme/neonmodem/ui/views"
2022-12-28 22:22:36 -05:00
"github.com/charmbracelet/bubbles/key"
2023-01-04 21:43:11 -05:00
"github.com/charmbracelet/bubbles/list"
2023-01-02 15:29:29 -05:00
"github.com/charmbracelet/bubbles/spinner"
2022-12-28 22:22:36 -05:00
tea "github.com/charmbracelet/bubbletea"
)
type KeyMap struct {
2023-01-04 21:43:11 -05:00
SystemSelect key.Binding
ForumSelect key.Binding
Close key.Binding
2022-12-28 22:22:36 -05:00
}
var DefaultKeyMap = KeyMap{
2023-01-04 21:43:11 -05:00
SystemSelect: key.NewBinding(
key.WithKeys("ctrl+e"),
key.WithHelp("C-e", "System selector"),
2023-01-04 21:43:11 -05:00
),
ForumSelect: key.NewBinding(
key.WithKeys("ctrl+t"),
key.WithHelp("C-t", "Forum selector"),
2023-01-04 21:43:11 -05:00
),
Close: key.NewBinding(
key.WithKeys("q", "esc"),
key.WithHelp("q/esc", "close"),
),
2022-12-28 22:22:36 -05:00
}
type Model struct {
keymap KeyMap
header header.Model
views []views.View
currentView int
wm *windowmanager.WM
ctx *ctx.Ctx
2023-01-02 19:27:23 -05:00
2023-01-05 11:43:52 -05:00
a *aggregator.Aggregator
2023-01-02 19:27:23 -05:00
viewcache string
2023-01-03 13:19:21 -05:00
viewcacheID string
2023-01-02 19:27:23 -05:00
renderOnlyFocused bool
2022-12-28 22:22:36 -05:00
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
keymap: DefaultKeyMap,
currentView: 0,
2023-01-02 16:45:42 -05:00
wm: windowmanager.New(c),
ctx: c,
2023-01-02 19:27:23 -05:00
viewcache: "",
renderOnlyFocused: false,
2022-12-28 22:22:36 -05:00
}
m.header = header.NewModel(m.ctx)
2023-01-06 19:41:28 -05:00
m.views = append(m.views, splash.NewModel(m.ctx))
2022-12-31 19:06:39 -05:00
m.views = append(m.views, posts.NewModel(m.ctx))
2022-12-28 22:22:36 -05:00
2023-01-05 11:43:52 -05:00
m.a, _ = aggregator.New(m.ctx)
2022-12-28 22:22:36 -05:00
return m
}
func (m Model) Init() tea.Cmd {
return tea.Batch(
tea.EnterAltScreen,
)
2022-12-28 22:22:36 -05:00
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := make([]tea.Cmd, 0)
2023-01-03 13:19:21 -05:00
m.viewcacheID = m.wm.Focused()
2022-12-28 22:22:36 -05:00
switch msg := msg.(type) {
2023-01-02 14:24:07 -05:00
2022-12-28 22:22:36 -05:00
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.Close):
2023-01-02 16:45:42 -05:00
closed, ccmds := m.wm.CloseFocused()
if !closed {
return m, tea.Quit
}
2023-01-02 16:45:42 -05:00
return m, tea.Batch(ccmds...)
2023-01-04 21:43:11 -05:00
case key.Matches(msg, m.keymap.SystemSelect):
var listItems []list.Item
2023-01-04 23:59:59 -05:00
all, _ := system.New("all", nil, m.ctx.Logger)
all.SetID(-1)
listItems = append(listItems, all)
2023-01-04 21:43:11 -05:00
for _, sys := range m.ctx.Systems {
listItems = append(listItems, *sys)
}
ccmds := m.wm.Open(
popuplist.WIN_ID,
popuplist.NewModel(m.ctx),
[4]int{
int(m.ctx.Content[1] / 2),
int(m.ctx.Content[1] / 4),
int(m.ctx.Content[1] / 2),
int(m.ctx.Content[1] / 4),
},
cmd.New(
cmd.WinOpen,
popuplist.WIN_ID,
cmd.Arg{Name: "selectionID", Value: "system"},
cmd.Arg{Name: "items", Value: listItems},
),
)
2023-01-05 12:12:14 -05:00
2023-01-04 21:43:11 -05:00
return m, tea.Batch(ccmds...)
2023-01-05 11:43:52 -05:00
case key.Matches(msg, m.keymap.ForumSelect):
var listItems []list.Item
2023-01-05 12:49:08 -05:00
ccmds := make([]tea.Cmd, 0)
2023-01-05 11:43:52 -05:00
2023-01-05 12:02:12 -05:00
all := forum.Forum{ID: "", Name: "All", SysIDX: m.ctx.GetCurrentSystem()}
2023-01-05 11:43:52 -05:00
listItems = append(listItems, all)
forums, errs := m.a.ListForums()
2023-01-05 12:49:08 -05:00
for _, err := range errs {
if err != nil {
2023-01-05 21:44:34 -05:00
m.ctx.Logger.Error(err)
2023-01-05 12:49:08 -05:00
ccmds = append(ccmds, cmd.New(
cmd.MsgError,
"*",
cmd.Arg{Name: "errors", Value: errs},
).Tea())
}
2023-01-05 11:43:52 -05:00
}
2023-01-05 12:49:08 -05:00
2023-01-05 11:43:52 -05:00
for _, f := range forums {
listItems = append(listItems, f)
}
2023-01-05 12:49:08 -05:00
ccmds = m.wm.Open(
2023-01-05 11:43:52 -05:00
popuplist.WIN_ID,
popuplist.NewModel(m.ctx),
[4]int{
int(m.ctx.Content[1] / 2),
int(m.ctx.Content[1] / 4),
int(m.ctx.Content[1] / 2),
int(m.ctx.Content[1] / 4),
},
cmd.New(
cmd.WinOpen,
popuplist.WIN_ID,
cmd.Arg{Name: "selectionID", Value: "forum"},
cmd.Arg{Name: "items", Value: listItems},
),
)
2023-01-05 12:12:14 -05:00
2023-01-05 11:43:52 -05:00
return m, tea.Batch(ccmds...)
2023-01-02 16:45:42 -05:00
default:
if m.wm.GetNumberOpen() > 0 {
cmd := m.wm.Update(m.wm.Focused(), msg)
return m, cmd
}
2022-12-28 22:22:36 -05:00
}
case tea.WindowSizeMsg:
m.setSizes(msg.Width, msg.Height)
for i := range m.views {
v, cmd := m.views[i].Update(msg)
m.views[i] = v
cmds = append(cmds, cmd)
}
2023-01-02 16:45:42 -05:00
m.ctx.Logger.Debugf("resizing all: %v\n", m.ctx.Content)
ccmds := m.wm.ResizeAll(m.ctx.Content[0], m.ctx.Content[1])
cmds = append(cmds, ccmds...)
case cmd.Command:
var ccmds []tea.Cmd
switch msg.Call {
2023-01-02 20:34:39 -05:00
2023-01-06 19:41:28 -05:00
case cmd.ViewOpen:
m.ctx.Logger.Debug("got cmd.ViewOpen")
switch msg.Target {
case posts.VIEW_ID:
m.currentView = 1
m.viewcache = m.buildView(false)
ccmds = append(ccmds,
cmd.New(cmd.ViewFocus, "*").Tea(),
cmd.New(cmd.ViewRefreshData, "*").Tea(),
)
return m, tea.Batch(ccmds...)
}
case cmd.WinOpen:
2023-01-02 18:46:26 -05:00
switch msg.Target {
case postshow.WIN_ID:
ccmds = m.wm.Open(
msg.Target,
postshow.NewModel(m.ctx),
2023-01-03 13:28:48 -05:00
[4]int{
3,
1,
6,
4,
},
2023-01-02 18:46:26 -05:00
&msg,
)
case postcreate.WIN_ID:
ccmds = m.wm.Open(
msg.Target,
postcreate.NewModel(m.ctx),
2023-01-03 13:28:48 -05:00
[4]int{
6,
m.ctx.Content[1] - 16,
10,
4,
},
2023-01-02 18:46:26 -05:00
&msg,
)
2023-01-03 13:19:21 -05:00
m.viewcache = m.buildView(false)
2023-01-02 18:46:26 -05:00
}
2023-01-02 20:34:39 -05:00
2023-01-02 19:27:23 -05:00
case cmd.WinClose:
switch msg.Target {
2023-01-05 12:12:14 -05:00
2023-01-02 19:27:23 -05:00
case postcreate.WIN_ID:
2023-01-05 21:44:34 -05:00
// TODO: Anything?
2023-01-05 12:12:14 -05:00
2023-01-04 21:43:11 -05:00
case popuplist.WIN_ID:
2023-01-04 23:59:59 -05:00
selectionIDIf := msg.GetArg("selectionID")
if selectionIDIf == nil {
return m, nil
}
switch selectionIDIf.(string) {
2023-01-04 21:43:11 -05:00
case "system":
selected := msg.GetArg("selected").(system.System)
m.ctx.SetCurrentSystem(selected.GetID())
2023-01-05 11:43:52 -05:00
m.ctx.SetCurrentForum(forum.Forum{})
2023-01-04 21:43:11 -05:00
case "forum":
selected := msg.GetArg("selected").(forum.Forum)
2023-01-05 11:43:52 -05:00
m.ctx.SetCurrentSystem(selected.SysIDX)
m.ctx.SetCurrentForum(selected)
2023-01-04 21:43:11 -05:00
}
2023-01-05 12:12:14 -05:00
return m, cmd.New(cmd.ViewRefreshData, "*").Tea()
2023-01-02 19:27:23 -05:00
}
2023-01-02 20:34:39 -05:00
case cmd.WMCloseWin:
2023-01-04 21:43:11 -05:00
if ok, clcmds := m.wm.Close(msg.Target, msg.GetArgs()...); ok {
2023-01-02 20:34:39 -05:00
cmds = append(cmds, clcmds...)
}
2023-01-03 00:32:48 -05:00
case cmd.MsgError:
ccmds = m.wm.Open(
msgerror.WIN_ID,
msgerror.NewModel(m.ctx),
2023-01-03 13:28:48 -05:00
[4]int{
int(m.ctx.Content[1] / 2),
int(m.ctx.Content[1] / 4),
int(m.ctx.Content[1] / 2),
int(m.ctx.Content[1] / 4),
},
2023-01-03 00:32:48 -05:00
&msg,
)
default:
2023-01-03 13:19:21 -05:00
m.ctx.Logger.Debugf("updating all with cmd: %v\n", msg)
ccmds = m.wm.UpdateAll(msg)
}
cmds = append(cmds, ccmds...)
2023-01-02 14:24:07 -05:00
2023-01-02 15:29:29 -05:00
case spinner.TickMsg:
// Do nothing
2023-01-02 14:24:07 -05:00
default:
2023-01-02 18:46:26 -05:00
m.ctx.Logger.Debugf("updating focused with default: %v\n", msg)
cmds = append(cmds, m.wm.UpdateFocused(msg)...)
2022-12-28 22:22:36 -05:00
}
v, vcmd := m.views[m.currentView].Update(msg)
m.views[m.currentView] = v
cmds = append(cmds, vcmd)
2022-12-28 22:22:36 -05:00
header, hcmd := m.header.Update(msg)
m.header = header
cmds = append(cmds, hcmd)
2022-12-28 22:22:36 -05:00
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
2023-01-02 19:27:23 -05:00
return m.buildView(true)
}
func (m Model) buildView(cached bool) string {
2022-12-28 22:22:36 -05:00
s := strings.Builder{}
2023-01-02 19:27:23 -05:00
var tmp string = ""
2023-01-03 13:19:21 -05:00
m.ctx.Logger.Debugf("viewcacheID: %s\n", m.viewcacheID)
if cached && m.viewcache != "" && m.viewcacheID == m.wm.Focused() &&
m.viewcacheID == postcreate.WIN_ID {
m.ctx.Logger.Debug("hitting UI viewcache")
2023-01-02 19:27:23 -05:00
tmp = m.viewcache
2023-01-03 13:19:21 -05:00
m.renderOnlyFocused = true
2023-01-02 19:27:23 -05:00
} else {
2023-01-03 13:19:21 -05:00
m.ctx.Logger.Debug("generating UI viewcache")
m.renderOnlyFocused = false
2023-01-06 19:41:28 -05:00
if m.currentView > 0 {
s.WriteString(m.header.View() + "\n")
}
2023-01-02 19:27:23 -05:00
s.WriteString(m.views[m.currentView].View())
tmp = s.String()
}
2023-01-02 19:27:23 -05:00
return m.wm.View(tmp, m.renderOnlyFocused)
2022-12-28 22:22:36 -05:00
}
func (m Model) setSizes(winWidth int, winHeight int) {
(*m.ctx).Screen[0] = winWidth
(*m.ctx).Screen[1] = winHeight
m.ctx.Content[0] = m.ctx.Screen[0]
m.ctx.Content[1] = m.ctx.Screen[1] - 8
2022-12-28 22:22:36 -05:00
}