-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
BackgroundSource.vala
151 lines (126 loc) · 5.7 KB
/
BackgroundSource.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// 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-opacity",
"picture-options",
"picture-uri",
"picture-uri-dark",
"primary-color",
"secondary-color"
};
public signal void changed ();
public Meta.Display display { get; construct; }
public Settings settings { get; construct; }
internal int use_count { get; set; default = 0; }
private Gee.HashMap<int,Background> backgrounds;
private uint[] hash_cache;
public BackgroundSource (Meta.Display display, string settings_schema) {
Object (display: display, settings: new Settings (settings_schema));
}
construct {
backgrounds = new Gee.HashMap<int,Background> ();
hash_cache = new uint[OPTIONS.length];
unowned var monitor_manager = display.get_context ().get_backend ().get_monitor_manager ();
monitor_manager.monitors_changed.connect (monitors_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] = settings.get_value (OPTIONS[i]).hash ();
}
settings.changed.connect ((key) => {
for (int i = 0; i < OPTIONS.length; i++) {
if (key == OPTIONS[i]) {
uint new_hash = settings.get_value (key).hash ();
if (hash_cache[i] != new_hash) {
hash_cache[i] = new_hash;
changed ();
break;
}
}
}
});
unowned var granite_settings = Granite.Settings.get_default ();
granite_settings.notify["prefers-color-scheme"].connect (() => changed ());
}
private void monitors_changed () {
var n = display.get_n_monitors ();
var i = 0;
foreach (var background in backgrounds.values) {
if (i++ < n) {
background.update_resolution ();
continue;
}
background.changed.disconnect (background_changed);
background.destroy ();
// TODO can we remove from a list while iterating?
backgrounds.unset (i);
}
}
public Background get_background (int monitor_index) {
string? filename = null;
var style = settings.get_enum ("picture-options");
if (style != GDesktop.BackgroundStyle.NONE) {
filename = get_background_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;
if (!backgrounds.has_key (monitor_index)) {
var background = new Background (display, monitor_index, filename, this, (GDesktop.BackgroundStyle) style);
background.changed.connect (background_changed);
backgrounds[monitor_index] = background;
}
return backgrounds[monitor_index];
}
private string get_background_path () {
if (Granite.Settings.get_default ().prefers_color_scheme == DARK) {
var uri = settings.get_string ("picture-uri-dark");
var path = File.new_for_uri (uri).get_path ();
if (FileUtils.test (path, EXISTS)) {
return path;
}
}
var uri = settings.get_string ("picture-uri");
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.unset (background.monitor_index);
}
public void destroy () {
unowned var monitor_manager = display.get_context ().get_backend ().get_monitor_manager ();
monitor_manager.monitors_changed.disconnect (monitors_changed);
foreach (var background in backgrounds.values) {
background.changed.disconnect (background_changed);
background.destroy ();
}
}
}
}