pixelize.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import GObject from 'gi://GObject';
  2. import * as utils from '../conveniences/utils.js';
  3. const Clutter = await utils.import_in_shell_only('gi://Clutter');
  4. import { UpscaleEffect } from './upscale.js';
  5. import { DownscaleEffect } from './downscale.js';
  6. const DEFAULT_PARAMS = {
  7. factor: 8, downsampling_mode: 0
  8. };
  9. export const PixelizeEffect = utils.IS_IN_PREFERENCES ?
  10. { default_params: DEFAULT_PARAMS } :
  11. new GObject.registerClass({
  12. GTypeName: "PixelizeEffect",
  13. Properties: {
  14. 'factor': GObject.ParamSpec.int(
  15. `factor`,
  16. `Factor`,
  17. `Factor`,
  18. GObject.ParamFlags.READWRITE,
  19. 0, 64,
  20. 8,
  21. ),
  22. 'downsampling_mode': GObject.ParamSpec.int(
  23. `downsampling_mode`,
  24. `Downsampling mode`,
  25. `Downsampling mode`,
  26. GObject.ParamFlags.READWRITE,
  27. 0, 2,
  28. 0,
  29. )
  30. }
  31. }, class PixelizeEffect extends Clutter.Effect {
  32. constructor(params) {
  33. super();
  34. this.upscale_effect = new UpscaleEffect({});
  35. this.downscale_effect = new DownscaleEffect({});
  36. utils.setup_params(this, params);
  37. }
  38. static get default_params() {
  39. return DEFAULT_PARAMS;
  40. }
  41. get factor() {
  42. // should be the same as `this.downscale_effect.divider`
  43. return this.upscale_effect.factor;
  44. }
  45. set factor(value) {
  46. this.upscale_effect.factor = value;
  47. this.downscale_effect.divider = value;
  48. }
  49. get downsampling_mode() {
  50. return this.downscale_effect.downsampling_mode;
  51. }
  52. set downsampling_mode(value) {
  53. this.downscale_effect.downsampling_mode = value;
  54. }
  55. vfunc_set_actor(actor) {
  56. // deattach effects from old actor
  57. this.upscale_effect?.actor?.remove_effect(this.upscale_effect);
  58. this.downscale_effect?.actor?.remove_effect(this.downscale_effect);
  59. // attach effects to new actor
  60. if (actor) {
  61. if (this.upscale_effect)
  62. actor.add_effect(this.upscale_effect);
  63. if (this.downscale_effect)
  64. actor.add_effect(this.downscale_effect);
  65. }
  66. super.vfunc_set_actor(actor);
  67. }
  68. vfunc_set_enabled(is_enabled) {
  69. this.upscale_effect?.set_enabled(is_enabled);
  70. this.downscale_effect?.set_enabled(is_enabled);
  71. super.vfunc_set_enabled(is_enabled);
  72. }
  73. });