Skip to content

Instantly share code, notes, and snippets.

@donadigo
Last active May 17, 2020 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save donadigo/6e93d4730b66f1a6f35100d43317fe18 to your computer and use it in GitHub Desktop.
Save donadigo/6e93d4730b66f1a6f35100d43317fe18 to your computer and use it in GitHub Desktop.
// Compile: valac --pkg=gtk+-3.0 --pkg=gdk-x11-3.0 BlurWindow.vala
[DBus (name = "org.pantheon.gala")]
public interface IGala : Object {
public abstract void disable_blur_behind (uint32 xid) throws Error;
public abstract void enable_blur_behind (uint32 xid, int x, int y, int width, int height, uint8 opacity) throws Error;
}
const string CSS_DATA = """
window {
background-color: %s;
color: #fff;
}
button {
color: #fff;
}
""";
Gtk.Window window;
Gtk.CssProvider provider;
Gtk.ColorButton cbutton;
Gtk.Scale op_slider;
public static int main (string[] args) {
Gtk.init (ref args);
window = new Gtk.Window ();
window.set_visual (Gdk.Screen.get_default ().get_rgba_visual ());
window.set_default_size (500, 500);
window.destroy.connect (() => Gtk.main_quit ());
window.show_all ();
provider = new Gtk.CssProvider ();
provider.load_from_data (CSS_DATA.printf ("rgba (255, 255, 255, 0.0)"));
Gtk.StyleContext.add_provider_for_screen (window.get_screen (), provider, 600);
var xwin = (Gdk.X11.Window)window.get_window ();
uint32 id = (uint32)xwin.get_xid ();
IGala? gala = Bus.get_proxy_sync (BusType.SESSION, "org.pantheon.gala", "/org/pantheon/gala");
Idle.add (() => {
gala.enable_blur_behind (id, 0, 0, 0, 0, 255);
return false;
});
op_slider = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 1, 0.1);
op_slider.hexpand = true;
op_slider.value_changed.connect (set_background);
cbutton = new Gtk.ColorButton ();
cbutton.color_set.connect (set_background);
var blur_switch = new Gtk.Switch ();
blur_switch.active = true;
blur_switch.halign = Gtk.Align.START;
blur_switch.notify["active"].connect (() => {
if (blur_switch.active) {
gala.enable_blur_behind (id, 0, 0, 0, 0, 255);
} else {
gala.disable_blur_behind (id);
}
});
var animate_button = new Gtk.Button.with_label ("Animate effect opacity");
animate_button.clicked.connect (() => {
uint8 current_opacity = 0;
Timeout.add (10, () => {
gala.enable_blur_behind (id, 0, 0, 0, 0, current_opacity);
bool ret = current_opacity < 255;
current_opacity += 5;
return ret;
});
});
var grid = new Gtk.Grid ();
grid.margin = 24;
grid.column_homogeneous = true;
grid.column_spacing = 6;
grid.row_spacing = 6;
grid.attach (new Gtk.Label ("Enable blur:"), 0, 0, 1, 1);
grid.attach (blur_switch, 1, 0, 1, 1);
grid.attach (new Gtk.Label ("Window tint:"), 0, 1, 1, 1);
grid.attach (cbutton, 1, 1, 1, 1);
grid.attach (new Gtk.Label ("Background opacity:"), 0, 2, 1, 1);
grid.attach (op_slider, 1, 2, 1, 1);
grid.attach (animate_button, 1, 3, 1, 1);
window.add (grid);
window.show_all ();
Gtk.main ();
return 0;
}
void set_background () {
Gtk.StyleContext.remove_provider_for_screen (window.get_screen (), provider);
var c = cbutton.rgba;
string str = "rgba (%i, %i, %i, %s)".printf ((int)(c.red * 255),
(int)(c.green * 255),
(int)(c.blue * 255),
op_slider.get_value ().to_string ().replace (",", "."));
provider = new Gtk.CssProvider ();
provider.load_from_data (CSS_DATA.printf (str));
Gtk.StyleContext.add_provider_for_screen (window.get_screen (), provider, 600);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment