From 9ec0a6d3f23c369d414a30fdde6fea0a62c34728 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 11 Aug 2020 15:25:50 -0700 Subject: [PATCH] Position ContextMenu on selected item when negative coordinates are provided --- CHANGELOG | 3 ++- contextmenu.go | 3 ++- go.mod | 2 +- go.sum | 4 ++-- list.go | 40 +++++++++++++++++----------------------- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f0ced2f..f6e1398 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/contextmenu.go b/contextmenu.go index ad0c7e5..13649f7 100644 --- a/contextmenu.go +++ b/contextmenu.go @@ -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() diff --git a/go.mod b/go.mod index 036426c..dd5e77e 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 6b2c18e..075ad8a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/list.go b/list.go index 13c3d52..22f9e06 100644 --- a/list.go +++ b/list.go @@ -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