package cview
import "sync"
type ContextMenu struct {
parent Primitive
item int
open bool
drag bool
list *List
x, y int
selected func (int , string , rune )
l sync .RWMutex
}
func NewContextMenu (parent Primitive ) *ContextMenu {
return &ContextMenu {
parent : parent ,
}
}
func (c *ContextMenu ) initializeList () {
if c .list != nil {
return
}
c .list = NewList ()
c .list .ShowSecondaryText (false )
c .list .SetHover (true )
c .list .SetWrapAround (true )
c .list .ShowFocus (false )
c .list .SetBorder (true )
c .list .SetPadding (
Styles .ContextMenuPaddingTop ,
Styles .ContextMenuPaddingBottom ,
Styles .ContextMenuPaddingLeft ,
Styles .ContextMenuPaddingRight )
}
func (c *ContextMenu ) ContextMenuList () *List {
c .l .Lock ()
defer c .l .Unlock ()
c .initializeList ()
return c .list
}
func (c *ContextMenu ) AddContextItem (text string , shortcut rune , selected func (index int )) {
c .l .Lock ()
defer c .l .Unlock ()
c .initializeList ()
item := NewListItem (text )
item .SetShortcut (shortcut )
item .SetSelectedFunc (c .wrap (selected ))
c .list .AddItem (item )
if text == "" && shortcut == 0 {
c .list .Lock ()
index := len (c .list .items ) - 1
c .list .items [index ].disabled = true
c .list .Unlock ()
}
}
func (c *ContextMenu ) wrap (f func (index int )) func () {
return func () {
f (c .item )
}
}
func (c *ContextMenu ) ClearContextMenu () {
c .l .Lock ()
defer c .l .Unlock ()
c .initializeList ()
c .list .Clear ()
}
func (c *ContextMenu ) SetContextSelectedFunc (handler func (index int , text string , shortcut rune )) {
c .l .Lock ()
defer c .l .Unlock ()
c .selected = handler
}
func (c *ContextMenu ) ShowContextMenu (item int , x int , y int , setFocus func (Primitive )) {
c .l .Lock ()
defer c .l .Unlock ()
c .show (item , x , y , setFocus )
}
func (c *ContextMenu ) HideContextMenu (setFocus func (Primitive )) {
c .l .Lock ()
defer c .l .Unlock ()
c .hide (setFocus )
}
func (c *ContextMenu ) ContextMenuVisible () bool {
c .l .Lock ()
defer c .l .Unlock ()
return c .open
}
func (c *ContextMenu ) show (item int , x int , y int , setFocus func (Primitive )) {
c .initializeList ()
if len (c .list .items ) == 0 {
return
}
c .open = true
c .item = item
c .x , c .y = x , y
c .list .Lock ()
for i , item := range c .list .items {
if !item .disabled {
c .list .currentItem = i
break
}
}
c .list .Unlock ()
c .list .SetSelectedFunc (func (index int , item *ListItem ) {
c .l .Lock ()
c .hide (setFocus )
if c .selected != nil {
c .l .Unlock ()
c .selected (index , string (item .mainText ), item .shortcut )
} else {
c .l .Unlock ()
}
})
c .list .SetDoneFunc (func () {
c .l .Lock ()
defer c .l .Unlock ()
c .hide (setFocus )
})
setFocus (c .list )
}
func (c *ContextMenu ) hide (setFocus func (Primitive )) {
c .initializeList ()
c .open = false
if c .list .HasFocus () {
setFocus (c .parent )
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .