Menus hide and deleting characters in InputField
This commit is contained in:
parent
443070f077
commit
03d0932b1a
@ -53,9 +53,21 @@ func (f *InputField) SetCursorPos(offset int) {
|
|||||||
|
|
||||||
func (f *InputField) Delete(forward bool) {
|
func (f *InputField) Delete(forward bool) {
|
||||||
if forward {
|
if forward {
|
||||||
//if f.cursorPos
|
if f.cursorPos < len(f.Text) { // If the cursor is not at the very end (past text)...
|
||||||
|
lineRunes := []rune(f.Text)
|
||||||
|
copy(lineRunes[f.cursorPos:], lineRunes[f.cursorPos+1:]) // Shift characters after cursor left
|
||||||
|
lineRunes = lineRunes[:len(lineRunes)-1] // Shrink line
|
||||||
|
f.Text = string(lineRunes) // Update line with new runes
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if f.cursorPos > 0 { // If the cursor is not at the beginning...
|
||||||
|
lineRunes := []rune(f.Text)
|
||||||
|
copy(lineRunes[f.cursorPos-1:], lineRunes[f.cursorPos:]) // Shift characters at cursor left
|
||||||
|
lineRunes = lineRunes[:len(lineRunes)-1] // Shrink line length
|
||||||
|
f.Text = string(lineRunes) // Update line with new runes
|
||||||
|
|
||||||
|
f.SetCursorPos(f.cursorPos-1) // Move cursor back
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,10 +124,21 @@ func (f *InputField) HandleEvent(event tcell.Event) bool {
|
|||||||
switch ev := event.(type) {
|
switch ev := event.(type) {
|
||||||
case *tcell.EventKey:
|
case *tcell.EventKey:
|
||||||
switch ev.Key() {
|
switch ev.Key() {
|
||||||
|
// Cursor movement
|
||||||
case tcell.KeyLeft:
|
case tcell.KeyLeft:
|
||||||
f.SetCursorPos(f.cursorPos - 1)
|
f.SetCursorPos(f.cursorPos - 1)
|
||||||
case tcell.KeyRight:
|
case tcell.KeyRight:
|
||||||
f.SetCursorPos(f.cursorPos + 1)
|
f.SetCursorPos(f.cursorPos + 1)
|
||||||
|
|
||||||
|
// Deleting
|
||||||
|
case tcell.KeyBackspace:
|
||||||
|
fallthrough
|
||||||
|
case tcell.KeyBackspace2:
|
||||||
|
f.Delete(false)
|
||||||
|
case tcell.KeyDelete:
|
||||||
|
f.Delete(true)
|
||||||
|
|
||||||
|
// Inserting
|
||||||
case tcell.KeyRune:
|
case tcell.KeyRune:
|
||||||
ch := ev.Rune()
|
ch := ev.Rune()
|
||||||
f.Text += string(ch)
|
f.Text += string(ch)
|
||||||
|
21
ui/menu.go
21
ui/menu.go
@ -49,7 +49,6 @@ type MenuBar struct {
|
|||||||
|
|
||||||
x, y int
|
x, y int
|
||||||
width, height int
|
width, height int
|
||||||
menuExpanded bool
|
|
||||||
focused bool
|
focused bool
|
||||||
selected int // Index of selection in MenuBar
|
selected int // Index of selection in MenuBar
|
||||||
|
|
||||||
@ -95,7 +94,7 @@ func (b *MenuBar) Draw(s tcell.Screen) {
|
|||||||
col += len(str)
|
col += len(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.menuExpanded {
|
if b.Menus[b.selected].Visible {
|
||||||
menu := b.Menus[b.selected]
|
menu := b.Menus[b.selected]
|
||||||
menu.Draw(s) // Draw menu when it is expanded / visible
|
menu.Draw(s) // Draw menu when it is expanded / visible
|
||||||
}
|
}
|
||||||
@ -105,7 +104,7 @@ func (b *MenuBar) Draw(s tcell.Screen) {
|
|||||||
func (b *MenuBar) SetFocused(v bool) {
|
func (b *MenuBar) SetFocused(v bool) {
|
||||||
b.focused = v
|
b.focused = v
|
||||||
if !v {
|
if !v {
|
||||||
b.menuExpanded = false
|
b.Menus[b.selected].SetFocused(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,12 +141,10 @@ 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:
|
||||||
if ev.Key() == tcell.KeyEnter && !b.menuExpanded {
|
if ev.Key() == tcell.KeyEnter && !b.Menus[b.selected].Visible {
|
||||||
menu := &b.Menus[b.selected]
|
menu := &b.Menus[b.selected]
|
||||||
menu.SetPos(b.GetMenuXPos(b.selected), b.y+1)
|
menu.SetPos(b.GetMenuXPos(b.selected), b.y+1)
|
||||||
menu.SetFocused(true)
|
menu.SetFocused(true) // Makes .Visible true for the Menu
|
||||||
|
|
||||||
b.menuExpanded = true // Tells Draw() to render the menu
|
|
||||||
} else if ev.Key() == tcell.KeyLeft {
|
} else if ev.Key() == tcell.KeyLeft {
|
||||||
if b.selected <= 0 {
|
if b.selected <= 0 {
|
||||||
b.selected = len(b.Menus) - 1 // Wrap to end
|
b.selected = len(b.Menus) - 1 // Wrap to end
|
||||||
@ -165,7 +162,7 @@ func (b *MenuBar) HandleEvent(event tcell.Event) bool {
|
|||||||
// Update position of new menu after changing menu selection
|
// Update position of new menu after changing menu selection
|
||||||
b.Menus[b.selected].SetPos(b.GetMenuXPos(b.selected), b.y+1)
|
b.Menus[b.selected].SetPos(b.GetMenuXPos(b.selected), b.y+1)
|
||||||
} else {
|
} else {
|
||||||
if b.menuExpanded {
|
if b.Menus[b.selected].Visible {
|
||||||
return b.Menus[b.selected].HandleEvent(event)
|
return b.Menus[b.selected].HandleEvent(event)
|
||||||
} else {
|
} else {
|
||||||
return false // Nobody to propogate our event to
|
return false // Nobody to propogate our event to
|
||||||
@ -178,8 +175,9 @@ func (b *MenuBar) HandleEvent(event tcell.Event) bool {
|
|||||||
|
|
||||||
// A Menu contains one or more ItemEntry or ItemMenus.
|
// A Menu contains one or more ItemEntry or ItemMenus.
|
||||||
type Menu struct {
|
type Menu struct {
|
||||||
Name string
|
Name string
|
||||||
Items []Item
|
Items []Item
|
||||||
|
Visible bool // True when focused
|
||||||
|
|
||||||
x, y int
|
x, y int
|
||||||
width, height int // Size may not be settable
|
width, height int // Size may not be settable
|
||||||
@ -223,7 +221,7 @@ func (m *Menu) Draw(s tcell.Screen) {
|
|||||||
|
|
||||||
// SetFocused does not do anything for a Menu.
|
// SetFocused does not do anything for a Menu.
|
||||||
func (m *Menu) SetFocused(v bool) {
|
func (m *Menu) SetFocused(v bool) {
|
||||||
// TODO: wat do
|
m.Visible = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPos returns the position of the Menu.
|
// GetPos returns the position of the Menu.
|
||||||
@ -263,6 +261,7 @@ func (m *Menu) HandleEvent(event tcell.Event) bool {
|
|||||||
switch ev := event.(type) {
|
switch ev := event.(type) {
|
||||||
case *tcell.EventKey:
|
case *tcell.EventKey:
|
||||||
if ev.Key() == tcell.KeyEnter {
|
if ev.Key() == tcell.KeyEnter {
|
||||||
|
m.SetFocused(false) // Hides the menu
|
||||||
switch item := m.Items[m.selected].(type) {
|
switch item := m.Items[m.selected].(type) {
|
||||||
case *ItemEntry:
|
case *ItemEntry:
|
||||||
item.Callback()
|
item.Callback()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user