1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-16 06:25:23 +00:00
neonmodem/ui/ui.go

217 lines
4.6 KiB
Go
Raw Normal View History

2022-12-29 03:22:36 +00:00
package ui
import (
// "fmt"
"strings"
"github.com/mrusme/gobbs/ui/cmd"
2022-12-29 03:22:36 +00:00
"github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/header"
2022-12-29 03:27:46 +00:00
"github.com/mrusme/gobbs/ui/views/posts"
"github.com/mrusme/gobbs/ui/windowmanager"
2023-01-03 05:32:48 +00:00
"github.com/mrusme/gobbs/ui/windows/msgerror"
2023-01-02 23:46:26 +00:00
"github.com/mrusme/gobbs/ui/windows/postcreate"
2023-01-02 20:33:12 +00:00
"github.com/mrusme/gobbs/ui/windows/postshow"
2022-12-29 03:22:36 +00:00
"github.com/mrusme/gobbs/ui/views"
"github.com/charmbracelet/bubbles/key"
2023-01-02 20:29:29 +00:00
"github.com/charmbracelet/bubbles/spinner"
2022-12-29 03:22:36 +00:00
tea "github.com/charmbracelet/bubbletea"
)
type KeyMap struct {
Up key.Binding
Down key.Binding
Close key.Binding
2022-12-29 03:22:36 +00:00
}
var DefaultKeyMap = KeyMap{
// Up: key.NewBinding(
// key.WithKeys("k", "up"),
// key.WithHelp("↑/k", "move up"),
// ),
// Down: key.NewBinding(
// key.WithKeys("j", "down"),
// key.WithHelp("↓/j", "move down"),
// ),
Close: key.NewBinding(
key.WithKeys("q", "esc"),
key.WithHelp("q/esc", "close"),
),
2022-12-29 03:22:36 +00:00
}
type Model struct {
keymap KeyMap
header header.Model
views []views.View
currentView int
wm *windowmanager.WM
ctx *ctx.Ctx
2023-01-03 00:27:23 +00:00
viewcache string
renderOnlyFocused bool
2022-12-29 03:22:36 +00:00
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
keymap: DefaultKeyMap,
currentView: 0,
2023-01-02 21:45:42 +00:00
wm: windowmanager.New(c),
ctx: c,
2023-01-03 00:27:23 +00:00
viewcache: "",
renderOnlyFocused: false,
2022-12-29 03:22:36 +00:00
}
m.header = header.NewModel(m.ctx)
2023-01-01 00:06:39 +00:00
m.views = append(m.views, posts.NewModel(m.ctx))
2022-12-29 03:22:36 +00:00
return m
}
func (m Model) Init() tea.Cmd {
return tea.Batch(
tea.EnterAltScreen,
cmd.New(cmd.ViewFocus, "*").Tea(),
cmd.New(cmd.ViewRefreshData, "*").Tea(),
)
2022-12-29 03:22:36 +00:00
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := make([]tea.Cmd, 0)
switch msg := msg.(type) {
2023-01-02 19:24:07 +00:00
2022-12-29 03:22:36 +00:00
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.Close):
m.ctx.Logger.Debug("close received")
2023-01-02 21:45:42 +00:00
closed, ccmds := m.wm.CloseFocused()
if !closed {
m.ctx.Logger.Debug("CloseFocused() was false, quitting")
return m, tea.Quit
}
2023-01-02 21:45:42 +00:00
return m, tea.Batch(ccmds...)
default:
if m.wm.GetNumberOpen() > 0 {
cmd := m.wm.Update(m.wm.Focused(), msg)
return m, cmd
}
2022-12-29 03:22:36 +00: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 21:45:42 +00: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-03 01:34:39 +00:00
case cmd.WinOpen:
2023-01-02 23:46:26 +00:00
switch msg.Target {
case postshow.WIN_ID:
m.ctx.Logger.Debugln("received WinOpen")
ccmds = m.wm.Open(
msg.Target,
postshow.NewModel(m.ctx),
[4]int{3, 1, 4, 4},
&msg,
)
case postcreate.WIN_ID:
m.ctx.Logger.Debugln("received WinOpen")
2023-01-03 00:27:23 +00:00
m.viewcache = m.buildView(false)
m.renderOnlyFocused = true
2023-01-02 23:46:26 +00:00
ccmds = m.wm.Open(
msg.Target,
postcreate.NewModel(m.ctx),
[4]int{6, int(m.ctx.Content[1] / 3), 8, 4},
&msg,
)
}
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)
2023-01-03 01:34:39 +00:00
2023-01-03 00:27:23 +00:00
case cmd.WinClose:
switch msg.Target {
case postcreate.WIN_ID:
m.ctx.Logger.Debugln("received WinClose")
m.renderOnlyFocused = false
}
2023-01-03 01:34:39 +00:00
case cmd.WMCloseWin:
if ok, clcmds := m.wm.Close(msg.Target); ok {
cmds = append(cmds, clcmds...)
}
2023-01-03 05:32:48 +00:00
case cmd.MsgError:
m.ctx.Logger.Debugln("received MsgError")
ccmds = m.wm.Open(
msgerror.WIN_ID,
msgerror.NewModel(m.ctx),
[4]int{9, int(m.ctx.Content[1] / 3), 12, int(m.ctx.Content[1] / 3)},
&msg,
)
default:
2023-01-02 20:29:29 +00:00
if msg.Call < cmd.ViewFocus {
m.ctx.Logger.Debugf("updating all with cmd: %v\n", msg)
ccmds = m.wm.UpdateAll(msg)
}
}
cmds = append(cmds, ccmds...)
2023-01-02 19:24:07 +00:00
2023-01-02 20:29:29 +00:00
case spinner.TickMsg:
// Do nothing
2023-01-02 19:24:07 +00:00
default:
2023-01-02 23:46:26 +00:00
m.ctx.Logger.Debugf("updating focused with default: %v\n", msg)
cmds = append(cmds, m.wm.UpdateFocused(msg)...)
2022-12-29 03:22:36 +00:00
}
v, vcmd := m.views[m.currentView].Update(msg)
m.views[m.currentView] = v
cmds = append(cmds, vcmd)
2022-12-29 03:22:36 +00:00
header, hcmd := m.header.Update(msg)
m.header = header
cmds = append(cmds, hcmd)
2022-12-29 03:22:36 +00:00
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
2023-01-03 00:27:23 +00:00
return m.buildView(true)
}
func (m Model) buildView(cached bool) string {
2022-12-29 03:22:36 +00:00
s := strings.Builder{}
2023-01-03 00:27:23 +00:00
var tmp string = ""
if m.viewcache != "" && m.renderOnlyFocused {
tmp = m.viewcache
} else {
s.WriteString(m.header.View() + "\n")
s.WriteString(m.views[m.currentView].View())
tmp = s.String()
}
2023-01-03 00:27:23 +00:00
return m.wm.View(tmp, m.renderOnlyFocused)
2022-12-29 03:22:36 +00: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] - 5
}