Bug fixes for previous features

This commit is contained in:
Luke I. Wilson
2021-03-20 18:03:38 -05:00
parent df106cbbff
commit 119aac195e
3 changed files with 23 additions and 16 deletions

View File

@@ -134,9 +134,17 @@ func (c *TabContainer) AddTab(name string, child Component) {
// false otherwise.
func (c *TabContainer) RemoveTab(idx int) bool {
if idx >= 0 && idx < len(c.children) {
if c.selected == idx {
c.children[idx].Child.SetFocused(false)
}
copy(c.children[idx:], c.children[idx+1:]) // Shift all items after idx to the left
c.children = c.children[:len(c.children)-1] // Shrink slice by one
if c.selected >= idx && idx > 0 {
c.selected-- // Keep the cursor within the bounds of available tabs
}
return true
}
return false
@@ -219,7 +227,7 @@ func (c *TabContainer) Draw(s tcell.Screen) {
// SetFocused calls SetFocused on the visible child Component.
func (c *TabContainer) SetFocused(v bool) {
c.focused = v
if c.selected < len(c.children) {
if len(c.children) > 0 {
c.children[c.selected].Child.SetFocused(v)
}
}

View File

@@ -209,8 +209,8 @@ func (b *MenuBar) SetSize(width, height int) {
func (b *MenuBar) HandleEvent(event tcell.Event) bool {
switch ev := event.(type) {
case *tcell.EventKey:
// Shortcuts (Ctrl + s or Ctrl + (Shift) A, for example)
if ev.Modifiers() & tcell.ModCtrl != 0 {
// Shortcuts (Ctrl-s or Ctrl-A, for example)
if ev.Modifiers() & tcell.ModCtrl != 0 && strings.HasPrefix(tcell.KeyNames[ev.Key()], "Ctrl-") {
// tcell calls Ctrl + rune keys "Ctrl-(RUNE)" so we want to remove the "Ctrl-"
// prefix, and turn the remaining part of the string into a rune.
keyRune := []rune(tcell.KeyNames[ev.Key()][5:])[0]