-
Notifications
You must be signed in to change notification settings - Fork 21
/
prefs.js
239 lines (191 loc) · 8.16 KB
/
prefs.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const GdkPixbuf = imports.gi.GdkPixbuf;
const Gettext = imports.gettext.domain('gnome-shell-extensions');
const _ = Gettext.gettext;
const N_ = function(e) { return e };
const ExtensionUtils = imports.misc.extensionUtils;
const WORKSPACE_COUNT_KEY = 'workspace-count';
const WORKSPACE_INDEX = 'workspace-index';
const WALLPAPERS_KEY = 'workspace-wallpapers';
const CURRENT_WALLPAPER_KEY = 'picture-uri';
const WalkpaperModel = new GObject.Class({
Name: 'Walkpaper.WalkpaperModel',
GTypeName: 'WalkpaperModel',
Extends: Gtk.ListStore,
Columns: {
NUMBER: 0,
PATH: 1,
},
Thumbnails: [],
_init: function(params) {
this.parent(params);
this.set_column_types([GObject.TYPE_STRING, GObject.TYPE_STRING]);
this._settings = ExtensionUtils.getSettings();
let workspaceNum = this._settings.get_int(WORKSPACE_COUNT_KEY);
for (let i = 0; i < workspaceNum; i++) {
this.Thumbnails.push(null);
}
this._reloadFromSettings();
this.connect('row-changed', this._onRowChanged.bind(this));
},
_reloadFromSettings: function() {
if (this._preventChanges)
return;
this._preventChanges = true;
let workspaceNum = this._settings.get_int(WORKSPACE_COUNT_KEY);
let newPaths = this._settings.get_strv(WALLPAPERS_KEY);
for (let i = newPaths.length; i < workspaceNum; i++) {
newPaths[i] = '';
}
let i = 0;
let [ok, iter] = this.get_iter_first();
while (ok && i < workspaceNum) {
this.set(iter, [this.Columns.PATH], [newPaths[i]]);
this.set(iter, [this.Columns.NUMBER], [parseInt(i+1)]);
ok = this.iter_next(iter);
i++;
}
while (ok)
ok = this.remove(iter);
//Adding new rows
for ( ; i < workspaceNum; i++) {
iter = this.append();
this.set(iter, [this.Columns.PATH], [newPaths[i]]);
this.set(iter, [this.Columns.NUMBER], [parseInt(i+1)]);
}
this._preventChanges = false;
},
_onRowChanged: function(self, path, iter) {
if (this._preventChanges)
return;
this._preventChanges = true;
let index = path.get_indices()[0];
//let index = this._settings.get_int(WORKSPACE_COUNT_KEY);
let paths = this._settings.get_strv(WALLPAPERS_KEY);
if (index >= paths.length) {
// fill with blanks
for (let i = paths.length; i <= index; i++)
paths[i] = '';
}
paths[index] = this.get_value(iter, this.Columns.PATH);
this._settings.set_strv(WALLPAPER_KEY, paths);
this._preventChanges = false;
},
});
const WalkpaperSettingsWidget = new GObject.Class({
Name: 'Walkpaper.WalkpaperSettingsWidget',
GTypeName: 'WalkpaperSettingsWidget',
Extends: Gtk.Box,
Signals: {
'change-wallpaper': { param_types: [ GObject.TYPE_STRING] },
},
_init: function(params) {
this.parent(params);
this.margin = 12;
this.orientation = Gtk.Orientation.VERTICAL;
this.connect('change-wallpaper', this.changeWallpaper)
let scrolled = new Gtk.ScrolledWindow();
//hscroll, vscroll policy
scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
this.append(scrolled);
let _store = new WalkpaperModel();
let _treeView = new Gtk.TreeView({ model: _store,
headers_visible: true,
reorderable: true,
hexpand: true,
vexpand: true });
//Workspace number
let columnNumbers = new Gtk.TreeViewColumn({ title: _("Workspace") });
let rendererNumbers = new Gtk.CellRendererText({ editable: false });
columnNumbers.pack_start(rendererNumbers, true);
columnNumbers.add_attribute(rendererNumbers, 'text', _store.Columns.NUMBER);
_treeView.append_column(columnNumbers);
//Preview picture
let columnImages = new Gtk.TreeViewColumn({title: "Preview" });
let rendererImages = new Gtk.CellRendererPixbuf();
rendererImages.set_fixed_size(240, 120);
columnImages.pack_start(rendererImages, true);
columnImages.set_cell_data_func(rendererImages, this.getCellPreviewPixbuf)
_treeView.append_column(columnImages);
//Workspace wallpapers paths
let columnPaths = new Gtk.TreeViewColumn({ title: _("Path to wallpaper") });
let rendererPaths = new Gtk.CellRendererText({ editable: false });
columnPaths.pack_start(rendererPaths, true);
columnPaths.add_attribute(rendererPaths, 'text', _store.Columns.PATH);
_treeView.append_column(columnPaths);
_treeView.connect('row-activated', this._editPath.bind(this));
scrolled.set_child(_treeView);
},
_editPath: function(renderer, path, data) {
let chooser = new Gtk.FileChooserDialog({
action: Gtk.FileChooserAction.OPEN,
select_multiple: false,
transient_for: renderer.get_ancestor(Gtk.Window),
title: 'Select Wallpaper'});
let filter = new Gtk.FileFilter();
filter.set_name("Wallpapers");
filter.add_pattern("*.png");
filter.add_pattern("*.jpg");
filter.add_pattern("*.jpeg");
filter.add_pattern("*.tga");
chooser.add_filter(filter);
chooser.add_button('Cancel', Gtk.ResponseType.CANCEL);
chooser.add_button('OK', Gtk.ResponseType.OK);
chooser.connect('response', this._onEditPath.bind(chooser, path, renderer));
chooser.show();
},
_onEditPath: function(path, parent, source, result) {
if (result === Gtk.ResponseType.OK) {
let file = source.get_file();
let filename = "file://" + file.get_path();
//We own the file and need to release them after being done
file.unref();
let _store = new WalkpaperModel();
let [ok, iter] = _store.get_iter(path);
if (ok) {
_store.set(iter, [_store.Columns.PATH], [filename]);
//Check if we changed current wallpaper
let _settings = ExtensionUtils.getSettings();
let index = _settings.get_int(WORKSPACE_INDEX);
if (_store.get_string_from_iter(iter) == '' + index) {
//We need to change the wallpaper immediately because workspace change
//triggers wallpaper save
parent.get_ancestor(WalkpaperSettingsWidget).emit('change-wallpaper', filename);
}
//Invalidate thumbnail so it is recreated
let thumb_index = parseInt(_store.get_string_from_iter(iter)) + 1;
_store.Thumbnails[thumb_index] = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename.replace(/file:\/\//, ""), 240, 160, true);
}
}
source.destroy();
},
changeWallpaper: function(source, wallpaper) {
const BACKGROUND_SCHEMA = 'org.gnome.desktop.background';
let backgroundSettings = new Gio.Settings({ schema_id: BACKGROUND_SCHEMA });
backgroundSettings.set_string(CURRENT_WALLPAPER_KEY, wallpaper);
},
getCellPreviewPixbuf: function(col, cell, model, iter, user_data) {
let index = model.get_value(iter, [model.Columns.NUMBER]);
if (model.Thumbnails[index] == null) {
let path = model.get_value(iter, [model.Columns.PATH]).replace(/file:\/\//, "");
if (path !== "") {
model.Thumbnails[index] = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, 240, 160, true);
}
}
if (model.Thumbnails[index] != null) {
cell.set_property('pixbuf', model.Thumbnails[index]);
}
}
});
function init() {
ExtensionUtils.initTranslations();
}
function buildPrefsWidget() {
let widget = new WalkpaperSettingsWidget();
widget.show();
return widget;
}