-
Notifications
You must be signed in to change notification settings - Fork 7
/
extension.js
160 lines (132 loc) · 4.6 KB
/
extension.js
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
152
153
154
155
156
157
158
159
160
const Lang = imports.lang;
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const GLib = imports.gi.GLib;
const St = imports.gi.St;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Util = imports.misc.util;
const Self = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Self.imports.utils;
const TIMER = {
minutes: 0,
hours: 0,
running: true,
toSeconds: function () {
return this.minutes * 60 + this.hours * 3600
}
}
let panelEntry;
function init() {
}
function enable() {
panelEntry = new WallpaperChangerEntry();
Main.panel.addToStatusArea('wallpaper-changer-menu', panelEntry);
}
function disable() {
panelEntry.destroy();
}
const WallpaperChangerEntry = new Lang.Class({
Extends: PanelMenu.Button,
Name: 'WallpaperChangerEntry',
_init: function () {
this.parent(0, 'WallpaperChangerEntry');
this.settings = Utils.getSettings();
this.settings.connect('changed::minutes', Lang.bind(this, this._applyTimer));
this.settings.connect('changed::hours', Lang.bind(this, this._applyTimer));
this.settings.connect('changed::provider', Lang.bind(this, this._applyProvider));
this.settings.connect('changed::debug', Lang.bind(this, function () {
Utils.DEBUG = this.settings.get_boolean('debug');
}));
Utils.DEBUG = this.settings.get_boolean('debug');
Utils.debug('_init', this.__name__);
this._applyProvider();
this._applyTimer();
const icon = new St.Icon({
icon_name: 'preferences-desktop-wallpaper-symbolic',
style_class: 'system-status-icon'
});
this.actor.add_child(icon);
// Construct items
const nextItem = new PopupMenu.PopupMenuItem('Next Wallpaper');
const settingsItem = new PopupMenu.PopupMenuItem('Settings');
const separatorItem = new PopupMenu.PopupSeparatorMenuItem('');
const pauseItem = new PopupMenu.PopupMenuItem('Pause');
// Add items to menu
this.menu.addMenuItem(nextItem);
this.menu.addMenuItem(pauseItem);
this.menu.addMenuItem(separatorItem);
this.menu.addMenuItem(settingsItem);
// Bind events
settingsItem.connect('activate', Lang.bind(this, this._openSettings));
nextItem.connect('activate', Lang.bind(this, this._nextWallpaper));
pauseItem.connect('activate', Lang.bind(this, this._pauseToggle(pauseItem)));
},
_openSettings: function () {
Utils.debug('_openSettings', this.__name__);
Util.spawn(['gnome-shell-extension-prefs', Self.uuid]);
},
_nextWallpaper: function () {
this.provider.next(Lang.bind(this, this._setWallpaper));
this._resetTimer();
},
_pauseToggle: function (pauseItem) {
return function () {
TIMER.running = !TIMER.running;
Utils.debug('pause - timer running = ' + TIMER.running);
pauseItem.label.set_text(TIMER.running ? 'Pause' : 'Unpause');
this._resetTimer();
}
},
_applyProvider: function () {
Utils.debug('_applyProvider', this.__name__);
this.provider = Utils.getProvider(this.settings.get_string('provider'));
this._nextWallpaper();
this.provider.connect('wallpapers-changed', Lang.bind(this, function (provider) {
if (provider === this.provider) {
Utils.debug('wallpapers-changed signal received', this.__name__);
this._nextWallpaper();
}
}));
},
_applyTimer: function () {
Utils.debug('_applyTimer', this.__name__);
TIMER.minutes = this.settings.get_int('minutes');
TIMER.hours = this.settings.get_int('hours');
this._resetTimer();
},
_resetTimer: function () {
Utils.debug('_resetTimer', this.__name__);
if (this.timer) {
GLib.Source.remove(this.timer);
}
if (TIMER.running && TIMER.toSeconds() > 0) {
Utils.debug('Set to ' + TIMER.toSeconds(), this.__name__);
this.timer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT,
TIMER.toSeconds(),
Lang.bind(this, function () {
this.timer = null;
this._nextWallpaper();
return false;
})
);
} else {
this.timer = null;
}
},
_setWallpaper: function (path) {
Utils.debug('_setWallpaper', this.__name__);
const background_setting = new Gio.Settings({ schema: 'org.gnome.desktop.background' });
if (background_setting.is_writable('picture-uri')) {
if (background_setting.set_string('picture-uri', 'file://' + path)) {
Utils.debug(path, this.__name__);
Gio.Settings.sync();
} else {
Utils.debug('Unable to set wallpaper', this.__name__)
}
} else {
Utils.debug('Can\'t write to org.gnome.desktop.background', this.__name__);
}
}
});