termbox-util/termbox_dropmenu.go

185 lines
4.9 KiB
Go
Raw Permalink Normal View History

2016-02-09 16:21:57 +00:00
package termboxUtil
import "github.com/nsf/termbox-go"
// DropMenu is a title that, when active drops a menu down
type DropMenu struct {
2019-03-25 15:10:25 +00:00
id string
title string
x, y, width, height int
bg, fg termbox.Attribute
activeFg, activeBg termbox.Attribute
cursorBg, cursorFg termbox.Attribute
menu *Menu
menuSelected bool
showMenu bool
bordered bool
tabSkip bool
active bool
2016-02-09 16:21:57 +00:00
}
// CreateDropMenu Creates a menu with the specified attributes
2019-03-25 15:10:25 +00:00
func CreateDropMenu(title string, options []string, x, y, width, height int, fg, bg, cursorFg, cursorBg termbox.Attribute) *DropMenu {
c := DropMenu{
2016-02-09 16:21:57 +00:00
title: title,
x: x, y: y, width: width, height: height,
2019-03-25 15:10:25 +00:00
fg: fg, bg: bg, activeFg: fg, activeBg: bg,
cursorFg: fg, cursorBg: bg,
2016-02-09 16:21:57 +00:00
}
2019-03-25 15:10:25 +00:00
c.menu = CreateMenu("", options, x, y+2, width, height, fg, bg)
return &c
2016-02-09 16:21:57 +00:00
}
// GetID returns this control's ID
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetID() string { return c.id }
2016-02-09 16:21:57 +00:00
// SetID sets this control's ID
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetID(newID string) {
c.id = newID
2016-02-09 16:21:57 +00:00
}
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetActiveFgColor(fg termbox.Attribute) { c.activeFg = fg }
func (c *DropMenu) SetActiveBgColor(bg termbox.Attribute) { c.activeBg = bg }
func (c *DropMenu) SetActive(a bool) { c.active = a }
func (c *DropMenu) IsActive() bool { return c.active }
2016-02-09 16:21:57 +00:00
// GetTitle returns the current title of the menu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetTitle() string { return c.title }
2016-02-09 16:21:57 +00:00
// SetTitle sets the current title of the menu to s
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetTitle(s string) {
c.title = s
2016-02-09 16:21:57 +00:00
}
// GetMenu returns the menu for this dropmenu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetMenu() *Menu {
return c.menu
2016-02-09 16:21:57 +00:00
}
// GetX returns the current x coordinate of the menu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetX() int { return c.x }
2016-02-09 16:21:57 +00:00
// SetX sets the current x coordinate of the menu to x
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetX(x int) {
c.x = x
2016-02-09 16:21:57 +00:00
}
// GetY returns the current y coordinate of the menu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetY() int { return c.y }
2016-02-09 16:21:57 +00:00
// SetY sets the current y coordinate of the menu to y
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetY(y int) {
c.y = y
2016-02-09 16:21:57 +00:00
}
// GetWidth returns the current width of the menu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetWidth() int { return c.width }
2016-02-09 16:21:57 +00:00
// SetWidth sets the current menu width to width
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetWidth(width int) {
c.width = width
2016-02-09 16:21:57 +00:00
}
// GetHeight returns the current height of the menu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetHeight() int { return c.height }
2016-02-09 16:21:57 +00:00
// SetHeight set the height of the menu to height
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetHeight(height int) {
c.height = height
2016-02-09 16:21:57 +00:00
}
// GetFgColor returns the foreground color
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetFgColor() termbox.Attribute { return c.fg }
2016-02-09 16:21:57 +00:00
// SetFgColor sets the foreground color
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetFgColor(fg termbox.Attribute) {
c.fg = fg
2016-02-09 16:21:57 +00:00
}
// GetBgColor returns the background color
2019-03-25 15:10:25 +00:00
func (c *DropMenu) GetBgColor() termbox.Attribute { return c.bg }
2016-02-09 16:21:57 +00:00
// SetBgColor sets the current background color
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetBgColor(bg termbox.Attribute) {
c.bg = bg
2016-02-09 16:21:57 +00:00
}
// IsBordered returns the bordered flag
2019-03-25 15:10:25 +00:00
func (c *DropMenu) IsBordered() bool { return c.bordered }
2016-02-09 16:21:57 +00:00
// SetBordered sets the bordered flag
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetBordered(b bool) {
c.bordered = b
c.menu.SetBordered(b)
2016-02-09 16:21:57 +00:00
}
// IsDone returns whether the user has answered the modal
2019-03-25 15:10:25 +00:00
func (c *DropMenu) IsDone() bool { return c.menu.isDone }
2016-02-09 16:21:57 +00:00
// SetDone sets whether the modal has completed it's purpose
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetDone(b bool) {
c.menu.isDone = b
2016-02-09 16:21:57 +00:00
}
// IsTabSkipped returns whether this modal has it's tabskip flag set
2019-03-25 15:10:25 +00:00
func (c *DropMenu) IsTabSkipped() bool {
return c.tabSkip
2016-02-09 16:21:57 +00:00
}
// SetTabSkip sets the tabskip flag for this control
2019-03-25 15:10:25 +00:00
func (c *DropMenu) SetTabSkip(b bool) {
c.tabSkip = b
2016-02-09 16:21:57 +00:00
}
// ShowMenu tells the menu to draw the options
2019-03-25 15:10:25 +00:00
func (c *DropMenu) ShowMenu() {
c.showMenu = true
c.menuSelected = true
2016-02-09 16:21:57 +00:00
}
// HideMenu tells the menu to hide the options
2019-03-25 15:10:25 +00:00
func (c *DropMenu) HideMenu() {
c.showMenu = false
c.menuSelected = false
2016-02-09 16:21:57 +00:00
}
// HandleEvent handles the termbox event and returns whether it was consumed
2019-03-25 15:10:25 +00:00
func (c *DropMenu) HandleEvent(event termbox.Event) bool {
moveUp := (event.Key == termbox.KeyArrowUp || (c.menu.vimMode && event.Ch == 'k'))
moveDown := (event.Key == termbox.KeyArrowDown || (c.menu.vimMode && event.Ch == 'j'))
if c.menuSelected {
selIdx := c.menu.GetSelectedIndex()
if (moveUp && selIdx == 0) || (moveDown && selIdx == (len(c.menu.options)-1)) {
c.menuSelected = false
} else {
2019-03-25 15:10:25 +00:00
if c.menu.HandleEvent(event) {
if c.menu.IsDone() {
c.HideMenu()
2016-02-09 16:21:57 +00:00
}
return true
2016-02-09 16:21:57 +00:00
}
}
} else {
2019-03-25 15:10:25 +00:00
c.ShowMenu()
return true
2016-02-09 16:21:57 +00:00
}
return false
}
// Draw draws the menu
2019-03-25 15:10:25 +00:00
func (c *DropMenu) Draw() {
2016-02-09 16:21:57 +00:00
// The title
2019-03-25 15:10:25 +00:00
ttlFg, ttlBg := c.fg, c.bg
if !c.menuSelected {
ttlFg, ttlBg = c.cursorFg, c.cursorBg
2016-02-09 16:21:57 +00:00
}
2019-03-25 15:10:25 +00:00
ttlTxt := c.title
if c.showMenu {
2016-02-09 16:21:57 +00:00
ttlTxt = ttlTxt + "-Showing Menu"
}
2019-03-25 15:10:25 +00:00
DrawStringAtPoint(AlignText(c.title, c.width, AlignLeft), c.x, c.y, ttlFg, ttlBg)
if c.showMenu {
c.menu.Draw()
2016-02-09 16:21:57 +00:00
}
}