Skip to content

Commit

Permalink
Fix issues with transparency (#301)
Browse files Browse the repository at this point in the history
* Fix issues with transparency
* Dispose of temporary alpha image
  • Loading branch information
liamg committed Jul 31, 2021
1 parent 8d4ea4d commit c18b702
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
1 change: 1 addition & 0 deletions internal/app/darktile/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var rootCmd = &cobra.Command{
gui.WithFontDPI(conf.Font.DPI),
gui.WithFontSize(conf.Font.Size),
gui.WithFontFamily(conf.Font.Family),
gui.WithOpacity(conf.Opacity),
}

if screenshotAfterMS > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/darktile/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func LoadThemeFromPath(conf *Config, path string) (*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{
termutil.ColourBlack: themeConf.Black,
Expand Down
50 changes: 28 additions & 22 deletions internal/app/darktile/gui/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
// Draw renders the terminal GUI to the ebtien window. Required to implement the ebiten interface.
func (g *GUI) Draw(screen *ebiten.Image) {

tmp := ebiten.NewImage(g.size.X, g.size.Y)

cellSize := g.fontManager.CharSize()
dotDepth := g.fontManager.DotDepth()

Expand All @@ -36,10 +38,10 @@ func (g *GUI) Draw(screen *ebiten.Image) {
extraW := float64(g.size.X) - endX
extraH := float64(g.size.Y) - endY
if extraW > 0 {
ebitenutil.DrawRect(screen, endX, 0, extraW, endY, defBg)
ebitenutil.DrawRect(tmp, endX, 0, extraW, endY, defBg)
}
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
Expand All @@ -52,7 +54,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
for y := int(buffer.ViewHeight() - 1); y >= 0; 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
for x := uint16(0); x < buffer.ViewWidth(); x++ {
cell := buffer.GetCell(x, uint16(y))
Expand All @@ -74,7 +76,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
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{
Line: uint64(y),
Expand Down Expand Up @@ -106,7 +108,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
}

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++ {
Expand Down Expand Up @@ -139,13 +141,13 @@ func (g *GUI) Draw(screen *ebiten.Image) {

if cell.Underline() {
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() {
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)
}

}
Expand All @@ -157,7 +159,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {

op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(sx, sy)
screen.DrawImage(
tmp.DrawImage(
ebiten.NewImageFromImage(sixel.Sixel.Image),
op,
)
Expand Down Expand Up @@ -237,19 +239,19 @@ func (g *GUI) Draw(screen *ebiten.Image) {
}

// 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(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(screen, 0, 0, float64(g.size.X), float64(highlightMin.Line*uint64(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(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(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))
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
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
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
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 tX int
Expand All @@ -259,7 +261,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {

op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(annotationX), annotationY)
screen.DrawImage(
tmp.DrawImage(
ebiten.NewImageFromImage(annotation.Image),
op,
)
Expand All @@ -271,7 +273,7 @@ func (g *GUI) Draw(screen *ebiten.Image) {
tX = 0
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
}

Expand All @@ -298,19 +300,23 @@ func (g *GUI) Draw(screen *ebiten.Image) {
boxWidth = endX / 8
}

ebitenutil.DrawRect(screen, 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-1), msgY-1, boxWidth+2, boxHeight+2, msg.Foreground)
ebitenutil.DrawRect(tmp, float64(msgX), msgY, boxWidth, boxHeight, msg.Background)
for y, line := range lines {
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)
}
}

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()
}
2 changes: 2 additions & 0 deletions internal/app/darktile/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type GUI struct {
screenshotFilename string
startupFuncs []func(g *GUI)
keyState *keyState
opacity float64
}

type PopupMessage struct {
Expand Down Expand Up @@ -88,6 +89,7 @@ func (g *GUI) Run() error {
}()

ebiten.SetScreenTransparent(true)
ebiten.SetScreenClearedEveryFrame(true)
ebiten.SetWindowResizable(true)
ebiten.SetRunnableOnUnfocused(true)
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMinimum)
Expand Down
7 changes: 7 additions & 0 deletions internal/app/darktile/gui/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
return func(g *GUI) error {
g.fontManager.SetSize(size)
Expand Down
19 changes: 9 additions & 10 deletions internal/app/darktile/termutil/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const (
)

type Theme struct {
alpha uint8
colourMap map[Colour]color.Color
}

Expand Down Expand Up @@ -87,47 +86,47 @@ func (t *Theme) ColourFrom4Bit(code uint8) color.Color {
func (t *Theme) DefaultBackground() color.Color {
c, ok := t.colourMap[ColourBackground]
if !ok {
return color.RGBA{0, 0, 0, t.alpha}
return color.RGBA{0, 0, 0, 0xff}
}
return c
}

func (t *Theme) DefaultForeground() color.Color {
c, ok := t.colourMap[ColourForeground]
if !ok {
return color.RGBA{255, 255, 255, t.alpha}
return color.RGBA{255, 255, 255, 0xff}
}
return c
}

func (t *Theme) SelectionBackground() color.Color {
c, ok := t.colourMap[ColourSelectionBackground]
if !ok {
return color.RGBA{0, 0, 0, t.alpha}
return color.RGBA{0, 0, 0, 0xff}
}
return c
}

func (t *Theme) SelectionForeground() color.Color {
c, ok := t.colourMap[ColourSelectionForeground]
if !ok {
return color.RGBA{255, 255, 255, t.alpha}
return color.RGBA{255, 255, 255, 0xff}
}
return c
}

func (t *Theme) CursorBackground() color.Color {
c, ok := t.colourMap[ColourCursorBackground]
if !ok {
return color.RGBA{255, 255, 255, t.alpha}
return color.RGBA{255, 255, 255, 0xff}
}
return c
}

func (t *Theme) CursorForeground() color.Color {
c, ok := t.colourMap[ColourCursorForeground]
if !ok {
return color.RGBA{0, 0, 0, t.alpha}
return color.RGBA{0, 0, 0, 0xff}
}
return c
}
Expand All @@ -149,12 +148,12 @@ func (t *Theme) ColourFrom8Bit(n string) (color.Color, error) {
R: byte(c),
G: byte(c),
B: byte(c),
A: t.alpha,
A: 0xff,
}, nil
}

var colour color.RGBA
colour.A = t.alpha
colour.A = 0xff
indexR := ((index - 16) / 36)
if indexR > 0 {
colour.R = uint8(55 + indexR*40)
Expand Down Expand Up @@ -188,7 +187,7 @@ func (t *Theme) ColourFrom24Bit(r, g, b string) (color.Color, error) {
R: byte(ri),
G: byte(gi),
B: byte(bi),
A: t.alpha,
A: 0xff,
}, nil
}

Expand Down
8 changes: 1 addition & 7 deletions internal/app/darktile/termutil/theme_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type ThemeFactory struct {
func NewThemeFactory() *ThemeFactory {
return &ThemeFactory{
theme: &Theme{
alpha: 0xff,
colourMap: map[Colour]color.Color{},
},
colourMap: make(map[Colour]color.Color),
Expand All @@ -24,17 +23,12 @@ func (t *ThemeFactory) Build() *Theme {
R: uint8(r / 0xff),
G: uint8(g / 0xff),
B: uint8(b / 0xff),
A: t.theme.alpha,
A: 0xff,
}
}
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 {
t.colourMap[key] = colour
return t
Expand Down

0 comments on commit c18b702

Please sign in to comment.