akiyosi.goneovim/popupmenu.go

294 lines
6.9 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-15 15:54:27 +01:00
widget *widgets.QWidget
layout *widgets.QGridLayout
items []*PopupItem
rawItems []interface{}
total int
showTotal int
selected int
hidden bool
top int
scrollBar *widgets.QWidget
scrollBarPos int
scrollBarHeight int
scrollCol *widgets.QWidget
x int
y int
2017-03-14 01:52:44 +00:00
}
// PopupItem is
type PopupItem struct {
2017-06-15 15:54:27 +01:00
kindLable *widgets.QLabel
kindText string
kindColor *RGBA
kindBg *RGBA
menuLable *widgets.QLabel
menuText string
menuTextRequest string
selected bool
selectedRequest bool
hidden 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)
2017-06-09 11:32:32 +01:00
scrollBar.SetStyleSheet("background-color: #3c3c3c;")
2017-06-07 15:52:17 +01:00
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()
2017-06-15 09:06:45 +01:00
popup := &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-06-15 09:06:45 +01:00
return popup
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-06-15 15:54:27 +01:00
func (p *PopupMenu) showItems(args []interface{}) {
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
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 {
2017-06-15 09:06:45 +01:00
popupItem.hide()
2017-03-14 01:52:44 +00:00
continue
}
item := items[i].([]interface{})
popupItem.setItem(item, selected == i)
2017-06-15 09:06:45 +01:00
popupItem.show()
2017-06-07 11:24:42 +01:00
}
2017-03-14 01:52:44 +00:00
2017-06-07 15:52:17 +01:00
if len(items) > p.showTotal {
2017-06-15 15:54:27 +01:00
p.scrollBarHeight = int(float64(p.showTotal) / float64(len(items)) * float64(itemHeight*p.showTotal))
2017-06-15 09:06:45 +01:00
p.scrollBarPos = 0
2017-06-16 08:43:05 +01:00
p.scrollBar.SetFixedHeight(p.scrollBarHeight)
p.scrollBar.Move2(0, p.scrollBarPos)
p.scrollCol.Show()
2017-06-07 15:52:17 +01:00
} else {
2017-06-16 08:43:05 +01:00
p.scrollCol.Hide()
2017-06-07 15:52:17 +01:00
}
2017-06-09 06:54:41 +01:00
2017-06-16 08:43:05 +01:00
p.widget.Move2(
2017-06-15 15:54:27 +01:00
int(float64(col)*editor.font.truewidth)-popupItems[0].kindLable.Width()-8,
(row+1)*editor.font.lineHeight,
)
p.show()
}
func (p *PopupMenu) show() {
2017-06-16 08:43:05 +01:00
p.widget.Show()
2017-06-15 15:54:27 +01:00
}
func (p *PopupMenu) hide() {
2017-06-16 08:43:05 +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 {
2017-06-15 09:06:45 +01:00
p.scroll(-p.top)
2017-06-07 11:24:42 +01:00
}
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-15 15:54:27 +01:00
popupItem.setSelected(selected == i+p.top)
2017-06-07 11:24:42 +01:00
}
}
func (p *PopupMenu) scroll(n int) {
2017-06-15 09:06:45 +01:00
// fmt.Println(len(p.rawItems), p.top, n)
2017-06-07 11:24:42 +01:00
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-15 09:06:45 +01:00
p.scrollBarPos = int((float64(p.top) / float64(len(items))) * float64(p.widget.Height()))
2017-06-16 08:43:05 +01:00
p.scrollBar.Move2(0, p.scrollBarPos)
2017-06-15 15:54:27 +01:00
p.hide()
p.show()
2017-03-14 01:52:44 +00:00
}
2017-06-15 15:54:27 +01:00
func (p *PopupItem) updateKind() {
p.kindLable.SetStyleSheet(fmt.Sprintf("background-color: %s; color: %s;", p.kindBg.String(), p.kindColor.String()))
p.kindLable.SetText(p.kindText)
}
2017-03-14 01:52:44 +00:00
2017-06-15 15:54:27 +01:00
func (p *PopupItem) updateMenu() {
if p.selected != p.selectedRequest {
p.selected = p.selectedRequest
2017-06-08 16:03:55 +01:00
if p.selected {
2017-06-15 15:54:27 +01:00
p.menuLable.SetStyleSheet(fmt.Sprintf("background-color: %s;", editor.selectedBg.String()))
} else {
2017-06-08 16:03:55 +01:00
p.menuLable.SetStyleSheet("")
}
2017-03-14 01:52:44 +00:00
}
2017-06-15 15:54:27 +01:00
if p.menuTextRequest != p.menuText {
p.menuText = p.menuTextRequest
p.menuLable.SetText(p.menuText)
}
}
func (p *PopupItem) setSelected(selected bool) {
p.selectedRequest = selected
2017-06-16 08:43:05 +01:00
p.updateMenu()
2017-06-15 15:54:27 +01:00
}
func (p *PopupItem) setItem(item []interface{}, selected bool) {
text := item[0].(string)
kindText := item[1].(string)
p.setKind(kindText, selected)
p.menuTextRequest = text
p.setSelected(selected)
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-15 15:54:27 +01:00
if kindText != p.kindText {
p.kindText = kindText
p.kindColor = color
p.kindBg = bg
2017-06-16 08:43:05 +01:00
p.updateKind()
2017-06-15 15:54:27 +01:00
}
2017-03-14 01:52:44 +00:00
}
func (p *PopupItem) hide() {
2017-06-15 09:06:45 +01:00
if p.hidden {
return
}
p.hidden = true
2017-06-16 08:43:05 +01:00
p.kindLable.Hide()
p.menuLable.Hide()
2017-06-15 09:06:45 +01:00
}
func (p *PopupItem) show() {
if !p.hidden {
return
}
p.hidden = false
2017-06-16 08:43:05 +01:00
p.kindLable.Show()
p.menuLable.Show()
2017-03-14 01:52:44 +00:00
}