prefs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Bing Wallpaper GNOME extension
  2. // Copyright (C) 2017-2025 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 BingLog = (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 delete_previous_switch = buildable.get_object('delete_previous_switch');
  66. const delete_previous_adjustment = buildable.get_object('delete_previous_adjustment');
  67. const always_export_switch = buildable.get_object('always_export_switch');
  68. const gallery_page = buildable.get_object('gallery_page');
  69. const carouselFlowBox = buildable.get_object('carouselFlowBox');
  70. const randomIntervalEntry = buildable.get_object('entry_random_interval');
  71. const debug_page = buildable.get_object('debug_page');
  72. const json_actionrow = buildable.get_object('json_actionrow');
  73. const about_page = buildable.get_object('about_page');
  74. const version_row = buildable.get_object('version_row');
  75. const change_log = buildable.get_object('change_log');
  76. window.add(settings_page);
  77. window.add(lockscreen_page);
  78. window.add(gallery_page);
  79. window.add(debug_page);
  80. window.add(about_page);
  81. iconEntry.set_value(1+Utils.icon_list.indexOf(settings.get_string('icon-name')));
  82. // shuffle intervals
  83. const shuffleIntervals = new Gtk.StringList;
  84. Utils.randomIntervals.forEach((x) => {
  85. shuffleIntervals.append(_(x.title));
  86. });
  87. shuffleInterval.set_model(shuffleIntervals);
  88. shuffleInterval.set_selected(Utils.randomIntervals.map( e => e.value).indexOf(settings.get_string('random-interval-mode')));
  89. // add wallpaper folder open and change buttons
  90. const openBtn = new Gtk.Button( {
  91. label: _('Open folder'),
  92. valign: Gtk.Align.CENTER,
  93. halign: Gtk.Align.CENTER,
  94. });
  95. const changeBtn = new Gtk.Button( {
  96. label: _('Change folder'),
  97. valign: Gtk.Align.CENTER,
  98. halign: Gtk.Align.CENTER,
  99. });
  100. folderRow.add_suffix(openBtn);
  101. folderRow.add_suffix(changeBtn);
  102. blurAdjustment.set_value(settings.get_int('lockscreen-blur-strength'));
  103. brightnessAdjustment.set_value(settings.get_int('lockscreen-blur-brightness'));
  104. const defaultBtn = new Gtk.Button( {
  105. label: _('Default'),
  106. valign: Gtk.Align.CENTER,
  107. halign: Gtk.Align.CENTER,
  108. });
  109. const noBlurBtn = new Gtk.Button( {
  110. label: _('No blur, slight dim'),
  111. valign: Gtk.Align.CENTER,
  112. halign: Gtk.Align.CENTER,
  113. });
  114. const slightBlurBtn = new Gtk.Button( {
  115. label: _('Slight blur & dim'),
  116. valign: Gtk.Align.CENTER,
  117. halign: Gtk.Align.CENTER,
  118. });
  119. // add to presets row
  120. blurPresets.add_suffix(defaultBtn);
  121. blurPresets.add_suffix(noBlurBtn);
  122. blurPresets.add_suffix(slightBlurBtn);
  123. randomIntervalEntry.set_value(settings.get_int('random-interval'));
  124. // these buttons either export or import saved JSON data
  125. const buttonImportData = new Gtk.Button( {
  126. label: _('Import'),
  127. valign: Gtk.Align.CENTER,
  128. halign: Gtk.Align.CENTER,
  129. });
  130. const buttonExportData = new Gtk.Button( {
  131. label: _('Export'),
  132. valign: Gtk.Align.CENTER,
  133. halign: Gtk.Align.CENTER,
  134. });
  135. json_actionrow.add_suffix(buttonImportData);
  136. json_actionrow.add_suffix(buttonExportData);
  137. version_row.set_subtitle(this.metadata.version.toString());
  138. try {
  139. httpSession = new Soup.Session();
  140. 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;
  141. }
  142. catch (e) {
  143. BingLog("Error creating httpSession: " + e);
  144. }
  145. const icon_image = buildable.get_object('icon_image');
  146. const app_icon_image = buildable.get_object('app_icon_image');
  147. // check that these are valid (can be edited through dconf-editor)
  148. Utils.validate_resolution(settings);
  149. Utils.validate_icon(settings, this.path, icon_image, app_icon_image);
  150. Utils.validate_interval(settings);
  151. // Indicator & notifications
  152. settings.bind('hide', hideSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  153. settings.bind('notify', notifySwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  154. settings.connect('changed::icon-name', () => {
  155. Utils.validate_icon(settings, this.path, icon_image, app_icon_image);
  156. iconEntry.set_value(1 + Utils.icon_list.indexOf(settings.get_string('icon-name')));
  157. });
  158. iconEntry.connect('output', () => {
  159. settings.set_string('icon-name', Utils.icon_list[iconEntry.get_value()-1]);
  160. });
  161. // connect switches to settings changes
  162. settings.bind('set-background', bgSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  163. settings.bind('debug-logging', debugSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  164. settings.bind('revert-to-current-image', revertSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  165. //settings.bind('override-unsafe-wayland', unsafeSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  166. settings.bind('random-interval', randomIntervalEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
  167. settings.bind('trash-deletes-images', trash_purge_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
  168. settings.bind('always-export-bing-json', always_export_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
  169. settings.bind('delete-previous', delete_previous_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
  170. // button opens Nautilus at our image folder
  171. openBtn.connect('clicked', (widget) => {
  172. Utils.openImageFolder(settings);
  173. });
  174. // we populate the tab (gtk4+, gnome 40+), this was previously a button to open a new window in gtk3
  175. carousel = new Carousel(settings, null, null, carouselFlowBox, this.dir.get_path()); // auto load carousel
  176. // this is intended for migrating image folders between computers (or even sharing) or backups
  177. // we export the Bing JSON data to the image directory, so this folder becomes portable
  178. buttonImportData.connect('clicked', () => {
  179. Utils.importBingJSON(settings);
  180. });
  181. buttonExportData.connect('clicked', () => {
  182. Utils.exportBingJSON(settings);
  183. });
  184. // change wallpaper button
  185. const dirChooser = new Gtk.FileDialog( {
  186. accept_label: "Select",
  187. modal: true,
  188. title: _("Select wallpaper download folder"),
  189. });
  190. changeBtn.connect('clicked', (widget) => {
  191. dirChooser.set_initial_folder(Gio.File.new_for_path(Utils.getWallpaperDir(settings)));
  192. dirChooser.select_folder(window, null, (self, res) => {
  193. let new_path = self.select_folder_finish(res).get_uri().replace('file://', '');
  194. BingLog(new_path);
  195. Utils.moveImagesToNewFolder(settings, Utils.getWallpaperDir(settings), new_path);
  196. Utils.setWallpaperDir(settings, new_path);
  197. });
  198. });
  199. // Resolution
  200. const resolutionModel = new Gtk.StringList();
  201. Utils.resolutions.forEach((res) => { // add res to dropdown list (aka a GtkComboText)
  202. resolutionModel.append(res);
  203. });
  204. resolutionEntry.set_model(resolutionModel);
  205. settings.connect('changed::resolution', () => {
  206. resolutionEntry.set_selected(Utils.resolutions.map( e => e.value).indexOf(settings.get_string('resolution')));
  207. });
  208. settings.connect('changed::resolution', () => {
  209. Utils.validate_resolution(settings);
  210. });
  211. // shuffle modes
  212. settings.bind('random-mode-enabled', shuffleSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  213. /*settings.bind('random-interval-mode', entryShuffleMode, 'active_id', Gio.SettingsBindFlags.DEFAULT);*/
  214. settings.connect('changed::random-interval-mode', () => {
  215. shuffleInterval.set_selected(Utils.randomIntervals.map( e => e.value).indexOf(settings.get_string('random-interval-mode')));
  216. });
  217. // GDM3 lockscreen blur override
  218. settings.bind('override-lockscreen-blur', overrideSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
  219. settings.bind('lockscreen-blur-strength', strengthEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
  220. settings.bind('lockscreen-blur-brightness', brightnessEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
  221. settings.bind('previous-days', delete_previous_adjustment, 'value', Gio.SettingsBindFlags.DEFAULT);
  222. // add a couple of preset buttons
  223. defaultBtn.connect('clicked', (widget) => {
  224. Utils.set_blur_preset(settings, Utils.PRESET_GNOME_DEFAULT);
  225. });
  226. noBlurBtn.connect('clicked', (widget) => {
  227. Utils.set_blur_preset(settings, Utils.PRESET_NO_BLUR);
  228. });
  229. slightBlurBtn.connect('clicked', (widget) => {
  230. Utils.set_blur_preset(settings, Utils.PRESET_SLIGHT_BLUR);
  231. });
  232. // fetch change log (on about page)
  233. if (httpSession)
  234. Utils.fetch_change_log(this.metadata.version.toString(), change_log, httpSession);
  235. }
  236. }