Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
akiyosi committed Jan 6, 2023
1 parent 30ebd31 commit e080f47
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 24 deletions.
10 changes: 6 additions & 4 deletions editor/guiwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Guiwidget struct {
winid nvim.Window
}

func initGuiwidget() *Guiwidget {
func newGuiwidget() *Guiwidget {
guiwidget := NewGuiwidget(nil, 0)
guiwidget.data = nil
guiwidget.width = 0
Expand All @@ -53,9 +53,10 @@ func (w *Workspace) handleRPCGuiwidgetput(updates []interface{}) {

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

g.id = id
Expand All @@ -71,7 +72,7 @@ func (w *Workspace) handleRPCGuiwidgetput(updates []interface{}) {

switch mime {
case "text/plain":
g.text = s
g.updateText(g.s.hlAttrDef[0], s)

case "image/svg",
"image/svg+xml",
Expand Down Expand Up @@ -140,9 +141,10 @@ func (w *Workspace) handleRPCGuiwidgetview(updates []interface{}) {

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

g.winid = winid
Expand Down
106 changes: 86 additions & 20 deletions editor/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,7 @@ func (w *Window) drawExtmarks(p *gui.QPainter, y int, col int, cols int) {
height := res.height * font.lineHeight

if res.mime == "text/plain" {
res.updateText(res.text)
// res.updateText(w.s.hlAttrDef[0], res.text)
p.FillRect5(
int(float64(x)*font.cellwidth),
y*font.lineHeight+scrollPixels,
Expand All @@ -2690,7 +2690,6 @@ func (w *Window) drawExtmarks(p *gui.QPainter, y int, col int, cols int) {
height,
p,
res.setQpainterFont,
res.getNthWidthAndShift,
w.devicePixelRatio,
)
}
Expand All @@ -2703,31 +2702,20 @@ func (w *Window) drawExtmarks(p *gui.QPainter, y int, col int, cols int) {
}
}

func (g *Guiwidget) drawExtmark(x, y, width, height int, p *gui.QPainter, f func(*gui.QPainter), h func(int) (float64, int), devicePixelRatio float64) {
func (g *Guiwidget) drawExtmark(x, y, width, height int, p *gui.QPainter, f func(*gui.QPainter), devicePixelRatio float64) {
f(p)

p.SetPen2(g.s.ws.foreground.QColor())

switch g.mime {
case "text/plain":
if g.text != "" {
r := []rune(g.text)
var pos float64
for k := 0; k < len(r); k++ {

width, shift := h(k)
pos += width

p.DrawText(
core.NewQPointF3(
float64(x)+pos,
float64(y+shift),
),
string(r[k]),
)
g.drawText(
float64(x),
float64(y),
float64(width),
float64(height),
p, g.setQpainterFont)

}
}
case "image/svg":
g.data = core.NewQByteArray2(g.raw, len(g.raw))

Expand Down Expand Up @@ -2784,6 +2772,84 @@ func (g *Guiwidget) drawExtmark(x, y, width, height int, p *gui.QPainter, f func
}
}

func (g *Guiwidget) drawText(x, y, width, height float64, p *gui.QPainter, f func(*gui.QPainter)) {
f(p)
font := p.Font()

p.SetPen2(g.s.ws.foreground.QColor())

if g.text == nil {
return
}

for _, chunk := range g.text {
fmt.Println(chunk)

fg := chunk.hl.fg()
bg := chunk.hl.bg()
// bold := chunk.hl.bold
underline := chunk.hl.underline
// undercurl := chunk.hl.undercurl
// strikethrough := chunk.hl.strikethrough
italic := chunk.hl.italic

// set foreground color
p.SetPen2(fg.QColor())

r := []rune(chunk.str)
for _, rr := range r {
// draw background
if !bg.equals(g.s.ws.background) {
p.FillRect4(
core.NewQRectF4(
x,
y,
chunk.width,
height,
),
bg.QColor(),
)
}

// set italic
if italic {
font.SetItalic(true)
} else {
font.SetItalic(false)
}

p.DrawText(
core.NewQPointF3(
x,
y+float64(g.font.shift),
),
string(rr),
)

// draw underline
if underline {
var underlinePos float64 = 1
if g.s.ws.palette.widget.IsVisible() {
underlinePos = 2
}

// draw underline
p.FillRect4(
core.NewQRectF4(
x,
y+height-underlinePos,
chunk.width,
underlinePos,
),
fg.QColor(),
)
}

x += chunk.width
}
}
}

func (g *Guiwidget) drawImage(p *gui.QPainter, image *gui.QImage, x, y, width, height int) {
if image == nil {
return
Expand Down

0 comments on commit e080f47

Please sign in to comment.