mirror of
https://github.com/mrusme/neonmodem.git
synced 2024-12-04 14:46:37 -05:00
Refactored MsgError to use ToolKit
This commit is contained in:
parent
c69287482e
commit
623def1263
@ -22,6 +22,7 @@ type MsgHandling struct {
|
||||
OnWinCloseCmd 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)
|
||||
OnMsgErrorCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
|
||||
}
|
||||
|
||||
func (tk *ToolKit) SetMsgHandling(mh MsgHandling) {
|
||||
@ -94,6 +95,10 @@ func (tk *ToolKit) HandleMsg(m interface{}, msg tea.Msg) (bool, []tea.Cmd) {
|
||||
if tk.mh.OnWinFreshDataCmd != nil {
|
||||
return tk.mh.OnWinFreshDataCmd(m, msg)
|
||||
}
|
||||
case cmd.MsgError:
|
||||
if tk.mh.OnMsgErrorCmd != nil {
|
||||
return tk.mh.OnMsgErrorCmd(m, msg)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
48
ui/windows/msgerror/handlers.go
Normal file
48
ui/windows/msgerror/handlers.go
Normal file
@ -0,0 +1,48 @@
|
||||
package msgerror
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/mrusme/gobbs/ui/cmd"
|
||||
)
|
||||
|
||||
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())
|
||||
viewportWidth := m.tk.ViewWidth() - 2
|
||||
viewportHeight := m.tk.ViewHeight() - 5
|
||||
|
||||
viewportStyle.Width(viewportWidth)
|
||||
viewportStyle.Height(viewportHeight)
|
||||
m.viewport = viewport.New(viewportWidth-4, viewportHeight-4)
|
||||
m.viewport.Width = viewportWidth - 4
|
||||
m.viewport.Height = viewportHeight + 1
|
||||
|
||||
return false, cmds
|
||||
}
|
||||
|
||||
func handleMsgErrorCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
|
||||
var m *Model = mi.(*Model)
|
||||
var cmds []tea.Cmd
|
||||
|
||||
m.err = c.GetArg("error").(error)
|
||||
if m.err != nil {
|
||||
m.viewport.SetContent(m.err.Error())
|
||||
m.tk.CacheView(m)
|
||||
}
|
||||
return true, cmds
|
||||
}
|
||||
|
||||
func handleWinCloseCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
|
||||
var m *Model = mi.(*Model)
|
||||
var cmds []tea.Cmd
|
||||
|
||||
if c.Target == WIN_ID {
|
||||
m.err = nil
|
||||
return true, cmds
|
||||
}
|
||||
|
||||
return false, cmds
|
||||
}
|
@ -1,14 +1,12 @@
|
||||
package msgerror
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/mrusme/gobbs/aggregator"
|
||||
"github.com/mrusme/gobbs/ui/cmd"
|
||||
"github.com/mrusme/gobbs/ui/ctx"
|
||||
"github.com/mrusme/gobbs/ui/toolkit"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -23,17 +21,11 @@ var (
|
||||
BorderBottom(false)
|
||||
)
|
||||
|
||||
type KeyMap struct {
|
||||
}
|
||||
|
||||
var DefaultKeyMap = KeyMap{}
|
||||
|
||||
type Model struct {
|
||||
ctx *ctx.Ctx
|
||||
keymap KeyMap
|
||||
wh [2]int
|
||||
focused bool
|
||||
xywh [4]int
|
||||
ctx *ctx.Ctx
|
||||
tk *toolkit.ToolKit
|
||||
xywh [4]int
|
||||
|
||||
viewport viewport.Model
|
||||
|
||||
a *aggregator.Aggregator
|
||||
@ -48,20 +40,15 @@ func (m Model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Model) Focus() {
|
||||
m.focused = true
|
||||
}
|
||||
|
||||
func (m Model) Blur() {
|
||||
m.focused = false
|
||||
}
|
||||
|
||||
func NewModel(c *ctx.Ctx) Model {
|
||||
m := Model{
|
||||
ctx: c,
|
||||
keymap: DefaultKeyMap,
|
||||
focused: false,
|
||||
xywh: [4]int{0, 0, 0, 0},
|
||||
ctx: c,
|
||||
tk: toolkit.New(
|
||||
WIN_ID,
|
||||
c.Theme,
|
||||
c.Logger,
|
||||
),
|
||||
xywh: [4]int{0, 0, 0, 0},
|
||||
|
||||
err: nil,
|
||||
|
||||
@ -70,6 +57,12 @@ func NewModel(c *ctx.Ctx) Model {
|
||||
}
|
||||
|
||||
m.a, _ = aggregator.New(m.ctx)
|
||||
m.tk.SetViewFunc(buildView)
|
||||
m.tk.SetMsgHandling(toolkit.MsgHandling{
|
||||
OnViewResize: handleViewResize,
|
||||
OnMsgErrorCmd: handleMsgErrorCmd,
|
||||
OnWinCloseCmd: handleWinCloseCmd,
|
||||
})
|
||||
|
||||
return m
|
||||
}
|
||||
@ -77,55 +70,9 @@ func NewModel(c *ctx.Ctx) Model {
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmds []tea.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case tea.WindowSizeMsg:
|
||||
m.wh[0] = msg.Width
|
||||
m.wh[1] = msg.Height
|
||||
m.ctx.Logger.Debugf("received WindowSizeMsg: %v\n", m.wh)
|
||||
viewportWidth := m.wh[0] - 2
|
||||
viewportHeight := m.wh[1] - 5
|
||||
|
||||
viewportStyle.Width(viewportWidth)
|
||||
viewportStyle.Height(viewportHeight)
|
||||
m.viewport = viewport.New(viewportWidth-4, viewportHeight-4)
|
||||
m.viewport.Width = viewportWidth - 4
|
||||
m.viewport.Height = viewportHeight + 1
|
||||
|
||||
case cmd.Command:
|
||||
m.ctx.Logger.Debugf("got command: %v\n", msg)
|
||||
switch msg.Call {
|
||||
case cmd.MsgError:
|
||||
m.err = msg.GetArg("error").(error)
|
||||
if m.err != nil {
|
||||
m.viewport.SetContent(m.err.Error())
|
||||
m.viewcache = m.buildView(false)
|
||||
}
|
||||
return m, nil
|
||||
case cmd.WinClose:
|
||||
if msg.Target == WIN_ID {
|
||||
m.err = nil
|
||||
return m, nil
|
||||
}
|
||||
case cmd.WinFocus:
|
||||
if msg.Target == WIN_ID ||
|
||||
msg.Target == "*" {
|
||||
m.focused = true
|
||||
m.viewcache = m.buildView(false)
|
||||
}
|
||||
return m, nil
|
||||
case cmd.WinBlur:
|
||||
if msg.Target == WIN_ID ||
|
||||
msg.Target == "*" {
|
||||
m.focused = false
|
||||
}
|
||||
return m, nil
|
||||
default:
|
||||
m.ctx.Logger.Debugf("received unhandled command: %v\n", msg)
|
||||
}
|
||||
|
||||
// default:
|
||||
// m.ctx.Logger.Debugf("received unhandled msg: %v\n", msg)
|
||||
ret, cmds := m.tk.HandleMsg(&m, msg)
|
||||
if ret {
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
var vcmd tea.Cmd
|
||||
@ -139,45 +86,3 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
return m.buildView(true)
|
||||
}
|
||||
|
||||
func (m Model) buildView(cached bool) string {
|
||||
var view strings.Builder = strings.Builder{}
|
||||
|
||||
if cached && m.focused == false && m.viewcache != "" {
|
||||
m.ctx.Logger.Debugln("Cached View()")
|
||||
|
||||
return m.viewcache
|
||||
}
|
||||
|
||||
title := "Error"
|
||||
titlebar := m.ctx.Theme.DialogBox.Titlebar.Focused.
|
||||
Align(lipgloss.Center).
|
||||
Width(m.wh[0]).
|
||||
Render(title)
|
||||
|
||||
bottombar := m.ctx.Theme.DialogBox.Bottombar.
|
||||
Width(m.wh[0]).
|
||||
Render("esc close")
|
||||
|
||||
ui := lipgloss.JoinVertical(
|
||||
lipgloss.Center,
|
||||
titlebar,
|
||||
viewportStyle.Render(m.viewport.View()),
|
||||
bottombar,
|
||||
)
|
||||
|
||||
var tmp string
|
||||
if m.focused {
|
||||
tmp = m.ctx.Theme.DialogBox.Window.Focused.Render(ui)
|
||||
} else {
|
||||
tmp = m.ctx.Theme.DialogBox.Window.Blurred.Render(ui)
|
||||
}
|
||||
|
||||
view.WriteString(tmp)
|
||||
|
||||
return view.String()
|
||||
}
|
||||
|
21
ui/windows/msgerror/view.go
Normal file
21
ui/windows/msgerror/view.go
Normal file
@ -0,0 +1,21 @@
|
||||
package msgerror
|
||||
|
||||
func (m Model) View() string {
|
||||
return m.tk.View(&m, true)
|
||||
}
|
||||
|
||||
func buildView(mi interface{}, cached bool) string {
|
||||
var m *Model = mi.(*Model)
|
||||
if cached && !m.tk.IsFocused() && m.tk.IsCached() {
|
||||
m.ctx.Logger.Debugln("Cached View()")
|
||||
|
||||
return m.tk.GetCachedView()
|
||||
}
|
||||
m.ctx.Logger.Debugln("View()")
|
||||
m.ctx.Logger.Debugf("IsFocused: %v\n", m.tk.IsFocused())
|
||||
|
||||
return m.tk.Dialog(
|
||||
"Post",
|
||||
viewportStyle.Render(m.viewport.View()),
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user