fix(renderer): debounce resize renders

This change introduces a debounce mechanism to the cursed renderer's
resize handling. When a resize event occurs, the renderer will now wait
for a short duration before processing the resize. This prevents
multiple full resize renders from occurring in quick succession, which
can lead to performance issues and visual artifacts.
This commit is contained in:
Ayman Bagabas 2025-11-24 11:11:32 -05:00
parent 15f884bd29
commit 95e366c689
No known key found for this signature in database
GPG key ID: C8D51D2157C919AC

View file

@ -7,6 +7,7 @@ import (
"io"
"strings"
"sync"
"time"
"github.com/charmbracelet/colorprofile"
uv "github.com/charmbracelet/ultraviolet"
@ -14,6 +15,11 @@ import (
"github.com/lucasb-eyer/go-colorful"
)
// resizeDuration is the duration to wait after a resize event before
// processing another resize event. This is to avoid processing multiple resize
// events in quick succession.
const resizeDuration = 100 * time.Millisecond // milliseconds
type cursedRenderer struct {
w io.Writer
buf bytes.Buffer // updates buffer to be flushed to [w]
@ -32,6 +38,7 @@ type cursedRenderer struct {
mapnl bool
syncdUpdates bool // whether to use synchronized output mode for updates
prependLines []string
resizeTime time.Time // last resize time
}
var _ renderer = &cursedRenderer{}
@ -261,11 +268,17 @@ func (s *cursedRenderer) flush(closing bool) error {
}
}
if s.lastView != nil && *s.lastView == view && frameArea == s.cellbuf.Bounds() {
// No changes, nothing to do.
if len(s.prependLines) == 0 && s.lastView != nil && *s.lastView == view && frameArea == s.cellbuf.Bounds() {
return nil
}
if s.lastView != nil && !s.resizeTime.IsZero() && time.Since(s.resizeTime) < resizeDuration {
// Avoid quick successive resizes rendering.
return nil
}
s.resizeTime = time.Time{} // reset resize time
if frameArea != s.cellbuf.Bounds() {
s.scr.Erase() // Force a full redraw to avoid artifacts.
@ -624,6 +637,7 @@ func (s *cursedRenderer) resize(w, h int) {
s.scr.Erase()
s.width, s.height = w, h
s.scr.Resize(s.width, s.height)
s.resizeTime = time.Now()
s.mu.Unlock()
}