akiyosi.goneovim/popupmenu.go

312 lines
7.3 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 (
2017-06-07 11:24:42 +01:00
"fmt"
2017-03-14 02:40:16 +00:00
"github.com/dzhou121/ui"
2017-06-07 11:24:42 +01:00
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
2017-03-14 02:40:16 +00:00
)
2017-03-14 01:52:44 +00:00
// PopupMenu is the popupmenu
type PopupMenu struct {
2017-06-07 11:24:42 +01:00
widget *widgets.QWidget
layout *widgets.QGridLayout
box *ui.Box
items []*PopupItem
rawItems []interface{}
total int
showTotal int
selected int
hidden bool
top int
2017-03-14 01:52:44 +00:00
}
// PopupItem is
type PopupItem struct {
2017-06-07 11:24:42 +01:00
kindLable *widgets.QLabel
menuLable *widgets.QLabel
kind *SpanHandler
menu *SpanHandler
}
func initPopupmenuNew() *PopupMenu {
layout := widgets.NewQGridLayout2()
layout.SetSpacing(0)
layout.SetContentsMargins(0, 0, 0, 0)
widget := widgets.NewQWidget(nil, 0)
widget.SetLayout(layout)
widget.SetContentsMargins(0, 0, 0, 0)
widget.SetStyleSheet("background-color: rgba(14, 17, 18, 1);")
shadow := widgets.NewQGraphicsDropShadowEffect(nil)
shadow.SetBlurRadius(20)
shadow.SetColor(gui.NewQColor3(0, 0, 0, 255))
shadow.SetOffset3(0, 2)
widget.SetGraphicsEffect(shadow)
max := 15
var popupItems []*PopupItem
for i := 0; i < max; i++ {
kind := widgets.NewQLabel(nil, 0)
kind.SetContentsMargins(10, 10, 10, 10)
menu := widgets.NewQLabel(nil, 0)
menu.SetContentsMargins(10, 10, 10, 10)
layout.AddWidget(kind, i, 0, 0)
layout.AddWidget(menu, i, 1, 0)
popupItem := &PopupItem{
kindLable: kind,
menuLable: menu,
}
popupItems = append(popupItems, popupItem)
}
widget.Hide()
return &PopupMenu{
widget: widget,
layout: layout,
items: popupItems,
total: max,
}
2017-03-14 01:52:44 +00:00
}
func initPopupmenu() *PopupMenu {
total := 10
box := ui.NewHorizontalBox()
var popupItems []*PopupItem
for i := 0; i < total; i++ {
kindSpanHandler := &SpanHandler{}
kindSpan := ui.NewArea(kindSpanHandler)
2017-05-10 07:28:44 +01:00
kindSpanHandler.area = kindSpan
2017-03-14 01:52:44 +00:00
menuSpanHandler := &SpanHandler{}
menuSpan := ui.NewArea(menuSpanHandler)
2017-05-10 07:28:44 +01:00
menuSpanHandler.area = menuSpan
2017-03-14 01:52:44 +00:00
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])
2017-06-07 11:24:42 +01:00
p.rawItems = items
p.selected = selected
p.top = 0
2017-03-14 01:52:44 +00:00
popupItems := p.items
2017-06-07 11:24:42 +01:00
itemHeight := editor.font.height + 20
itemHeightReal := popupItems[0].menuLable.Height()
if itemHeightReal < itemHeight {
itemHeight = itemHeightReal
}
heightLeft := editor.screen.height - row*editor.font.lineHeight
total := heightLeft / itemHeight
fmt.Println(editor.screen.height, itemHeight, heightLeft)
if total < p.total {
p.showTotal = total
} else {
p.showTotal = p.total
}
for i := 0; i < p.total; i++ {
2017-03-14 01:52:44 +00:00
popupItem := popupItems[i]
2017-06-07 11:24:42 +01:00
if i >= len(items) || i >= total {
popupItem.kindLable.Hide()
popupItem.menuLable.Hide()
2017-03-14 01:52:44 +00:00
continue
}
item := items[i].([]interface{})
popupItem.setItem(item, selected == i)
2017-06-07 11:24:42 +01:00
popupItem.kindLable.Show()
popupItem.menuLable.Show()
}
2017-03-14 01:52:44 +00:00
2017-06-07 11:24:42 +01:00
p.widget.Move2(
int(float64(col)*editor.font.truewidth)-popupItems[0].kindLable.Width()-10,
(row+1)*editor.font.lineHeight,
)
p.widget.Show()
2017-03-14 02:40:16 +00:00
2017-06-07 11:24:42 +01:00
// popupItems := p.items
// i := 0
// kindWidth := 0
// menuWidthMax := 0
// heightSum := 0
// height := 0
// for i = 0; i < p.total; i++ {
// popupItem := popupItems[i]
// if i >= len(items) {
// popupItem.hide()
// continue
// }
2017-03-14 01:52:44 +00:00
2017-06-07 11:24:42 +01:00
// item := items[i].([]interface{})
// popupItem.setItem(item, selected == i)
2017-03-14 01:52:44 +00:00
2017-06-07 11:24:42 +01:00
// var menuWidth int
// menuWidth, height = popupItem.menu.getSize()
// kindWidth, height = popupItem.kind.getSize()
// if menuWidth > menuWidthMax {
// menuWidthMax = menuWidth
// }
// y := heightSum
// heightSum += height
// ui.QueueMain(func() {
// popupItem.kind.area.SetPosition(0, y)
// popupItem.menu.area.SetPosition(kindWidth, y)
// })
// }
// for i = 0; i < p.total; i++ {
// if i >= len(items) {
// continue
// }
// popupItem := popupItems[i]
// ui.QueueMain(func() {
// popupItem.kind.area.SetSize(kindWidth, height)
// popupItem.kind.area.Show()
// popupItem.kind.area.QueueRedrawAll()
// popupItem.menu.area.SetSize(menuWidthMax, height)
// popupItem.menu.area.Show()
// popupItem.menu.area.QueueRedrawAll()
// })
// }
// ui.QueueMain(func() {
// p.box.SetPosition(
// int(float64(col)*editor.font.truewidth)-kindWidth-p.items[0].menu.paddingLeft,
// (row+1)*editor.font.lineHeight,
// )
// p.box.SetSize(menuWidthMax+kindWidth, heightSum)
// p.box.Show()
// })
2017-03-14 01:52:44 +00:00
}
func (p *PopupMenu) hide(args []interface{}) {
2017-03-14 02:40:16 +00:00
p.hidden = true
2017-06-07 11:24:42 +01:00
p.widget.Hide()
2017-03-14 01:52:44 +00:00
}
func (p *PopupMenu) selectItem(args []interface{}) {
selected := reflectToInt(args[0].([]interface{})[0])
2017-06-07 11:24:42 +01:00
if selected == -1 {
p.top = 0
}
if selected-p.top >= p.showTotal {
p.scroll(selected - p.top - p.showTotal + 1)
}
if selected >= 0 && selected-p.top < 0 {
p.scroll(-1)
}
fg := newRGBA(205, 211, 222, 1)
for i := 0; i < p.showTotal; i++ {
2017-03-14 01:52:44 +00:00
popupItem := p.items[i]
2017-06-07 11:24:42 +01:00
bg := newRGBA(14, 17, 18, 1)
if selected == i+p.top {
bg = editor.selectedBg
2017-03-14 01:52:44 +00:00
}
2017-06-07 11:24:42 +01:00
popupItem.menuLable.SetStyleSheet(fmt.Sprintf("background-color: %s; color: %s;", bg.String(), fg.String()))
}
}
func (p *PopupMenu) scroll(n int) {
p.top += n
items := p.rawItems
popupItems := p.items
for i := 0; i < p.showTotal; i++ {
popupItem := popupItems[i]
item := items[i+p.top].([]interface{})
popupItem.setItem(item, false)
2017-03-14 01:52:44 +00:00
}
2017-06-07 11:24:42 +01:00
p.widget.Hide()
p.widget.Show()
2017-03-14 01:52:44 +00:00
}
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)
2017-06-07 11:24:42 +01:00
bg := newRGBA(14, 17, 18, 1)
2017-03-14 01:52:44 +00:00
if selected {
2017-06-07 11:24:42 +01:00
bg = editor.selectedBg
2017-03-14 01:52:44 +00:00
}
2017-06-07 11:24:42 +01:00
p.menuLable.SetStyleSheet(fmt.Sprintf("background-color: %s; color: %s;", bg.String(), fg.String()))
p.menuLable.SetFont(editor.font.fontNew)
p.menuLable.SetText(text)
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 {
2017-03-14 07:37:15 +00:00
case "function", "func":
2017-03-14 02:40:16 +00:00
kindText = "f"
color = newRGBA(97, 174, 239, 1)
bg = newRGBA(97, 174, 239, 0.2)
2017-03-14 07:37:15 +00:00
case "var", "statement", "instance", "param", "import":
kindText = "v"
color = newRGBA(223, 106, 115, 1)
bg = newRGBA(223, 106, 115, 0.2)
case "const":
kindText = "c"
color = newRGBA(223, 106, 115, 1)
bg = newRGBA(223, 106, 115, 0.2)
case "class":
kindText = "c"
color = newRGBA(229, 193, 124, 1)
bg = newRGBA(229, 193, 124, 0.2)
case "type":
kindText = "t"
color = newRGBA(229, 193, 124, 1)
bg = newRGBA(229, 193, 124, 0.2)
case "module":
kindText = "m"
color = newRGBA(42, 161, 152, 1)
bg = newRGBA(42, 161, 152, 0.2)
case "keyword":
kindText = "k"
color = newRGBA(42, 161, 152, 1)
bg = newRGBA(42, 161, 152, 0.2)
case "package":
kindText = "p"
color = newRGBA(42, 161, 152, 1)
bg = newRGBA(42, 161, 152, 0.2)
2017-03-14 01:52:44 +00:00
default:
kindText = "b"
}
2017-06-07 11:24:42 +01:00
p.kindLable.SetStyleSheet(fmt.Sprintf("background-color: %s; color: %s;", bg.String(), color.String()))
p.kindLable.SetFont(editor.font.fontNew)
p.kindLable.SetText(kindText)
2017-03-14 01:52:44 +00:00
}
func (p *PopupItem) hide() {
ui.QueueMain(func() {
2017-05-10 07:28:44 +01:00
p.kind.area.Hide()
p.menu.area.Hide()
2017-03-14 01:52:44 +00:00
})
}