Skip to content

Commit 89f780b

Browse files
committed
Improved background color rendering process
1 parent e37b77b commit 89f780b

File tree

2 files changed

+79
-86
lines changed

2 files changed

+79
-86
lines changed

editor/screen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var globalOrder int
2323
// Screen is the main editor area
2424
type Screen struct {
2525
cache Cache
26+
bgcache Cache
2627
tooltip *IMETooltip
2728
font *Font
2829
fallbackfonts []*Font
@@ -57,6 +58,7 @@ func newScreen() *Screen {
5758
cursor: [2]int{0, 0},
5859
highlightGroup: make(map[string]int),
5960
cache: newCache(),
61+
bgcache: newBrushCache(),
6062
}
6163

6264
widget.ConnectMousePressEvent(screen.mousePressEvent)

editor/window.go

Lines changed: 77 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ type HlBgKey struct {
7575
length int
7676
}
7777

78+
type BrushKey struct {
79+
pattern core.Qt__BrushStyle
80+
color *RGBA
81+
transparent int
82+
}
83+
7884
// Cell is
7985
type Cell struct {
8086
highlight *Highlight
@@ -187,6 +193,11 @@ func purgeQimage(key, value interface{}) {
187193
image.DestroyQImage()
188194
}
189195

196+
func purgeQBrush(key, value interface{}) {
197+
brush := value.(*gui.QBrush)
198+
brush.DestroyQBrush()
199+
}
200+
190201
func newCache() Cache {
191202
g := gcache.New(editor.config.Editor.CacheSize).LRU().
192203
EvictedFunc(purgeQimage).
@@ -195,6 +206,14 @@ func newCache() Cache {
195206
return *(*Cache)(unsafe.Pointer(&g))
196207
}
197208

209+
func newBrushCache() Cache {
210+
g := gcache.New(64).LRU().
211+
EvictedFunc(purgeQBrush).
212+
PurgeVisitorFunc(purgeQBrush).
213+
Build()
214+
return *(*Cache)(unsafe.Pointer(&g))
215+
}
216+
198217
func (c *Cache) set(key, value interface{}) error {
199218
return c.Set(key, value)
200219
}
@@ -2045,70 +2064,49 @@ func (w *Window) fillCellRect(p *gui.QPainter, lastHighlight *Highlight, lastBg
20452064
}
20462065

20472066
font := w.getFont()
2067+
pattern, color, transparent := w.getFillpatternAndTransparent(lastHighlight)
2068+
2069+
var brush *gui.QBrush
2070+
if editor.config.Editor.CachedDrawing {
2071+
brushv, err := w.s.bgcache.get(BrushKey{
2072+
pattern: pattern,
2073+
color: color,
2074+
transparent: transparent,
2075+
})
2076+
if err != nil {
2077+
brush = newBgBrushCache(pattern, color, transparent)
2078+
w.setBgBrushCache(pattern, color, transparent, brush)
2079+
} else {
2080+
brush = brushv.(*gui.QBrush)
2081+
}
2082+
} else {
2083+
brush = newBgBrushCache(pattern, color, transparent)
2084+
}
20482085

20492086
if verScrollPixels == 0 ||
20502087
verScrollPixels > 0 && (y < w.rows-w.viewportMargins[1]-1) ||
20512088
verScrollPixels < 0 && (y > w.viewportMargins[0]) {
2052-
if editor.config.Editor.CachedDrawing {
2053-
cache := w.getCache()
2054-
if cache == (Cache{}) {
2055-
return
2056-
}
2057-
var image *gui.QImage
2058-
imagev, err := cache.get(HlBgKey{
2059-
bg: lastHighlight.bg(),
2060-
length: width,
2061-
})
2062-
2063-
if err != nil {
2064-
image = w.newBgCache(lastHighlight, width)
2065-
w.setBgCache(lastHighlight, width, image)
2066-
} else {
2067-
image = imagev.(*gui.QImage)
2068-
}
20692089

2070-
p.DrawImage9(
2071-
int(float64(start)*font.cellwidth+float64(horScrollPixels)),
2072-
int(float64((y)*font.lineHeight+verScrollPixels)),
2073-
image,
2074-
0, 0,
2075-
-1, -1,
2076-
core.Qt__AutoColor,
2077-
)
2090+
// Fill background with pattern
2091+
rectF := core.NewQRectF4(
2092+
float64(start)*font.cellwidth+float64(horScrollPixels),
2093+
float64((y)*font.lineHeight+verScrollPixels),
2094+
float64(width)*font.cellwidth,
2095+
float64(font.lineHeight),
2096+
)
20782097

2079-
} else {
2080-
// Set diff pattern
2081-
pattern, color, transparent := w.getFillpatternAndTransparent(lastHighlight)
2098+
p.FillRect(
2099+
rectF,
2100+
brush,
2101+
)
20822102

2083-
// Fill background with pattern
2084-
rectF := core.NewQRectF4(
2085-
float64(start)*font.cellwidth+float64(horScrollPixels),
2086-
float64((y)*font.lineHeight+verScrollPixels),
2087-
float64(width)*font.cellwidth,
2088-
float64(font.lineHeight),
2089-
)
2090-
p.FillRect(
2091-
rectF,
2092-
gui.NewQBrush3(
2093-
gui.NewQColor3(
2094-
color.R,
2095-
color.G,
2096-
color.B,
2097-
transparent,
2098-
),
2099-
pattern,
2100-
),
2101-
)
2102-
}
21032103
}
21042104

21052105
// Addresses an issue where smooth scrolling with a touchpad causes incomplete
21062106
// background rendering at the top or bottom of floating windows.
21072107
// Adds compensation drawing for areas partially scrolled into view by checking
21082108
// `verScrollPixels` and filling the necessary background to prevent visual gaps.
21092109

2110-
pattern, color, transparent := w.getFillpatternAndTransparent(lastHighlight)
2111-
21122110
if verScrollPixels > 0 {
21132111
if y == w.viewportMargins[0] {
21142112

@@ -2126,15 +2124,7 @@ func (w *Window) fillCellRect(p *gui.QPainter, lastHighlight *Highlight, lastBg
21262124
)
21272125
p.FillRect(
21282126
rectF,
2129-
gui.NewQBrush3(
2130-
gui.NewQColor3(
2131-
color.R,
2132-
color.G,
2133-
color.B,
2134-
transparent,
2135-
),
2136-
pattern,
2137-
),
2127+
brush,
21382128
)
21392129
}
21402130

@@ -2148,15 +2138,7 @@ func (w *Window) fillCellRect(p *gui.QPainter, lastHighlight *Highlight, lastBg
21482138
)
21492139
p.FillRect(
21502140
rectF,
2151-
gui.NewQBrush3(
2152-
gui.NewQColor3(
2153-
color.R,
2154-
color.G,
2155-
color.B,
2156-
transparent,
2157-
),
2158-
pattern,
2159-
),
2141+
brush,
21602142
)
21612143
}
21622144

@@ -2172,15 +2154,7 @@ func (w *Window) fillCellRect(p *gui.QPainter, lastHighlight *Highlight, lastBg
21722154
)
21732155
p.FillRect(
21742156
rectF,
2175-
gui.NewQBrush3(
2176-
gui.NewQColor3(
2177-
color.R,
2178-
color.G,
2179-
color.B,
2180-
transparent,
2181-
),
2182-
pattern,
2183-
),
2157+
brush,
21842158
)
21852159
}
21862160
if y == w.rows-w.viewportMargins[1]-1 {
@@ -2197,22 +2171,39 @@ func (w *Window) fillCellRect(p *gui.QPainter, lastHighlight *Highlight, lastBg
21972171
)
21982172
p.FillRect(
21992173
rectF,
2200-
gui.NewQBrush3(
2201-
gui.NewQColor3(
2202-
color.R,
2203-
color.G,
2204-
color.B,
2205-
transparent,
2206-
),
2207-
pattern,
2208-
),
2174+
brush,
22092175
)
22102176
}
22112177

22122178
}
22132179

22142180
}
22152181

2182+
func newBgBrushCache(pattern core.Qt__BrushStyle, color *RGBA, transparent int) *gui.QBrush {
2183+
2184+
return gui.NewQBrush3(
2185+
gui.NewQColor3(
2186+
color.R,
2187+
color.G,
2188+
color.B,
2189+
transparent,
2190+
),
2191+
pattern,
2192+
)
2193+
2194+
}
2195+
2196+
func (w *Window) setBgBrushCache(pattern core.Qt__BrushStyle, color *RGBA, transparent int, brush *gui.QBrush) {
2197+
w.s.bgcache.set(
2198+
BrushKey{
2199+
pattern: pattern,
2200+
color: color,
2201+
transparent: transparent,
2202+
},
2203+
brush,
2204+
)
2205+
}
2206+
22162207
func (w *Window) newBgCache(lastHighlight *Highlight, length int) *gui.QImage {
22172208
font := w.getFont()
22182209
width := float64(length) * font.cellwidth

0 commit comments

Comments
 (0)