forked from tslocum/cview
Rename matchesKeys to HitShortcut
This commit is contained in:
parent
91afabde29
commit
b65f57f2fc
|
@ -151,11 +151,11 @@ func (b *Button) Draw(screen tcell.Screen) {
|
|||
func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
return b.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
// Process key event.
|
||||
if matchesKeys(event, Keys.Select) {
|
||||
if HitShortcut(event, Keys.Select) {
|
||||
if b.selected != nil {
|
||||
b.selected()
|
||||
}
|
||||
} else if matchesKeys(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
|
||||
} else if HitShortcut(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
|
||||
if b.blur != nil {
|
||||
b.blur(event.Key())
|
||||
}
|
||||
|
|
2
doc.go
2
doc.go
|
@ -146,7 +146,7 @@ feel of the primitives to your preferred style.
|
|||
Keyboard Shortcuts
|
||||
|
||||
Widgets use keyboard shortcuts (a.k.a. keybindings) such as arrow keys and
|
||||
H/J/k/L by default. You may replace these defaults by modifying the shortcuts
|
||||
H/J/K/L by default. You may replace these defaults by modifying the shortcuts
|
||||
listed in Keys. You may also override keyboard shortcuts globally by setting a
|
||||
handler with Application.SetInputCapture.
|
||||
|
||||
|
|
24
keys.go
24
keys.go
|
@ -1,5 +1,10 @@
|
|||
package cview
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
"gitlab.com/tslocum/cbind"
|
||||
)
|
||||
|
||||
// Key defines the keyboard shortcuts of an application.
|
||||
type Key struct {
|
||||
Cancel []string
|
||||
|
@ -39,3 +44,22 @@ var Keys = Key{
|
|||
|
||||
ShowContextMenu: []string{"Alt+Enter"},
|
||||
}
|
||||
|
||||
// HitShortcut returns whether the EventKey provided is present in one or more
|
||||
// sets of keybindings.
|
||||
func HitShortcut(event *tcell.EventKey, keybindings ...[]string) bool {
|
||||
enc, err := cbind.Encode(event.Modifiers(), event.Key(), event.Rune())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, binds := range keybindings {
|
||||
for _, key := range binds {
|
||||
if key == enc {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
18
list.go
18
list.go
|
@ -852,7 +852,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
|
|||
|
||||
previousItem := l.currentItem
|
||||
|
||||
if matchesKeys(event, Keys.Cancel) {
|
||||
if HitShortcut(event, Keys.Cancel) {
|
||||
if l.ContextMenu.open {
|
||||
l.Unlock()
|
||||
|
||||
|
@ -867,7 +867,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
|
|||
l.Unlock()
|
||||
}
|
||||
return
|
||||
} else if matchesKeys(event, Keys.Select) {
|
||||
} else if HitShortcut(event, Keys.Select) {
|
||||
if l.currentItem >= 0 && l.currentItem < len(l.items) {
|
||||
item := l.items[l.currentItem]
|
||||
if item.Enabled {
|
||||
|
@ -883,7 +883,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if matchesKeys(event, Keys.ShowContextMenu) {
|
||||
} else if HitShortcut(event, Keys.ShowContextMenu) {
|
||||
// Do we show any shortcuts?
|
||||
var showShortcuts bool
|
||||
for _, item := range l.items {
|
||||
|
@ -939,17 +939,17 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
|
|||
}
|
||||
|
||||
if !matchesShortcut {
|
||||
if matchesKeys(event, Keys.FirstItem) {
|
||||
if HitShortcut(event, Keys.FirstItem) {
|
||||
l.transform(TransformFirstItem)
|
||||
} else if matchesKeys(event, Keys.LastItem) {
|
||||
} else if HitShortcut(event, Keys.LastItem) {
|
||||
l.transform(TransformLastItem)
|
||||
} else if matchesKeys(event, Keys.PreviousItem, Keys.PreviousField) {
|
||||
} else if HitShortcut(event, Keys.PreviousItem, Keys.PreviousField) {
|
||||
l.transform(TransformPreviousItem)
|
||||
} else if matchesKeys(event, Keys.NextItem, Keys.NextField) {
|
||||
} else if HitShortcut(event, Keys.NextItem, Keys.NextField) {
|
||||
l.transform(TransformNextItem)
|
||||
} else if matchesKeys(event, Keys.PreviousPage) {
|
||||
} else if HitShortcut(event, Keys.PreviousPage) {
|
||||
l.transform(TransformPreviousPage)
|
||||
} else if matchesKeys(event, Keys.NextPage) {
|
||||
} else if HitShortcut(event, Keys.NextPage) {
|
||||
l.transform(TransformNextPage)
|
||||
}
|
||||
}
|
||||
|
|
16
treeview.go
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 matchesKeys(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
|
||||
if HitShortcut(event, Keys.Cancel, Keys.PreviousField, Keys.NextField) {
|
||||
if t.done != nil {
|
||||
t.Unlock()
|
||||
t.done(event.Key())
|
||||
t.Lock()
|
||||
}
|
||||
} else if matchesKeys(event, Keys.FirstItem) {
|
||||
} else if HitShortcut(event, Keys.FirstItem) {
|
||||
t.movement = treeHome
|
||||
} else if matchesKeys(event, Keys.LastItem) {
|
||||
} else if HitShortcut(event, Keys.LastItem) {
|
||||
t.movement = treeEnd
|
||||
} else if matchesKeys(event, Keys.PreviousItem, Keys.PreviousField) {
|
||||
} else if HitShortcut(event, Keys.PreviousItem, Keys.PreviousField) {
|
||||
t.movement = treeUp
|
||||
} else if matchesKeys(event, Keys.NextItem, Keys.NextField) {
|
||||
} else if HitShortcut(event, Keys.NextItem, Keys.NextField) {
|
||||
t.movement = treeDown
|
||||
} else if matchesKeys(event, Keys.PreviousPage) {
|
||||
} else if HitShortcut(event, Keys.PreviousPage) {
|
||||
t.movement = treePageUp
|
||||
} else if matchesKeys(event, Keys.NextPage) {
|
||||
} else if HitShortcut(event, Keys.NextPage) {
|
||||
t.movement = treePageDown
|
||||
} else if matchesKeys(event, Keys.Select) || event.Rune() == ' ' { // TODO space is hardcoded
|
||||
} else if HitShortcut(event, Keys.Select) || event.Rune() == ' ' { // TODO space is hardcoded
|
||||
t.Unlock()
|
||||
selectNode()
|
||||
t.Lock()
|
||||
|
|
20
util.go
20
util.go
|
@ -9,7 +9,6 @@ import (
|
|||
"github.com/gdamore/tcell"
|
||||
runewidth "github.com/mattn/go-runewidth"
|
||||
"github.com/rivo/uniseg"
|
||||
"gitlab.com/tslocum/cbind"
|
||||
)
|
||||
|
||||
// Text alignment within a box.
|
||||
|
@ -695,22 +694,3 @@ func RenderScrollBar(screen tcell.Screen, visibility ScrollBarVisibility, x int,
|
|||
}
|
||||
Print(screen, scrollBar, x, y, 1, AlignLeft, color)
|
||||
}
|
||||
|
||||
// matchesKeys returns whether the EventKey provided is present in one or more
|
||||
// set of keybindings.
|
||||
func matchesKeys(event *tcell.EventKey, keybindings ...[]string) bool {
|
||||
enc, err := cbind.Encode(event.Modifiers(), event.Key(), event.Rune())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, binds := range keybindings {
|
||||
for _, key := range binds {
|
||||
if key == enc {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue