Browse Source

Use Keys shortcuts with Button, CheckBox, Grid and Table

merge-requests/4/head
Trevor Slocum 2 years ago
parent
commit
777d2f09fc
  1. 2
      button.go
  2. 13
      checkbox.go
  3. 28
      grid.go
  4. 58
      keys.go
  5. 12
      list.go
  6. 36
      table.go
  7. 40
      textview.go
  8. 16
      treeview.go

2
button.go

@ -155,7 +155,7 @@ func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Prim
if b.selected != nil {
b.selected()
}
} else if HitShortcut(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
} else if HitShortcut(event, Keys.Cancel, Keys.MovePreviousField, Keys.MoveNextField) {
if b.blur != nil {
b.blur(event.Key())
}

13
checkbox.go

@ -253,24 +253,19 @@ func (c *CheckBox) Draw(screen tcell.Screen) {
// InputHandler returns the handler for this primitive.
func (c *CheckBox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return c.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
// Process key event.
switch key := event.Key(); key {
case tcell.KeyRune, tcell.KeyEnter: // Check.
if key == tcell.KeyRune && event.Rune() != ' ' {
break
}
if HitShortcut(event, Keys.Select, Keys.SelectAlt) {
c.Lock()
c.checked = !c.checked
c.Unlock()
if c.changed != nil {
c.changed(c.checked)
}
case tcell.KeyTab, tcell.KeyBacktab, tcell.KeyEscape: // We're done.
} else if HitShortcut(event, Keys.Cancel, Keys.MovePreviousField, Keys.MoveNextField) {
if c.done != nil {
c.done(key)
c.done(event.Key())
}
if c.finished != nil {
c.finished(key)
c.finished(event.Key())
}
}
})

28
grid.go

@ -324,33 +324,17 @@ func (g *Grid) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
g.Lock()
defer g.Unlock()
switch event.Key() {
case tcell.KeyRune:
switch event.Rune() {
case 'g':
g.rowOffset, g.columnOffset = 0, 0
case 'G':
g.rowOffset = math.MaxInt32
case 'j':
g.rowOffset++
case 'k':
g.rowOffset--
case 'h':
g.columnOffset--
case 'l':
g.columnOffset++
}
case tcell.KeyHome:
if HitShortcut(event, Keys.MoveFirst) {
g.rowOffset, g.columnOffset = 0, 0
case tcell.KeyEnd:
} else if HitShortcut(event, Keys.MoveLast) {
g.rowOffset = math.MaxInt32
case tcell.KeyUp:
} else if HitShortcut(event, Keys.MoveUp, Keys.MovePreviousField) {
g.rowOffset--
case tcell.KeyDown:
} else if HitShortcut(event, Keys.MoveDown, Keys.MoveNextField) {
g.rowOffset++
case tcell.KeyLeft:
} else if HitShortcut(event, Keys.MoveLeft) {
g.columnOffset--
case tcell.KeyRight:
} else if HitShortcut(event, Keys.MoveRight) {
g.columnOffset++
}
})

58
keys.go

@ -7,40 +7,42 @@ import (
// Key defines the keyboard shortcuts of an application.
type Key struct {
Cancel []string
Select []string
FirstItem []string
LastItem []string
PreviousItem []string
NextItem []string
PreviousField []string
NextField []string
PreviousPage []string
NextPage []string
Select []string
SelectAlt []string // SelectAlt is also used when not focusing a text input.
Cancel []string
MoveUp []string
MoveDown []string
MoveLeft []string
MoveRight []string
MoveFirst []string
MoveLast []string
MovePreviousField []string
MoveNextField []string
MovePreviousPage []string
MoveNextPage []string
ShowContextMenu []string
}
// Keys defines the keyboard shortcuts of an application.
var Keys = Key{
Cancel: []string{"Escape"},
Select: []string{"Enter", "Ctrl+J"}, // Ctrl+J = keypad enter
FirstItem: []string{"Home", "g"},
LastItem: []string{"End", "G"},
PreviousItem: []string{"Up", "k"},
NextItem: []string{"Down", "j"},
PreviousField: []string{"Backtab"},
NextField: []string{"Tab"},
PreviousPage: []string{"PageUp"},
NextPage: []string{"PageDown"},
Select: []string{"Enter", "Ctrl+J"}, // Ctrl+J = keypad enter
SelectAlt: []string{"Space"},
Cancel: []string{"Escape"},
MoveUp: []string{"Up", "k"},
MoveDown: []string{"Down", "j"},
MoveLeft: []string{"Left", "h"},
MoveRight: []string{"Right", "l"},
MoveFirst: []string{"Home", "g"},
MoveLast: []string{"End", "G"},
MovePreviousField: []string{"Backtab"},
MoveNextField: []string{"Tab"},
MovePreviousPage: []string{"PageUp", "Ctrl+B"},
MoveNextPage: []string{"PageDown", "Ctrl+F"},
ShowContextMenu: []string{"Alt+Enter"},
}

12
list.go

@ -939,17 +939,17 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
}
if !matchesShortcut {
if HitShortcut(event, Keys.FirstItem) {
if HitShortcut(event, Keys.MoveFirst) {
l.transform(TransformFirstItem)
} else if HitShortcut(event, Keys.LastItem) {
} else if HitShortcut(event, Keys.MoveLast) {
l.transform(TransformLastItem)
} else if HitShortcut(event, Keys.PreviousItem, Keys.PreviousField) {
} else if HitShortcut(event, Keys.MoveUp, Keys.MovePreviousField) {
l.transform(TransformPreviousItem)
} else if HitShortcut(event, Keys.NextItem, Keys.NextField) {
} else if HitShortcut(event, Keys.MoveDown, Keys.MoveNextField) {
l.transform(TransformNextItem)
} else if HitShortcut(event, Keys.PreviousPage) {
} else if HitShortcut(event, Keys.MovePreviousPage) {
l.transform(TransformPreviousPage)
} else if HitShortcut(event, Keys.NextPage) {
} else if HitShortcut(event, Keys.MoveNextPage) {
l.transform(TransformNextPage)
}
}

36
table.go

@ -1379,39 +1379,23 @@ func (t *Table) InputHandler() func(event *tcell.EventKey, setFocus func(p Primi
}
)
switch key {
case tcell.KeyRune:
switch event.Rune() {
case 'g':
home()
case 'G':
end()
case 'j':
down()
case 'k':
up()
case 'h':
left()
case 'l':
right()
}
case tcell.KeyHome:
if HitShortcut(event, Keys.MoveFirst) {
home()
case tcell.KeyEnd:
} else if HitShortcut(event, Keys.MoveLast) {
end()
case tcell.KeyUp:
} else if HitShortcut(event, Keys.MoveUp, Keys.MovePreviousField) {
up()
case tcell.KeyDown:
} else if HitShortcut(event, Keys.MoveDown, Keys.MoveNextField) {
down()
case tcell.KeyLeft:
} else if HitShortcut(event, Keys.MoveLeft) {
left()
case tcell.KeyRight:
} else if HitShortcut(event, Keys.MoveRight) {
right()
case tcell.KeyPgDn, tcell.KeyCtrlF:
pageDown()
case tcell.KeyPgUp, tcell.KeyCtrlB:
} else if HitShortcut(event, Keys.MovePreviousPage) {
pageUp()
case tcell.KeyEnter:
} else if HitShortcut(event, Keys.MoveNextPage) {
pageDown()
} else if HitShortcut(event, Keys.Select) {
if (t.rowsSelectable || t.columnsSelectable) && t.selected != nil {
t.Unlock()
t.selected(t.selectedRow, t.selectedColumn)

40
textview.go

@ -1172,7 +1172,7 @@ func (t *TextView) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
key := event.Key()
if key == tcell.KeyEscape || key == tcell.KeyEnter || key == tcell.KeyTab || key == tcell.KeyBacktab {
if HitShortcut(event, Keys.Cancel, Keys.Select, Keys.MovePreviousField, Keys.MoveNextField) {
if t.done != nil {
t.done(key)
}
@ -1186,47 +1186,27 @@ func (t *TextView) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
return
}
switch key {
case tcell.KeyRune:
switch event.Rune() {
case 'g': // Home.
t.trackEnd = false
t.lineOffset = 0
t.columnOffset = 0
case 'G': // End.
t.trackEnd = true
t.columnOffset = 0
case 'j': // Down.
t.lineOffset++
case 'k': // Up.
t.trackEnd = false
t.lineOffset--
case 'h': // Left.
t.columnOffset--
case 'l': // Right.
t.columnOffset++
}
case tcell.KeyHome:
if HitShortcut(event, Keys.MoveFirst) {
t.trackEnd = false
t.lineOffset = 0
t.columnOffset = 0
case tcell.KeyEnd:
} else if HitShortcut(event, Keys.MoveLast) {
t.trackEnd = true
t.columnOffset = 0
case tcell.KeyUp:
} else if HitShortcut(event, Keys.MoveUp) {
t.trackEnd = false
t.lineOffset--
case tcell.KeyDown:
} else if HitShortcut(event, Keys.MoveDown) {
t.lineOffset++
case tcell.KeyLeft:
} else if HitShortcut(event, Keys.MoveLeft) {
t.columnOffset--
case tcell.KeyRight:
} else if HitShortcut(event, Keys.MoveRight) {
t.columnOffset++
case tcell.KeyPgDn, tcell.KeyCtrlF:
t.lineOffset += t.pageSize
case tcell.KeyPgUp, tcell.KeyCtrlB:
} else if HitShortcut(event, Keys.MovePreviousPage) {
t.trackEnd = false
t.lineOffset -= t.pageSize
} else if HitShortcut(event, Keys.MoveNextPage) {
t.lineOffset += t.pageSize
}
})
}

16
treeview.go

@ -889,25 +889,25 @@ func (t *TreeView) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
// Because the tree is flattened into a list only at drawing time, we also
// postpone the (selection) movement to drawing time.
if HitShortcut(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
if HitShortcut(event, Keys.Cancel, Keys.MovePreviousField, Keys.MoveNextField) {
if t.done != nil {
t.Unlock()
t.done(event.Key())
t.Lock()
}
} else if HitShortcut(event, Keys.FirstItem) {
} else if HitShortcut(event, Keys.MoveFirst) {
t.movement = treeHome
} else if HitShortcut(event, Keys.LastItem) {
} else if HitShortcut(event, Keys.MoveLast) {
t.movement = treeEnd
} else if HitShortcut(event, Keys.PreviousItem, Keys.PreviousField) {
} else if HitShortcut(event, Keys.MoveUp, Keys.MovePreviousField) {
t.movement = treeUp
} else if HitShortcut(event, Keys.NextItem, Keys.NextField) {
} else if HitShortcut(event, Keys.MoveDown, Keys.MoveNextField) {
t.movement = treeDown
} else if HitShortcut(event, Keys.PreviousPage) {
} else if HitShortcut(event, Keys.MovePreviousPage) {
t.movement = treePageUp
} else if HitShortcut(event, Keys.NextPage) {
} else if HitShortcut(event, Keys.MoveNextPage) {
t.movement = treePageDown
} else if HitShortcut(event, Keys.Select) || event.Rune() == ' ' { // TODO space is hardcoded
} else if HitShortcut(event, Keys.Select, Keys.SelectAlt) {
t.Unlock()
selectNode()
t.Lock()

Loading…
Cancel
Save