prefs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Bing Wallpaper GNOME extension
  2. // Copyright (C) 2017-2023 Michael Carroll
  3. // This extension is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Lesser General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // See the GNU General Public License, version 3 or later for details.
  8. // Based on GNOME shell extension NASA APOD by Elia Argentieri https://github.com/Elinvention/gnome-shell-extension-nasa-apod
  9. import Gtk from 'gi://Gtk';
  10. import Gdk from 'gi://Gdk';
  11. import Gio from 'gi://Gio';
  12. import Soup from 'gi://Soup';
  13. import Adw from 'gi://Adw';
  14. import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
  15. import * as Config from 'resource:///org/gnome/Shell/Extensions/js/misc/config.js';
  16. import * as Utils from './utils.js';
  17. import Carousel from './carousel.js';
  18. const BingImageURL = Utils.BingImageURL;
  19. var DESKTOP_SCHEMA = 'org.gnome.desktop.background';
  20. // this is pretty wide because of the size of the gallery
  21. var PREFS_DEFAULT_WIDTH = 750;
  22. var PREFS_DEFAULT_HEIGHT = 750;
  23. export default class BingWallpaperExtensionPreferences extends ExtensionPreferences {
  24. fillPreferencesWindow(window) {
  25. // formally globals
  26. let settings = this.getSettings(Utils.BING_SCHEMA);
  27. //let desktop_settings = this.getSettings(Utils.DESKTOP_SCHEMA);
  28. window.set_default_size(PREFS_DEFAULT_WIDTH, PREFS_DEFAULT_HEIGHT);
  29. /*let icon_image = null;*/
  30. let provider = new Gtk.CssProvider();
  31. provider.load_from_path(this.dir.get_path() + '/ui/prefs.css');
  32. Gtk.StyleContext.add_provider_for_display(
  33. Gdk.Display.get_default(),
  34. provider,
  35. Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
  36. let carousel = null;
  37. let httpSession = null;
  38. let log = (msg) => { // avoids need for globals
  39. if (settings.get_boolean('debug-logging'))
  40. console.log("BingWallpaper extension: " + msg); // disable to keep the noise down in journal
  41. }
  42. let buildable = new Gtk.Builder();
  43. // GTK4 removes some properties, and builder breaks when it sees them
  44. buildable.add_from_file( this.dir.get_path() + '/ui/prefsadw.ui' );
  45. // adw or gtk objects we'll attach to below
  46. const settings_page = buildable.get_object('settings_page');
  47. const hideSwitch = buildable.get_object('hideSwitch');
  48. const notifySwitch = buildable.get_object('notifySwitch');
  49. const iconEntry = buildable.get_object('iconEntry');
  50. const bgSwitch = buildable.get_object('bgSwitch');
  51. const shuffleSwitch = buildable.get_object('shuffleSwitch');
  52. const shuffleInterval = buildable.get_object('shuffleInterval');
  53. const folderRow = buildable.get_object('folderRow');
  54. const lockscreen_page = buildable.get_object('lockscreen_page');
  55. const overrideSwitch = buildable.get_object('overrideSwitch');
  56. const blurPresets = buildable.get_object('blurPresets');
  57. const strengthEntry = buildable.get_object('strengthEntry');
  58. const brightnessEntry = buildable.get_object('brightnessEntry');
  59. const blurAdjustment = buildable.get_object('blurAdjustment');
  60. const brightnessAdjustment = buildable.get_object('brightnessAdjustment');
  61. const resolutionEntry = buildable.get_object('resolutionEntry');
  62. const debugSwitch = buildable.get_object('debug_switch');
  63. const revertSwitch = buildable.get_object('revert_switch');
  64. const trash_purge_switch = buildable.get_object('trash_purge_switch');
  65. const always_export_switch = buildable.get_object('always_export_switch');
  66. const gallery_page = buildable.get_object('gallery_page');
  67. const carouselFlowBox = buildable.get_object('carouselFlowBox');
  68. const randomIntervalEntry = buildable.get_object('entry_random_interval');
  69. const debug_page = buildable.get_object('debug_page');
  70. const json_actionrow = buildable.get_object('json_actionrow');
  71. const about_page = buildable.get_object('about_page');
  72. const version_button = buildable.get_object('version_button');
  73. const change_log = buildable.get_object('change_log');
  74. window.add(settings_page);
  75. window.add(lockscreen_page);
  76. window.add(gallery_page);
  77. window.add(debug_page);
  78. window.add(about_page);
  79. iconEntry.set_value(1+Utils.icon_list.indexOf(settings.get_string('icon-name')));
  80. // shuffle intervals
  81. const shuffleIntervals = new Gtk.StringList;
  82. Utils.randomIntervals.forEach((x) => {
  83. shuffleIntervals.append(_(x.title));
  84. });
  85. shuffleInterval.set_model(shuffleIntervals);
  86. shuffleInterval.set_selected(Utils.randomIntervals.map( e => e.value).indexOf(settings.get_string('random-interval-mode')));
  87. // add wallpaper folder open and change buttons
  88. const openBtn = new Gtk.Button( {
  89. child: new Adw.ButtonContent({
  90. icon_name: 'folder-pictures-symbolic',
  91. label: _('Open folder'),
  92. },),
  93. valign: Gtk.Align.CENTER,
  94. halign: Gtk.Align.CENTER,
  95. });
  96. const changeBtn = new Gtk.Button( {
  97. child: new Adw.ButtonContent({
  98. icon_name: 'folder-download-symbolic',
  99. label: _('Change folder'),
  100. },),
  101. valign: Gtk.Align.CENTER,
  102. halign: Gtk.Align.CENTER,
  103. });
  104. folderRow.add_suffix(openBtn);
  105. folderRow.add_suffix(changeBtn);
  106. blurAdjustment.set_value(settings.get_int('lockscreen-blur-strength'));
  107. brightnessAdjustment.set_value(settings.get_int('lockscreen-blur-brightness'));
  108. const defaultBtn = new Gtk.Button( {
  109. child: new Adw.ButtonContent({
  110. icon_name: 'emblem-default-symbolic',
  111. label: _('Default'),
  112. },),
  113. valign: Gtk.Align.CENTER,
  114. halign: Gtk.Align.CENTER,
  115. });
  116. const noBlurBtn = new Gtk.Button( {
  117. child: new Adw.ButtonContent({
  118. icon_name: 'emblem-default-symbolic',
  119. label: _('No blur, slight dim'),
  120. },),
  121. valign: Gtk.Align.CENTER,
  122. halign: Gtk.Align.CENTER,
  123. });
  124. const slightBlurBtn = new Gtk.Button( {
  125. child: new Adw.ButtonContent({
  126. icon_name: 'emblem-default-symbolic',
  127. label: _('Slight blur & dim'),
  128. },),
  129. valign: Gtk.Align.CENTER,
  130. halign: Gtk.Align.CENTER,
  131. });
  132. // add to presets row
  133. blurPresets.add_suffix(defaultBtn);
  134. blurPresets.add_suffix(noBlurBtn);
  135. blurPresets.add_suffix(slightBlurBtn);
  136. randomIntervalEntry.set_value(settings.get_int('random-interval'));
  137. // these buttons either export or import saved JSON data
  138. const buttonImportData = new Gtk.Button( {
  139. child: new Adw.ButtonContent({
  140. icon_name: 'document-send-symbolic',
  141. label: _('Import'),
  142. },),
  143. valign: Gtk.Align.CENTER,
  144. halign: Gtk.Align.CENTER,
  145. });
  146. const buttonExportData = new Gtk.Button( {
  147. child: new Adw.ButtonContent({
  148. icon_name: 'document-save-symbolic',
  149. label: _('Export'),
  150. },),
  151. valign: Gtk.Align.CENTER,
  152. halign: Gtk.Align.CENTER,
  153. });
  154. json_actionrow.add_suffix(buttonImportData);
  155. json_actionrow.add_suffix(buttonExportData);
  156. version_button.set_label(this.metadata.version.toString());
  157. try {
  158. httpSession = new Soup.Session();
  159. httpSession.user_agent = 'User-Agent: Mozilla/5.0 (X11; GNOME Shell/' + Config.PACKAGE_VERSION + '; Linux x86_64; +https://github.com/neffo/bing-wallpaper-gnome-extension ) BingWallpaper Gnome Extension/' + this.metadata.version;
  160. }
  161. catch (e) {
  162. log("Error creating httpSession: " + e);
  163. }
  164. const icon_image = buildable.get_object('icon_image');
  165. // check that these are valid (can be edited through dconf-editor)
  166. Utils.validate_resolution(settings);
  167. Utils.validate_icon(settings, this.path, icon_image);
  168. Utils.validate_interval(settings);
  169. // Indicator & notifications
  170. settings.bind('hide', hideSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  171. settings.bind('notify', notifySwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  172. settings.connect('changed::icon-name', () => {
  173. Utils.validate_icon(settings, this.path, icon_image);
  174. iconEntry.set_value(1 + Utils.icon_list.indexOf(settings.get_string('icon-name')));
  175. });
  176. iconEntry.connect('output', () => {
  177. settings.set_string('icon-name', Utils.icon_list[iconEntry.get_value()-1]);
  178. });
  179. // connect switches to settings changes
  180. settings.bind('set-background', bgSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  181. settings.bind('debug-logging', debugSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  182. settings.bind('revert-to-current-image', revertSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  183. //settings.bind('override-unsafe-wayland', unsafeSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  184. settings.bind('random-interval', randomIntervalEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
  185. settings.bind('trash-deletes-images', trash_purge_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
  186. settings.bind('always-export-bing-json', always_export_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
  187. // button opens Nautilus at our image folder
  188. openBtn.connect('clicked', (widget) => {
  189. Utils.openImageFolder(settings);
  190. });
  191. // we populate the tab (gtk4+, gnome 40+), this was previously a button to open a new window in gtk3
  192. carousel = new Carousel(settings, null, null, carouselFlowBox, this.dir.get_path()); // auto load carousel
  193. // this is intended for migrating image folders between computers (or even sharing) or backups
  194. // we export the Bing JSON data to the image directory, so this folder becomes portable
  195. buttonImportData.connect('clicked', () => {
  196. Utils.importBingJSON(settings);
  197. });
  198. buttonExportData.connect('clicked', () => {
  199. Utils.exportBingJSON(settings);
  200. });
  201. // change wallpaper button
  202. const dirChooser = new Gtk.FileDialog( {
  203. accept_label: "Select",
  204. modal: true,
  205. title: _("Select wallpaper download folder"),
  206. });
  207. changeBtn.connect('clicked', (widget) => {
  208. dirChooser.set_initial_folder(Gio.File.new_for_path(Utils.getWallpaperDir(settings)));
  209. dirChooser.select_folder(window, null, (self, res) => {
  210. let new_path = self.select_folder_finish(res).get_uri().replace('file://', '');
  211. log(new_path);
  212. Utils.moveImagesToNewFolder(settings, Utils.getWallpaperDir(settings), new_path);
  213. Utils.setWallpaperDir(settings, new_path);
  214. });
  215. });
  216. // Resolution
  217. const resolutionModel = new Gtk.StringList();
  218. Utils.resolutions.forEach((res) => { // add res to dropdown list (aka a GtkComboText)
  219. resolutionModel.append(res);
  220. });
  221. resolutionEntry.set_model(resolutionModel);
  222. settings.connect('changed::resolution', () => {
  223. resolutionEntry.set_selected(Utils.resolutions.map( e => e.value).indexOf(settings.get_string('resolution')));
  224. });
  225. settings.connect('changed::resolution', () => {
  226. Utils.validate_resolution(settings);
  227. });
  228. // shuffle modes
  229. settings.bind('random-mode-enabled', shuffleSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  230. /*settings.bind('random-interval-mode', entryShuffleMode, 'active_id', Gio.SettingsBindFlags.DEFAULT);*/
  231. settings.connect('changed::random-interval-mode', () => {
  232. shuffleInterval.set_selected(Utils.randomIntervals.map( e => e.value).indexOf(settings.get_string('random-interval-mode')));
  233. });
  234. // GDM3 lockscreen blur override
  235. settings.bind('override-lockscreen-blur', overrideSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  236. settings.bind('lockscreen-blur-strength', strengthEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
  237. settings.bind('lockscreen-blur-brightness', brightnessEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
  238. // add a couple of preset buttons
  239. defaultBtn.connect('clicked', (widget) => {
  240. Utils.set_blur_preset(settings, Utils.PRESET_GNOME_DEFAULT);
  241. });
  242. noBlurBtn.connect('clicked', (widget) => {
  243. Utils.set_blur_preset(settings, Utils.PRESET_NO_BLUR);
  244. });
  245. slightBlurBtn.connect('clicked', (widget) => {
  246. Utils.set_blur_preset(settings, Utils.PRESET_SLIGHT_BLUR);
  247. });
  248. // fetch change log (on about page)
  249. if (httpSession)
  250. Utils.fetch_change_log(this.metadata.version.toString(), change_log, httpSession);
  251. }
  252. }