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