Bug fixes for previous features
This commit is contained in:
parent
df106cbbff
commit
119aac195e
25
main.go
25
main.go
@ -91,10 +91,10 @@ func main() {
|
|||||||
|
|
||||||
fileMenu := ui.NewMenu("_File", &theme)
|
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
|
textEdit := ui.NewTextEdit(&s, "", "", &theme) // No file path, no contents
|
||||||
tabContainer.AddTab("noname", textEdit)
|
tabContainer.AddTab("noname", textEdit)
|
||||||
}}, &ui.ItemEntry{Name: "_Open...", Callback: func() {
|
}}, &ui.ItemEntry{Name: "_Open...", Shortcut: 'o', Callback: func() {
|
||||||
callback := func(filePaths []string) {
|
callback := func(filePaths []string) {
|
||||||
for _, path := range filePaths {
|
for _, path := range filePaths {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
@ -163,14 +163,18 @@ func main() {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
changeFocus(fileSelector)
|
changeFocus(fileSelector)
|
||||||
}}, &ui.ItemSeparator{}, &ui.ItemEntry{Name: "E_xit", Callback: func() {
|
}}, &ui.ItemSeparator{}, &ui.ItemEntry{Name: "_Close", Shortcut: 'q', Callback: func() {
|
||||||
s.Fini()
|
if tabContainer.GetTabCount() > 0 {
|
||||||
os.Exit(0)
|
tabContainer.RemoveTab(tabContainer.GetSelectedTabIdx())
|
||||||
|
} else { // No tabs open; close the editor
|
||||||
|
s.Fini()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
}}})
|
}}})
|
||||||
|
|
||||||
editMenu := ui.NewMenu("_Edit", &theme)
|
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 {
|
if tabContainer.GetTabCount() > 0 {
|
||||||
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
||||||
te := tab.Child.(*ui.TextEdit)
|
te := tab.Child.(*ui.TextEdit)
|
||||||
@ -181,7 +185,7 @@ func main() {
|
|||||||
_ = ClipWrite(selectedStr) // Add the selectedStr to clipboard
|
_ = 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 {
|
if tabContainer.GetTabCount() > 0 {
|
||||||
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
||||||
te := tab.Child.(*ui.TextEdit)
|
te := tab.Child.(*ui.TextEdit)
|
||||||
@ -190,7 +194,7 @@ func main() {
|
|||||||
_ = ClipWrite(selectedStr) // Add selectedStr to clipboard
|
_ = ClipWrite(selectedStr) // Add selectedStr to clipboard
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}}, &ui.ItemEntry{Name: "_Paste", Callback: func() {
|
}}, &ui.ItemEntry{Name: "_Paste", Shortcut: 'p', Callback: func() {
|
||||||
if tabContainer.GetTabCount() > 0 {
|
if tabContainer.GetTabCount() > 0 {
|
||||||
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
||||||
te := tab.Child.(*ui.TextEdit)
|
te := tab.Child.(*ui.TextEdit)
|
||||||
@ -215,7 +219,6 @@ func main() {
|
|||||||
|
|
||||||
changeFocus(tabContainer) // TabContainer is focused by default
|
changeFocus(tabContainer) // TabContainer is focused by default
|
||||||
|
|
||||||
main_loop:
|
|
||||||
for {
|
for {
|
||||||
s.Clear()
|
s.Clear()
|
||||||
|
|
||||||
@ -283,10 +286,6 @@ main_loop:
|
|||||||
changeFocus(tabContainer)
|
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 {
|
if ev.Modifiers() & tcell.ModCtrl != 0 {
|
||||||
handled := bar.HandleEvent(ev)
|
handled := bar.HandleEvent(ev)
|
||||||
|
@ -134,9 +134,17 @@ func (c *TabContainer) AddTab(name string, child Component) {
|
|||||||
// false otherwise.
|
// false otherwise.
|
||||||
func (c *TabContainer) RemoveTab(idx int) bool {
|
func (c *TabContainer) RemoveTab(idx int) bool {
|
||||||
if idx >= 0 && idx < len(c.children) {
|
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
|
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
|
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 true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -219,7 +227,7 @@ func (c *TabContainer) Draw(s tcell.Screen) {
|
|||||||
// SetFocused calls SetFocused on the visible child Component.
|
// SetFocused calls SetFocused on the visible child Component.
|
||||||
func (c *TabContainer) SetFocused(v bool) {
|
func (c *TabContainer) SetFocused(v bool) {
|
||||||
c.focused = v
|
c.focused = v
|
||||||
if c.selected < len(c.children) {
|
if len(c.children) > 0 {
|
||||||
c.children[c.selected].Child.SetFocused(v)
|
c.children[c.selected].Child.SetFocused(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,8 +209,8 @@ func (b *MenuBar) SetSize(width, height int) {
|
|||||||
func (b *MenuBar) HandleEvent(event tcell.Event) bool {
|
func (b *MenuBar) HandleEvent(event tcell.Event) bool {
|
||||||
switch ev := event.(type) {
|
switch ev := event.(type) {
|
||||||
case *tcell.EventKey:
|
case *tcell.EventKey:
|
||||||
// Shortcuts (Ctrl + s or Ctrl + (Shift) A, for example)
|
// Shortcuts (Ctrl-s or Ctrl-A, for example)
|
||||||
if ev.Modifiers() & tcell.ModCtrl != 0 {
|
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-"
|
// 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.
|
// prefix, and turn the remaining part of the string into a rune.
|
||||||
keyRune := []rune(tcell.KeyNames[ev.Key()][5:])[0]
|
keyRune := []rune(tcell.KeyNames[ev.Key()][5:])[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user