refactor menu widget, add menuitem struct

This commit is contained in:
Bradley Cicenas 2017-02-15 04:44:03 +00:00
parent 97a561260a
commit d031433ec4
2 changed files with 47 additions and 30 deletions

View File

@ -58,8 +58,8 @@ func SortMenu() {
// set cursor position to current sort field // set cursor position to current sort field
current := config.Get("sortField") current := config.Get("sortField")
for n, field := range m.Items { for n, item := range m.Items {
if field == current { if item.Val == current {
m.CursorPos = n m.CursorPos = n
} }
} }
@ -67,7 +67,7 @@ func SortMenu() {
ui.Render(m) ui.Render(m)
m.NavigationHandlers() m.NavigationHandlers()
ui.Handle("/sys/kbd/<enter>", func(ui.Event) { ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
config.Update("sortField", m.Items[m.CursorPos]) config.Update("sortField", m.Items[m.CursorPos].Val)
ui.StopLoop() ui.StopLoop()
}) })
ui.Loop() ui.Loop()

View File

@ -6,39 +6,55 @@ import (
type Padding [2]int // x,y padding type Padding [2]int // x,y padding
type MenuItem struct {
Val string
Text string
}
type Menu struct { type Menu struct {
ui.Block ui.Block
Items []string Items []MenuItem
DisplayItems []string TextFgColor ui.Attribute
TextFgColor ui.Attribute TextBgColor ui.Attribute
TextBgColor ui.Attribute Selectable bool
Selectable bool CursorPos int
CursorPos int padding Padding
padding Padding
} }
func NewMenu(items []string) *Menu { func NewMenu(items []string) *Menu {
var mItems []MenuItem
for _, s := range items {
mItems = append(mItems, MenuItem{Val: s})
}
m := &Menu{ m := &Menu{
Block: *ui.NewBlock(), Block: *ui.NewBlock(),
Items: items, Items: mItems,
DisplayItems: []string{}, TextFgColor: ui.ThemeAttr("par.text.fg"),
TextFgColor: ui.ThemeAttr("par.text.fg"), TextBgColor: ui.ThemeAttr("par.text.bg"),
TextBgColor: ui.ThemeAttr("par.text.bg"), Selectable: false,
Selectable: false, CursorPos: 0,
CursorPos: 0, padding: Padding{4, 2},
padding: Padding{4, 2},
} }
m.calcSize() m.calcSize()
return m return m
} }
func (m *Menu) SetItems(items []MenuItem) {
m.Items = items
m.calcSize()
}
func (m *Menu) SelectedItem() MenuItem {
return m.Items[m.CursorPos]
}
func (m *Menu) Buffer() ui.Buffer { func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell var cell ui.Cell
buf := m.Block.Buffer() buf := m.Block.Buffer()
for n, item := range m.displayItems() { for n, item := range m.Items {
x := m.padding[0] x := m.padding[0]
for _, ch := range item { for _, ch := range getDisplayText(item) {
// invert bg/fg colors on currently selected row // invert bg/fg colors on currently selected row
if m.Selectable && n == m.CursorPos { if m.Selectable && n == m.CursorPos {
cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor} cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor}
@ -74,20 +90,13 @@ func (m *Menu) NavigationHandlers() {
ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() }) ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() })
} }
// override display of items, if given
func (m *Menu) displayItems() []string {
if len(m.DisplayItems) == len(m.Items) {
return m.DisplayItems
}
return m.Items
}
// Set width and height based on menu items // Set width and height based on menu items
func (m *Menu) calcSize() { func (m *Menu) calcSize() {
m.Width = 8 // minimum width m.Width = 8 // minimum width
items := m.displayItems() items := m.Items
for _, s := range items { for _, i := range m.Items {
s := getDisplayText(i)
if len(s) > m.Width { if len(s) > m.Width {
m.Width = len(s) m.Width = len(s)
} }
@ -96,3 +105,11 @@ func (m *Menu) calcSize() {
m.Width += (m.padding[0] * 2) m.Width += (m.padding[0] * 2)
m.Height = len(items) + (m.padding[1] * 2) m.Height = len(items) + (m.padding[1] * 2)
} }
// override display text of item, if given
func getDisplayText(m MenuItem) string {
if m.Text != "" {
return m.Text
}
return m.Val
}