Browse Source

Add List.Transform

merge-requests/2/head
Trevor Slocum 2 years ago
parent
commit
54834cdd54
  1. 2
      CHANGELOG
  2. 103
      list.go

2
CHANGELOG

@ -1,7 +1,7 @@
v1.4.5 (WIP)
- Add multithreading support
- Add ContextMenu (initially supported by List)
- Add TreeView.Transform
- Add List.Transform and TreeView.Transform
- Merge upstream mouse support
v1.4.4 (2020-02-24)

103
list.go

@ -537,6 +537,63 @@ func (l *List) HasFocus() bool {
return l.hasFocus
}
// Transform modifies the current selection.
func (l *List) Transform(tr Transformation) {
l.Lock()
defer l.Unlock()
l.transform(tr)
}
func (l *List) transform(tr Transformation) {
var decreasing bool
switch tr {
case TransformFirstItem:
l.currentItem = 0
decreasing = true
case TransformLastItem:
l.currentItem = len(l.items) - 1
case TransformPreviousItem:
l.currentItem -= 1
decreasing = true
case TransformNextItem:
l.currentItem += 1
case TransformPreviousPage:
l.currentItem -= 5
decreasing = true
case TransformNextPage:
l.currentItem += 5
}
for i := 0; i < len(l.items); i++ {
if l.currentItem < 0 {
if l.wrapAround {
l.currentItem = len(l.items) - 1
} else {
l.currentItem = 0
}
} else if l.currentItem >= len(l.items) {
if l.wrapAround {
l.currentItem = 0
} else {
l.currentItem = len(l.items) - 1
}
}
item := l.items[l.currentItem]
if item.Enabled {
break
}
if decreasing {
l.currentItem--
} else {
l.currentItem++
}
}
}
// Draw draws this primitive onto the screen.
func (l *List) Draw(screen tcell.Screen) {
l.Box.Draw(screen)
@ -750,18 +807,18 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
previousItem := l.currentItem
switch key := event.Key(); key {
case tcell.KeyTab, tcell.KeyDown, tcell.KeyRight:
l.currentItem++
case tcell.KeyBacktab, tcell.KeyUp, tcell.KeyLeft:
l.currentItem--
case tcell.KeyHome:
l.currentItem = 0
l.transform(TransformFirstItem)
case tcell.KeyEnd:
l.currentItem = len(l.items) - 1
case tcell.KeyPgDn:
l.currentItem += 5
l.transform(TransformLastItem)
case tcell.KeyBacktab, tcell.KeyUp, tcell.KeyLeft:
l.transform(TransformPreviousItem)
case tcell.KeyTab, tcell.KeyDown, tcell.KeyRight:
l.transform(TransformNextItem)
case tcell.KeyPgUp:
l.currentItem -= 5
l.transform(TransformPreviousPage)
case tcell.KeyPgDn:
l.transform(TransformNextPage)
case tcell.KeyEnter:
if event.Modifiers()&tcell.ModAlt != 0 {
// Do we show any shortcuts?
@ -829,34 +886,6 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
}
}
decreasing := l.currentItem < previousItem
for i := 0; i < len(l.items); i++ {
if l.currentItem < 0 {
if l.wrapAround {
l.currentItem = len(l.items) - 1
} else {
l.currentItem = 0
}
} else if l.currentItem >= len(l.items) {
if l.wrapAround {
l.currentItem = 0
} else {
l.currentItem = len(l.items) - 1
}
}
item := l.items[l.currentItem]
if item.Enabled {
break
}
if decreasing {
l.currentItem--
} else {
l.currentItem++
}
}
if l.currentItem != previousItem && l.currentItem < len(l.items) && l.changed != nil {
item := l.items[l.currentItem]
l.Unlock()

Loading…
Cancel
Save