akiyosi.goneovim/popupmenu.go

354 lines
8.6 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
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
items []*PopupItem
rawItems []interface{}
total int
showTotal int
selected int
hidden bool
top int
2017-06-07 15:52:17 +01:00
scrollBar *widgets.QWidget
scrollCol *widgets.QWidget
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
2017-06-08 16:03:55 +01:00
selected bool
2017-06-07 11:24:42 +01:00
}
2017-06-07 11:31:10 +01:00
func initPopupmenuNew(font *Font) *PopupMenu {
2017-06-07 11:24:42 +01:00
layout := widgets.NewQGridLayout2()
layout.SetSpacing(0)
layout.SetContentsMargins(0, 0, 0, 0)
2017-06-07 15:52:17 +01:00
scrollCol := widgets.NewQWidget(nil, 0)
scrollCol.SetContentsMargins(0, 0, 0, 0)
scrollCol.SetFixedWidth(5)
scrollBar := widgets.NewQWidget(scrollCol, 0)
scrollBar.SetFixedWidth(5)
scrollBar.SetStyleSheet("background-color: rgba(255,255,255,0.5);")
mainLayout := widgets.NewQHBoxLayout()
mainLayout.AddLayout(layout, 0)
mainLayout.AddWidget(scrollCol, 0, 0)
mainLayout.SetContentsMargins(0, 0, 0, 0)
mainLayout.SetSpacing(0)
2017-06-07 11:24:42 +01:00
widget := widgets.NewQWidget(nil, 0)
2017-06-07 15:52:17 +01:00
widget.SetLayout(mainLayout)
2017-06-07 11:24:42 +01:00
widget.SetContentsMargins(0, 0, 0, 0)
2017-06-08 16:03:55 +01:00
widget.SetStyleSheet("background-color: rgba(14, 17, 18, 1); color: rgba(205, 211, 222, 1);")
2017-06-07 11:24:42 +01:00
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)
2017-06-08 16:03:55 +01:00
kind.SetContentsMargins(8, 8, 8, 8)
2017-06-07 11:31:10 +01:00
kind.SetFont(font.fontNew)
2017-06-07 11:24:42 +01:00
menu := widgets.NewQLabel(nil, 0)
2017-06-08 16:03:55 +01:00
menu.SetContentsMargins(8, 8, 8, 8)
2017-06-07 11:31:10 +01:00
menu.SetFont(font.fontNew)
2017-06-07 11:24:42 +01:00
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{
2017-06-07 15:52:17 +01:00
widget: widget,
layout: layout,
items: popupItems,
total: max,
scrollBar: scrollBar,
scrollCol: scrollCol,
2017-06-07 11:24:42 +01:00
}
2017-03-14 01:52:44 +00:00
}
func initPopupmenu() *PopupMenu {
2017-06-08 02:35:58 +01:00
// total := 10
// box := ui.NewHorizontalBox()
// var popupItems []*PopupItem
// for i := 0; i < total; i++ {
// kindSpanHandler := &SpanHandler{}
// kindSpan := ui.NewArea(kindSpanHandler)
// kindSpanHandler.area = kindSpan
// menuSpanHandler := &SpanHandler{}
// menuSpan := ui.NewArea(menuSpanHandler)
// menuSpanHandler.area = menuSpan
// popupItem := &PopupItem{
// kind: kindSpanHandler,
// menu: menuSpanHandler,
// }
2017-03-14 01:52:44 +00:00
2017-06-08 02:35:58 +01:00
// popupItems = append(popupItems, popupItem)
// box.Append(kindSpan, false)
// box.Append(menuSpan, false)
// }
// box.SetShadow(0, 2, 0, 0, 0, 1, 4)
// box.Hide()
2017-03-14 01:52:44 +00:00
return &PopupMenu{
2017-06-08 02:35:58 +01:00
// box: box,
// items: popupItems,
// total: total,
2017-03-14 01:52:44 +00:00
}
}
2017-06-07 11:31:10 +01:00
func (p *PopupMenu) updateFont(font *Font) {
for i := 0; i < p.total; i++ {
popupItem := p.items[i]
popupItem.kindLable.SetFont(font.fontNew)
popupItem.menuLable.SetFont(font.fontNew)
}
}
2017-03-14 01:52:44 +00:00
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
2017-06-07 15:52:17 +01:00
// itemHeightReal := popupItems[0].menuLable.Height()
// if itemHeightReal < itemHeight {
// itemHeight = itemHeightReal
// }
// fmt.Println(itemHeight, itemHeightReal)
heightLeft := editor.screen.height - (row+1)*editor.font.lineHeight
2017-06-07 11:24:42 +01:00
total := heightLeft / itemHeight
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(
2017-06-08 16:03:55 +01:00
int(float64(col)*editor.font.truewidth)-popupItems[0].kindLable.Width()-8,
2017-06-07 11:24:42 +01:00
(row+1)*editor.font.lineHeight,
)
p.widget.Show()
2017-03-14 02:40:16 +00:00
2017-06-07 15:52:17 +01:00
if len(items) > p.showTotal {
p.scrollBar.SetFixedHeight(int(float64(p.showTotal) / float64(len(items)) * float64(itemHeight*p.showTotal)))
p.scrollBar.Move2(0, 0)
p.scrollCol.Show()
} else {
p.scrollCol.Hide()
}
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)
}
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
if selected == i+p.top {
2017-06-08 16:03:55 +01:00
if !popupItem.selected {
popupItem.selected = true
bg := editor.selectedBg
popupItem.menuLable.SetStyleSheet(fmt.Sprintf("background-color: %s;", bg.String()))
}
} else {
if popupItem.selected {
popupItem.selected = false
popupItem.menuLable.SetStyleSheet("")
}
2017-03-14 01:52:44 +00:00
}
2017-06-07 11:24:42 +01:00
}
}
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 15:52:17 +01:00
p.scrollBar.Move2(0, int((float64(p.top)/float64(len(items)))*float64(p.widget.Height())))
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)
2017-06-08 16:03:55 +01:00
// fg := newRGBA(205, 211, 222, 1)
// bg := newRGBA(14, 17, 18, 1)
2017-03-14 01:52:44 +00:00
if selected {
2017-06-08 16:03:55 +01:00
if !p.selected {
p.selected = true
bg := editor.selectedBg
p.menuLable.SetStyleSheet(fmt.Sprintf("background-color: %s;", bg.String()))
}
} else {
if p.selected {
p.selected = false
p.menuLable.SetStyleSheet("")
}
2017-03-14 01:52:44 +00:00
}
2017-06-07 11:24:42 +01:00
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.SetText(kindText)
2017-03-14 01:52:44 +00:00
}
func (p *PopupItem) hide() {
2017-06-08 02:35:58 +01:00
// ui.QueueMain(func() {
// p.kind.area.Hide()
// p.menu.area.Hide()
// })
2017-03-14 01:52:44 +00:00
}