Skip to content

Commit 19a47b5

Browse files
committed
BackgroundSource: inline namespace
1 parent de8752f commit 19a47b5

File tree

1 file changed

+123
-136
lines changed

1 file changed

+123
-136
lines changed
Lines changed: 123 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,153 @@
1-
//
2-
// Copyright (C) 2014 Tom Beckmann
3-
//
4-
// This program is free software: you can redistribute it and/or modify
5-
// it under the terms of the GNU General Public License as published by
6-
// the Free Software Foundation, either version 3 of the License, or
7-
// (at your option) any later version.
8-
//
9-
// This program is distributed in the hope that it will be useful,
10-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
// GNU General Public License for more details.
13-
//
14-
// You should have received a copy of the GNU General Public License
15-
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
//
17-
18-
namespace Gala {
19-
public class BackgroundSource : Object {
20-
// list of keys that are actually relevant for us
21-
private const string[] OPTIONS = {
22-
"color-shading-type",
23-
"picture-options",
24-
"picture-uri",
25-
"picture-uri-dark",
26-
"primary-color",
27-
"secondary-color"
28-
};
29-
30-
public signal void changed ();
31-
32-
public Meta.Display display { get; construct; }
33-
public GLib.Settings gnome_background_settings { get; private set; }
34-
35-
internal int use_count { get; set; default = 0; }
36-
37-
private GLib.HashTable<int, Background> backgrounds;
38-
private uint[] hash_cache;
39-
private Meta.MonitorManager? monitor_manager;
40-
private GLib.Settings gala_background_settings;
41-
42-
public bool should_dim {
43-
get {
44-
return (
45-
Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK &&
46-
gala_background_settings.get_boolean ("dim-wallpaper-in-dark-style")
47-
);
48-
}
49-
}
50-
51-
public BackgroundSource (Meta.Display display) {
52-
Object (display: display);
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2014 Tom Beckmann
4+
* 2025 elementary, Inc. (https://elementary.io)
5+
*/
6+
7+
public class Gala.BackgroundSource : Object {
8+
// list of keys that are actually relevant for us
9+
private const string[] OPTIONS = {
10+
"color-shading-type",
11+
"picture-options",
12+
"picture-uri",
13+
"picture-uri-dark",
14+
"primary-color",
15+
"secondary-color"
16+
};
17+
18+
public signal void changed ();
19+
20+
public Meta.Display display { get; construct; }
21+
public GLib.Settings gnome_background_settings { get; private set; }
22+
23+
internal int use_count { get; set; default = 0; }
24+
25+
private GLib.HashTable<int, Background> backgrounds;
26+
private uint[] hash_cache;
27+
private Meta.MonitorManager? monitor_manager;
28+
private GLib.Settings gala_background_settings;
29+
30+
public bool should_dim {
31+
get {
32+
return (
33+
Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK &&
34+
gala_background_settings.get_boolean ("dim-wallpaper-in-dark-style")
35+
);
5336
}
37+
}
5438

55-
construct {
56-
backgrounds = new GLib.HashTable<int, Background> (GLib.direct_hash, GLib.direct_equal);
57-
hash_cache = new uint[OPTIONS.length];
39+
public BackgroundSource (Meta.Display display) {
40+
Object (display: display);
41+
}
5842

59-
monitor_manager = display.get_context ().get_backend ().get_monitor_manager ();
60-
monitor_manager.monitors_changed.connect (monitors_changed);
43+
construct {
44+
backgrounds = new GLib.HashTable<int, Background> (GLib.direct_hash, GLib.direct_equal);
45+
hash_cache = new uint[OPTIONS.length];
6146

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

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

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

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

76-
gnome_background_settings.changed.connect ((key) => {
77-
for (int i = 0; i < OPTIONS.length; i++) {
78-
if (key == OPTIONS[i]) {
79-
uint new_hash = gnome_background_settings.get_value (key).hash ();
80-
if (hash_cache[i] != new_hash) {
81-
hash_cache[i] = new_hash;
82-
changed ();
83-
break;
84-
}
85-
}
86-
}
87-
});
57+
// unfortunately the settings sometimes tend to fire random changes even though
58+
// nothing actually happened. The code below is used to prevent us from spamming
59+
// new actors all the time, which lead to some problems in other areas of the code
60+
for (int i = 0; i < OPTIONS.length; i++) {
61+
hash_cache[i] = gnome_background_settings.get_value (OPTIONS[i]).hash ();
8862
}
8963

90-
private void monitors_changed () {
91-
var n = display.get_n_monitors ();
92-
var i = 0;
93-
94-
backgrounds.foreach_remove ((hash, background) => {
95-
if (i++ < n) {
96-
background.update_resolution ();
97-
return false;
98-
} else {
99-
background.changed.disconnect (background_changed);
100-
background.destroy ();
101-
return true;
64+
gnome_background_settings.changed.connect ((key) => {
65+
for (int i = 0; i < OPTIONS.length; i++) {
66+
if (key == OPTIONS[i]) {
67+
uint new_hash = gnome_background_settings.get_value (key).hash ();
68+
if (hash_cache[i] != new_hash) {
69+
hash_cache[i] = new_hash;
70+
changed ();
71+
break;
72+
}
10273
}
103-
});
104-
}
74+
}
75+
});
76+
}
10577

106-
public Background get_background (int monitor_index) {
107-
string? filename = null;
78+
private void monitors_changed () {
79+
var n = display.get_n_monitors ();
80+
var i = 0;
10881

109-
var style = gnome_background_settings.get_enum ("picture-options");
110-
if (style != GDesktop.BackgroundStyle.NONE) {
111-
filename = get_background_path ();
82+
backgrounds.foreach_remove ((hash, background) => {
83+
if (i++ < n) {
84+
background.update_resolution ();
85+
return false;
86+
} else {
87+
background.changed.disconnect (background_changed);
88+
background.destroy ();
89+
return true;
11290
}
91+
});
92+
}
11393

114-
// Animated backgrounds are (potentially) per-monitor, since
115-
// they can have variants that depend on the aspect ratio and
116-
// size of the monitor; for other backgrounds we can use the
117-
// same background object for all monitors.
118-
if (filename == null || !filename.has_suffix (".xml"))
119-
monitor_index = 0;
120-
121-
var background = backgrounds.lookup (monitor_index);
122-
if (background == null) {
123-
background = new Background (display, monitor_index, filename, this, (GDesktop.BackgroundStyle) style);
124-
background.changed.connect (background_changed);
125-
backgrounds.insert (monitor_index, background);
126-
}
94+
public Background get_background (int monitor_index) {
95+
string? filename = null;
12796

128-
return background;
97+
var style = gnome_background_settings.get_enum ("picture-options");
98+
if (style != GDesktop.BackgroundStyle.NONE) {
99+
filename = get_background_path ();
129100
}
130101

131-
private string get_background_path () {
132-
if (Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK) {
133-
var uri = gnome_background_settings.get_string ("picture-uri-dark");
134-
var path = File.new_for_uri (uri).get_path ();
135-
if (FileUtils.test (path, EXISTS)) {
136-
return path;
137-
}
138-
}
102+
// Animated backgrounds are (potentially) per-monitor, since
103+
// they can have variants that depend on the aspect ratio and
104+
// size of the monitor; for other backgrounds we can use the
105+
// same background object for all monitors.
106+
if (filename == null || !filename.has_suffix (".xml"))
107+
monitor_index = 0;
108+
109+
var background = backgrounds.lookup (monitor_index);
110+
if (background == null) {
111+
background = new Background (display, monitor_index, filename, this, (GDesktop.BackgroundStyle) style);
112+
background.changed.connect (background_changed);
113+
backgrounds.insert (monitor_index, background);
114+
}
115+
116+
return background;
117+
}
139118

140-
var uri = gnome_background_settings.get_string ("picture-uri");
119+
private string get_background_path () {
120+
if (Drawing.StyleManager.get_instance ().prefers_color_scheme == DARK) {
121+
var uri = gnome_background_settings.get_string ("picture-uri-dark");
141122
var path = File.new_for_uri (uri).get_path ();
142123
if (FileUtils.test (path, EXISTS)) {
143124
return path;
144125
}
145-
146-
return uri;
147126
}
148127

149-
private void background_changed (Background background) {
150-
background.changed.disconnect (background_changed);
151-
background.destroy ();
152-
backgrounds.remove (background.monitor_index);
128+
var uri = gnome_background_settings.get_string ("picture-uri");
129+
var path = File.new_for_uri (uri).get_path ();
130+
if (FileUtils.test (path, EXISTS)) {
131+
return path;
153132
}
154133

155-
public void destroy () {
156-
monitor_manager.monitors_changed.disconnect (monitors_changed);
157-
monitor_manager = null;
134+
return uri;
135+
}
136+
137+
private void background_changed (Background background) {
138+
background.changed.disconnect (background_changed);
139+
background.destroy ();
140+
backgrounds.remove (background.monitor_index);
141+
}
158142

159-
backgrounds.foreach_remove ((hash, background) => {
160-
background.changed.disconnect (background_changed);
161-
background.destroy ();
162-
return true;
163-
});
164-
}
143+
public void destroy () {
144+
monitor_manager.monitors_changed.disconnect (monitors_changed);
145+
monitor_manager = null;
146+
147+
backgrounds.foreach_remove ((hash, background) => {
148+
background.changed.disconnect (background_changed);
149+
background.destroy ();
150+
return true;
151+
});
165152
}
166153
}

0 commit comments

Comments
 (0)