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 BackgroundCache : Object {
20- private static BackgroundCache ? instance = null ;
21-
22- public static unowned BackgroundCache get_default () {
23- if (instance == null )
24- instance = new BackgroundCache ();
25-
26- return instance;
27- }
1+ /*
2+ * SPDX-License-Identifier: GPL-3.0-or-later
3+ * SPDX-FileCopyrightText: 2014 Tom Beckmann
4+ * 2025 elementary, Inc. (https://elementary.io)
5+ */
286
29- public signal void file_changed (string filename );
7+ public class Gala.BackgroundCache : Object {
8+ private static BackgroundCache ? instance = null ;
309
31- private Gee . HashMap<string,FileMonitor > file_monitors;
32- private BackgroundSource background_source;
10+ public static unowned BackgroundCache get_default () {
11+ if (instance == null )
12+ instance = new BackgroundCache ();
3313
34- private Animation animation;
14+ return instance;
15+ }
3516
36- public BackgroundCache () {
37- Object ();
38- }
17+ public signal void file_changed (string filename );
3918
40- construct {
41- file_monitors = new Gee .HashMap<string,FileMonitor > ();
42- }
19+ private Gee . HashMap<string,FileMonitor > file_monitors;
20+ private BackgroundSource background_source;
4321
44- public void monitor_file (string filename ) {
45- if (file_monitors. has_key (filename))
46- return ;
47-
48- var file = File . new_for_path (filename);
49- try {
50- var monitor = file. monitor (FileMonitorFlags . NONE , null );
51- monitor. changed. connect (() = > {
52- file_changed (filename);
53- });
54-
55- file_monitors[filename] = monitor;
56- } catch (Error e) {
57- warning (" Failed to monitor %s : %s " , filename, e. message);
58- }
59- }
22+ private Animation animation;
6023
61- public async Animation get_animation ( string filename ) {
62- if (animation != null && animation . filename == filename) {
63- Idle . add (() = > {
64- get_animation . callback ();
65- return Source . REMOVE ;
66- } );
67- yield ;
24+ public BackgroundCache ( ) {
25+ Object ();
26+ }
27+
28+ construct {
29+ file_monitors = new Gee . HashMap<string, FileMonitor > ( );
30+ }
6831
69- return animation;
70- }
32+ public void monitor_file (string filename ) {
33+ if (file_monitors. has_key (filename))
34+ return ;
7135
72- var animation = new Animation (filename);
36+ var file = File . new_for_path (filename);
37+ try {
38+ var monitor = file. monitor (FileMonitorFlags . NONE , null );
39+ monitor. changed. connect (() = > {
40+ file_changed (filename);
41+ });
7342
74- yield animation. load ();
43+ file_monitors[filename] = monitor;
44+ } catch (Error e) {
45+ warning (" Failed to monitor %s : %s " , filename, e. message);
46+ }
47+ }
7548
49+ public async Animation get_animation (string filename ) {
50+ if (animation != null && animation. filename == filename) {
7651 Idle . add (() = > {
7752 get_animation. callback ();
7853 return Source . REMOVE ;
@@ -82,20 +57,32 @@ namespace Gala {
8257 return animation;
8358 }
8459
85- public BackgroundSource get_background_source (Meta .Display display ) {
86- if (background_source == null ) {
87- background_source = new BackgroundSource (display);
88- background_source. use_count = 1 ;
89- } else
90- background_source. use_count++ ;
60+ var animation = new Animation (filename);
9161
92- return background_source;
93- }
62+ yield animation. load ();
63+
64+ Idle . add (() = > {
65+ get_animation. callback ();
66+ return Source . REMOVE ;
67+ });
68+ yield ;
69+
70+ return animation;
71+ }
72+
73+ public BackgroundSource get_background_source (Meta .Display display ) {
74+ if (background_source == null ) {
75+ background_source = new BackgroundSource (display);
76+ background_source. use_count = 1 ;
77+ } else
78+ background_source. use_count++ ;
79+
80+ return background_source;
81+ }
9482
95- public void release_background_source () {
96- if (-- background_source. use_count == 0 ) {
97- background_source. destroy ();
98- }
83+ public void release_background_source () {
84+ if (-- background_source. use_count == 0 ) {
85+ background_source. destroy ();
9986 }
10087 }
10188}
0 commit comments