diff --git a/ui.go b/ui.go index c38b8b7..7b4f6fb 100644 --- a/ui.go +++ b/ui.go @@ -109,13 +109,13 @@ func (b *Barnard) OnTextInput(ui *uiterm.Ui, textbox *uiterm.Textbox, text strin } func (b *Barnard) OnUiInitialize(ui *uiterm.Ui) { - ui.SetView(uiViewLogo, 0, 0, 0, 0, &uiterm.Label{ + ui.Add(uiViewLogo, &uiterm.Label{ Text: " barnard ", Fg: uiterm.ColorWhite | uiterm.AttrBold, Bg: uiterm.ColorMagenta, }) - ui.SetView(uiViewTop, 0, 0, 0, 0, &uiterm.Label{ + ui.Add(uiViewTop, &uiterm.Label{ Fg: uiterm.ColorWhite, Bg: uiterm.ColorBlue, }) @@ -125,26 +125,26 @@ func (b *Barnard) OnUiInitialize(ui *uiterm.Ui) { Fg: uiterm.ColorBlack, Bg: uiterm.ColorWhite, } - ui.SetView(uiViewStatus, 0, 0, 0, 0, &b.UiStatus) + ui.Add(uiViewStatus, &b.UiStatus) b.UiInput = uiterm.Textbox{ Fg: uiterm.ColorWhite, Bg: uiterm.ColorBlack, Input: b.OnTextInput, } - ui.SetView(uiViewInput, 0, 0, 0, 0, &b.UiInput) + ui.Add(uiViewInput, &b.UiInput) b.UiInputStatus = uiterm.Label{ Fg: uiterm.ColorBlack, Bg: uiterm.ColorWhite, } - ui.SetView(uiViewInputStatus, 0, 0, 0, 0, &b.UiInputStatus) + ui.Add(uiViewInputStatus, &b.UiInputStatus) b.UiOutput = uiterm.Textview{ Fg: uiterm.ColorWhite, Bg: uiterm.ColorBlack, } - ui.SetView(uiViewOutput, 0, 0, 0, 0, &b.UiOutput) + ui.Add(uiViewOutput, &b.UiOutput) b.UiTree = uiterm.Tree{ Generator: b.TreeItem, @@ -152,7 +152,7 @@ func (b *Barnard) OnUiInitialize(ui *uiterm.Ui) { Fg: uiterm.ColorWhite, Bg: uiterm.ColorBlack, } - ui.SetView(uiViewTree, 0, 0, 0, 0, &b.UiTree) + ui.Add(uiViewTree, &b.UiTree) b.Ui.AddKeyListener(b.OnFocusPress, uiterm.KeyTab) b.Ui.AddKeyListener(b.OnVoiceToggle, uiterm.KeyF1) @@ -165,11 +165,11 @@ func (b *Barnard) OnUiInitialize(ui *uiterm.Ui) { } func (b *Barnard) OnUiResize(ui *uiterm.Ui, width, height int) { - ui.SetView(uiViewLogo, 0, 0, 9, 1, nil) - ui.SetView(uiViewTop, 9, 0, width-6, 1, nil) - ui.SetView(uiViewStatus, width-6, 0, width, 1, nil) - ui.SetView(uiViewInput, 0, height-1, width, height, nil) - ui.SetView(uiViewInputStatus, 0, height-2, width, height-1, nil) - ui.SetView(uiViewOutput, 0, 1, width-20, height-2, nil) - ui.SetView(uiViewTree, width-20, 1, width, height-2, nil) + ui.SetBounds(uiViewLogo, 0, 0, 9, 1) + ui.SetBounds(uiViewTop, 9, 0, width-6, 1) + ui.SetBounds(uiViewStatus, width-6, 0, width, 1) + ui.SetBounds(uiViewInput, 0, height-1, width, height) + ui.SetBounds(uiViewInputStatus, 0, height-2, width, height-1) + ui.SetBounds(uiViewOutput, 0, 1, width-20, height-2) + ui.SetBounds(uiViewTree, width-20, 1, width, height-2) } diff --git a/uiterm/ui.go b/uiterm/ui.go index 63a0051..14423ae 100644 --- a/uiterm/ui.go +++ b/uiterm/ui.go @@ -1,6 +1,8 @@ package uiterm import ( + "errors" + "github.com/nsf/termbox-go" ) @@ -14,6 +16,9 @@ type UiManager interface { } type Ui struct { + Fg Attribute + Bg Attribute + close chan bool manager UiManager @@ -21,9 +26,6 @@ type Ui struct { activeElement *uiElement keyListeners map[Key][]KeyListener - - fg Attribute - bg Attribute } type uiElement struct { @@ -49,7 +51,7 @@ func (ui *Ui) Close() { func (ui *Ui) Refresh() { if termbox.IsInit { - termbox.Clear(termbox.Attribute(ui.fg), termbox.Attribute(ui.bg)) + termbox.Clear(termbox.Attribute(ui.Fg), termbox.Attribute(ui.Bg)) termbox.HideCursor() for _, element := range ui.elements { element.View.draw() @@ -74,11 +76,6 @@ func (ui *Ui) SetActive(name string) { ui.Refresh() } -func (ui *Ui) SetClear(fg, bg Attribute) { - ui.fg = fg - ui.bg = bg -} - func (ui *Ui) Run() error { if termbox.IsInit { return nil @@ -138,32 +135,28 @@ func (ui *Ui) onKeyEvent(mod Modifier, key Key) { } } -func (ui *Ui) SetView(name string, x0, y0, x1, y1 int, view View) { - if element, ok := ui.elements[name]; ok { - element.X0 = x0 - element.Y0 = y0 - element.X1 = x1 - element.Y1 = y1 - view = element.View - } else { - ui.elements[name] = &uiElement{ - X0: x0, - Y0: y0, - X1: x1, - Y1: y1, - View: view, - } - view.uiInitialize(ui) +func (ui *Ui) Add(name string, view View) error { + if _, ok := ui.elements[name]; ok { + return errors.New("view already exists") } - view.setBounds(x0, y0, x1, y1) + ui.elements[name] = &uiElement{ + View: view, + } + view.uiInitialize(ui) + return nil } -func (ui *Ui) View(name string) View { - if element, ok := ui.elements[name]; !ok { - return nil - } else { - return element.View +func (ui *Ui) SetBounds(name string, x0, y0, x1, y1 int) error { + element, ok := ui.elements[name] + if !ok { + return errors.New("view cannot be found") } + element.X0 = x0 + element.Y0 = y0 + element.X1 = x1 + element.Y1 = y1 + element.View.setBounds(x0, y0, x1, y1) + return nil } func (ui *Ui) AddKeyListener(listener KeyListener, key Key) {