Skip to content

Commit f608b1c

Browse files
committed
Add support gui_widgets
1 parent de65d2d commit f608b1c

File tree

6 files changed

+549
-29
lines changed

6 files changed

+549
-29
lines changed

editor/extmarks.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package editor
2+
3+
import (
4+
"github.com/akiyosi/goneovim/util"
5+
"github.com/neovim/go-client/nvim"
6+
)
7+
8+
// windowExtmarks is
9+
// ["win_extmarks", win, ns_id, mark_id, row, col]
10+
// Updates the position of an extmark which is currently visible in a
11+
// window. Only emitted after the client calls `nvim_ui_watch_extmark` to
12+
// watch for a specific namespace `ns_id`. `row` and `col` are relative
13+
// to the window.
14+
func (ws *Workspace) windowExtmarks(args []interface{}) {
15+
for _, e := range args {
16+
arg := e.([]interface{})
17+
grid := util.ReflectToInt(arg[0])
18+
_ = (arg[1]).(nvim.Window)
19+
_ = util.ReflectToInt(arg[2])
20+
markID := util.ReflectToInt(arg[3])
21+
row := util.ReflectToInt(arg[4])
22+
col := util.ReflectToInt(arg[5])
23+
24+
win, ok := ws.screen.getWindow(grid)
25+
if !ok {
26+
return
27+
}
28+
29+
g := ws.getGuiwidgetFromMarkID(markID)
30+
if g == nil {
31+
continue
32+
}
33+
cell := win.content[row][col]
34+
if cell != nil {
35+
cell.decal = &Decal{
36+
exists: true,
37+
markid: markID,
38+
}
39+
}
40+
}
41+
}

editor/guiwidget.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package editor
2+
3+
import (
4+
_ "image/gif"
5+
_ "image/jpeg"
6+
_ "image/png"
7+
8+
"github.com/akiyosi/goneovim/util"
9+
"github.com/therecipe/qt/core"
10+
)
11+
12+
type Guiwidget struct {
13+
Tooltip
14+
15+
raw string
16+
data *core.QByteArray
17+
mime string
18+
width int
19+
height int
20+
id int
21+
markID int
22+
}
23+
24+
func initGuiwidget() *Guiwidget {
25+
guiwidget := NewGuiwidget(nil, 0)
26+
guiwidget.data = nil
27+
guiwidget.width = 0
28+
guiwidget.height = 0
29+
guiwidget.mime = ""
30+
guiwidget.id = 0
31+
guiwidget.ConnectPaintEvent(guiwidget.paint)
32+
33+
return guiwidget
34+
}
35+
36+
// GuiWidgetPut pushes visual resource data to the front-end
37+
func (w *Workspace) handleRPCGuiwidgetput(updates []interface{}) {
38+
for _, update := range updates {
39+
a := update.(map[string]interface{})
40+
41+
idITF, ok := a["id"]
42+
if !ok {
43+
continue
44+
}
45+
id := util.ReflectToInt(idITF)
46+
47+
var g *Guiwidget
48+
if g, ok = w.getGuiwidgetFromResID(id); !ok {
49+
g = initGuiwidget()
50+
w.storeGuiwidget(id, g)
51+
g.s = w.screen
52+
}
53+
54+
g.id = id
55+
56+
mime, ok := a["mime"]
57+
if ok {
58+
g.mime = mime.(string)
59+
}
60+
61+
data, ok := a["data"]
62+
if ok {
63+
s := data.(string)
64+
65+
switch mime {
66+
case "text/plain":
67+
g.text = s
68+
69+
case "image/svg",
70+
"image/svg+xml",
71+
"image/png",
72+
"image/gif",
73+
"image/jpeg",
74+
"image/*":
75+
g.raw = s
76+
default:
77+
}
78+
}
79+
}
80+
}
81+
82+
// GuiWidgetUpdateView sends a list of "placements".
83+
// A placement associates an extmark with a resource id, and provides
84+
// display options for a widget (width, height, mouse events etc.).
85+
func (w *Workspace) handleRPCGuiwidgetview(updates []interface{}) {
86+
var markid, resid, width, height int
87+
for _, update := range updates {
88+
a := update.(map[string]interface{})
89+
90+
_, ok := a["buf"]
91+
if !ok {
92+
continue
93+
}
94+
95+
widgets, ok := a["widgets"]
96+
if !ok {
97+
continue
98+
}
99+
for _, e := range widgets.([]interface{}) {
100+
for k, ee := range e.([]interface{}) {
101+
if k == 0 {
102+
markid = util.ReflectToInt(ee)
103+
} else if k == 1 {
104+
resid = util.ReflectToInt(ee)
105+
} else if k == 2 {
106+
width = util.ReflectToInt(ee)
107+
} else if k == 3 {
108+
height = util.ReflectToInt(ee)
109+
}
110+
if k >= 4 {
111+
}
112+
}
113+
114+
var g *Guiwidget
115+
if g, ok = w.getGuiwidgetFromResID(resid); !ok {
116+
g = initGuiwidget()
117+
w.storeGuiwidget(resid, g)
118+
g.s = w.screen
119+
}
120+
121+
g.markID = markid
122+
g.width = width
123+
g.height = height
124+
switch g.mime {
125+
case "text/plain":
126+
baseFont := g.s.ws.font
127+
g.font = initFontNew(
128+
baseFont.fontNew.Family(),
129+
float64(g.height*baseFont.height)*0.8,
130+
0,
131+
0,
132+
)
133+
default:
134+
}
135+
136+
}
137+
138+
}
139+
140+
}

editor/screen.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,8 @@ func (s *Screen) gridScroll(args []interface{}) {
12371237
}
12381238

12391239
func (s *Screen) update() {
1240+
// shownMarks := []int{}
1241+
12401242
s.windows.Range(func(grid, winITF interface{}) bool {
12411243
win := winITF.(*Window)
12421244
// if grid is dirty, we remove this grid
@@ -1261,10 +1263,25 @@ func (s *Screen) update() {
12611263
win.fill()
12621264
}
12631265
win.update()
1266+
1267+
// shownMarksWin := win.updateExtMarks()
1268+
// shownMarks = append(shownMarks, shownMarksWin...)
12641269
}
12651270

12661271
return true
12671272
})
1273+
1274+
// for _, res := range s.ws.guiWidgets {
1275+
// shown := false
1276+
// for _, id := range shownMarks {
1277+
// if res.markID == id {
1278+
// shown = true
1279+
// }
1280+
// }
1281+
// if !shown {
1282+
// res.Hide()
1283+
// }
1284+
// }
12681285
}
12691286

12701287
func (s *Screen) runeTextWidth(font *Font, text string) float64 {

editor/tooltip.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ func (t *Tooltip) drawForeground(p *gui.QPainter, f func(*gui.QPainter), g func(
5959
}
6060

6161
func (t *Tooltip) setQpainterFont(p *gui.QPainter) {
62+
if p == nil {
63+
return
64+
}
65+
if t.font == nil {
66+
return
67+
}
6268
p.SetFont(t.font.fontNew)
6369
}
6470

@@ -131,11 +137,20 @@ func (t *Tooltip) update() {
131137
tooltipWidth += w
132138
}
133139

140+
font := t.font
141+
if font == nil {
142+
font = t.s.ws.font
143+
}
144+
134145
// update widget size
135146
t.SetFixedSize2(
136147
int(tooltipWidth),
137148
t.font.lineHeight,
138149
)
139150

151+
t.SetAutoFillBackground(true)
152+
p := gui.NewQPalette()
153+
p.SetColor2(gui.QPalette__Background, t.s.ws.background.QColor())
154+
t.SetPalette(p)
140155
t.Update()
141156
}

0 commit comments

Comments
 (0)