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

Refactored event/msg handling

This commit is contained in:
マリウス 2023-01-03 16:27:01 -05:00
parent 3587cb9ccc
commit 740c69167e
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
3 changed files with 230 additions and 136 deletions

96
ui/toolkit/msg.go Normal file
View File

@ -0,0 +1,96 @@
package toolkit
import (
"strconv"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/ui/cmd"
)
type MsgHandlingKeymapKey struct {
ID string
Handler func(m interface{}) (bool, []tea.Cmd)
}
type MsgHandling struct {
OnKeymapKey []MsgHandlingKeymapKey
OnAnyNumberKey func(m interface{}, n int8) (bool, []tea.Cmd)
OnAnyUncaughtKey func(m interface{}, k tea.KeyMsg) (bool, []tea.Cmd)
OnViewResize func(m interface{}) (bool, []tea.Cmd)
OnWinOpenCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
OnWinRefreshDataCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
OnWinFreshDataCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
}
func (tk *ToolKit) SetMsgHandling(mh MsgHandling) {
tk.mh = mh
}
func (tk *ToolKit) HandleMsg(m interface{}, msg tea.Msg) (bool, []tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
for i := 0; i < len(tk.mh.OnKeymapKey); i++ {
if key.Matches(msg, tk.KeymapGet(tk.mh.OnKeymapKey[i].ID)) {
return tk.mh.OnKeymapKey[i].Handler(m)
}
}
if tk.mh.OnAnyNumberKey != nil {
switch msg.String() {
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
n, _ := strconv.Atoi(msg.String())
return tk.mh.OnAnyNumberKey(m, int8(n))
}
}
if tk.mh.OnAnyUncaughtKey != nil {
return tk.mh.OnAnyUncaughtKey(m, msg)
}
case tea.WindowSizeMsg:
tk.wh[0] = msg.Width
tk.wh[1] = msg.Height
if tk.mh.OnViewResize != nil {
return tk.mh.OnViewResize(m)
}
return false, cmds
case cmd.Command:
tk.logger.Debugf("got command: %v\n", msg)
switch msg.Call {
case cmd.WinFocus:
if msg.Target == tk.winID ||
msg.Target == "*" {
tk.logger.Debug("got WinFocus")
tk.Focus(m)
}
tk.logger.Debugf("focused: %v", tk.focused)
return true, nil
case cmd.WinBlur:
if msg.Target == tk.winID ||
msg.Target == "*" {
tk.logger.Debug("got WinBlur")
tk.Blur(m)
}
tk.logger.Debugf("focused: %v", tk.focused)
return true, nil
case cmd.WinOpen:
if tk.mh.OnWinOpenCmd != nil {
return tk.mh.OnWinOpenCmd(m, msg)
}
case cmd.WinRefreshData:
if tk.mh.OnWinRefreshDataCmd != nil {
return tk.mh.OnWinRefreshDataCmd(m, msg)
}
case cmd.WinFreshData:
if tk.mh.OnWinFreshDataCmd != nil {
return tk.mh.OnWinFreshDataCmd(m, msg)
}
}
}
return false, cmds
}

View File

@ -2,8 +2,6 @@ package toolkit
import ( import (
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/ui/cmd"
"github.com/mrusme/gobbs/ui/theme" "github.com/mrusme/gobbs/ui/theme"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -15,6 +13,8 @@ type ToolKit struct {
theme *theme.Theme theme *theme.Theme
logger *zap.SugaredLogger logger *zap.SugaredLogger
mh MsgHandling
m interface{} m interface{}
wh [2]int wh [2]int
focused bool focused bool
@ -31,6 +31,8 @@ func New(winID string, t *theme.Theme, l *zap.SugaredLogger) *ToolKit {
tk.theme = t tk.theme = t
tk.logger = l tk.logger = l
tk.mh = MsgHandling{}
tk.wh = [2]int{0, 0} tk.wh = [2]int{0, 0}
tk.focused = false tk.focused = false
@ -90,37 +92,3 @@ func (tk *ToolKit) ViewWidth() int {
func (tk *ToolKit) ViewHeight() int { func (tk *ToolKit) ViewHeight() int {
return tk.wh[1] return tk.wh[1]
} }
func (tk *ToolKit) HandleMsg(m interface{}, msg tea.Msg) (bool, []tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
tk.wh[0] = msg.Width
tk.wh[1] = msg.Height
return false, cmds
case cmd.Command:
tk.logger.Debugf("got command: %v\n", msg)
switch msg.Call {
case cmd.WinFocus:
if msg.Target == tk.winID ||
msg.Target == "*" {
tk.logger.Debug("got WinFocus")
tk.Focus(m)
}
tk.logger.Debugf("focused: %v", tk.focused)
return true, nil
case cmd.WinBlur:
if msg.Target == tk.winID ||
msg.Target == "*" {
tk.logger.Debug("got WinBlur")
tk.Blur(m)
}
tk.logger.Debugf("focused: %v", tk.focused)
return true, nil
}
}
return false, cmds
}

View File

@ -5,7 +5,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour" "github.com/charmbracelet/glamour"
@ -69,27 +68,32 @@ func NewModel(c *ctx.Ctx) Model {
m.tk.KeymapAdd("reply", "reply", "r") m.tk.KeymapAdd("reply", "reply", "r")
m.tk.SetMsgHandling(toolkit.MsgHandling{
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
{
ID: "reply",
Handler: handleReply,
},
},
OnAnyNumberKey: handleNumberKeys,
OnAnyUncaughtKey: handleUncaughtKeys,
OnViewResize: handleViewResize,
OnWinOpenCmd: handleWinOpenCmd,
OnWinRefreshDataCmd: handleWinOpenCmd,
OnWinFreshDataCmd: handleWinFreshDataCmd,
})
return m return m
} }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func handleReply(mi interface{}) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd var cmds []tea.Cmd
ret, cmds := m.tk.HandleMsg(&m, msg)
if ret {
return m, tea.Batch(cmds...)
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.tk.KeymapGet("reply")):
var replyToIdx int = 0 var replyToIdx int = 0
var err error var err error
// m.viewcache = m.buildView(false) // m.viewcache = m.buildView(false)
m.tk.CacheView(&m) m.tk.CacheView(m)
if m.buffer != "" { if m.buffer != "" {
replyToIdx, err = strconv.Atoi(m.buffer) replyToIdx, err = strconv.Atoi(m.buffer)
@ -125,19 +129,31 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx.Logger.Debugf("buffer: %s", m.buffer) m.ctx.Logger.Debugf("buffer: %s", m.buffer)
// m.viewcache = m.buildView(false) // m.viewcache = m.buildView(false)
return m, tea.Batch(cmds...) return true, cmds
}
func handleNumberKeys(mi interface{}, n int8) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
m.buffer += strconv.Itoa(int(n))
return false, cmds
}
func handleUncaughtKeys(mi interface{}, k tea.KeyMsg) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
default:
switch msg.String() {
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
m.buffer += msg.String()
return m, nil
default:
m.buffer = "" m.buffer = ""
}
return false, cmds
} }
case tea.WindowSizeMsg: func handleViewResize(mi interface{}) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
m.ctx.Logger.Debugf("received WindowSizeMsg: %vx%v\n", m.tk.ViewWidth(), m.tk.ViewHeight()) m.ctx.Logger.Debugf("received WindowSizeMsg: %vx%v\n", m.tk.ViewWidth(), m.tk.ViewHeight())
viewportWidth := m.tk.ViewWidth() - 2 viewportWidth := m.tk.ViewWidth() - 2
viewportHeight := m.tk.ViewHeight() - 5 viewportHeight := m.tk.ViewHeight() - 5
@ -149,34 +165,48 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Height = viewportHeight + 1 m.viewport.Height = viewportHeight + 1
// cmds = append(cmds, viewport.Sync(m.viewport)) // cmds = append(cmds, viewport.Sync(m.viewport))
case cmd.Command: return false, cmds
m.ctx.Logger.Debugf("got command: %v\n", msg) }
switch msg.Call {
case cmd.WinOpen, cmd.WinRefreshData: func handleWinOpenCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
if msg.Target == WIN_ID { var m *Model = mi.(*Model)
var cmds []tea.Cmd
if c.Target == WIN_ID {
m.ctx.Logger.Debug("got own WinOpen command") m.ctx.Logger.Debug("got own WinOpen command")
m.activePost = msg.GetArg("post").(*post.Post) m.activePost = c.GetArg("post").(*post.Post)
m.viewport.SetContent(m.renderViewport(m.activePost)) m.viewport.SetContent(m.renderViewport(m.activePost))
m.ctx.Logger.Debugf("loading post: %v", m.activePost.ID) m.ctx.Logger.Debugf("loading post: %v", m.activePost.ID)
m.ctx.Loading = true m.ctx.Loading = true
return m, m.loadPost(m.activePost) cmds = append(cmds, m.loadPost(m.activePost))
} return true, cmds
return m, nil
case cmd.WinFreshData:
if msg.Target == WIN_ID ||
msg.Target == "*" {
m.ctx.Logger.Debug("got *post.Post")
m.activePost = msg.GetArg("post").(*post.Post)
m.viewport.SetContent(m.renderViewport(m.activePost))
m.ctx.Loading = false
return m, nil
}
default:
m.ctx.Logger.Debugf("received unhandled command: %v\n", msg)
} }
default: return false, cmds
m.ctx.Logger.Debugf("received unhandled msg: %v\n", msg) }
func handleWinFreshDataCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
if c.Target == WIN_ID ||
c.Target == "*" {
m.ctx.Logger.Debug("got *post.Post")
m.activePost = c.GetArg("post").(*post.Post)
m.viewport.SetContent(m.renderViewport(m.activePost))
m.ctx.Loading = false
return true, cmds
}
return false, cmds
}
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 var cmd tea.Cmd