Skip to content

Commit

Permalink
Add support gui_widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
akiyosi committed Sep 23, 2022
1 parent eccdce4 commit a6e1d3a
Show file tree
Hide file tree
Showing 7 changed files with 628 additions and 33 deletions.
52 changes: 52 additions & 0 deletions editor/extmarks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package editor

import (
"github.com/akiyosi/goneovim/util"
"github.com/neovim/go-client/nvim"
)

// windowExtmarks is
// ["win_extmark", grid, win, ns_id, mark_id, row, col]
// Updates the position of an extmark which is currently visible in a
// window. Only emitted if the mark has the `ui_watched` attribute.
func (ws *Workspace) windowExtmarks(args []interface{}) {
for _, e := range args {
arg := e.([]interface{})
grid := util.ReflectToInt(arg[0])
winid := (arg[1]).(nvim.Window)
_ = util.ReflectToInt(arg[2])
markid := util.ReflectToInt(arg[3])
row := util.ReflectToInt(arg[4])
col := util.ReflectToInt(arg[5])

win, ok := ws.screen.getWindow(grid)
if !ok {
return
}

var gw *Guiwidget
ws.guiWidgets.Range(func(_, gITF interface{}) bool {
g := gITF.(*Guiwidget)
if g == nil {
return true
}
if markid == g.markID && g.winid == winid {
gw = g
return false
}
return true
})
if gw == nil {
continue
}

win.storeGuiwidget(gw.id, gw)
cell := win.content[row][col]
if cell != nil {
cell.decal = &Decal{
exists: true,
markid: markid,
}
}
}
}
163 changes: 163 additions & 0 deletions editor/guiwidget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package editor

import (
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"strconv"
"time"

"github.com/akiyosi/goneovim/util"
"github.com/neovim/go-client/nvim"
"github.com/therecipe/qt/core"
)

type Guiwidget struct {
Tooltip

raw string
data *core.QByteArray
mime string
width int
height int
id int
markID int
winid nvim.Window
}

func initGuiwidget() *Guiwidget {
guiwidget := NewGuiwidget(nil, 0)
guiwidget.data = nil
guiwidget.width = 0
guiwidget.height = 0
guiwidget.mime = ""
guiwidget.id = 0
guiwidget.ConnectPaintEvent(guiwidget.paint)

return guiwidget
}

// GuiWidgetPut pushes visual resource data to the front-end
func (w *Workspace) handleRPCGuiwidgetput(updates []interface{}) {
for _, update := range updates {
a := update.(map[string]interface{})

idITF, ok := a["id"]
if !ok {
continue
}
id := util.ReflectToInt(idITF)

var g *Guiwidget
if g, ok = w.getGuiwidgetFromResID(id); !ok {
g = initGuiwidget()
w.storeGuiwidget(id, g)
g.s = w.screen
}

g.id = id

mime, ok := a["mime"]
if ok {
g.mime = mime.(string)
}

data, ok := a["data"]
if ok {
s := data.(string)

switch mime {
case "text/plain":
g.text = s

case "image/svg",
"image/svg+xml",
"image/png",
"image/gif",
"image/jpeg",
"image/*":
g.raw = s
default:
}
}
}
}

// GuiWidgetUpdateView sends a list of "placements".
// A placement associates an extmark with a resource id, and provides
// display options for a widget (width, height, mouse events etc.).
func (w *Workspace) handleRPCGuiwidgetview(updates []interface{}) {
var markid, resid, width, height int
for _, update := range updates {
a := update.(map[string]interface{})

buf, ok := a["buf"]
if !ok {
continue
}

errChan := make(chan error, 60)
var err error
var outstr string
go func() {
outstr, err = w.nvim.CommandOutput(
fmt.Sprintf("echo bufwinid(%d)", util.ReflectToInt(buf)),
)
errChan <- err
}()
select {
case <-errChan:
case <-time.After(40 * time.Millisecond):
}

out, _ := strconv.Atoi(outstr)
winid := (nvim.Window)(out)

widgets, ok := a["widgets"]
if !ok {
continue
}
for _, e := range widgets.([]interface{}) {
for k, ee := range e.([]interface{}) {
if k == 0 {
markid = util.ReflectToInt(ee)
} else if k == 1 {
resid = util.ReflectToInt(ee)
} else if k == 2 {
width = util.ReflectToInt(ee)
} else if k == 3 {
height = util.ReflectToInt(ee)
}
if k >= 4 {
}
}

var g *Guiwidget
if g, ok = w.getGuiwidgetFromResID(resid); !ok {
g = initGuiwidget()
w.storeGuiwidget(resid, g)
g.s = w.screen
}

g.winid = winid
g.markID = markid
g.width = width
g.height = height
switch g.mime {
case "text/plain":
baseFont := g.s.ws.font
g.font = initFontNew(
baseFont.fontNew.Family(),
float64(g.height*baseFont.height)*0.8,
0,
0,
)
default:
}

}

}

}
5 changes: 5 additions & 0 deletions editor/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,8 @@ func (s *Screen) gridScroll(args []interface{}) {
}

func (s *Screen) update() {
// shownMarks := []int{}

s.windows.Range(func(grid, winITF interface{}) bool {
win := winITF.(*Window)
// if grid is dirty, we remove this grid
Expand All @@ -1264,6 +1266,9 @@ func (s *Screen) update() {
win.fill()
}
win.update()

// shownMarksWin := win.updateExtMarks()
// shownMarks = append(shownMarks, shownMarksWin...)
}

return true
Expand Down
40 changes: 20 additions & 20 deletions editor/screen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ func TestWindow_updateLine(t *testing.T) {
},
},
[]Cell{
Cell{hldef[7], "~", true},
Cell{hldef[7], " ", true},
Cell{hldef[7], " ", true},
Cell{hldef[7], " ", true},
Cell{hldef[7], " ", true},
Cell{hldef[7], "~", true, nil},
Cell{hldef[7], " ", true, nil},
Cell{hldef[7], " ", true, nil},
Cell{hldef[7], " ", true, nil},
Cell{hldef[7], " ", true, nil},
},
},
{
Expand All @@ -248,11 +248,11 @@ func TestWindow_updateLine(t *testing.T) {
},
},
[]Cell{
Cell{hldef[7], "~", true},
Cell{hldef[7], " ", true},
Cell{hldef[7], " ", true},
Cell{hldef[6], "*", true},
Cell{hldef[6], "*", true},
Cell{hldef[7], "~", true, nil},
Cell{hldef[7], " ", true, nil},
Cell{hldef[7], " ", true, nil},
Cell{hldef[6], "*", true, nil},
Cell{hldef[6], "*", true, nil},
},
},
{
Expand All @@ -276,11 +276,11 @@ func TestWindow_updateLine(t *testing.T) {
},
},
[]Cell{
Cell{hldef[7], "~", true},
Cell{hldef[6], "@", true},
Cell{hldef[6], "v", true},
Cell{hldef[6], "i", true},
Cell{hldef[6], "m", true},
Cell{hldef[7], "~", true, nil},
Cell{hldef[6], "@", true, nil},
Cell{hldef[6], "v", true, nil},
Cell{hldef[6], "i", true, nil},
Cell{hldef[6], "m", true, nil},
},
},
{
Expand All @@ -302,11 +302,11 @@ func TestWindow_updateLine(t *testing.T) {
},
},
[]Cell{
Cell{hldef[7], " ", true},
Cell{hldef[7], " ", true},
Cell{hldef[7], "J", true},
Cell{hldef[6], "i", true},
Cell{hldef[6], "m", true},
Cell{hldef[7], " ", true, nil},
Cell{hldef[7], " ", true, nil},
Cell{hldef[7], "J", true, nil},
Cell{hldef[6], "i", true, nil},
Cell{hldef[6], "m", true, nil},
},
},
}
Expand Down
15 changes: 15 additions & 0 deletions editor/tooltip.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (t *Tooltip) drawForeground(p *gui.QPainter, f func(*gui.QPainter), g func(
}

func (t *Tooltip) setQpainterFont(p *gui.QPainter) {
if p == nil {
return
}
if t.font == nil {
return
}
p.SetFont(t.font.fontNew)
}

Expand Down Expand Up @@ -131,11 +137,20 @@ func (t *Tooltip) update() {
tooltipWidth += w
}

font := t.font
if font == nil {
font = t.s.ws.font
}

// update widget size
t.SetFixedSize2(
int(tooltipWidth),
t.font.lineHeight,
)

t.SetAutoFillBackground(true)
p := gui.NewQPalette()
p.SetColor2(gui.QPalette__Background, t.s.ws.background.QColor())
t.SetPalette(p)
t.Update()
}
Loading

0 comments on commit a6e1d3a

Please sign in to comment.