2023-01-03 14:53:32 -05:00
|
|
|
package toolkit
|
|
|
|
|
|
|
|
import (
|
2023-01-03 15:16:34 -05:00
|
|
|
"github.com/charmbracelet/bubbles/key"
|
2023-01-06 19:46:41 -05:00
|
|
|
"github.com/mrusme/neonmodem/ui/theme"
|
2023-01-03 14:53:32 -05:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ViewFunc func(m interface{}, cached bool) string
|
|
|
|
|
|
|
|
type ToolKit struct {
|
|
|
|
winID string
|
2023-01-03 15:02:43 -05:00
|
|
|
theme *theme.Theme
|
2023-01-03 14:53:32 -05:00
|
|
|
logger *zap.SugaredLogger
|
|
|
|
|
2023-01-03 16:27:01 -05:00
|
|
|
mh MsgHandling
|
|
|
|
|
2023-01-03 14:53:32 -05:00
|
|
|
m interface{}
|
|
|
|
wh [2]int
|
|
|
|
focused bool
|
|
|
|
|
2023-01-03 15:16:34 -05:00
|
|
|
keybindings map[string]key.Binding
|
|
|
|
|
2023-01-03 14:53:32 -05:00
|
|
|
viewfunc ViewFunc
|
|
|
|
viewcache string
|
|
|
|
}
|
|
|
|
|
2023-01-03 15:02:43 -05:00
|
|
|
func New(winID string, t *theme.Theme, l *zap.SugaredLogger) *ToolKit {
|
2023-01-03 14:53:32 -05:00
|
|
|
tk := new(ToolKit)
|
|
|
|
tk.winID = winID
|
2023-01-03 15:02:43 -05:00
|
|
|
tk.theme = t
|
2023-01-03 14:53:32 -05:00
|
|
|
tk.logger = l
|
|
|
|
|
2023-01-03 16:27:01 -05:00
|
|
|
tk.mh = MsgHandling{}
|
|
|
|
|
2023-01-03 14:53:32 -05:00
|
|
|
tk.wh = [2]int{0, 0}
|
|
|
|
tk.focused = false
|
|
|
|
|
2023-01-03 15:16:34 -05:00
|
|
|
tk.keybindings = make(map[string]key.Binding)
|
|
|
|
|
2023-01-03 14:53:32 -05:00
|
|
|
return tk
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) SetViewFunc(fn ViewFunc) {
|
|
|
|
tk.viewfunc = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) CacheView(m interface{}) bool {
|
|
|
|
if tk.viewfunc != nil {
|
|
|
|
tk.viewcache = tk.viewfunc(m, false)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) GetCachedView() string {
|
|
|
|
return tk.viewcache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) IsCached() bool {
|
|
|
|
return tk.viewcache != ""
|
|
|
|
}
|
|
|
|
|
2023-01-04 15:28:25 -05:00
|
|
|
func (tk *ToolKit) DefaultCaching(cached bool) string {
|
|
|
|
if cached && !tk.IsFocused() && tk.IsCached() {
|
|
|
|
return tk.GetCachedView()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:53:32 -05:00
|
|
|
func (tk *ToolKit) View(m interface{}, cached bool) string {
|
|
|
|
return tk.viewfunc(m, cached)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) Focus(m interface{}) {
|
|
|
|
tk.focused = true
|
|
|
|
|
|
|
|
if tk.viewfunc != nil {
|
|
|
|
tk.viewcache = tk.viewfunc(m, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) Blur(m interface{}) {
|
|
|
|
tk.focused = false
|
|
|
|
|
|
|
|
if tk.viewfunc != nil {
|
|
|
|
tk.viewcache = tk.viewfunc(m, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) IsFocused() bool {
|
|
|
|
return tk.focused
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) ViewWidth() int {
|
|
|
|
return tk.wh[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *ToolKit) ViewHeight() int {
|
|
|
|
return tk.wh[1]
|
|
|
|
}
|