Skip to content

Commit e64cd0d

Browse files
committed
Add support gui_widgets
1 parent de65d2d commit e64cd0d

File tree

7 files changed

+628
-49
lines changed

7 files changed

+628
-49
lines changed

editor/extmarks.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_extmark", grid, win, ns_id, mark_id, row, col]
10+
// Updates the position of an extmark which is currently visible in a
11+
// window. Only emitted if the mark has the `ui_watched` attribute.
12+
func (ws *Workspace) windowExtmarks(args []interface{}) {
13+
for _, e := range args {
14+
arg := e.([]interface{})
15+
grid := util.ReflectToInt(arg[0])
16+
winid := (arg[1]).(nvim.Window)
17+
_ = util.ReflectToInt(arg[2])
18+
markid := util.ReflectToInt(arg[3])
19+
row := util.ReflectToInt(arg[4])
20+
col := util.ReflectToInt(arg[5])
21+
22+
win, ok := ws.screen.getWindow(grid)
23+
if !ok {
24+
return
25+
}
26+
27+
var gw *Guiwidget
28+
ws.guiWidgets.Range(func(_, gITF interface{}) bool {
29+
g := gITF.(*Guiwidget)
30+
if g == nil {
31+
return true
32+
}
33+
if markid == g.markID && g.winid == winid {
34+
gw = g
35+
return false
36+
}
37+
return true
38+
})
39+
if gw == nil {
40+
continue
41+
}
42+
43+
win.storeGuiwidget(gw.id, gw)
44+
cell := win.content[row][col]
45+
if cell != nil {
46+
cell.decal = &Decal{
47+
exists: true,
48+
markid: markid,
49+
}
50+
}
51+
}
52+
}

editor/guiwidget.go

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

editor/screen.go

Lines changed: 5 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,6 +1263,9 @@ 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

editor/screen_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ func TestWindow_updateLine(t *testing.T) {
219219
},
220220
},
221221
[]Cell{
222-
Cell{hldef[7], "~", true},
223-
Cell{hldef[7], " ", true},
224-
Cell{hldef[7], " ", true},
225-
Cell{hldef[7], " ", true},
226-
Cell{hldef[7], " ", true},
222+
Cell{hldef[7], "~", true, nil},
223+
Cell{hldef[7], " ", true, nil},
224+
Cell{hldef[7], " ", true, nil},
225+
Cell{hldef[7], " ", true, nil},
226+
Cell{hldef[7], " ", true, nil},
227227
},
228228
},
229229
{
@@ -243,11 +243,11 @@ func TestWindow_updateLine(t *testing.T) {
243243
},
244244
},
245245
[]Cell{
246-
Cell{hldef[7], "~", true},
247-
Cell{hldef[7], " ", true},
248-
Cell{hldef[7], " ", true},
249-
Cell{hldef[6], "*", true},
250-
Cell{hldef[6], "*", true},
246+
Cell{hldef[7], "~", true, nil},
247+
Cell{hldef[7], " ", true, nil},
248+
Cell{hldef[7], " ", true, nil},
249+
Cell{hldef[6], "*", true, nil},
250+
Cell{hldef[6], "*", true, nil},
251251
},
252252
},
253253
{
@@ -270,11 +270,11 @@ func TestWindow_updateLine(t *testing.T) {
270270
},
271271
},
272272
[]Cell{
273-
Cell{hldef[7], "~", true},
274-
Cell{hldef[6], "@", true},
275-
Cell{hldef[6], "v", true},
276-
Cell{hldef[6], "i", true},
277-
Cell{hldef[6], "m", true},
273+
Cell{hldef[7], "~", true, nil},
274+
Cell{hldef[6], "@", true, nil},
275+
Cell{hldef[6], "v", true, nil},
276+
Cell{hldef[6], "i", true, nil},
277+
Cell{hldef[6], "m", true, nil},
278278
},
279279
},
280280
{
@@ -295,11 +295,11 @@ func TestWindow_updateLine(t *testing.T) {
295295
},
296296
},
297297
[]Cell{
298-
Cell{hldef[7], " ", true},
299-
Cell{hldef[7], " ", true},
300-
Cell{hldef[7], "J", true},
301-
Cell{hldef[6], "i", true},
302-
Cell{hldef[6], "m", true},
298+
Cell{hldef[7], " ", true, nil},
299+
Cell{hldef[7], " ", true, nil},
300+
Cell{hldef[7], "J", true, nil},
301+
Cell{hldef[6], "i", true, nil},
302+
Cell{hldef[6], "m", true, nil},
303303
},
304304
},
305305
}

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)