Window Opacity in Linux

If any of you are looking to both “get” and “set” the opacity level of a desktop window in Linux, here’s the code that worked for me. I’m running Gnome 47 (by the way).

I created a Module and then added 2 public extension methods:
getOpacity and setOpacity

Code for getOpacity:
Function signature is: Public Function getOpacity(Extends w as DesktopWindow) As Double

Now for the method’s code:
#if TargetLinux then
Declare Function gtk_widget_get_opacity Lib “libgtk-3” (handle As Ptr) As Double
Return gtk_widget_get_opacity(w.Handle)
#EndIf

====================

Code for setOpacity:
Sub Procedure signature is: setOpacity(Extends w as DesktopWindow, value as Double)

Now for the method’s code:
#if TargetLinux then
Declare Sub gtk_widget_set_opacity Lib “libgtk-3” (handle As Ptr, opacity As Double)
gtk_widget_set_opacity(w.Handle, value)
#EndIf

====================

To test them, I added a Button to a Window and in the “pressed” event, I added the following code (it simply toggles from transparent back to opaque each time you click the Button):

// call extension methods in Global Module
if self.getOpacity() = 1.0 then
self.setOpacity(.70)
else
self.setOpacity(1.0)
end if

I hope some of you find this useful. I know that macOS and Windows have similar APIs that you can look up online.

4 Likes

Thanks for sharing.

1 Like

This is great, thank you! Is there a way I can keep the button’s opacity at 1.0 when the window is .70?

1 Like