@@ -2,6 +2,7 @@ package ui
22
33import (
44 "image/color"
5+ "runtime"
56 "sync/atomic"
67
78 "fyne.io/fyne/v2"
@@ -16,6 +17,14 @@ var transparencyKey = color.NRGBA{R: 1, G: 1, B: 1, A: 255}
1617// transparencyActive is 1 when a color-key background should be used.
1718var transparencyActive atomic.Int32
1819
20+ // linuxOpacityAlpha stores the alpha value for Linux background transparency.
21+ // Range: 0 (fully transparent) to 255 (fully opaque). Default is 255.
22+ var linuxOpacityAlpha atomic.Int32
23+
24+ func init () {
25+ linuxOpacityAlpha .Store (255 )
26+ }
27+
1928// SetTransparencyActive switches the background color key on or off.
2029func SetTransparencyActive (active bool ) {
2130 if active {
@@ -25,6 +34,19 @@ func SetTransparencyActive(active bool) {
2534 }
2635}
2736
37+ // SetLinuxOpacityAlpha sets the background alpha value for Linux transparency.
38+ // opacityPercent should be 0–100. This is converted to a 0–255 alpha value.
39+ func SetLinuxOpacityAlpha (opacityPercent int ) {
40+ alpha := opacityPercent * 255 / 100
41+ if alpha < 0 {
42+ alpha = 0
43+ }
44+ if alpha > 255 {
45+ alpha = 255
46+ }
47+ linuxOpacityAlpha .Store (int32 (alpha ))
48+ }
49+
2850// widgetTheme is a Fyne theme that replaces the window background with the
2951// color key when transparency is active, leaving all other colors unchanged.
3052type widgetTheme struct {
@@ -40,6 +62,15 @@ func (t *widgetTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant)
4062 if transparencyActive .Load () == 1 {
4163 switch name {
4264 case theme .ColorNameBackground , theme .ColorNameOverlayBackground :
65+ if runtime .GOOS == "linux" {
66+ // On Linux/Wayland, use actual alpha transparency.
67+ // The Fyne OpenGL/EGL renderer respects the alpha channel,
68+ // giving us background-only transparency (text/icons stay opaque).
69+ alpha := uint8 (linuxOpacityAlpha .Load ())
70+ return color.NRGBA {R : 30 , G : 30 , B : 30 , A : alpha }
71+ }
72+ // On Windows, use the color-key approach (Win32 LWA_COLORKEY
73+ // makes this exact color invisible).
4374 return transparencyKey
4475 }
4576 }
0 commit comments