Remove the 'Active' flag... I didn't like it

This commit is contained in:
Brian Buller 2016-02-19 08:51:24 -06:00
parent 062c970c0d
commit c3e595e205
12 changed files with 102 additions and 232 deletions

View File

@ -19,7 +19,6 @@ type AlertModal struct {
isVisible bool isVisible bool
bordered bool bordered bool
tabSkip bool tabSkip bool
active bool
} }
// CreateAlertModal Creates a confirmation modal with the specified attributes // CreateAlertModal Creates a confirmation modal with the specified attributes
@ -177,21 +176,11 @@ func (i *AlertModal) Clear() {
i.isDone = false i.isDone = false
} }
// SetActiveFlag sets this control's active flag
func (i *AlertModal) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *AlertModal) IsActive() bool { return i.active }
// HandleEvent handles the termbox event and returns whether it was consumed // HandleEvent handles the termbox event and returns whether it was consumed
func (i *AlertModal) HandleEvent(event termbox.Event) bool { func (i *AlertModal) HandleEvent(event termbox.Event) bool {
if !i.active { if event.Key == termbox.KeyEnter {
if event.Key == termbox.KeyEnter { i.isDone = true
i.isDone = true return true
return true
}
} }
return false return false
} }

View File

@ -14,7 +14,6 @@ type ASCIIArt struct {
bg, fg termbox.Attribute bg, fg termbox.Attribute
bordered bool bordered bool
tabSkip bool tabSkip bool
active bool
} }
// CreateASCIIArt Create an ASCII art object from a string slice // CreateASCIIArt Create an ASCII art object from a string slice
@ -23,14 +22,6 @@ func CreateASCIIArt(c []string, x, y int, fg, bg termbox.Attribute) *ASCIIArt {
return &i return &i
} }
// SetActiveFlag sets this control's active flag
func (i *ASCIIArt) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *ASCIIArt) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *ASCIIArt) GetID() string { return i.id } func (i *ASCIIArt) GetID() string { return i.id }

View File

@ -19,7 +19,6 @@ type ConfirmModal struct {
isVisible bool isVisible bool
bordered bool bordered bool
tabSkip bool tabSkip bool
active bool
} }
// CreateConfirmModal Creates a confirmation modal with the specified attributes // CreateConfirmModal Creates a confirmation modal with the specified attributes
@ -32,14 +31,6 @@ func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.At
return &i return &i
} }
// SetActiveFlag sets this control's active flag
func (i *ConfirmModal) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *ConfirmModal) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *ConfirmModal) GetID() string { return i.id } func (i *ConfirmModal) GetID() string { return i.id }
@ -171,16 +162,14 @@ func (i *ConfirmModal) SetTabSkip(b bool) {
// HandleEvent handles the termbox event and returns whether it was consumed // HandleEvent handles the termbox event and returns whether it was consumed
func (i *ConfirmModal) HandleEvent(event termbox.Event) bool { func (i *ConfirmModal) HandleEvent(event termbox.Event) bool {
if i.active { if event.Ch == 'Y' || event.Ch == 'y' {
if event.Ch == 'Y' || event.Ch == 'y' { i.accepted = true
i.accepted = true i.isDone = true
i.isDone = true return true
return true } else if event.Ch == 'N' || event.Ch == 'n' {
} else if event.Ch == 'N' || event.Ch == 'n' { i.accepted = false
i.accepted = false i.isDone = true
i.isDone = true return true
return true
}
} }
return false return false
} }

View File

@ -14,7 +14,6 @@ type DropMenu struct {
showMenu bool showMenu bool
bordered bool bordered bool
tabSkip bool tabSkip bool
active bool
} }
// CreateDropMenu Creates a menu with the specified attributes // CreateDropMenu Creates a menu with the specified attributes
@ -125,16 +124,6 @@ func (i *DropMenu) SetTabSkip(b bool) {
i.tabSkip = b i.tabSkip = b
} }
// SetActiveFlag sets the dropmenu active flag
func (i *DropMenu) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether the DropMenu is active
func (i *DropMenu) IsActive() bool {
return i.active
}
// ShowMenu tells the menu to draw the options // ShowMenu tells the menu to draw the options
func (i *DropMenu) ShowMenu() { func (i *DropMenu) ShowMenu() {
i.showMenu = true i.showMenu = true
@ -149,25 +138,23 @@ func (i *DropMenu) HideMenu() {
// HandleEvent handles the termbox event and returns whether it was consumed // HandleEvent handles the termbox event and returns whether it was consumed
func (i *DropMenu) HandleEvent(event termbox.Event) bool { func (i *DropMenu) HandleEvent(event termbox.Event) bool {
if i.active { moveUp := (event.Key == termbox.KeyArrowUp || (i.menu.vimMode && event.Ch == 'k'))
moveUp := (event.Key == termbox.KeyArrowUp || (i.menu.vimMode && event.Ch == 'k')) moveDown := (event.Key == termbox.KeyArrowDown || (i.menu.vimMode && event.Ch == 'j'))
moveDown := (event.Key == termbox.KeyArrowDown || (i.menu.vimMode && event.Ch == 'j')) if i.menuSelected {
if i.menuSelected { selIdx := i.menu.GetSelectedIndex()
selIdx := i.menu.GetSelectedIndex() if (moveUp && selIdx == 0) || (moveDown && selIdx == (len(i.menu.options)-1)) {
if (moveUp && selIdx == 0) || (moveDown && selIdx == (len(i.menu.options)-1)) { i.menuSelected = false
i.menuSelected = false
} else {
if i.menu.HandleEvent(event) {
if i.menu.IsDone() {
i.HideMenu()
}
return true
}
}
} else { } else {
i.ShowMenu() if i.menu.HandleEvent(event) {
return true if i.menu.IsDone() {
i.HideMenu()
}
return true
}
} }
} else {
i.ShowMenu()
return true
} }
return false return false
} }
@ -176,7 +163,7 @@ func (i *DropMenu) HandleEvent(event termbox.Event) bool {
func (i *DropMenu) Draw() { func (i *DropMenu) Draw() {
// The title // The title
ttlFg, ttlBg := i.fg, i.bg ttlFg, ttlBg := i.fg, i.bg
if i.active && !i.menuSelected { if !i.menuSelected {
ttlFg, ttlBg = i.selectedFg, i.selectedBg ttlFg, ttlBg = i.selectedFg, i.selectedBg
} }
ttlTxt := i.title ttlTxt := i.title

View File

@ -12,7 +12,6 @@ type Frame struct {
bordered bool bordered bool
controls []termboxControl controls []termboxControl
tabSkip bool tabSkip bool
active bool
} }
// CreateFrame creates a Frame at x, y that is w by h // CreateFrame creates a Frame at x, y that is w by h
@ -21,14 +20,6 @@ func CreateFrame(x, y, w, h int, fg, bg termbox.Attribute) *Frame {
return &s return &s
} }
// SetActiveFlag sets this control's active flag
func (i *Frame) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *Frame) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *Frame) GetID() string { return i.id } func (i *Frame) GetID() string { return i.id }
@ -163,14 +154,11 @@ func (i *Frame) GetBottomY() int {
// HandleEvent accepts the termbox event and returns whether it was consumed // HandleEvent accepts the termbox event and returns whether it was consumed
func (i *Frame) HandleEvent(event termbox.Event) bool { func (i *Frame) HandleEvent(event termbox.Event) bool {
if i.active { if event.Key == termbox.KeyTab {
if event.Key == termbox.KeyTab { i.FindNextTabStop()
i.FindNextTabStop() return true
return true
}
return i.controls[i.tabIdx].HandleEvent(event)
} }
return false return i.controls[i.tabIdx].HandleEvent(event)
} }
// FindNextTabStop finds the next control that can be tabbed to // FindNextTabStop finds the next control that can be tabbed to
@ -184,9 +172,6 @@ func (i *Frame) FindNextTabStop() bool {
break break
} }
} }
for idx := range i.controls {
i.controls[idx].SetActiveFlag(idx == i.tabIdx)
}
return i.tabIdx != startTab return i.tabIdx != startTab
} }

View File

@ -9,27 +9,19 @@ type InputField struct {
x, y, width, height int x, y, width, height int
cursor int cursor int
fg, bg termbox.Attribute fg, bg termbox.Attribute
cursorFg, cursorBg termbox.Attribute
bordered bool bordered bool
wrap bool wrap bool
multiline bool multiline bool
tabSkip bool tabSkip bool
active bool
} }
// CreateInputField creates an input field at x, y that is w by h // CreateInputField creates an input field at x, y that is w by h
func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField { func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField {
i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg, active: true} i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg, cursorFg: bg, cursorBg: fg}
return &i return &i
} }
// SetActiveFlag sets this control's active flag
func (i *InputField) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *InputField) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *InputField) GetID() string { return i.id } func (i *InputField) GetID() string { return i.id }
@ -130,56 +122,53 @@ func (i *InputField) SetMultiline(b bool) {
// HandleEvent accepts the termbox event and returns whether it was consumed // HandleEvent accepts the termbox event and returns whether it was consumed
func (i *InputField) HandleEvent(event termbox.Event) bool { func (i *InputField) HandleEvent(event termbox.Event) bool {
if i.active { if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { if len(i.value) > 0 {
if len(i.value) > 0 { i.value = i.value[:len(i.value)-1]
i.value = i.value[:len(i.value)-1] }
} } else if event.Key == termbox.KeyArrowLeft {
} else if event.Key == termbox.KeyArrowLeft { if i.cursor+len(i.value) > 0 {
if i.cursor+len(i.value) > 0 { i.cursor--
i.cursor-- }
} } else if event.Key == termbox.KeyArrowRight {
} else if event.Key == termbox.KeyArrowRight { if i.cursor < 0 {
if i.cursor < 0 { i.cursor++
i.cursor++ }
} } else if event.Key == termbox.KeyCtrlU {
} else if event.Key == termbox.KeyCtrlU { // Ctrl+U Clears the Input (before the cursor)
// Ctrl+U Clears the Input (before the cursor) i.value = i.value[i.cursor:]
i.value = i.value[i.cursor:] } else {
} else { // Get the rune to add to our value. Space and Tab are special cases where
// Get the rune to add to our value. Space and Tab are special cases where // we can't use the event's rune directly
// we can't use the event's rune directly var ch string
var ch string switch event.Key {
switch event.Key { case termbox.KeySpace:
case termbox.KeySpace: ch = " "
ch = " " case termbox.KeyTab:
case termbox.KeyTab: ch = "\t"
ch = "\t" /* Multiline is disabled right now
/* Multiline is disabled right now case termbox.KeyEnter:
case termbox.KeyEnter: if i.multiline {
if i.multiline { ch = "\n"
ch = "\n"
}
*/
default:
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
ch = string(event.Ch)
} }
} */
default:
if i.cursor+len(i.value) == 0 { if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
i.value = string(ch) + i.value ch = string(event.Ch)
} else if i.cursor == 0 {
i.value = i.value + string(ch)
} else {
strPt1 := i.value[:(len(i.value) + i.cursor)]
strPt2 := i.value[(len(i.value) + i.cursor):]
i.value = strPt1 + string(ch) + strPt2
} }
} }
return true
if i.cursor+len(i.value) == 0 {
i.value = string(ch) + i.value
} else if i.cursor == 0 {
i.value = i.value + string(ch)
} else {
strPt1 := i.value[:(len(i.value) + i.cursor)]
strPt2 := i.value[(len(i.value) + i.cursor):]
i.value = strPt1 + string(ch) + strPt2
}
} }
return false return true
} }
// Draw outputs the input field on the screen // Draw outputs the input field on the screen
@ -238,11 +227,7 @@ func (i *InputField) Draw() {
y++ y++
x = startX x = startX
} }
if i.active { termbox.SetCell(x, y, cursorRune, i.cursorFg, i.cursorBg)
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
} else {
termbox.SetCell(x, y, cursorRune, i.fg, i.bg)
}
x++ x++
if len(strPt2) > 0 { if len(strPt2) > 0 {
lenLeft := maxWidth - len(strPt1) - 1 lenLeft := maxWidth - len(strPt1) - 1
@ -271,11 +256,7 @@ func (i *InputField) Draw() {
} }
} }
x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
if i.active { termbox.SetCell(x, y, cursorRune, i.cursorFg, i.cursorBg)
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
} else {
termbox.SetCell(x, y, cursorRune, i.fg, i.bg)
}
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
} }
} }

View File

@ -18,7 +18,6 @@ type InputModal struct {
isVisible bool isVisible bool
bordered bool bordered bool
tabSkip bool tabSkip bool
active bool
} }
// CreateInputModal Create an input modal with the given attributes // CreateInputModal Create an input modal with the given attributes
@ -31,14 +30,6 @@ func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attr
return &i return &i
} }
// SetActiveFlag sets this control's active flag
func (i *InputModal) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *InputModal) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *InputModal) GetID() string { return i.id } func (i *InputModal) GetID() string { return i.id }
@ -183,15 +174,12 @@ func (i *InputModal) Clear() {
// HandleEvent Handle the termbox event, return true if it was consumed // HandleEvent Handle the termbox event, return true if it was consumed
func (i *InputModal) HandleEvent(event termbox.Event) bool { func (i *InputModal) HandleEvent(event termbox.Event) bool {
if i.active { if event.Key == termbox.KeyEnter {
if event.Key == termbox.KeyEnter { // Done editing
// Done editing i.isDone = true
i.isDone = true return true
return true
}
return i.input.HandleEvent(event)
} }
return false return i.input.HandleEvent(event)
} }
// Draw Draw the modal // Draw Draw the modal

View File

@ -12,7 +12,6 @@ type Label struct {
bordered bool bordered bool
wrap bool wrap bool
multiline bool multiline bool
active bool
} }
// CreateLabel creates an input field at x, y that is w by h // CreateLabel creates an input field at x, y that is w by h
@ -21,14 +20,6 @@ func CreateLabel(lbl string, x, y, w, h int, fg, bg termbox.Attribute) *Label {
return &i return &i
} }
// SetActiveFlag sets this control's active flag
func (i *Label) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *Label) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *Label) GetID() string { return i.id } func (i *Label) GetID() string { return i.id }

View File

@ -22,7 +22,6 @@ type Menu struct {
bordered bool bordered bool
vimMode bool vimMode bool
tabSkip bool tabSkip bool
active bool
} }
// CreateMenu Creates a menu with the specified attributes // CreateMenu Creates a menu with the specified attributes
@ -250,39 +249,29 @@ func (i *Menu) DisableVimMode() {
i.vimMode = false i.vimMode = false
} }
// SetActiveFlag sets this control's active flag
func (i *Menu) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *Menu) IsActive() bool { return i.active }
// HandleEvent handles the termbox event and returns whether it was consumed // HandleEvent handles the termbox event and returns whether it was consumed
func (i *Menu) HandleEvent(event termbox.Event) bool { func (i *Menu) HandleEvent(event termbox.Event) bool {
if i.active { if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace {
if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace { i.isDone = true
i.isDone = true return true
return true }
} currentIdx := i.GetSelectedIndex()
currentIdx := i.GetSelectedIndex() switch event.Key {
switch event.Key { case termbox.KeyArrowUp:
case termbox.KeyArrowUp: i.SelectPrevOption()
i.SelectPrevOption() case termbox.KeyArrowDown:
case termbox.KeyArrowDown: i.SelectNextOption()
}
if i.vimMode {
switch event.Ch {
case 'j':
i.SelectNextOption() i.SelectNextOption()
case 'k':
i.SelectPrevOption()
} }
if i.vimMode { }
switch event.Ch { if i.GetSelectedIndex() != currentIdx {
case 'j': return true
i.SelectNextOption()
case 'k':
i.SelectPrevOption()
}
}
if i.GetSelectedIndex() != currentIdx {
return true
}
} }
return false return false
} }

View File

@ -18,7 +18,6 @@ type ProgressBar struct {
x, y int x, y int
width, height int width, height int
bg, fg termbox.Attribute bg, fg termbox.Attribute
active bool
} }
// CreateProgressBar Create a progress bar object // CreateProgressBar Create a progress bar object
@ -32,14 +31,6 @@ func CreateProgressBar(tot, x, y int, fg, bg termbox.Attribute) *ProgressBar {
return &i return &i
} }
// SetActiveFlag sets this control's active flag
func (i *ProgressBar) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *ProgressBar) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *ProgressBar) GetID() string { return i.id } func (i *ProgressBar) GetID() string { return i.id }

View File

@ -12,7 +12,6 @@ type ScrollFrame struct {
fg, bg termbox.Attribute fg, bg termbox.Attribute
bordered bool bordered bool
controls []termboxControl controls []termboxControl
active bool
} }
// CreateScrollFrame creates Scrolling Frame at x, y that is w by h // CreateScrollFrame creates Scrolling Frame at x, y that is w by h
@ -21,14 +20,6 @@ func CreateScrollFrame(x, y, w, h int, fg, bg termbox.Attribute) *ScrollFrame {
return &s return &s
} }
// SetActiveFlag sets this control's active flag
func (i *ScrollFrame) SetActiveFlag(b bool) {
i.active = b
}
// IsActive returns whether this control is active
func (i *ScrollFrame) IsActive() bool { return i.active }
// GetID returns this control's ID // GetID returns this control's ID
func (i *ScrollFrame) GetID() string { return i.id } func (i *ScrollFrame) GetID() string { return i.id }

View File

@ -26,8 +26,6 @@ type termboxControl interface {
SetBordered(bool) SetBordered(bool)
SetTabSkip(bool) SetTabSkip(bool)
IsTabSkipped() bool IsTabSkipped() bool
IsActive() bool
SetActiveFlag(bool)
Draw() Draw()
} }