-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefs.js
159 lines (136 loc) · 5.96 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
/* Desktop Icons GNOME Shell extension
*
* Copyright (C) 2017 Carlos Soriano <[email protected]>
*
* 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/>.
*/
const Gtk = imports.gi.Gtk;
const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;
const GioSSS = Gio.SettingsSchemaSource;
const ExtensionUtils = imports.misc.extensionUtils;
const Gettext = imports.gettext;
const Config = imports.misc.config;
var _ = Gettext.domain('desktop-icons').gettext;
const SCHEMA_NAUTILUS = 'org.gnome.nautilus.preferences';
const SCHEMA_GTK = 'org.gtk.Settings.FileChooser';
const SCHEMA = 'org.gnome.shell.extensions.desktop-icons';
const ICON_SIZE = { 'small': 48, 'standard': 64, 'large': 96 };
const ICON_WIDTH = { 'small': 108, 'standard': 116, 'large': 116 };
const ICON_HEIGHT = { 'small': 86, 'standard': 102, 'large': 134 };
var FileType = {
NONE: null,
USER_DIRECTORY_HOME: 'show-home',
USER_DIRECTORY_TRASH: 'show-trash',
}
var nautilusSettings;
var gtkSettings;
var settings;
// This is already in Nautilus settings, so it should not be made tweakable here
var CLICK_POLICY_SINGLE = false;
function initTranslations() {
let extension = ExtensionUtils.getCurrentExtension();
let localedir = extension.dir.get_child('locale');
if (localedir.query_exists(null))
Gettext.bindtextdomain('desktop-icons', localedir.get_path());
else
Gettext.bindtextdomain('desktop-icons', Config.LOCALEDIR);
}
function init() {
let schemaSource = GioSSS.get_default();
let schemaGtk = schemaSource.lookup(SCHEMA_GTK, true);
gtkSettings = new Gio.Settings({ settings_schema: schemaGtk });
let schemaObj = schemaSource.lookup(SCHEMA_NAUTILUS, true);
if (!schemaObj) {
nautilusSettings = null;
} else {
nautilusSettings = new Gio.Settings({ settings_schema: schemaObj });;
nautilusSettings.connect('changed', _onNautilusSettingsChanged);
_onNautilusSettingsChanged();
}
settings = get_schema(SCHEMA);
}
function get_schema(schema) {
let extension = ExtensionUtils.getCurrentExtension();
// check if this extension was built with "make zip-file", and thus
// has the schema files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell (and therefore schemas are available
// in the standard folders)
let schemaDir = extension.dir.get_child('schemas');
let schemaSource;
if (schemaDir.query_exists(null))
schemaSource = GioSSS.new_from_directory(schemaDir.get_path(), GioSSS.get_default(), false);
else
schemaSource = GioSSS.get_default();
let schemaObj = schemaSource.lookup(schema, true);
if (!schemaObj)
throw new Error('Schema ' + schema + ' could not be found for extension ' + extension.metadata.uuid + '. Please check your installation.');
return new Gio.Settings({ settings_schema: schemaObj });
}
function buildPrefsWidget() {
initTranslations();
let frame = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, border_width: 10, spacing: 10 });
frame.add(buildSelector('icon-size', _("Size for the desktop icons"), { 'small': _("Small"), 'standard': _("Standard"), 'large': _("Large") }));
frame.add(buildSwitcher('show-home', _("Show the personal folder in the desktop")));
frame.add(buildSwitcher('show-trash', _("Show the trash icon in the desktop")));
frame.show_all();
return frame;
}
function buildSwitcher(key, labelText) {
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });
let label = new Gtk.Label({ label: labelText, xalign: 0 });
let switcher = new Gtk.Switch({ active: settings.get_boolean(key) });
settings.bind(key, switcher, 'active', 3);
hbox.pack_start(label, true, true, 0);
hbox.add(switcher);
return hbox;
}
function buildSelector(key, labelText, elements) {
let listStore = new Gtk.ListStore();
listStore.set_column_types ([GObject.TYPE_STRING, GObject.TYPE_STRING]);
let schemaKey = settings.settings_schema.get_key(key);
let values = schemaKey.get_range().get_child_value(1).get_child_value(0).get_strv();
for (let val of values) {
let iter = listStore.append();
let visibleText = val;
if (visibleText in elements)
visibleText = elements[visibleText];
listStore.set (iter, [0, 1], [visibleText, val]);
}
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });
let label = new Gtk.Label({ label: labelText, xalign: 0 });
let combo = new Gtk.ComboBox({model: listStore});
let rendererText = new Gtk.CellRendererText();
combo.pack_start (rendererText, false);
combo.add_attribute (rendererText, 'text', 0);
combo.set_id_column(1);
settings.bind(key, combo, 'active-id', 3);
hbox.pack_start(label, true, true, 0);
hbox.add(combo);
return hbox;
}
function _onNautilusSettingsChanged() {
CLICK_POLICY_SINGLE = nautilusSettings.get_string('click-policy') == 'single';
}
function get_icon_size() {
// this one doesn't need scaling because Gnome Shell automagically scales the icons
return ICON_SIZE[settings.get_string('icon-size')];
}
function getDesiredWidth(scale_factor, margin) {
return (ICON_WIDTH[settings.get_string('icon-size')] + margin) * scale_factor;
}
function getDesiredHeight(scale_factor, margin) {
return (ICON_HEIGHT[settings.get_string('icon-size')] + margin) * scale_factor;
}