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

21
main.go
View File

@ -91,10 +91,10 @@ func main() {
fileMenu := ui.NewMenu("_File", &theme)
fileMenu.AddItems([]ui.Item{&ui.ItemEntry{Name: "_New File", Callback: func() {
fileMenu.AddItems([]ui.Item{&ui.ItemEntry{Name: "_New File", Shortcut: 'n', Callback: func() {
textEdit := ui.NewTextEdit(&s, "", "", &theme) // No file path, no contents
tabContainer.AddTab("noname", textEdit)
}}, &ui.ItemEntry{Name: "_Open...", Callback: func() {
}}, &ui.ItemEntry{Name: "_Open...", Shortcut: 'o', Callback: func() {
callback := func(filePaths []string) {
for _, path := range filePaths {
file, err := os.Open(path)
@ -163,14 +163,18 @@ func main() {
},
)
changeFocus(fileSelector)
}}, &ui.ItemSeparator{}, &ui.ItemEntry{Name: "E_xit", Callback: func() {
}}, &ui.ItemSeparator{}, &ui.ItemEntry{Name: "_Close", Shortcut: 'q', Callback: func() {
if tabContainer.GetTabCount() > 0 {
tabContainer.RemoveTab(tabContainer.GetSelectedTabIdx())
} else { // No tabs open; close the editor
s.Fini()
os.Exit(0)
}
}}})
editMenu := ui.NewMenu("_Edit", &theme)
editMenu.AddItems([]ui.Item{&ui.ItemEntry{Name: "_Cut", Callback: func() {
editMenu.AddItems([]ui.Item{&ui.ItemEntry{Name: "_Cut", Shortcut: 'x', Callback: func() {
if tabContainer.GetTabCount() > 0 {
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
te := tab.Child.(*ui.TextEdit)
@ -181,7 +185,7 @@ func main() {
_ = ClipWrite(selectedStr) // Add the selectedStr to clipboard
}
}
}}, &ui.ItemEntry{Name: "_Copy", Callback: func() {
}}, &ui.ItemEntry{Name: "_Copy", Shortcut: 'c', Callback: func() {
if tabContainer.GetTabCount() > 0 {
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
te := tab.Child.(*ui.TextEdit)
@ -190,7 +194,7 @@ func main() {
_ = ClipWrite(selectedStr) // Add selectedStr to clipboard
}
}
}}, &ui.ItemEntry{Name: "_Paste", Callback: func() {
}}, &ui.ItemEntry{Name: "_Paste", Shortcut: 'p', Callback: func() {
if tabContainer.GetTabCount() > 0 {
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
te := tab.Child.(*ui.TextEdit)
@ -215,7 +219,6 @@ func main() {
changeFocus(tabContainer) // TabContainer is focused by default
main_loop:
for {
s.Clear()
@ -283,10 +286,6 @@ main_loop:
changeFocus(tabContainer)
}
}
// Ctrl + Q is a shortcut to exit
if ev.Key() == tcell.KeyCtrlQ { // TODO: replace with shortcut keys in menus
break main_loop
}
if ev.Modifiers() & tcell.ModCtrl != 0 {
handled := bar.HandleEvent(ev)

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]