uiterm: add prevention of doing possibly unneeded screen flushes

This commit is contained in:
Tim Cooper
2014-12-06 22:30:16 -04:00
parent 54584f8e30
commit 634e28f2d3
5 changed files with 26 additions and 2 deletions

View File

@@ -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()
}
}