utils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. import Gio from 'gi://Gio';
  5. import Gtk from 'gi://Gtk';
  6. import St from 'gi://St';
  7. import {PACKAGE_VERSION} from 'resource:///org/gnome/shell/misc/config.js';
  8. export const SHELL_MAJOR_VERSION = Number(PACKAGE_VERSION.split('.')[0]);
  9. export const HAS_ST_ORIENTATION = SHELL_MAJOR_VERSION >= 48;
  10. export const HAS_MESSAGELIST_NOTIFICATIONMESSAGE = SHELL_MAJOR_VERSION >= 48;
  11. /**
  12. * Get a themed icon, using fallbacks from GSConnect's GResource when necessary.
  13. *
  14. * @param {string} name - A themed icon name
  15. * @returns {Gio.Icon} A themed icon
  16. */
  17. export function getIcon(name) {
  18. if (getIcon._resource === undefined) {
  19. // Setup the desktop icons
  20. const settings = St.Settings.get();
  21. getIcon._desktop = new Gtk.IconTheme();
  22. getIcon._desktop.set_theme_name(settings.gtk_icon_theme);
  23. settings.connect('notify::gtk-icon-theme', (settings_, key_) => {
  24. getIcon._desktop.set_theme_name(settings_.gtk_icon_theme);
  25. });
  26. // Preload our fallbacks
  27. const iconPath = 'resource://org/gnome/Shell/Extensions/GSConnect/icons';
  28. const iconNames = [
  29. 'org.gnome.Shell.Extensions.GSConnect',
  30. 'org.gnome.Shell.Extensions.GSConnect-symbolic',
  31. 'computer-symbolic',
  32. 'laptop-symbolic',
  33. 'smartphone-symbolic',
  34. 'tablet-symbolic',
  35. 'tv-symbolic',
  36. 'phonelink-ring-symbolic',
  37. 'sms-symbolic',
  38. ];
  39. getIcon._resource = {};
  40. for (const iconName of iconNames) {
  41. getIcon._resource[iconName] = new Gio.FileIcon({
  42. file: Gio.File.new_for_uri(`${iconPath}/${iconName}.svg`),
  43. });
  44. }
  45. }
  46. // Check the desktop icon theme
  47. if (getIcon._desktop.has_icon(name))
  48. return new Gio.ThemedIcon({name: name});
  49. // Check our GResource
  50. if (getIcon._resource[name] !== undefined)
  51. return getIcon._resource[name];
  52. // Fallback to hoping it's in the theme somewhere
  53. return new Gio.ThemedIcon({name: name});
  54. }