Do not wrap around form by default

This commit is contained in:
Trevor Slocum 2020-02-22 10:14:57 -08:00
parent 3bf6bb259c
commit afcd7dcdab
6 changed files with 39 additions and 12 deletions

View File

@ -2,12 +2,13 @@ v1.4.4 (WIP)
- Fix panic when navigating empty list
- Fix resize event dimensions on Windows
- Clarify that Box does not have inner text
- Do not wrap around form by default
v1.4.3 (2020-02-13)
- Add SetFocusedFunc to TreeNode
- Add option to always show scroll bar
- Fix scrolling Table with PageDown and PageUp
- Do not wrap around list by default
- Do not wrap around List by default
v1.4.2 (2020-02-02)
- Add scroll bar to List, DropDown, Table and TreeView

View File

@ -26,10 +26,14 @@ maintainers and allowing code changes which may be outside of tview's scope.
# Differences
## QueueUpdate and QueueUpdateDraw do not block
## Application.QueueUpdate and Application.QueueUpdateDraw do not block
tview [blocks until the queued function returns](https://github.com/rivo/tview/blob/fe3052019536251fd145835dbaa225b33b7d3088/application.go#L510).
## Lists and Forms do not wrap around by default
Call `SetWrapAround(true)` to wrap around when navigating.
# Merged pull requests
The following tview pull requests have been merged into cview:

View File

@ -10,7 +10,7 @@ See [FORK.md](https://gitlab.com/tslocum/cview/blob/master/FORK.md) for more inf
## Demo
Try the demo: ```ssh cview.rocketnine.space -p 20000```
`ssh cview.rocketnine.space -p 20000`
[![Recording of presentation demo](https://gitlab.com/tslocum/cview/-/raw/master/cview.gif)](https://gitlab.com/tslocum/cview/tree/master/demos/presentation)
@ -57,7 +57,8 @@ func main() {
}
```
Examples are available via [godoc](https://docs.rocketnine.space/gitlab.com/tslocum/cview#pkg-examples) and in the "demos" subdirectory.
Examples are available via [godoc](https://docs.rocketnine.space/gitlab.com/tslocum/cview#pkg-examples)
and in the "demos" subdirectory.
For a presentation highlighting the features of this package, compile and run
the program in the "demos/presentation" subdirectory.

View File

@ -9,6 +9,7 @@ import (
// ProgressBar demonstrates the ProgressBar.
func ProgressBar(nextSlide func()) (title string, content cview.Primitive) {
grid := cview.NewGrid().SetColumns(-1, 6, 4, 30, -1).SetRows(-1, 12, 4, 4, -1)
grid.SetBackgroundColor(cview.Styles.PrimitiveBackgroundColor)
verticalProgressBar := cview.NewProgressBar()
verticalProgressBar.SetBorder(true)

22
form.go
View File

@ -63,6 +63,9 @@ type Form struct {
// focus so that the last element that had focus keeps it.
focusedElement int
// Whether or not navigating the form will wrap around.
wrapAround bool
// The label color.
labelColor tcell.Color
@ -355,6 +358,16 @@ func (f *Form) GetFocusedItemIndex() (formItem, button int) {
return -1, index - len(f.items)
}
// SetWrapAround sets the flag that determines whether navigating the form will
// wrap around. That is, navigating downwards on the last item will move the
// selection to the first item (similarly in the other direction). If set to
// false, the selection won't change when navigating downwards on the last item
// or navigating upwards on the first item.
func (f *Form) SetWrapAround(wrapAround bool) *Form {
f.wrapAround = wrapAround
return f
}
// SetCancelFunc sets a handler which is called when the user hits the Escape
// key.
func (f *Form) SetCancelFunc(callback func()) *Form {
@ -566,11 +579,18 @@ func (f *Form) Focus(delegate func(p Primitive)) {
switch key {
case tcell.KeyTab, tcell.KeyEnter:
f.focusedElement++
if !f.wrapAround && f.focusedElement >= len(f.items)+len(f.buttons) {
f.focusedElement = (len(f.items) + len(f.buttons)) - 1
}
f.Focus(delegate)
case tcell.KeyBacktab:
f.focusedElement--
if f.focusedElement < 0 {
f.focusedElement = len(f.items) + len(f.buttons) - 1
if f.wrapAround {
f.focusedElement = len(f.items) + len(f.buttons) - 1
} else {
f.focusedElement = 0
}
}
f.Focus(delegate)
case tcell.KeyEscape:

View File

@ -35,11 +35,11 @@ type ProgressBar struct {
// NewProgressBar returns a new progress bar.
func NewProgressBar() *ProgressBar {
return &ProgressBar{
Box: NewBox(),
Box: NewBox().SetBackgroundColor(Styles.PrimitiveBackgroundColor),
EmptyRune: ' ',
EmptyColor: tcell.ColorDefault,
EmptyColor: Styles.PrimitiveBackgroundColor,
FilledRune: tcell.RuneBlock,
FilledColor: tcell.ColorDefault,
FilledColor: Styles.PrimaryTextColor,
max: 100,
Mutex: new(sync.Mutex),
}
@ -117,16 +117,16 @@ func (p *ProgressBar) Draw(screen tcell.Screen) {
for i := 0; i < barSize; i++ {
for j := 0; j < barLength; j++ {
if p.Vertical {
screen.SetContent(x+i, y+(height-1-j), p.FilledRune, nil, tcell.StyleDefault.Foreground(p.FilledColor))
screen.SetContent(x+i, y+(height-1-j), p.FilledRune, nil, tcell.StyleDefault.Foreground(p.FilledColor).Background(p.backgroundColor))
} else {
screen.SetContent(x+j, y+i, p.FilledRune, nil, tcell.StyleDefault.Foreground(p.FilledColor))
screen.SetContent(x+j, y+i, p.FilledRune, nil, tcell.StyleDefault.Foreground(p.FilledColor).Background(p.backgroundColor))
}
}
for j := barLength; j < maxLength; j++ {
if p.Vertical {
screen.SetContent(x+i, y+(height-1-j), p.EmptyRune, nil, tcell.StyleDefault.Foreground(p.EmptyColor))
screen.SetContent(x+i, y+(height-1-j), p.EmptyRune, nil, tcell.StyleDefault.Foreground(p.EmptyColor).Background(p.backgroundColor))
} else {
screen.SetContent(x+j, y+i, p.EmptyRune, nil, tcell.StyleDefault.Foreground(p.EmptyColor))
screen.SetContent(x+j, y+i, p.EmptyRune, nil, tcell.StyleDefault.Foreground(p.EmptyColor).Background(p.backgroundColor))
}
}
}