qedit/ui/menu.go

436 lines
10 KiB
Go

package ui
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
)
// Item is an interface implemented by ItemEntry and ItemMenu to be listed in Menus.
type Item interface {
GetName() string
// GetShortcut returns the shortcut character to activate the Item without opening
// the Menu. For example, if the shortcut were 'a', then the user would have to press
// `Ctrl + a`. If the shortcut were 'A', then the user would press `Ctrl + Shift + a`.
// The zero value means "no shortcut".
GetShortcut() rune
}
// An ItemSeparator is like a blank Item that cannot actually be selected. It is useful
// for separating items in a Menu.
type ItemSeparator struct{}
// GetName returns an empty string.
func (i *ItemSeparator) GetName() string {
return ""
}
func (i *ItemSeparator) GetShortcut() rune {
return 0
}
// ItemEntry is a listing in a Menu with a name and callback.
type ItemEntry struct {
Name string
Callback func()
Shortcut rune
}
// GetName returns the name of the ItemEntry.
func (i *ItemEntry) GetName() string {
return i.Name
}
func (i *ItemEntry) GetShortcut() rune {
return i.Shortcut
}
// GetName returns the name of the Menu.
func (m *Menu) GetName() string {
return m.Name
}
func (m *Menu) GetShortcut() rune {
return 0 // Menus don't have shortcuts
}
// A MenuBar is a horizontal list of menus.
type MenuBar struct {
ItemSelectedCallback func()
menus []*Menu
x, y int
width, height int
focused bool
selected int // Index of selection in MenuBar
menusVisible bool // Whether to draw the selected menu
Theme *Theme
}
func NewMenuBar(theme *Theme) *MenuBar {
return &MenuBar{
menus: make([]*Menu, 0, 6),
Theme: theme,
}
}
func (b *MenuBar) AddMenu(menu *Menu) {
menu.itemSelectedCallback = func() {
b.menusVisible = false
menu.SetFocused(false)
if b.ItemSelectedCallback != nil {
b.ItemSelectedCallback()
}
}
b.menus = append(b.menus, menu)
}
// GetMenuXPos returns the X position of the name of Menu at `idx` visually.
func (b *MenuBar) GetMenuXPos(idx int) int {
x := 1
for i := 0; i < idx; i++ {
x += len(b.menus[i].Name) + 2 // two for padding
}
return x
}
func (b *MenuBar) ActivateMenuUnderCursor() {
b.menusVisible = true // Show menus
menu := &b.menus[b.selected]
(*menu).SetPos(b.GetMenuXPos(b.selected), b.y+1)
(*menu).SetFocused(true)
}
func (b *MenuBar) CursorLeft() {
if b.menusVisible {
b.menus[b.selected].SetFocused(false) // Unfocus current menu
}
if b.selected <= 0 {
b.selected = len(b.menus) - 1 // Wrap to end
} else {
b.selected--
}
if b.menusVisible {
// Update position of new menu after changing menu selection
b.menus[b.selected].SetPos(b.GetMenuXPos(b.selected), b.y+1)
b.menus[b.selected].SetFocused(true) // Focus new menu
}
}
func (b *MenuBar) CursorRight() {
if b.menusVisible {
b.menus[b.selected].SetFocused(false)
}
if b.selected >= len(b.menus)-1 {
b.selected = 0 // Wrap to beginning
} else {
b.selected++
}
if b.menusVisible {
// Update position of new menu after changing menu selection
b.menus[b.selected].SetPos(b.GetMenuXPos(b.selected), b.y+1)
b.menus[b.selected].SetFocused(true) // Focus new menu
}
}
// Draw renders the MenuBar and its sub-menus.
func (b *MenuBar) Draw(s tcell.Screen) {
normalStyle := b.Theme.GetOrDefault("MenuBar")
// Draw menus based on whether b.focused and which is selected
DrawRect(s, b.x, b.y, b.width, 1, ' ', normalStyle)
col := b.x + 1
for i, item := range b.menus {
str := fmt.Sprintf(" %s ", item.Name) // Surround the name in spaces
sty := normalStyle
if b.focused && b.selected == i {
sty = b.Theme.GetOrDefault("MenuBarSelected") // Use special style for selected item
}
DrawQuickCharStr(s, col, b.y, str, sty)
col += len(str)
}
if b.menusVisible {
menu := b.menus[b.selected]
menu.Draw(s) // Draw menu when it is expanded / visible
}
}
// SetFocused highlights the MenuBar and focuses any sub-menus.
func (b *MenuBar) SetFocused(v bool) {
b.focused = v
b.menus[b.selected].SetFocused(v)
if !v {
b.selected = 0 // Reset cursor position every time component is unfocused
if b.menusVisible {
b.menusVisible = false
}
}
}
func (b *MenuBar) SetTheme(theme *Theme) {
b.Theme = theme
}
// GetPos returns the position of the MenuBar.
func (b *MenuBar) GetPos() (int, int) {
return b.x, b.y
}
// SetPos sets the position of the MenuBar.
func (b *MenuBar) SetPos(x, y int) {
b.x, b.y = x, y
}
func (b *MenuBar) GetMinSize() (int, int) {
return 0, 1
}
// GetSize returns the size of the MenuBar.
func (b *MenuBar) GetSize() (int, int) {
return b.width, b.height
}
// SetSize sets the size of the MenuBar.
func (b *MenuBar) SetSize(width, height int) {
b.width, b.height = width, height
}
// HandleEvent will propogate events to sub-menus and returns true if
// any of them handled the event.
func (b *MenuBar) HandleEvent(event tcell.Event) bool {
switch ev := event.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEnter:
if !b.menusVisible { // If menus are not visible...
b.ActivateMenuUnderCursor()
} else { // The selected Menu is visible, send the event to it
return b.menus[b.selected].HandleEvent(event)
}
case tcell.KeyLeft:
b.CursorLeft()
case tcell.KeyRight:
b.CursorRight()
case tcell.KeyTab:
if b.menusVisible {
return b.menus[b.selected].HandleEvent(event)
} else {
b.CursorRight()
}
case tcell.KeyRune: // Search for the matching quick char in menu names
if !b.menusVisible { // If the selected Menu is not open/visible
for i, m := range b.menus {
found, r := QuickCharInString(m.Name)
if found && r == ev.Rune() {
b.selected = i // Select menu at i
b.ActivateMenuUnderCursor() // Show menu
break
}
}
} else {
return b.menus[b.selected].HandleEvent(event) // Have menu handle quick char event
}
default:
if b.menusVisible {
return b.menus[b.selected].HandleEvent(event)
} else {
return false // Nobody to propogate our event to
}
}
return true
}
return false
}
// A Menu contains one or more ItemEntry or ItemMenus.
type Menu struct {
Name string
Items []Item
x, y int
width, height int // Size may not be settable
selected int // Index of selected Item
itemSelectedCallback func() // Used internally to hide menus on selection
Theme *Theme
}
// New creates a new Menu. `items` can be `nil`.
func NewMenu(name string, theme *Theme) *Menu {
return &Menu{
Name: name,
Items: make([]Item, 0, 6),
Theme: theme,
}
}
func (m *Menu) AddItem(item Item) {
switch typ := item.(type) {
case *Menu:
typ.itemSelectedCallback = func() {
m.itemSelectedCallback()
}
}
m.Items = append(m.Items, item)
}
func (m *Menu) AddItems(items []Item) {
for _, item := range items {
m.AddItem(item)
}
}
func (m *Menu) ActivateItemUnderCursor() {
switch item := m.Items[m.selected].(type) {
case *ItemEntry:
item.Callback()
m.itemSelectedCallback()
case *Menu:
// TODO: implement sub-menus ...
}
}
func (m *Menu) CursorUp() {
if m.selected <= 0 {
m.selected = len(m.Items) - 1 // Wrap to end
} else {
m.selected--
}
switch m.Items[m.selected].(type) {
case *ItemSeparator:
m.CursorUp() // Recursion; stack overflow if the only item in a Menu is a separator.
default:
}
}
func (m *Menu) CursorDown() {
if m.selected >= len(m.Items)-1 {
m.selected = 0 // Wrap to beginning
} else {
m.selected++
}
switch m.Items[m.selected].(type) {
case *ItemSeparator:
m.CursorDown() // Recursion; stack overflow if the only item in a Menu is a separator.
default:
}
}
// Draw renders the Menu at its position.
func (m *Menu) Draw(s tcell.Screen) {
defaultStyle := m.Theme.GetOrDefault("Menu")
m.GetSize() // Call this to update internal width and height
DrawRect(s, m.x, m.y, m.width, m.height, ' ', defaultStyle) // Fill background
DrawRectOutlineDefault(s, m.x, m.y, m.width, m.height, defaultStyle) // Draw outline
// Draw items based on whether m.focused and which is selected
for i, item := range m.Items {
switch item.(type) {
case *ItemSeparator:
str := fmt.Sprintf("%s%s%s", "├", strings.Repeat("─", m.width-2), "┤")
DrawStr(s, m.x, m.y+1+i, str, defaultStyle)
default: // Handle sub-menus and item entries the same
var sty tcell.Style
if m.selected == i {
sty = m.Theme.GetOrDefault("MenuSelected")
} else {
sty = defaultStyle
}
len := DrawQuickCharStr(s, m.x+1, m.y+1+i, item.GetName(), sty)
str := strings.Repeat(" ", m.width-2-len) // Fill space after menu names to border
DrawStr(s, m.x+1+len, m.y+1+i, str, sty)
}
}
}
// SetFocused does not do anything for a Menu.
func (m *Menu) SetFocused(v bool) {
// TODO: when adding sub-menus, set all focus to v
if !v {
m.selected = 0
}
}
// GetPos returns the position of the Menu.
func (m *Menu) GetPos() (int, int) {
return m.x, m.y
}
// SetPos sets the position of the Menu.
func (m *Menu) SetPos(x, y int) {
m.x, m.y = x, y
}
// GetSize returns the size of the Menu.
func (m *Menu) GetSize() (int, int) {
// TODO: no, pls don't do this
maxLen := 0
for _, item := range m.Items {
len := len(item.GetName())
if len > maxLen {
maxLen = len
}
}
m.width = maxLen + 2 // Add two for padding
m.height = len(m.Items) + 2 // And another two for the same reason ...
return m.width, m.height
}
// SetSize sets the size of the Menu.
func (m *Menu) SetSize(width, height int) {
// Cannot set the size of a Menu
}
// HandleEvent will handle events for a Menu and may propogate them
// to sub-menus. Returns true if the event was handled.
func (m *Menu) HandleEvent(event tcell.Event) bool {
// TODO: simplify this function
switch ev := event.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEnter:
m.ActivateItemUnderCursor()
case tcell.KeyUp:
m.CursorUp()
case tcell.KeyTab:
fallthrough
case tcell.KeyDown:
m.CursorDown()
case tcell.KeyRune:
// TODO: support quick chars for sub-menus
for i, item := range m.Items {
if m.selected == i {
continue // Skip the item we're on
}
found, r := QuickCharInString(item.GetName())
if found && r == ev.Rune() {
m.selected = i
break
}
}
default:
return false
}
return true
}
return false
}