screenshot.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Shell from 'gi://Shell';
  2. import Meta from 'gi://Meta';
  3. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  4. export const ScreenshotBlur = class ScreenshotBlur {
  5. constructor(connections, settings, effects_manager) {
  6. this.connections = connections;
  7. this.effects = [];
  8. this.settings = settings;
  9. this.effects_manager = effects_manager;
  10. }
  11. enable() {
  12. this._log("blurring screenshot's window selector");
  13. // connect to every background change (even without changing image)
  14. // FIXME this signal is fired very often, so we should find another one
  15. // fired only when necessary (but that still catches all cases)
  16. this.connections.connect(
  17. Main.layoutManager._backgroundGroup,
  18. 'notify',
  19. _ => {
  20. this._log("updated background for screenshot's window selector");
  21. this.update_backgrounds();
  22. }
  23. );
  24. // connect to monitors change
  25. this.connections.connect(
  26. Main.layoutManager,
  27. 'monitors-changed',
  28. _ => {
  29. if (Main.screenShield && !Main.screenShield.locked) {
  30. this._log("changed monitors for screenshot's window selector");
  31. this.update_backgrounds();
  32. }
  33. }
  34. );
  35. // update backgrounds when the component is enabled
  36. this.update_backgrounds();
  37. }
  38. update_backgrounds() {
  39. // remove every old background
  40. this.remove();
  41. // add new backgrounds
  42. for (let i = 0; i < Main.screenshotUI._windowSelectors.length; i++) {
  43. const actor = Main.screenshotUI._windowSelectors[i];
  44. const monitor = Main.layoutManager.monitors[i];
  45. if (!monitor)
  46. continue;
  47. const bg_actor = this.create_background_actor(monitor);
  48. actor.insert_child_at_index(bg_actor, 0);
  49. actor._blur_actor = bg_actor;
  50. }
  51. }
  52. create_background_actor(monitor) {
  53. let bg_actor = new Meta.BackgroundActor({
  54. meta_display: global.display,
  55. monitor: monitor.index
  56. });
  57. let background = Main.layoutManager._backgroundGroup.get_child_at_index(
  58. Main.layoutManager.monitors.length - monitor.index - 1
  59. );
  60. if (!background) {
  61. this._warn("could not get background for screenshot's window selector");
  62. return bg_actor;
  63. }
  64. bg_actor.content.set({
  65. background: background.get_content().background
  66. });
  67. let blur_effect = new Shell.BlurEffect({
  68. brightness: this.settings.screenshot.CUSTOMIZE
  69. ? this.settings.screenshot.BRIGHTNESS
  70. : this.settings.BRIGHTNESS,
  71. sigma: this.settings.screenshot.CUSTOMIZE
  72. ? this.settings.screenshot.SIGMA
  73. : this.settings.SIGMA
  74. * monitor.geometry_scale,
  75. mode: Shell.BlurMode.ACTOR
  76. });
  77. // store the scale in the effect in order to retrieve it in set_sigma
  78. blur_effect.scale = monitor.geometry_scale;
  79. let color_effect = this.effects_manager.new_color_effect({
  80. color: this.settings.screenshot.CUSTOMIZE
  81. ? this.settings.screenshot.COLOR
  82. : this.settings.COLOR
  83. }, this.settings);
  84. let noise_effect = this.effects_manager.new_noise_effect({
  85. noise: this.settings.screenshot.CUSTOMIZE
  86. ? this.settings.screenshot.NOISE_AMOUNT
  87. : this.settings.NOISE_AMOUNT,
  88. lightness: this.settings.screenshot.CUSTOMIZE
  89. ? this.settings.screenshot.NOISE_LIGHTNESS
  90. : this.settings.NOISE_LIGHTNESS
  91. }, this.settings);
  92. bg_actor.add_effect(color_effect);
  93. bg_actor.add_effect(noise_effect);
  94. bg_actor.add_effect(blur_effect);
  95. this.effects.push({ blur_effect, color_effect, noise_effect });
  96. return bg_actor;
  97. }
  98. set_sigma(s) {
  99. this.effects.forEach(effect => {
  100. effect.blur_effect.sigma = s * effect.blur_effect;
  101. });
  102. }
  103. set_brightness(b) {
  104. this.effects.forEach(effect => {
  105. effect.blur_effect.brightness = b;
  106. });
  107. }
  108. set_color(c) {
  109. this.effects.forEach(effect => {
  110. effect.color_effect.color = c;
  111. });
  112. }
  113. set_noise_amount(n) {
  114. this.effects.forEach(effect => {
  115. effect.noise_effect.noise = n;
  116. });
  117. }
  118. set_noise_lightness(l) {
  119. this.effects.forEach(effect => {
  120. effect.noise_effect.lightness = l;
  121. });
  122. }
  123. remove() {
  124. Main.screenshotUI._windowSelectors.forEach(actor => {
  125. if (actor._blur_actor) {
  126. actor.remove_child(actor._blur_actor);
  127. actor._blur_actor.destroy();
  128. }
  129. });
  130. this.effects = [];
  131. }
  132. disable() {
  133. this._log("removing blur from screenshot's window selector");
  134. this.remove();
  135. this.connections.disconnect_all();
  136. }
  137. _log(str) {
  138. if (this.settings.DEBUG)
  139. console.log(`[Blur my Shell > screenshot] ${str}`);
  140. }
  141. _warn(str) {
  142. console.warn(`[Blur my Shell > screenshot] ${str}`);
  143. }
  144. };