Position ContextMenu on selected item when negative coordinates are provided

This commit is contained in:
Trevor Slocum 2020-08-11 15:25:50 -07:00
parent cdeff20296
commit 9ec0a6d3f2
5 changed files with 24 additions and 28 deletions

View File

@ -1,8 +1,9 @@
v1.4.8 (WIP)
v1.4.8 (2020-08-11)
- Add italic text formatting flag
- Add Modal.GetForm and Modal.GetFrame
- Fix Form.Clear deadlock
- Fill nil Flex space with default background color
- Position ContextMenu on selected item when negative coordinates are provided
- Use sync.RWMutex in all widgets
v1.4.7 (2020-06-09)

View File

@ -101,7 +101,8 @@ func (c *ContextMenu) SetContextSelectedFunc(handler func(index int, text string
return c
}
// ShowContextMenu shows the context menu.
// ShowContextMenu shows the context menu. Provide -1 for both to position on
// the selected item, or specify a position.
func (c *ContextMenu) ShowContextMenu(item int, x int, y int, setFocus func(Primitive)) {
c.l.Lock()
defer c.l.Unlock()

2
go.mod
View File

@ -8,6 +8,6 @@ require (
github.com/mattn/go-runewidth v0.0.9
github.com/rivo/uniseg v0.1.0
gitlab.com/tslocum/cbind v0.1.1
golang.org/x/sys v0.0.0-20200802091954-4b90ce9b60b3 // indirect
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed // indirect
golang.org/x/text v0.3.3 // indirect
)

4
go.sum
View File

@ -23,8 +23,8 @@ gitlab.com/tslocum/cbind v0.1.1/go.mod h1:rX7vkl0pUSg/yy427MmD1FZAf99S7WwpUlxF/q
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200802091954-4b90ce9b60b3 h1:qDJKu1y/1SjhWac4BQZjLljqvqiWUhjmDMnonmVGDAU=
golang.org/x/sys v0.0.0-20200802091954-4b90ce9b60b3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed h1:WBkVNH1zd9jg/dK4HCM4lNANnmd12EHC9z+LmcCG4ns=
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=

40
list.go
View File

@ -857,13 +857,26 @@ func (l *List) Draw(screen tcell.Screen) {
cx, cy := l.ContextMenu.x, l.ContextMenu.y
if cx < 0 || cy < 0 {
cx = x + (width / 2)
cy = y + (height / 2)
offsetX := 7
if showShortcuts {
offsetX += 4
}
offsetY := l.currentItem
if l.showSecondaryText {
offsetY *= 2
}
x, y, _, _ := l.GetInnerRect()
cx, cy = x+offsetX, y+offsetY
}
_, sheight := screen.Size()
if cy+lheight >= sheight && cy-2 > lheight-cy {
cy = y - lheight
for i := (cy + lheight) - sheight; i > 0; i-- {
cy--
if cy+lheight < sheight {
break
}
}
if cy < 0 {
cy = 0
}
@ -920,26 +933,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
}
}
} else if HitShortcut(event, Keys.ShowContextMenu) {
// Do we show any shortcuts?
var showShortcuts bool
for _, item := range l.items {
if item.Shortcut != 0 {
showShortcuts = true
break
}
}
offsetX := 7
if showShortcuts {
offsetX += 4
}
offsetY := l.currentItem
if l.showSecondaryText {
offsetY *= 2
}
x, y, _, _ := l.GetInnerRect()
defer l.ContextMenu.show(l.currentItem, x+offsetX, y+offsetY, setFocus)
defer l.ContextMenu.show(l.currentItem, -1, -1, setFocus)
} else if len(l.items) == 0 {
l.Unlock()
return