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
bordered bool
tabSkip bool
active bool
}
// CreateAlertModal Creates a confirmation modal with the specified attributes
@ -177,21 +176,11 @@ func (i *AlertModal) Clear() {
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
func (i *AlertModal) HandleEvent(event termbox.Event) bool {
if !i.active {
if event.Key == termbox.KeyEnter {
i.isDone = true
return true
}
if event.Key == termbox.KeyEnter {
i.isDone = true
return true
}
return false
}

View File

@ -14,7 +14,6 @@ type ASCIIArt struct {
bg, fg termbox.Attribute
bordered bool
tabSkip bool
active bool
}
// 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
}
// 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
func (i *ASCIIArt) GetID() string { return i.id }

View File

@ -19,7 +19,6 @@ type ConfirmModal struct {
isVisible bool
bordered bool
tabSkip bool
active bool
}
// 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
}
// 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
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
func (i *ConfirmModal) HandleEvent(event termbox.Event) bool {
if i.active {
if event.Ch == 'Y' || event.Ch == 'y' {
i.accepted = true
i.isDone = true
return true
} else if event.Ch == 'N' || event.Ch == 'n' {
i.accepted = false
i.isDone = true
return true
}
if event.Ch == 'Y' || event.Ch == 'y' {
i.accepted = true
i.isDone = true
return true
} else if event.Ch == 'N' || event.Ch == 'n' {
i.accepted = false
i.isDone = true
return true
}
return false
}

View File

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

View File

@ -12,7 +12,6 @@ type Frame struct {
bordered bool
controls []termboxControl
tabSkip bool
active bool
}
// 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
}
// 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
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
func (i *Frame) HandleEvent(event termbox.Event) bool {
if i.active {
if event.Key == termbox.KeyTab {
i.FindNextTabStop()
return true
}
return i.controls[i.tabIdx].HandleEvent(event)
if event.Key == termbox.KeyTab {
i.FindNextTabStop()
return true
}
return false
return i.controls[i.tabIdx].HandleEvent(event)
}
// FindNextTabStop finds the next control that can be tabbed to
@ -184,9 +172,6 @@ func (i *Frame) FindNextTabStop() bool {
break
}
}
for idx := range i.controls {
i.controls[idx].SetActiveFlag(idx == i.tabIdx)
}
return i.tabIdx != startTab
}

View File

@ -9,27 +9,19 @@ type InputField struct {
x, y, width, height int
cursor int
fg, bg termbox.Attribute
cursorFg, cursorBg termbox.Attribute
bordered bool
wrap bool
multiline bool
tabSkip bool
active bool
}
// 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 {
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
}
// 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
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
func (i *InputField) HandleEvent(event termbox.Event) bool {
if i.active {
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
if len(i.value) > 0 {
i.value = i.value[:len(i.value)-1]
}
} else if event.Key == termbox.KeyArrowLeft {
if i.cursor+len(i.value) > 0 {
i.cursor--
}
} else if event.Key == termbox.KeyArrowRight {
if i.cursor < 0 {
i.cursor++
}
} else if event.Key == termbox.KeyCtrlU {
// Ctrl+U Clears the Input (before the cursor)
i.value = i.value[i.cursor:]
} else {
// Get the rune to add to our value. Space and Tab are special cases where
// we can't use the event's rune directly
var ch string
switch event.Key {
case termbox.KeySpace:
ch = " "
case termbox.KeyTab:
ch = "\t"
/* Multiline is disabled right now
case termbox.KeyEnter:
if i.multiline {
ch = "\n"
}
*/
default:
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
ch = string(event.Ch)
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
if len(i.value) > 0 {
i.value = i.value[:len(i.value)-1]
}
} else if event.Key == termbox.KeyArrowLeft {
if i.cursor+len(i.value) > 0 {
i.cursor--
}
} else if event.Key == termbox.KeyArrowRight {
if i.cursor < 0 {
i.cursor++
}
} else if event.Key == termbox.KeyCtrlU {
// Ctrl+U Clears the Input (before the cursor)
i.value = i.value[i.cursor:]
} else {
// Get the rune to add to our value. Space and Tab are special cases where
// we can't use the event's rune directly
var ch string
switch event.Key {
case termbox.KeySpace:
ch = " "
case termbox.KeyTab:
ch = "\t"
/* Multiline is disabled right now
case termbox.KeyEnter:
if i.multiline {
ch = "\n"
}
}
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
*/
default:
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
ch = string(event.Ch)
}
}
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
@ -238,11 +227,7 @@ func (i *InputField) Draw() {
y++
x = startX
}
if i.active {
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
} else {
termbox.SetCell(x, y, cursorRune, i.fg, i.bg)
}
termbox.SetCell(x, y, cursorRune, i.cursorFg, i.cursorBg)
x++
if len(strPt2) > 0 {
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)
if i.active {
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
} else {
termbox.SetCell(x, y, cursorRune, i.fg, i.bg)
}
termbox.SetCell(x, y, cursorRune, i.cursorFg, i.cursorBg)
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
}
}

View File

@ -18,7 +18,6 @@ type InputModal struct {
isVisible bool
bordered bool
tabSkip bool
active bool
}
// 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
}
// 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
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
func (i *InputModal) HandleEvent(event termbox.Event) bool {
if i.active {
if event.Key == termbox.KeyEnter {
// Done editing
i.isDone = true
return true
}
return i.input.HandleEvent(event)
if event.Key == termbox.KeyEnter {
// Done editing
i.isDone = true
return true
}
return false
return i.input.HandleEvent(event)
}
// Draw Draw the modal

View File

@ -12,7 +12,6 @@ type Label struct {
bordered bool
wrap bool
multiline bool
active bool
}
// 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
}
// 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
func (i *Label) GetID() string { return i.id }

View File

@ -22,7 +22,6 @@ type Menu struct {
bordered bool
vimMode bool
tabSkip bool
active bool
}
// CreateMenu Creates a menu with the specified attributes
@ -250,39 +249,29 @@ func (i *Menu) DisableVimMode() {
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
func (i *Menu) HandleEvent(event termbox.Event) bool {
if i.active {
if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace {
i.isDone = true
return true
}
currentIdx := i.GetSelectedIndex()
switch event.Key {
case termbox.KeyArrowUp:
i.SelectPrevOption()
case termbox.KeyArrowDown:
if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace {
i.isDone = true
return true
}
currentIdx := i.GetSelectedIndex()
switch event.Key {
case termbox.KeyArrowUp:
i.SelectPrevOption()
case termbox.KeyArrowDown:
i.SelectNextOption()
}
if i.vimMode {
switch event.Ch {
case 'j':
i.SelectNextOption()
case 'k':
i.SelectPrevOption()
}
if i.vimMode {
switch event.Ch {
case 'j':
i.SelectNextOption()
case 'k':
i.SelectPrevOption()
}
}
if i.GetSelectedIndex() != currentIdx {
return true
}
}
if i.GetSelectedIndex() != currentIdx {
return true
}
return false
}

View File

@ -18,7 +18,6 @@ type ProgressBar struct {
x, y int
width, height int
bg, fg termbox.Attribute
active bool
}
// CreateProgressBar Create a progress bar object
@ -32,14 +31,6 @@ func CreateProgressBar(tot, x, y int, fg, bg termbox.Attribute) *ProgressBar {
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
func (i *ProgressBar) GetID() string { return i.id }

View File

@ -12,7 +12,6 @@ type ScrollFrame struct {
fg, bg termbox.Attribute
bordered bool
controls []termboxControl
active bool
}
// 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
}
// 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
func (i *ScrollFrame) GetID() string { return i.id }

View File

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