From 119aac195e00a7e109c358a27f0ca39b3566579b Mon Sep 17 00:00:00 2001 From: "Luke I. Wilson" Date: Sat, 20 Mar 2021 18:03:38 -0500 Subject: [PATCH] Bug fixes for previous features --- main.go | 25 ++++++++++++------------- ui/container.go | 10 +++++++++- ui/menu.go | 4 ++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 4eedfab..65b6d0c 100644 --- a/main.go +++ b/main.go @@ -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() { - s.Fini() - os.Exit(0) + }}, &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) diff --git a/ui/container.go b/ui/container.go index 15257a5..bbd755d 100644 --- a/ui/container.go +++ b/ui/container.go @@ -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) } } diff --git a/ui/menu.go b/ui/menu.go index e6be78d..8ad66de 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -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]