1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-25 23:03:13 +00:00

Making the menu widget better

This commit is contained in:
Brian Buller 2020-02-06 10:26:52 -06:00
parent c168c0df31
commit f403b69188

View File

@ -14,12 +14,15 @@ type Menu struct {
bg, fg termbox.Attribute bg, fg termbox.Attribute
selectedBg, selectedFg termbox.Attribute selectedBg, selectedFg termbox.Attribute
disabledBg, disabledFg termbox.Attribute disabledBg, disabledFg termbox.Attribute
selectedDisabledBg termbox.Attribute
selectedDisabledFg termbox.Attribute
activeFg, activeBg termbox.Attribute activeFg, activeBg termbox.Attribute
isDone bool isDone bool
bordered bool bordered bool
vimMode bool vimMode bool
tabSkip bool tabSkip bool
active bool active bool
canSelectDisabled bool
} }
// CreateMenu Creates a menu with the specified attributes // CreateMenu Creates a menu with the specified attributes
@ -189,9 +192,11 @@ func (c *Menu) SelectPrevOption() {
for idx >= 0 { for idx >= 0 {
idx-- idx--
testOption := c.GetOptionFromIndex(idx) testOption := c.GetOptionFromIndex(idx)
if testOption != nil && !testOption.IsDisabled() { if testOption != nil {
c.SetSelectedOption(testOption) if c.canSelectDisabled || !testOption.IsDisabled() {
return c.SetSelectedOption(testOption)
return
}
} }
} }
} }
@ -202,9 +207,11 @@ func (c *Menu) SelectNextOption() {
for idx < len(c.options) { for idx < len(c.options) {
idx++ idx++
testOption := c.GetOptionFromIndex(idx) testOption := c.GetOptionFromIndex(idx)
if testOption != nil && !testOption.IsDisabled() { if testOption != nil {
c.SetSelectedOption(testOption) if c.canSelectDisabled || !testOption.IsDisabled() {
return c.SetSelectedOption(testOption)
return
}
} }
} }
} }
@ -265,21 +272,25 @@ func (c *Menu) ShowHelp(b bool) {
c.showHelp = b c.showHelp = b
} }
// GetFgColor returns the foreground color func (c *Menu) GetFgColor() termbox.Attribute { return c.fg }
func (c *Menu) GetFgColor() termbox.Attribute { return c.fg } func (c *Menu) SetFgColor(fg termbox.Attribute) { c.fg = fg }
func (c *Menu) GetBgColor() termbox.Attribute { return c.bg }
func (c *Menu) SetBgColor(bg termbox.Attribute) { c.bg = bg }
// SetFgColor sets the foreground color func (c *Menu) GetSelectedFgColor() termbox.Attribute { return c.selectedFg }
func (c *Menu) SetFgColor(fg termbox.Attribute) { func (c *Menu) SetSelectedFgColor(fg termbox.Attribute) { c.selectedFg = fg }
c.fg = fg func (c *Menu) GetSelectedBgColor() termbox.Attribute { return c.selectedBg }
} func (c *Menu) SetSelectedBgColor(bg termbox.Attribute) { c.selectedBg = bg }
// GetBgColor returns the background color func (c *Menu) GetSelectedDisabledFgColor() termbox.Attribute { return c.selectedDisabledFg }
func (c *Menu) GetBgColor() termbox.Attribute { return c.bg } func (c *Menu) SetSelectedDisabledFgColor(fg termbox.Attribute) { c.selectedDisabledFg = fg }
func (c *Menu) GetSelectedDisabledBgColor() termbox.Attribute { return c.selectedDisabledBg }
func (c *Menu) SetSelectedDisabledBgColor(bg termbox.Attribute) { c.selectedDisabledBg = bg }
// SetBgColor sets the current background color func (c *Menu) GetDisabledFgColor() termbox.Attribute { return c.disabledFg }
func (c *Menu) SetBgColor(bg termbox.Attribute) { func (c *Menu) SetDisabledFgColor(fg termbox.Attribute) { c.disabledFg = fg }
c.bg = bg func (c *Menu) GetDisabledBgColor() termbox.Attribute { return c.disabledBg }
} func (c *Menu) SetDisabledBgColor(bg termbox.Attribute) { c.disabledBg = bg }
// IsDone returns whether the user has answered the modal // IsDone returns whether the user has answered the modal
func (c *Menu) IsDone() bool { return c.isDone } func (c *Menu) IsDone() bool { return c.isDone }
@ -307,6 +318,10 @@ func (c *Menu) DisableVimMode() {
c.vimMode = false c.vimMode = false
} }
func (c *Menu) SetCanSelectDisabled(b bool) {
c.canSelectDisabled = b
}
// HandleEvent handles the termbox event and returns whether it was consumed // HandleEvent handles the termbox event and returns whether it was consumed
func (c *Menu) HandleEvent(event termbox.Event) bool { func (c *Menu) HandleEvent(event termbox.Event) bool {
if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace { if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace {
@ -391,11 +406,15 @@ func (c *Menu) Draw() {
firstDispIdx = c.GetSelectedIndex() - (c.height - 2) firstDispIdx = c.GetSelectedIndex() - (c.height - 2)
lastDispIdx = c.GetSelectedIndex() lastDispIdx = c.GetSelectedIndex()
} }
for idx := firstDispIdx; idx < lastDispIdx; idx++ { for idx := firstDispIdx; idx < lastDispIdx+1; idx++ {
currOpt := &c.options[idx] currOpt := &c.options[idx]
outTxt := currOpt.GetText() outTxt := currOpt.GetText()
if currOpt.IsDisabled() { if currOpt.IsDisabled() {
DrawStringAtPoint(outTxt, optionStartX, optionStartY, c.disabledFg, c.disabledBg) if c.GetSelectedOption() == currOpt {
DrawStringAtPoint(outTxt, optionStartX, optionStartY, c.selectedDisabledFg, c.selectedDisabledBg)
} else {
DrawStringAtPoint(outTxt, optionStartX, optionStartY, c.disabledFg, c.disabledBg)
}
} else if c.GetSelectedOption() == currOpt { } else if c.GetSelectedOption() == currOpt {
DrawStringAtPoint(outTxt, optionStartX, optionStartY, c.selectedFg, c.selectedBg) DrawStringAtPoint(outTxt, optionStartX, optionStartY, c.selectedFg, c.selectedBg)
} else { } else {