Fix issues with transparency (#301)

* Fix issues with transparency
* Dispose of temporary alpha image
This commit is contained in:
Liam Galvin 2021-07-31 17:35:26 +01:00 committed by GitHub
parent 8d4ea4d7fe
commit c18b702b61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 40 deletions

View File

@ -90,6 +90,7 @@ var rootCmd = &cobra.Command{
gui.WithFontDPI(conf.Font.DPI), gui.WithFontDPI(conf.Font.DPI),
gui.WithFontSize(conf.Font.Size), gui.WithFontSize(conf.Font.Size),
gui.WithFontFamily(conf.Font.Family), gui.WithFontFamily(conf.Font.Family),
gui.WithOpacity(conf.Opacity),
} }
if screenshotAfterMS > 0 { if screenshotAfterMS > 0 {

View File

@ -73,7 +73,7 @@ func LoadThemeFromPath(conf *Config, path string) (*termutil.Theme, error) {
func loadThemeFromConf(conf *Config, themeConf *Theme) (*termutil.Theme, error) { func loadThemeFromConf(conf *Config, themeConf *Theme) (*termutil.Theme, error) {
factory := termutil.NewThemeFactory().WithOpacity(conf.Opacity) factory := termutil.NewThemeFactory()
colours := map[termutil.Colour]string{ colours := map[termutil.Colour]string{
termutil.ColourBlack: themeConf.Black, termutil.ColourBlack: themeConf.Black,

View File

@ -14,6 +14,8 @@ import (
// Draw renders the terminal GUI to the ebtien window. Required to implement the ebiten interface. // Draw renders the terminal GUI to the ebtien window. Required to implement the ebiten interface.
func (g *GUI) Draw(screen *ebiten.Image) { func (g *GUI) Draw(screen *ebiten.Image) {
tmp := ebiten.NewImage(g.size.X, g.size.Y)
cellSize := g.fontManager.CharSize() cellSize := g.fontManager.CharSize()
dotDepth := g.fontManager.DotDepth() dotDepth := g.fontManager.DotDepth()
@ -36,10 +38,10 @@ func (g *GUI) Draw(screen *ebiten.Image) {
extraW := float64(g.size.X) - endX extraW := float64(g.size.X) - endX
extraH := float64(g.size.Y) - endY extraH := float64(g.size.Y) - endY
if extraW > 0 { if extraW > 0 {
ebitenutil.DrawRect(screen, endX, 0, extraW, endY, defBg) ebitenutil.DrawRect(tmp, endX, 0, extraW, endY, defBg)
} }
if extraH > 0 { if extraH > 0 {
ebitenutil.DrawRect(screen, 0, endY, float64(g.size.X), extraH, defBg) ebitenutil.DrawRect(tmp, 0, endY, float64(g.size.X), extraH, defBg)
} }
var inHighlight bool var inHighlight bool
@ -52,7 +54,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
for y := int(buffer.ViewHeight() - 1); y >= 0; y-- { for y := int(buffer.ViewHeight() - 1); y >= 0; y-- {
py := cellSize.Y * y py := cellSize.Y * y
ebitenutil.DrawRect(screen, 0, float64(py), float64(g.size.X), float64(cellSize.Y), defBg) ebitenutil.DrawRect(tmp, 0, float64(py), float64(g.size.X), float64(cellSize.Y), defBg)
inHighlight = false inHighlight = false
for x := uint16(0); x < buffer.ViewWidth(); x++ { for x := uint16(0); x < buffer.ViewWidth(); x++ {
cell := buffer.GetCell(x, uint16(y)) cell := buffer.GetCell(x, uint16(y))
@ -74,7 +76,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
colour = defBg colour = defBg
} }
ebitenutil.DrawRect(screen, float64(px), float64(py), float64(cellSize.X), float64(cellSize.Y), colour) ebitenutil.DrawRect(tmp, float64(px), float64(py), float64(cellSize.X), float64(cellSize.Y), colour)
if buffer.IsHighlighted(termutil.Position{ if buffer.IsHighlighted(termutil.Position{
Line: uint64(y), Line: uint64(y),
@ -106,7 +108,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
} }
if isCursor && !ebiten.IsFocused() { if isCursor && !ebiten.IsFocused() {
ebitenutil.DrawRect(screen, float64(px)+1, float64(py)+1, float64(cellSize.X)-2, float64(cellSize.Y)-2, g.terminal.Theme().DefaultBackground()) ebitenutil.DrawRect(tmp, float64(px)+1, float64(py)+1, float64(cellSize.X)-2, float64(cellSize.Y)-2, g.terminal.Theme().DefaultBackground())
} }
} }
for x := uint16(0); x < buffer.ViewWidth(); x++ { for x := uint16(0); x < buffer.ViewWidth(); x++ {
@ -139,13 +141,13 @@ func (g *GUI) Draw(screen *ebiten.Image) {
if cell.Underline() { if cell.Underline() {
uly := float64(py + (dotDepth+cellSize.Y)/2) uly := float64(py + (dotDepth+cellSize.Y)/2)
ebitenutil.DrawLine(screen, float64(px), uly, float64(px+cellSize.X), uly, colour) ebitenutil.DrawLine(tmp, float64(px), uly, float64(px+cellSize.X), uly, colour)
} }
text.Draw(screen, string(cell.Rune().Rune), useFace, px, py+dotDepth, colour) text.Draw(tmp, string(cell.Rune().Rune), useFace, px, py+dotDepth, colour)
if cell.Strikethrough() { if cell.Strikethrough() {
ebitenutil.DrawLine(screen, float64(px), float64(py+(cellSize.Y/2)), float64(px+cellSize.X), float64(py+(cellSize.Y/2)), colour) ebitenutil.DrawLine(tmp, float64(px), float64(py+(cellSize.Y/2)), float64(px+cellSize.X), float64(py+(cellSize.Y/2)), colour)
} }
} }
@ -157,7 +159,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(sx, sy) op.GeoM.Translate(sx, sy)
screen.DrawImage( tmp.DrawImage(
ebiten.NewImageFromImage(sixel.Sixel.Image), ebiten.NewImageFromImage(sixel.Sixel.Image),
op, op,
) )
@ -237,19 +239,19 @@ func (g *GUI) Draw(screen *ebiten.Image) {
} }
// draw opaque box below and above highlighted line(s) // draw opaque box below and above highlighted line(s)
ebitenutil.DrawRect(screen, 0, float64(highlightMin.Line*uint64(cellSize.Y)), float64(cellSize.X*int(highlightMin.Col)), float64(cellSize.Y), color.RGBA{A: 0x80}) ebitenutil.DrawRect(tmp, 0, float64(highlightMin.Line*uint64(cellSize.Y)), float64(cellSize.X*int(highlightMin.Col)), float64(cellSize.Y), color.RGBA{A: 0x80})
ebitenutil.DrawRect(screen, float64((cellSize.X)*int(highlightMax.Col+1)), float64(highlightMax.Line*uint64(cellSize.Y)), float64(g.size.X), float64(cellSize.Y), color.RGBA{A: 0x80}) ebitenutil.DrawRect(tmp, float64((cellSize.X)*int(highlightMax.Col+1)), float64(highlightMax.Line*uint64(cellSize.Y)), float64(g.size.X), float64(cellSize.Y), color.RGBA{A: 0x80})
ebitenutil.DrawRect(screen, 0, 0, float64(g.size.X), float64(highlightMin.Line*uint64(cellSize.Y)), color.RGBA{A: 0x80}) ebitenutil.DrawRect(tmp, 0, 0, float64(g.size.X), float64(highlightMin.Line*uint64(cellSize.Y)), color.RGBA{A: 0x80})
afterLineY := float64((1 + highlightMax.Line) * uint64(cellSize.Y)) afterLineY := float64((1 + highlightMax.Line) * uint64(cellSize.Y))
ebitenutil.DrawRect(screen, 0, afterLineY, float64(g.size.X), float64(g.size.Y)-afterLineY, color.RGBA{A: 0x80}) ebitenutil.DrawRect(tmp, 0, afterLineY, float64(g.size.X), float64(g.size.Y)-afterLineY, color.RGBA{A: 0x80})
// annotation border // annotation border
ebitenutil.DrawRect(screen, float64(annotationX)-padding, annotationY-padding, float64(annotationWidth)+(padding*2), annotationHeight+(padding*2), g.terminal.Theme().SelectionBackground()) ebitenutil.DrawRect(tmp, float64(annotationX)-padding, annotationY-padding, float64(annotationWidth)+(padding*2), annotationHeight+(padding*2), g.terminal.Theme().SelectionBackground())
// annotation background // annotation background
ebitenutil.DrawRect(screen, 1+float64(annotationX)-padding, 1+annotationY-padding, float64(annotationWidth)+(padding*2)-2, annotationHeight+(padding*2)-2, g.terminal.Theme().DefaultBackground()) ebitenutil.DrawRect(tmp, 1+float64(annotationX)-padding, 1+annotationY-padding, float64(annotationWidth)+(padding*2)-2, annotationHeight+(padding*2)-2, g.terminal.Theme().DefaultBackground())
// vertical line // vertical line
ebitenutil.DrawLine(screen, lineX, float64(lineY), lineX, lineY+lineHeight, g.terminal.Theme().SelectionBackground()) ebitenutil.DrawLine(tmp, lineX, float64(lineY), lineX, lineY+lineHeight, g.terminal.Theme().SelectionBackground())
var tY int var tY int
var tX int var tX int
@ -259,7 +261,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(annotationX), annotationY) op.GeoM.Translate(float64(annotationX), annotationY)
screen.DrawImage( tmp.DrawImage(
ebiten.NewImageFromImage(annotation.Image), ebiten.NewImageFromImage(annotation.Image),
op, op,
) )
@ -271,7 +273,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
tX = 0 tX = 0
continue continue
} }
text.Draw(screen, string(r), regularFace, annotationX+tX, int(annotationY)+dotDepth+tY, g.terminal.Theme().DefaultForeground()) text.Draw(tmp, string(r), regularFace, annotationX+tX, int(annotationY)+dotDepth+tY, g.terminal.Theme().DefaultForeground())
tX += cellSize.X tX += cellSize.X
} }
@ -298,11 +300,11 @@ func (g *GUI) Draw(screen *ebiten.Image) {
boxWidth = endX / 8 boxWidth = endX / 8
} }
ebitenutil.DrawRect(screen, float64(msgX-1), msgY-1, boxWidth+2, boxHeight+2, msg.Foreground) ebitenutil.DrawRect(tmp, float64(msgX-1), msgY-1, boxWidth+2, boxHeight+2, msg.Foreground)
ebitenutil.DrawRect(screen, float64(msgX), msgY, boxWidth, boxHeight, msg.Background) ebitenutil.DrawRect(tmp, float64(msgX), msgY, boxWidth, boxHeight, msg.Background)
for y, line := range lines { for y, line := range lines {
for x, r := range line { for x, r := range line {
text.Draw(screen, string(r), regularFace, msgX+pad+(x*cellSize.X), pad+(y*cellSize.Y)+int(msgY)+dotDepth, msg.Foreground) text.Draw(tmp, string(r), regularFace, msgX+pad+(x*cellSize.X), pad+(y*cellSize.Y)+int(msgY)+dotDepth, msg.Foreground)
} }
} }
msgEndY = msgEndY - float64(pad*4) - float64(len(lines)*g.CellSize().Y) msgEndY = msgEndY - float64(pad*4) - float64(len(lines)*g.CellSize().Y)
@ -310,7 +312,11 @@ func (g *GUI) Draw(screen *ebiten.Image) {
} }
if g.screenshotRequested { if g.screenshotRequested {
g.takeScreenshot(screen) g.takeScreenshot(tmp)
} }
opt := &ebiten.DrawImageOptions{}
opt.ColorM.Scale(1, 1, 1, g.opacity)
screen.DrawImage(tmp, opt)
tmp.Dispose()
} }

View File

@ -39,6 +39,7 @@ type GUI struct {
screenshotFilename string screenshotFilename string
startupFuncs []func(g *GUI) startupFuncs []func(g *GUI)
keyState *keyState keyState *keyState
opacity float64
} }
type PopupMessage struct { type PopupMessage struct {
@ -88,6 +89,7 @@ func (g *GUI) Run() error {
}() }()
ebiten.SetScreenTransparent(true) ebiten.SetScreenTransparent(true)
ebiten.SetScreenClearedEveryFrame(true)
ebiten.SetWindowResizable(true) ebiten.SetWindowResizable(true)
ebiten.SetRunnableOnUnfocused(true) ebiten.SetRunnableOnUnfocused(true)
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMinimum) ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMinimum)

View File

@ -8,6 +8,13 @@ func WithFontFamily(family string) func(g *GUI) error {
} }
} }
func WithOpacity(opacity float64) func(g *GUI) error {
return func(g *GUI) error {
g.opacity = opacity
return nil
}
}
func WithFontSize(size float64) func(g *GUI) error { func WithFontSize(size float64) func(g *GUI) error {
return func(g *GUI) error { return func(g *GUI) error {
g.fontManager.SetSize(size) g.fontManager.SetSize(size)

View File

@ -35,7 +35,6 @@ const (
) )
type Theme struct { type Theme struct {
alpha uint8
colourMap map[Colour]color.Color colourMap map[Colour]color.Color
} }
@ -87,7 +86,7 @@ func (t *Theme) ColourFrom4Bit(code uint8) color.Color {
func (t *Theme) DefaultBackground() color.Color { func (t *Theme) DefaultBackground() color.Color {
c, ok := t.colourMap[ColourBackground] c, ok := t.colourMap[ColourBackground]
if !ok { if !ok {
return color.RGBA{0, 0, 0, t.alpha} return color.RGBA{0, 0, 0, 0xff}
} }
return c return c
} }
@ -95,7 +94,7 @@ func (t *Theme) DefaultBackground() color.Color {
func (t *Theme) DefaultForeground() color.Color { func (t *Theme) DefaultForeground() color.Color {
c, ok := t.colourMap[ColourForeground] c, ok := t.colourMap[ColourForeground]
if !ok { if !ok {
return color.RGBA{255, 255, 255, t.alpha} return color.RGBA{255, 255, 255, 0xff}
} }
return c return c
} }
@ -103,7 +102,7 @@ func (t *Theme) DefaultForeground() color.Color {
func (t *Theme) SelectionBackground() color.Color { func (t *Theme) SelectionBackground() color.Color {
c, ok := t.colourMap[ColourSelectionBackground] c, ok := t.colourMap[ColourSelectionBackground]
if !ok { if !ok {
return color.RGBA{0, 0, 0, t.alpha} return color.RGBA{0, 0, 0, 0xff}
} }
return c return c
} }
@ -111,7 +110,7 @@ func (t *Theme) SelectionBackground() color.Color {
func (t *Theme) SelectionForeground() color.Color { func (t *Theme) SelectionForeground() color.Color {
c, ok := t.colourMap[ColourSelectionForeground] c, ok := t.colourMap[ColourSelectionForeground]
if !ok { if !ok {
return color.RGBA{255, 255, 255, t.alpha} return color.RGBA{255, 255, 255, 0xff}
} }
return c return c
} }
@ -119,7 +118,7 @@ func (t *Theme) SelectionForeground() color.Color {
func (t *Theme) CursorBackground() color.Color { func (t *Theme) CursorBackground() color.Color {
c, ok := t.colourMap[ColourCursorBackground] c, ok := t.colourMap[ColourCursorBackground]
if !ok { if !ok {
return color.RGBA{255, 255, 255, t.alpha} return color.RGBA{255, 255, 255, 0xff}
} }
return c return c
} }
@ -127,7 +126,7 @@ func (t *Theme) CursorBackground() color.Color {
func (t *Theme) CursorForeground() color.Color { func (t *Theme) CursorForeground() color.Color {
c, ok := t.colourMap[ColourCursorForeground] c, ok := t.colourMap[ColourCursorForeground]
if !ok { if !ok {
return color.RGBA{0, 0, 0, t.alpha} return color.RGBA{0, 0, 0, 0xff}
} }
return c return c
} }
@ -149,12 +148,12 @@ func (t *Theme) ColourFrom8Bit(n string) (color.Color, error) {
R: byte(c), R: byte(c),
G: byte(c), G: byte(c),
B: byte(c), B: byte(c),
A: t.alpha, A: 0xff,
}, nil }, nil
} }
var colour color.RGBA var colour color.RGBA
colour.A = t.alpha colour.A = 0xff
indexR := ((index - 16) / 36) indexR := ((index - 16) / 36)
if indexR > 0 { if indexR > 0 {
colour.R = uint8(55 + indexR*40) colour.R = uint8(55 + indexR*40)
@ -188,7 +187,7 @@ func (t *Theme) ColourFrom24Bit(r, g, b string) (color.Color, error) {
R: byte(ri), R: byte(ri),
G: byte(gi), G: byte(gi),
B: byte(bi), B: byte(bi),
A: t.alpha, A: 0xff,
}, nil }, nil
} }

View File

@ -10,7 +10,6 @@ type ThemeFactory struct {
func NewThemeFactory() *ThemeFactory { func NewThemeFactory() *ThemeFactory {
return &ThemeFactory{ return &ThemeFactory{
theme: &Theme{ theme: &Theme{
alpha: 0xff,
colourMap: map[Colour]color.Color{}, colourMap: map[Colour]color.Color{},
}, },
colourMap: make(map[Colour]color.Color), colourMap: make(map[Colour]color.Color),
@ -24,17 +23,12 @@ func (t *ThemeFactory) Build() *Theme {
R: uint8(r / 0xff), R: uint8(r / 0xff),
G: uint8(g / 0xff), G: uint8(g / 0xff),
B: uint8(b / 0xff), B: uint8(b / 0xff),
A: t.theme.alpha, A: 0xff,
} }
} }
return t.theme return t.theme
} }
func (t *ThemeFactory) WithOpacity(opacity float64) *ThemeFactory {
t.theme.alpha = uint8(0xff * opacity)
return t
}
func (t *ThemeFactory) WithColour(key Colour, colour color.Color) *ThemeFactory { func (t *ThemeFactory) WithColour(key Colour, colour color.Color) *ThemeFactory {
t.colourMap[key] = colour t.colourMap[key] = colour
return t return t