Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 123 additions & 136 deletions src/Background/BackgroundSource.vala
Original file line number Diff line number Diff line change
@@ -1,166 +1,153 @@
//
// Copyright (C) 2014 Tom Beckmann
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

namespace Gala {
public class BackgroundSource : Object {
// list of keys that are actually relevant for us
private const string[] OPTIONS = {
"color-shading-type",
"picture-options",
"picture-uri",
"picture-uri-dark",
"primary-color",
"secondary-color"
};

public signal void changed ();

public Meta.Display display { get; construct; }
public GLib.Settings gnome_background_settings { get; private set; }

internal int use_count { get; set; default = 0; }

private GLib.HashTable<int, Background> backgrounds;
private uint[] hash_cache;
private Meta.MonitorManager? monitor_manager;
private GLib.Settings gala_background_settings;

public bool should_dim {
get {
return (
Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK &&
gala_background_settings.get_boolean ("dim-wallpaper-in-dark-style")
);
}
}

public BackgroundSource (Meta.Display display) {
Object (display: display);
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2014 Tom Beckmann
* 2025 elementary, Inc. (https://elementary.io)
*/

public class Gala.BackgroundSource : Object {
// list of keys that are actually relevant for us
private const string[] OPTIONS = {
"color-shading-type",
"picture-options",
"picture-uri",
"picture-uri-dark",
"primary-color",
"secondary-color"
};

public signal void changed ();

public Meta.Display display { get; construct; }
public GLib.Settings gnome_background_settings { get; private set; }

internal int use_count { get; set; default = 0; }

private GLib.HashTable<int, Background> backgrounds;
private uint[] hash_cache;
private Meta.MonitorManager? monitor_manager;
private GLib.Settings gala_background_settings;

public bool should_dim {
get {
return (
Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK &&
gala_background_settings.get_boolean ("dim-wallpaper-in-dark-style")
);
}
}

construct {
backgrounds = new GLib.HashTable<int, Background> (GLib.direct_hash, GLib.direct_equal);
hash_cache = new uint[OPTIONS.length];
public BackgroundSource (Meta.Display display) {
Object (display: display);
}

monitor_manager = display.get_context ().get_backend ().get_monitor_manager ();
monitor_manager.monitors_changed.connect (monitors_changed);
construct {
backgrounds = new GLib.HashTable<int, Background> (GLib.direct_hash, GLib.direct_equal);
hash_cache = new uint[OPTIONS.length];

gala_background_settings = new GLib.Settings ("io.elementary.desktop.background");
gala_background_settings.changed["dim-wallpaper-in-dark-style"].connect (() => changed ());
monitor_manager = display.get_context ().get_backend ().get_monitor_manager ();
monitor_manager.monitors_changed.connect (monitors_changed);

Drawing.StyleManager.get_instance ().notify["prefers-color-scheme"].connect (() => changed ());
gala_background_settings = new GLib.Settings ("io.elementary.desktop.background");
gala_background_settings.changed["dim-wallpaper-in-dark-style"].connect (() => changed ());

gnome_background_settings = new GLib.Settings ("org.gnome.desktop.background");
Drawing.StyleManager.get_instance ().notify["prefers-color-scheme"].connect (() => changed ());

// unfortunately the settings sometimes tend to fire random changes even though
// nothing actually happened. The code below is used to prevent us from spamming
// new actors all the time, which lead to some problems in other areas of the code
for (int i = 0; i < OPTIONS.length; i++) {
hash_cache[i] = gnome_background_settings.get_value (OPTIONS[i]).hash ();
}
gnome_background_settings = new GLib.Settings ("org.gnome.desktop.background");

gnome_background_settings.changed.connect ((key) => {
for (int i = 0; i < OPTIONS.length; i++) {
if (key == OPTIONS[i]) {
uint new_hash = gnome_background_settings.get_value (key).hash ();
if (hash_cache[i] != new_hash) {
hash_cache[i] = new_hash;
changed ();
break;
}
}
}
});
// unfortunately the settings sometimes tend to fire random changes even though
// nothing actually happened. The code below is used to prevent us from spamming
// new actors all the time, which lead to some problems in other areas of the code
for (int i = 0; i < OPTIONS.length; i++) {
hash_cache[i] = gnome_background_settings.get_value (OPTIONS[i]).hash ();
}

private void monitors_changed () {
var n = display.get_n_monitors ();
var i = 0;

backgrounds.foreach_remove ((hash, background) => {
if (i++ < n) {
background.update_resolution ();
return false;
} else {
background.changed.disconnect (background_changed);
background.destroy ();
return true;
gnome_background_settings.changed.connect ((key) => {
for (int i = 0; i < OPTIONS.length; i++) {
if (key == OPTIONS[i]) {
uint new_hash = gnome_background_settings.get_value (key).hash ();
if (hash_cache[i] != new_hash) {
hash_cache[i] = new_hash;
changed ();
break;
}
}
});
}
}
});
}

public Background get_background (int monitor_index) {
string? filename = null;
private void monitors_changed () {
var n = display.get_n_monitors ();
var i = 0;

var style = gnome_background_settings.get_enum ("picture-options");
if (style != GDesktop.BackgroundStyle.NONE) {
filename = get_background_path ();
backgrounds.foreach_remove ((hash, background) => {
if (i++ < n) {
background.update_resolution ();
return false;
} else {
background.changed.disconnect (background_changed);
background.destroy ();
return true;
}
});
}

// Animated backgrounds are (potentially) per-monitor, since
// they can have variants that depend on the aspect ratio and
// size of the monitor; for other backgrounds we can use the
// same background object for all monitors.
if (filename == null || !filename.has_suffix (".xml"))
monitor_index = 0;

var background = backgrounds.lookup (monitor_index);
if (background == null) {
background = new Background (display, monitor_index, filename, this, (GDesktop.BackgroundStyle) style);
background.changed.connect (background_changed);
backgrounds.insert (monitor_index, background);
}
public Background get_background (int monitor_index) {
string? filename = null;

return background;
var style = gnome_background_settings.get_enum ("picture-options");
if (style != GDesktop.BackgroundStyle.NONE) {
filename = get_background_path ();
}

private string get_background_path () {
if (Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK) {
var uri = gnome_background_settings.get_string ("picture-uri-dark");
var path = File.new_for_uri (uri).get_path ();
if (FileUtils.test (path, EXISTS)) {
return path;
}
}
// Animated backgrounds are (potentially) per-monitor, since
// they can have variants that depend on the aspect ratio and
// size of the monitor; for other backgrounds we can use the
// same background object for all monitors.
if (filename == null || !filename.has_suffix (".xml"))
monitor_index = 0;

var background = backgrounds.lookup (monitor_index);
if (background == null) {
background = new Background (display, monitor_index, filename, this, (GDesktop.BackgroundStyle) style);
background.changed.connect (background_changed);
backgrounds.insert (monitor_index, background);
}

return background;
}

var uri = gnome_background_settings.get_string ("picture-uri");
private string get_background_path () {
if (Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK) {
var uri = gnome_background_settings.get_string ("picture-uri-dark");
var path = File.new_for_uri (uri).get_path ();
if (FileUtils.test (path, EXISTS)) {
return path;
}

return uri;
}

private void background_changed (Background background) {
background.changed.disconnect (background_changed);
background.destroy ();
backgrounds.remove (background.monitor_index);
var uri = gnome_background_settings.get_string ("picture-uri");
var path = File.new_for_uri (uri).get_path ();
if (FileUtils.test (path, EXISTS)) {
return path;
}

public void destroy () {
monitor_manager.monitors_changed.disconnect (monitors_changed);
monitor_manager = null;
return uri;
}

private void background_changed (Background background) {
background.changed.disconnect (background_changed);
background.destroy ();
backgrounds.remove (background.monitor_index);
}

backgrounds.foreach_remove ((hash, background) => {
background.changed.disconnect (background_changed);
background.destroy ();
return true;
});
}
public void destroy () {
monitor_manager.monitors_changed.disconnect (monitors_changed);
monitor_manager = null;

backgrounds.foreach_remove ((hash, background) => {
background.changed.disconnect (background_changed);
background.destroy ();
return true;
});
}
}
Loading