add displayitems override, padding updates to menu

This commit is contained in:
Bradley Cicenas
2017-01-09 15:02:34 +00:00
parent ee2b63d21d
commit 2e77e40044
6 changed files with 63 additions and 37 deletions

View File

@@ -5,27 +5,30 @@ import (
)
var (
padding = 2
minWidth = 30
x_padding = 4
y_padding = 2
minWidth = 8
)
type Menu struct {
ui.Block
Items []string
TextFgColor ui.Attribute
TextBgColor ui.Attribute
Selectable bool
CursorPos int
Items []string
DisplayItems []string
TextFgColor ui.Attribute
TextBgColor ui.Attribute
Selectable bool
CursorPos int
}
func NewMenu(items []string) *Menu {
m := &Menu{
Block: *ui.NewBlock(),
Items: items,
TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"),
Selectable: false,
CursorPos: 0,
Block: *ui.NewBlock(),
Items: items,
DisplayItems: []string{},
TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"),
Selectable: false,
CursorPos: 0,
}
m.Width, m.Height = calcSize(items)
return m
@@ -35,8 +38,14 @@ func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell
buf := m.Block.Buffer()
for n, item := range m.Items {
x := padding
// override display of items, if given
items := m.Items
if len(m.DisplayItems) == len(m.Items) {
items = m.DisplayItems
}
for n, item := range items {
x := x_padding
for _, ch := range item {
// invert bg/fg colors on currently selected row
if m.Selectable && n == m.CursorPos {
@@ -44,7 +53,7 @@ func (m *Menu) Buffer() ui.Buffer {
} else {
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
}
buf.Set(x, n+padding, cell)
buf.Set(x, n+y_padding, cell)
x++
}
}
@@ -75,7 +84,7 @@ func (m *Menu) NavigationHandlers() {
// return width and height based on menu items
func calcSize(items []string) (w, h int) {
h = len(items) + (padding * 2)
h = len(items) + (y_padding * 2)
w = minWidth
for _, s := range items {
@@ -83,7 +92,7 @@ func calcSize(items []string) (w, h int) {
w = len(s)
}
}
w += (padding * 2)
w += (x_padding * 2)
return w, h
}