1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 21:43:14 +00:00

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,22 +176,12 @@ 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,7 +162,6 @@ 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
@ -181,7 +171,6 @@ func (i *ConfirmModal) HandleEvent(event termbox.Event) bool {
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,7 +138,6 @@ 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 {
@ -168,7 +156,6 @@ func (i *DropMenu) HandleEvent(event termbox.Event) bool {
i.ShowMenu() i.ShowMenu()
return true 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 i.controls[i.tabIdx].HandleEvent(event)
}
return false
} }
// 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,7 +122,6 @@ 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]
@ -178,8 +169,6 @@ func (i *InputField) HandleEvent(event termbox.Event) bool {
} }
} }
return true return true
}
return false
} }
// 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 i.input.HandleEvent(event)
}
return false
} }
// 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,17 +249,8 @@ 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
@ -283,7 +273,6 @@ func (i *Menu) HandleEvent(event termbox.Event) bool {
if i.GetSelectedIndex() != currentIdx { if i.GetSelectedIndex() != currentIdx {
return true 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()
} }