Przeglądaj źródła

uiterm: remove *Ui argument from View methods

master
Tim Cooper 10 lat temu
rodzic
commit
9b0214d112
6 zmienionych plików z 60 dodań i 36 usunięć
  1. +10
    -5
      uiterm/label.go
  2. +13
    -8
      uiterm/textbox.go
  3. +11
    -5
      uiterm/textview.go
  4. +13
    -7
      uiterm/tree.go
  5. +7
    -6
      uiterm/ui.go
  6. +6
    -5
      uiterm/view.go

+ 10
- 5
uiterm/label.go Wyświetl plik

@@ -11,20 +11,25 @@ type Label struct {
Fg Attribute Fg Attribute
Bg Attribute Bg Attribute


ui *Ui
x0, y0, x1, y1 int x0, y0, x1, y1 int
} }


func (l *Label) setActive(ui *Ui, active bool) { func (l *Label) uiInitialize(ui *Ui) {
l.ui = ui
} }


func (l *Label) setBounds(ui *Ui, x0, y0, x1, y1 int) { func (l *Label) setActive(active bool) {
}

func (l *Label) setBounds(x0, y0, x1, y1 int) {
l.x0 = x0 l.x0 = x0
l.y0 = y0 l.y0 = y0
l.x1 = x1 l.x1 = x1
l.y1 = y1 l.y1 = y1
} }


func (l *Label) draw(ui *Ui) { func (l *Label) draw() {
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++ {
@@ -39,8 +44,8 @@ func (l *Label) draw(ui *Ui) {
} }
} }


func (l *Label) keyEvent(ui *Ui, mod Modifier, key Key) { func (l *Label) keyEvent(mod Modifier, key Key) {
} }


func (l *Label) characterEvent(ui *Ui, chr rune) { func (l *Label) characterEvent(chr rune) {
} }

+ 13
- 8
uiterm/textbox.go Wyświetl plik

@@ -16,22 +16,27 @@ type Textbox struct {


Input InputFunc Input InputFunc


ui *Ui
active bool active bool
x0, y0, x1, y1 int x0, y0, x1, y1 int
} }


func (t *Textbox) setBounds(ui *Ui, x0, y0, x1, y1 int) { func (t *Textbox) uiInitialize(ui *Ui) {
t.ui = ui
}

func (t *Textbox) setBounds(x0, y0, x1, y1 int) {
t.x0 = x0 t.x0 = x0
t.y0 = y0 t.y0 = y0
t.x1 = x1 t.x1 = x1
t.y1 = y1 t.y1 = y1
} }


func (t *Textbox) setActive(ui *Ui, active bool) { func (t *Textbox) setActive(active bool) {
t.active = active t.active = active
} }


func (t *Textbox) draw(ui *Ui) { func (t *Textbox) draw() {
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++ {
@@ -51,7 +56,7 @@ func (t *Textbox) draw(ui *Ui) {
} }
} }


func (t *Textbox) keyEvent(ui *Ui, mod Modifier, key Key) { func (t *Textbox) keyEvent(mod Modifier, key Key) {
redraw := false redraw := false
switch key { switch key {
case KeyCtrlC: case KeyCtrlC:
@@ -59,7 +64,7 @@ func (t *Textbox) keyEvent(ui *Ui, mod Modifier, key Key) {
redraw = true redraw = true
case KeyEnter: case KeyEnter:
if t.Input != nil { if t.Input != nil {
t.Input(ui, t, t.Text) t.Input(t.ui, t, t.Text)
} }
t.Text = "" t.Text = ""
redraw = true redraw = true
@@ -76,13 +81,13 @@ func (t *Textbox) keyEvent(ui *Ui, mod Modifier, key Key) {
} }
} }
if redraw { if redraw {
t.draw(ui) t.draw()
termbox.Flush() termbox.Flush()
} }
} }


func (t *Textbox) characterEvent(ui *Ui, chr rune) { func (t *Textbox) characterEvent(chr rune) {
t.Text = t.Text + string(chr) t.Text = t.Text + string(chr)
t.draw(ui) t.draw()
termbox.Flush() termbox.Flush()
} }

+ 11
- 5
uiterm/textview.go Wyświetl plik

@@ -13,13 +13,19 @@ type Textview struct {
Bg Attribute Bg Attribute


parsedLines []string parsedLines []string

ui *Ui
x0, y0, x1, y1 int x0, y0, x1, y1 int
} }


func (t *Textview) setActive(ui *Ui, active bool) { func (t *Textview) uiInitialize(ui *Ui) {
t.ui = ui
}

func (t *Textview) setActive(active bool) {
} }


func (t *Textview) setBounds(ui *Ui, x0, y0, x1, y1 int) { func (t *Textview) setBounds(x0, y0, x1, y1 int) {
t.x0 = x0 t.x0 = x0
t.y0 = y0 t.y0 = y0
t.x1 = x1 t.x1 = x1
@@ -96,7 +102,7 @@ func (t *Textview) Clear() {
t.parsedLines = nil t.parsedLines = nil
} }


func (t *Textview) draw(ui *Ui) { func (t *Textview) draw() {
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 {
@@ -134,8 +140,8 @@ func (t *Textview) draw(ui *Ui) {
} }
} }


func (t *Textview) keyEvent(ui *Ui, mod Modifier, key Key) { func (t *Textview) keyEvent(mod Modifier, key Key) {
} }


func (t *Textview) characterEvent(ui *Ui, chr rune) { func (t *Textview) characterEvent(chr rune) {
} }

+ 13
- 7
uiterm/tree.go Wyświetl plik

@@ -28,6 +28,8 @@ type Tree struct {


lines []renderedTreeItem lines []renderedTreeItem
activeLine int activeLine int

ui *Ui
active bool active bool
x0, y0, x1, y1 int x0, y0, x1, y1 int
} }
@@ -42,7 +44,11 @@ func bounded(i, lower, upper int) int {
return i return i
} }


func (t *Tree) setBounds(ui *Ui, x0, y0, x1, y1 int) { func (t *Tree) uiInitialize(ui *Ui) {
t.ui = ui
}

func (t *Tree) setBounds(x0, y0, x1, y1 int) {
t.x0 = x0 t.x0 = x0
t.y0 = y0 t.y0 = y0
t.x1 = x1 t.x1 = x1
@@ -85,7 +91,7 @@ func (t *Tree) rebuild_rec(parent TreeItem, level int) []renderedTreeItem {
return lines return lines
} }


func (t *Tree) draw(ui *Ui) { func (t *Tree) draw() {
if t.lines == nil { if t.lines == nil {
t.Rebuild() t.Rebuild()
} }
@@ -118,11 +124,11 @@ func (t *Tree) draw(ui *Ui) {
} }
} }


func (t *Tree) setActive(ui *Ui, active bool) { func (t *Tree) setActive(active bool) {
t.active = active t.active = active
} }


func (t *Tree) keyEvent(ui *Ui, mod Modifier, key Key) { func (t *Tree) keyEvent(mod Modifier, key Key) {
switch key { switch key {
case KeyArrowUp: case KeyArrowUp:
t.activeLine = bounded(t.activeLine-1, 0, len(t.lines)-1) t.activeLine = bounded(t.activeLine-1, 0, len(t.lines)-1)
@@ -130,11 +136,11 @@ func (t *Tree) keyEvent(ui *Ui, mod Modifier, key Key) {
t.activeLine = bounded(t.activeLine+1, 0, len(t.lines)-1) t.activeLine = bounded(t.activeLine+1, 0, len(t.lines)-1)
case KeyEnter: case KeyEnter:
if t.Listener != nil && t.activeLine >= 0 && t.activeLine < len(t.lines) { if t.Listener != nil && t.activeLine >= 0 && t.activeLine < len(t.lines) {
t.Listener(ui, t, t.lines[t.activeLine].Item) t.Listener(t.ui, t, t.lines[t.activeLine].Item)
} }
} }
ui.Refresh() t.ui.Refresh()
} }


func (t *Tree) characterEvent(ui *Ui, ch rune) { func (t *Tree) characterEvent(ch rune) {
} }

+ 7
- 6
uiterm/ui.go Wyświetl plik

@@ -52,7 +52,7 @@ func (ui *Ui) Refresh() {
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.draw(ui) element.View.draw()
} }
termbox.Flush() termbox.Flush()
} }
@@ -65,11 +65,11 @@ func (ui *Ui) Active() View {
func (ui *Ui) SetActive(name string) { func (ui *Ui) SetActive(name string) {
element, _ := ui.elements[name] element, _ := ui.elements[name]
if ui.activeElement != nil { if ui.activeElement != nil {
ui.activeElement.View.setActive(ui, false) ui.activeElement.View.setActive(false)
} }
ui.activeElement = element ui.activeElement = element
if element != nil { if element != nil {
element.View.setActive(ui, true) element.View.setActive(true)
} }
ui.Refresh() ui.Refresh()
} }
@@ -123,7 +123,7 @@ func (ui *Ui) Run() error {


func (ui *Ui) onCharacterEvent(ch rune) { func (ui *Ui) onCharacterEvent(ch rune) {
if ui.activeElement != nil { if ui.activeElement != nil {
ui.activeElement.View.characterEvent(ui, ch) ui.activeElement.View.characterEvent(ch)
} }
} }


@@ -134,7 +134,7 @@ func (ui *Ui) onKeyEvent(mod Modifier, key Key) {
} }
} }
if ui.activeElement != nil { if ui.activeElement != nil {
ui.activeElement.View.keyEvent(ui, mod, key) ui.activeElement.View.keyEvent(mod, key)
} }
} }


@@ -153,8 +153,9 @@ func (ui *Ui) SetView(name string, x0, y0, x1, y1 int, view View) {
Y1: y1, Y1: y1,
View: view, View: view,
} }
view.uiInitialize(ui)
} }
view.setBounds(ui, x0, y0, x1, y1) view.setBounds(x0, y0, x1, y1)
} }


func (ui *Ui) View(name string) View { func (ui *Ui) View(name string) View {


+ 6
- 5
uiterm/view.go Wyświetl plik

@@ -1,9 +1,10 @@
package uiterm package uiterm


type View interface { type View interface {
setActive(ui *Ui, active bool) uiInitialize(ui *Ui)
setBounds(ui *Ui, x0, y0, x1, y1 int) setActive(active bool)
draw(ui *Ui) setBounds(x0, y0, x1, y1 int)
keyEvent(ui *Ui, mod Modifier, key Key) draw()
characterEvent(ui *Ui, ch rune) keyEvent(mod Modifier, key Key)
characterEvent(ch rune)
} }

||||||
x
 
000:0
Ładowanie…
Anuluj
Zapisz