prefs.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Desktop Icons GNOME Shell extension
  2. *
  3. * Copyright (C) 2017 Carlos Soriano <csoriano@redhat.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. const Gtk = imports.gi.Gtk;
  19. const GObject = imports.gi.GObject;
  20. const Gio = imports.gi.Gio;
  21. const GioSSS = Gio.SettingsSchemaSource;
  22. const ExtensionUtils = imports.misc.extensionUtils;
  23. const Gettext = imports.gettext;
  24. const Config = imports.misc.config;
  25. var _ = Gettext.domain('desktop-icons').gettext;
  26. const SCHEMA_NAUTILUS = 'org.gnome.nautilus.preferences';
  27. const SCHEMA_GTK = 'org.gtk.Settings.FileChooser';
  28. const SCHEMA = 'org.gnome.shell.extensions.desktop-icons';
  29. const ICON_SIZE = { 'small': 48, 'standard': 64, 'large': 96 };
  30. const ICON_WIDTH = { 'small': 108, 'standard': 116, 'large': 116 };
  31. const ICON_HEIGHT = { 'small': 86, 'standard': 102, 'large': 134 };
  32. var FileType = {
  33. NONE: null,
  34. USER_DIRECTORY_HOME: 'show-home',
  35. USER_DIRECTORY_TRASH: 'show-trash',
  36. MOUNT_DISK: 'mount-disk',
  37. }
  38. var nautilusSettings;
  39. var gtkSettings;
  40. var settings;
  41. // This is already in Nautilus settings, so it should not be made tweakable here
  42. var CLICK_POLICY_SINGLE = false;
  43. function initTranslations() {
  44. let extension = ExtensionUtils.getCurrentExtension();
  45. let localedir = extension.dir.get_child('locale');
  46. if (localedir.query_exists(null))
  47. Gettext.bindtextdomain('desktop-icons', localedir.get_path());
  48. else
  49. Gettext.bindtextdomain('desktop-icons', Config.LOCALEDIR);
  50. }
  51. function init() {
  52. let schemaSource = GioSSS.get_default();
  53. let schemaGtk = schemaSource.lookup(SCHEMA_GTK, true);
  54. gtkSettings = new Gio.Settings({ settings_schema: schemaGtk });
  55. let schemaObj = schemaSource.lookup(SCHEMA_NAUTILUS, true);
  56. if (!schemaObj) {
  57. nautilusSettings = null;
  58. } else {
  59. nautilusSettings = new Gio.Settings({ settings_schema: schemaObj });;
  60. nautilusSettings.connect('changed', _onNautilusSettingsChanged);
  61. _onNautilusSettingsChanged();
  62. }
  63. settings = get_schema(SCHEMA);
  64. }
  65. function get_schema(schema) {
  66. let extension = ExtensionUtils.getCurrentExtension();
  67. // check if this extension was built with "make zip-file", and thus
  68. // has the schema files in a subfolder
  69. // otherwise assume that extension has been installed in the
  70. // same prefix as gnome-shell (and therefore schemas are available
  71. // in the standard folders)
  72. let schemaDir = extension.dir.get_child('schemas');
  73. let schemaSource;
  74. if (schemaDir.query_exists(null))
  75. schemaSource = GioSSS.new_from_directory(schemaDir.get_path(), GioSSS.get_default(), false);
  76. else
  77. schemaSource = GioSSS.get_default();
  78. let schemaObj = schemaSource.lookup(schema, true);
  79. if (!schemaObj)
  80. throw new Error('Schema ' + schema + ' could not be found for extension ' + extension.metadata.uuid + '. Please check your installation.');
  81. return new Gio.Settings({ settings_schema: schemaObj });
  82. }
  83. function buildPrefsWidget() {
  84. initTranslations();
  85. let frame = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, border_width: 10, spacing: 10 });
  86. frame.add(buildSelector('icon-size', _("Size for the desktop icons"), { 'small': _("Small"), 'standard': _("Standard"), 'large': _("Large") }));
  87. frame.add(buildSwitcher('show-home', _("Show the personal folder in the desktop")));
  88. frame.add(buildSwitcher('show-trash', _("Show the trash icon in the desktop")));
  89. frame.add(buildSwitcher('show-mount', _("Show mounted drives in the desktop")));
  90. frame.show_all();
  91. return frame;
  92. }
  93. function buildSwitcher(key, labelText) {
  94. let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });
  95. let label = new Gtk.Label({ label: labelText, xalign: 0 });
  96. let switcher = new Gtk.Switch({ active: settings.get_boolean(key) });
  97. settings.bind(key, switcher, 'active', 3);
  98. hbox.pack_start(label, true, true, 0);
  99. hbox.add(switcher);
  100. return hbox;
  101. }
  102. function buildSelector(key, labelText, elements) {
  103. let listStore = new Gtk.ListStore();
  104. listStore.set_column_types ([GObject.TYPE_STRING, GObject.TYPE_STRING]);
  105. let schemaKey = settings.settings_schema.get_key(key);
  106. let values = schemaKey.get_range().get_child_value(1).get_child_value(0).get_strv();
  107. for (let val of values) {
  108. let iter = listStore.append();
  109. let visibleText = val;
  110. if (visibleText in elements)
  111. visibleText = elements[visibleText];
  112. listStore.set (iter, [0, 1], [visibleText, val]);
  113. }
  114. let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });
  115. let label = new Gtk.Label({ label: labelText, xalign: 0 });
  116. let combo = new Gtk.ComboBox({model: listStore});
  117. let rendererText = new Gtk.CellRendererText();
  118. combo.pack_start (rendererText, false);
  119. combo.add_attribute (rendererText, 'text', 0);
  120. combo.set_id_column(1);
  121. settings.bind(key, combo, 'active-id', 3);
  122. hbox.pack_start(label, true, true, 0);
  123. hbox.add(combo);
  124. return hbox;
  125. }
  126. function _onNautilusSettingsChanged() {
  127. CLICK_POLICY_SINGLE = nautilusSettings.get_string('click-policy') == 'single';
  128. }
  129. function get_icon_size() {
  130. // this one doesn't need scaling because Gnome Shell automagically scales the icons
  131. return ICON_SIZE[settings.get_string('icon-size')];
  132. }
  133. function getDesiredWidth(scale_factor, margin) {
  134. return (ICON_WIDTH[settings.get_string('icon-size')] + margin) * scale_factor;
  135. }
  136. function getDesiredHeight(scale_factor, margin) {
  137. return (ICON_HEIGHT[settings.get_string('icon-size')] + margin) * scale_factor;
  138. }