akiyosi.goneovim/popupmenu.go

203 lines
4.1 KiB
Go
Raw Normal View History

2017-03-14 01:52:44 +00:00
package gonvim
2017-03-14 02:40:16 +00:00
import (
"time"
"github.com/dzhou121/ui"
)
2017-03-14 01:52:44 +00:00
// PopupMenu is the popupmenu
type PopupMenu struct {
2017-03-14 02:40:16 +00:00
box *ui.Box
items []*PopupItem
total int
hidden bool
2017-03-14 01:52:44 +00:00
}
// PopupItem is
type PopupItem struct {
kind *SpanHandler
menu *SpanHandler
}
func initPopupmenu() *PopupMenu {
total := 10
box := ui.NewHorizontalBox()
var popupItems []*PopupItem
for i := 0; i < total; i++ {
kindSpanHandler := &SpanHandler{}
kindSpan := ui.NewArea(kindSpanHandler)
kindSpanHandler.span = kindSpan
menuSpanHandler := &SpanHandler{}
menuSpan := ui.NewArea(menuSpanHandler)
menuSpanHandler.span = menuSpan
popupItem := &PopupItem{
kind: kindSpanHandler,
menu: menuSpanHandler,
}
popupItems = append(popupItems, popupItem)
box.Append(kindSpan, false)
box.Append(menuSpan, false)
}
2017-03-14 03:02:24 +00:00
box.SetShadow(0, 2, 0, 0, 0, 1, 4)
2017-03-14 01:52:44 +00:00
box.Hide()
return &PopupMenu{
box: box,
items: popupItems,
total: total,
}
}
func (p *PopupMenu) show(args []interface{}) {
2017-03-14 02:40:16 +00:00
p.hidden = false
2017-03-14 01:52:44 +00:00
arg := args[0].([]interface{})
items := arg[0].([]interface{})
selected := reflectToInt(arg[1])
row := reflectToInt(arg[2])
col := reflectToInt(arg[3])
popupItems := p.items
i := 0
2017-03-14 02:40:16 +00:00
kindWidth := 0
menuWidthMax := 0
2017-03-14 01:52:44 +00:00
heightSum := 0
2017-03-14 02:40:16 +00:00
height := 0
2017-03-14 01:52:44 +00:00
for i = 0; i < p.total; i++ {
popupItem := popupItems[i]
if i >= len(items) {
popupItem.hide()
continue
}
item := items[i].([]interface{})
popupItem.setItem(item, selected == i)
2017-03-14 02:40:16 +00:00
var menuWidth int
menuWidth, height = popupItem.menu.getSize()
kindWidth, height = popupItem.kind.getSize()
if menuWidth > menuWidthMax {
menuWidthMax = menuWidth
2017-03-14 01:52:44 +00:00
}
y := heightSum
heightSum += height
ui.QueueMain(func() {
2017-03-14 02:40:16 +00:00
popupItem.kind.span.SetPosition(0, y)
popupItem.menu.span.SetPosition(kindWidth, y)
2017-03-14 01:52:44 +00:00
})
}
for i = 0; i < p.total; i++ {
if i >= len(items) {
continue
}
popupItem := popupItems[i]
ui.QueueMain(func() {
2017-03-14 02:40:16 +00:00
popupItem.kind.span.SetSize(kindWidth, height)
popupItem.kind.span.Show()
popupItem.kind.span.QueueRedrawAll()
popupItem.menu.span.SetSize(menuWidthMax, height)
2017-03-14 01:52:44 +00:00
popupItem.menu.span.Show()
popupItem.menu.span.QueueRedrawAll()
})
}
ui.QueueMain(func() {
p.box.SetPosition(
2017-03-14 07:20:31 +00:00
col*editor.font.width-kindWidth-p.items[0].menu.paddingLeft,
(row+1)*editor.font.lineHeight,
2017-03-14 01:52:44 +00:00
)
2017-03-14 02:40:16 +00:00
p.box.SetSize(menuWidthMax+kindWidth, heightSum)
2017-03-14 01:52:44 +00:00
p.box.Show()
})
}
func (p *PopupMenu) hide(args []interface{}) {
2017-03-14 02:40:16 +00:00
p.hidden = true
time.AfterFunc(50*time.Millisecond, func() {
if p.hidden {
ui.QueueMain(func() {
p.box.Hide()
})
}
2017-03-14 01:52:44 +00:00
})
}
func (p *PopupMenu) selectItem(args []interface{}) {
selected := reflectToInt(args[0].([]interface{})[0])
for i := 0; i < p.total; i++ {
popupItem := p.items[i]
if selected == i {
popupItem.menu.SetBackground(newRGBA(81, 154, 186, 1))
ui.QueueMain(func() {
popupItem.menu.span.QueueRedrawAll()
})
} else {
popupItem.menu.SetBackground(newRGBA(14, 17, 18, 1))
ui.QueueMain(func() {
popupItem.menu.span.QueueRedrawAll()
})
}
}
}
func (p *PopupItem) setItem(item []interface{}, selected bool) {
text := item[0].(string)
kindText := item[1].(string)
p.setKind(kindText, selected)
fg := newRGBA(205, 211, 222, 1)
p.menu.SetColor(fg)
if selected {
p.menu.SetBackground(newRGBA(81, 154, 186, 1))
} else {
p.menu.SetBackground(newRGBA(14, 17, 18, 1))
}
2017-03-14 07:20:31 +00:00
p.menu.SetFont(editor.font.font)
2017-03-14 01:52:44 +00:00
p.menu.SetText(text)
2017-03-14 02:40:16 +00:00
2017-03-14 01:52:44 +00:00
p.menu.paddingLeft = 10
p.menu.paddingRight = 10
2017-03-14 02:40:16 +00:00
p.menu.paddingTop = 8
p.menu.paddingBottom = 8
p.kind.paddingLeft = 10
p.kind.paddingRight = 10
p.kind.paddingTop = 8
p.kind.paddingBottom = 8
2017-03-14 01:52:44 +00:00
}
func (p *PopupItem) setKind(kindText string, selected bool) {
2017-03-14 02:40:16 +00:00
color := newRGBA(151, 195, 120, 1)
bg := newRGBA(151, 195, 120, 0.2)
2017-03-14 01:52:44 +00:00
switch kindText {
case "function":
kindText = "f"
2017-03-14 02:40:16 +00:00
color = newRGBA(97, 174, 239, 1)
bg = newRGBA(97, 174, 239, 0.2)
case "func":
kindText = "f"
color = newRGBA(97, 174, 239, 1)
bg = newRGBA(97, 174, 239, 0.2)
2017-03-14 01:52:44 +00:00
default:
kindText = "b"
}
2017-03-14 02:40:16 +00:00
p.kind.SetColor(color)
p.kind.SetBackground(bg)
2017-03-14 07:20:31 +00:00
p.kind.SetFont(editor.font.font)
2017-03-14 01:52:44 +00:00
p.kind.SetText(kindText)
}
func (p *PopupItem) hide() {
ui.QueueMain(func() {
p.kind.span.Hide()
p.menu.span.Hide()
})
}