utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import GLib from 'gi://GLib';
  2. export const IS_IN_PREFERENCES = typeof global === 'undefined';
  3. // Taken from https://github.com/Schneegans/Burn-My-Windows/blob/main/src/utils.js
  4. // This method can be used to import a module in the GNOME Shell process only. This
  5. // is useful if you want to use a module in extension.js, but not in the preferences
  6. // process. This method returns null if it is called in the preferences process.
  7. export async function import_in_shell_only(module) {
  8. if (IS_IN_PREFERENCES)
  9. return null;
  10. return (await import(module)).default;
  11. }
  12. // In use for the effects, to prevent boilerplate code
  13. export function setup_params(outer_this, params) {
  14. // setup each parameter, either with the given or the default value
  15. for (const params_name in outer_this.constructor.default_params) {
  16. outer_this["_" + params_name] = null;
  17. outer_this[params_name] = params_name in params ?
  18. params[params_name] :
  19. outer_this.constructor.default_params[params_name];
  20. }
  21. };
  22. export const get_shader_source = (Shell, shader_filename, self_uri) => {
  23. if (!Shell)
  24. return;
  25. const shader_path = GLib.filename_from_uri(
  26. GLib.uri_resolve_relative(self_uri, shader_filename, GLib.UriFlags.NONE)
  27. )[0];
  28. try {
  29. return Shell.get_file_contents_utf8_sync(shader_path);
  30. } catch (e) {
  31. console.warn(`[Blur my Shell > effect] error loading shader from ${shader_path}: ${e}`);
  32. return null;
  33. }
  34. };