uiterm: add prevention of doing possibly unneeded screen flushes
This commit is contained in:
@@ -30,6 +30,9 @@ func (l *Label) uiSetBounds(x0, y0, x1, y1 int) {
|
||||
}
|
||||
|
||||
func (l *Label) uiDraw() {
|
||||
l.ui.beginDraw()
|
||||
defer l.ui.endDraw()
|
||||
|
||||
reader := strings.NewReader(l.Text)
|
||||
for y := l.y0; y < l.y1; y++ {
|
||||
for x := l.x0; x < l.x1; x++ {
|
||||
|
||||
@@ -35,6 +35,9 @@ func (t *Textbox) uiSetBounds(x0, y0, x1, y1 int) {
|
||||
}
|
||||
|
||||
func (t *Textbox) uiDraw() {
|
||||
t.ui.beginDraw()
|
||||
defer t.ui.endDraw()
|
||||
|
||||
var setCursor = false
|
||||
reader := strings.NewReader(t.Text)
|
||||
for y := t.y0; y < t.y1; y++ {
|
||||
@@ -80,12 +83,10 @@ func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
|
||||
}
|
||||
if redraw {
|
||||
t.uiDraw()
|
||||
termbox.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Textbox) uiCharacterEvent(chr rune) {
|
||||
t.Text = t.Text + string(chr)
|
||||
t.uiDraw()
|
||||
termbox.Flush()
|
||||
}
|
||||
|
||||
@@ -103,6 +103,9 @@ func (t *Textview) Clear() {
|
||||
}
|
||||
|
||||
func (t *Textview) uiDraw() {
|
||||
t.ui.beginDraw()
|
||||
defer t.ui.endDraw()
|
||||
|
||||
var reader *strings.Reader
|
||||
line := len(t.parsedLines) - 1 - t.CurrentLine
|
||||
if line < 0 {
|
||||
|
||||
@@ -96,6 +96,9 @@ func (t *Tree) rebuild_rec(parent TreeItem, level int) []renderedTreeItem {
|
||||
}
|
||||
|
||||
func (t *Tree) uiDraw() {
|
||||
t.ui.beginDraw()
|
||||
defer t.ui.endDraw()
|
||||
|
||||
if t.lines == nil {
|
||||
t.Rebuild()
|
||||
}
|
||||
|
||||
14
uiterm/ui.go
14
uiterm/ui.go
@@ -2,6 +2,7 @@ package uiterm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/nsf/termbox-go"
|
||||
)
|
||||
@@ -20,6 +21,7 @@ type Ui struct {
|
||||
close chan bool
|
||||
manager UiManager
|
||||
|
||||
drawCount int32
|
||||
elements map[string]*uiElement
|
||||
activeElement *uiElement
|
||||
|
||||
@@ -50,11 +52,23 @@ func (ui *Ui) Close() {
|
||||
|
||||
func (ui *Ui) Refresh() {
|
||||
if termbox.IsInit {
|
||||
ui.beginDraw()
|
||||
defer ui.endDraw()
|
||||
|
||||
termbox.Clear(termbox.Attribute(ui.Fg), termbox.Attribute(ui.Bg))
|
||||
termbox.HideCursor()
|
||||
for _, element := range ui.elements {
|
||||
element.View.uiDraw()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ui *Ui) beginDraw() {
|
||||
atomic.AddInt32(&ui.drawCount, 1)
|
||||
}
|
||||
|
||||
func (ui *Ui) endDraw() {
|
||||
if count := atomic.AddInt32(&ui.drawCount, -1); count == 0 {
|
||||
termbox.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user