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

Implemented UI caching

This commit is contained in:
マリウス 2023-01-02 19:27:23 -05:00
parent 0714431811
commit 6775d6df17
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
3 changed files with 54 additions and 19 deletions

View File

@ -48,6 +48,9 @@ type Model struct {
currentView int
wm *windowmanager.WM
ctx *ctx.Ctx
viewcache string
renderOnlyFocused bool
}
func NewModel(c *ctx.Ctx) Model {
@ -56,6 +59,9 @@ func NewModel(c *ctx.Ctx) Model {
currentView: 0,
wm: windowmanager.New(c),
ctx: c,
viewcache: "",
renderOnlyFocused: false,
}
m.header = header.NewModel(m.ctx)
@ -121,6 +127,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)
case postcreate.WIN_ID:
m.ctx.Logger.Debugln("received WinOpen")
m.viewcache = m.buildView(false)
m.renderOnlyFocused = true
ccmds = m.wm.Open(
msg.Target,
postcreate.NewModel(m.ctx),
@ -129,6 +137,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)
}
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)
case cmd.WinClose:
switch msg.Target {
case postcreate.WIN_ID:
m.ctx.Logger.Debugln("received WinClose")
m.renderOnlyFocused = false
}
default:
if msg.Call < cmd.ViewFocus {
m.ctx.Logger.Debugf("updating all with cmd: %v\n", msg)
@ -158,11 +172,22 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m Model) View() string {
s := strings.Builder{}
s.WriteString(m.header.View() + "\n")
s.WriteString(m.views[m.currentView].View())
return m.buildView(true)
}
return m.wm.View(s.String())
func (m Model) buildView(cached bool) string {
s := strings.Builder{}
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()
}
return m.wm.View(tmp, m.renderOnlyFocused)
}
func (m Model) setSizes(winWidth int, winHeight int) {

View File

@ -196,10 +196,18 @@ func (wm *WM) ResizeAll(w int, h int) []tea.Cmd {
return tcmds
}
func (wm *WM) View(view string) string {
func (wm *WM) View(view string, onlyFocused bool) string {
var v string = view
var j int = 0
for i := 0; i < len(wm.stack); i++ {
if onlyFocused {
j = len(wm.stack) - 1
if j < 0 {
j = 0
}
}
for i := j; i < len(wm.stack); i++ {
v = helpers.PlaceOverlay(
wm.stack[i].XYWH[0],
wm.stack[i].XYWH[1]+(wm.ctx.Screen[1]-wm.ctx.Content[1]),

View File

@ -15,6 +15,7 @@ import (
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/ui/cmd"
"github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/helpers"
)
var (
@ -151,6 +152,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg.Target == WIN_ID ||
msg.Target == "*" {
m.focused = true
m.viewcache = m.buildView(false)
}
return m, nil
case cmd.WinBlur:
@ -166,8 +168,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case cursor.BlinkMsg:
m.ctx.Logger.Debugf("textarea is focused: %v\n", m.textarea.Focused())
default:
m.ctx.Logger.Debugf("received unhandled msg: %v\n", msg)
// default:
// m.ctx.Logger.Debugf("received unhandled msg: %v\n", msg)
}
var tcmd tea.Cmd
@ -198,17 +200,17 @@ func (m Model) View() string {
func (m Model) buildView(cached bool) string {
var view strings.Builder = strings.Builder{}
// if cached && m.viewcache != "" {
// m.ctx.Logger.Debugln("Cached View()")
//
// m.textarea.SetWidth(m.viewcacheTextareaXY[2])
// m.textarea.SetHeight(m.viewcacheTextareaXY[3])
//
// return helpers.PlaceOverlay(
// m.viewcacheTextareaXY[0], m.viewcacheTextareaXY[1],
// m.textarea.View(), m.viewcache,
// false)
// }
if cached && m.viewcache != "" {
m.ctx.Logger.Debugln("Cached View()")
m.textarea.SetWidth(m.viewcacheTextareaXY[2])
m.textarea.SetHeight(m.viewcacheTextareaXY[3])
return helpers.PlaceOverlay(
m.viewcacheTextareaXY[0], m.viewcacheTextareaXY[1],
m.textarea.View(), m.viewcache,
false)
}
title := "Reply"
if m.replyToIdx != 0 {