Work
This commit is contained in:
42
menu.go
42
menu.go
@@ -41,6 +41,8 @@ type Menu struct {
|
||||
onPressed func() bool
|
||||
manualExpand bool
|
||||
expanded bool
|
||||
|
||||
keyMap KeyMap
|
||||
}
|
||||
|
||||
type MenuType int
|
||||
@@ -60,19 +62,7 @@ func (w *Menu) Init(id string, style tcell.Style) {
|
||||
w.id = id
|
||||
w.style = style
|
||||
w.visible = true
|
||||
}
|
||||
func (w *Menu) Id() string { return w.id }
|
||||
func (w *Menu) HandleResize(ev *tcell.EventResize) {}
|
||||
func (w *Menu) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active {
|
||||
return false
|
||||
}
|
||||
// See if the active menuitem consumes this event
|
||||
if w.items[w.cursor].HandleKey(ev) {
|
||||
return true
|
||||
}
|
||||
// Otherwise see if we handle it
|
||||
if h.HandleKeys(*ev, map[tcell.Key]func() bool{
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func() bool{
|
||||
tcell.KeyRight: w.MoveRight,
|
||||
tcell.KeyLeft: w.MoveLeft,
|
||||
tcell.KeyUp: w.MoveUp,
|
||||
@@ -83,7 +73,18 @@ func (w *Menu) HandleKey(ev *tcell.EventKey) bool {
|
||||
}
|
||||
return false
|
||||
},
|
||||
}) {
|
||||
})
|
||||
}
|
||||
func (w *Menu) Id() string { return w.id }
|
||||
func (w *Menu) HandleResize(ev *tcell.EventResize) {}
|
||||
func (w *Menu) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active {
|
||||
return false
|
||||
} else if w.items[w.cursor].HandleKey(ev) {
|
||||
// See if the active menuitem consumes this event
|
||||
return true
|
||||
} else if ok := w.keyMap.Handle(ev); ok {
|
||||
// Otherwise see if we handle it
|
||||
return true
|
||||
}
|
||||
// See if we can find an item that matches the key pressed
|
||||
@@ -109,13 +110,14 @@ func (w *Menu) drawHMenu(screen tcell.Screen) {
|
||||
h.DrawText(x, y, w.label, st, screen)
|
||||
x = x + len(w.label) + 1
|
||||
}
|
||||
h.DrawText(x, y, "-", w.style, screen)
|
||||
x += 2
|
||||
for i := range w.items {
|
||||
w.items[i].SetActive(w.active && w.cursor == i)
|
||||
w.items[i].SetPos(Coord{X: x, Y: y})
|
||||
w.items[i].Draw(screen)
|
||||
x += w.items[i].WantW() + 1
|
||||
if w.expanded || (w.active && !w.manualExpand) {
|
||||
for i := range w.items {
|
||||
w.items[i].SetActive(w.active && w.cursor == i)
|
||||
w.items[i].SetPos(Coord{X: x, Y: y})
|
||||
w.items[i].Draw(screen)
|
||||
x += w.items[i].WantW() + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user