applications.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import Meta from 'gi://Meta';
  2. import Gio from 'gi://Gio';
  3. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  4. import { ApplicationsService } from '../dbus/services.js';
  5. import { PaintSignals } from '../conveniences/paint_signals.js';
  6. import { DummyPipeline } from '../conveniences/dummy_pipeline.js';
  7. export const ApplicationsBlur = class ApplicationsBlur {
  8. constructor(connections, settings, effects_manager) {
  9. this.connections = connections;
  10. this.settings = settings;
  11. this.effects_manager = effects_manager;
  12. this.paint_signals = new PaintSignals(connections);
  13. // stores every blurred meta window
  14. this.meta_window_map = new Map();
  15. }
  16. enable() {
  17. this._log("blurring applications...");
  18. // export dbus service for preferences
  19. this.service = new ApplicationsService;
  20. this.service.export();
  21. this.mutter_gsettings = new Gio.Settings({ schema: 'org.gnome.mutter' });
  22. // blur already existing windows
  23. this.update_all_windows();
  24. // blur every new window
  25. this.connections.connect(
  26. global.display,
  27. 'window-created',
  28. (_meta_display, meta_window) => {
  29. this._log("window created");
  30. if (meta_window)
  31. this.track_new(meta_window);
  32. }
  33. );
  34. // update window blur when focus is changed
  35. this.focused_window_pid = null;
  36. this.init_dynamic_opacity();
  37. this.connections.connect(
  38. global.display,
  39. 'focus-window',
  40. (_meta_display, meta_window, _p0) => {
  41. if (meta_window && meta_window.bms_pid != this.focused_window_pid)
  42. this.set_focus_for_window(meta_window);
  43. else if (!meta_window)
  44. this.set_focus_for_window(null);
  45. }
  46. );
  47. this.connect_to_overview();
  48. }
  49. /// Initializes the dynamic opacity for windows, without touching to the connections.
  50. /// This is used both when enabling the component, and when changing the dynamic-opacity pref.
  51. init_dynamic_opacity() {
  52. if (this.settings.applications.DYNAMIC_OPACITY) {
  53. // make the currently focused window solid
  54. if (global.display.focus_window)
  55. this.set_focus_for_window(global.display.focus_window);
  56. } else {
  57. // remove old focused window if the pref was changed
  58. if (this.focused_window_pid)
  59. this.set_focus_for_window(null);
  60. }
  61. }
  62. /// Connect to the overview being opened/closed to force the blur being
  63. /// shown on every window of the workspaces viewer.
  64. connect_to_overview() {
  65. this.connections.disconnect_all_for(Main.overview);
  66. if (this.settings.applications.BLUR_ON_OVERVIEW) {
  67. // when the overview is opened, show every window actors (which
  68. // allows the blur to be shown too)
  69. this.connections.connect(
  70. Main.overview, 'showing',
  71. _ => this.meta_window_map.forEach((meta_window, _pid) => {
  72. let window_actor = meta_window.get_compositor_private();
  73. window_actor?.show();
  74. })
  75. );
  76. // when the overview is closed, hide every actor that is not on the
  77. // current workspace (to mimic the original behaviour)
  78. this.connections.connect(
  79. Main.overview, 'hidden',
  80. _ => {
  81. this.meta_window_map.forEach((meta_window, _pid) => {
  82. let window_actor = meta_window.get_compositor_private();
  83. if (
  84. !meta_window.get_workspace().active
  85. )
  86. window_actor.hide();
  87. });
  88. }
  89. );
  90. }
  91. }
  92. /// Iterate through all existing windows and add blur as needed.
  93. update_all_windows() {
  94. // remove all previously blurred windows, in the case where the
  95. // whitelist was changed
  96. this.meta_window_map.forEach(((_meta_window, pid) => {
  97. this.remove_blur(pid);
  98. }));
  99. for (
  100. let i = 0;
  101. i < global.workspace_manager.get_n_workspaces();
  102. ++i
  103. ) {
  104. let workspace = global.workspace_manager.get_workspace_by_index(i);
  105. let windows = workspace.list_windows();
  106. windows.forEach(meta_window => this.track_new(meta_window));
  107. }
  108. }
  109. /// Adds the needed signals to every new tracked window, and adds blur if
  110. /// needed.
  111. /// Accepts only untracked meta windows (i.e no `bms_pid` set)
  112. track_new(meta_window) {
  113. // create a pid that will follow the window during its whole life
  114. const pid = ("" + Math.random()).slice(2, 16);
  115. meta_window.bms_pid = pid;
  116. this._log(`new window tracked, pid: ${pid}`);
  117. // register the blurred window
  118. this.meta_window_map.set(pid, meta_window);
  119. // update the blur when wm-class is changed
  120. this.connections.connect(
  121. meta_window, 'notify::wm-class',
  122. _ => this.check_blur(meta_window)
  123. );
  124. // update the position and size when the window size changes
  125. this.connections.connect(
  126. meta_window, 'size-changed',
  127. _ => this.update_size(pid)
  128. );
  129. // remove the blur when the window is unmanaged
  130. this.connections.connect(
  131. meta_window, 'unmanaging',
  132. _ => this.untrack_meta_window(pid)
  133. );
  134. this.check_blur(meta_window);
  135. }
  136. /// Updates the size of the blur actor associated to a meta window from its pid.
  137. /// Accepts only tracked meta window (i.e `bms_pid` set), be it blurred or not.
  138. update_size(pid) {
  139. if (this.meta_window_map.has(pid)) {
  140. const meta_window = this.meta_window_map.get(pid);
  141. const blur_actor = meta_window.blur_actor;
  142. if (blur_actor) {
  143. const allocation = this.compute_allocation(meta_window);
  144. blur_actor.x = allocation.x;
  145. blur_actor.y = allocation.y;
  146. blur_actor.width = allocation.width;
  147. blur_actor.height = allocation.height;
  148. }
  149. } else
  150. // the pid was visibly not removed
  151. this.untrack_meta_window(pid);
  152. }
  153. /// Checks if the given actor needs to be blurred.
  154. /// Accepts only tracked meta window, be it blurred or not.
  155. ///
  156. /// In order to be blurred, a window either:
  157. /// - is whitelisted in the user preferences if not enable-all
  158. /// - is not blacklisted if enable-all
  159. check_blur(meta_window) {
  160. const window_wm_class = meta_window.get_wm_class();
  161. const enable_all = this.settings.applications.ENABLE_ALL;
  162. const whitelist = this.settings.applications.WHITELIST;
  163. const blacklist = this.settings.applications.BLACKLIST;
  164. if (window_wm_class)
  165. this._log(`pid ${meta_window.bms_pid} associated to wm class name ${window_wm_class}`);
  166. // if we are in blacklist mode and the window is not blacklisted
  167. // or if we are in whitelist mode and the window is whitelisted
  168. if (
  169. window_wm_class !== ""
  170. && ((enable_all && !blacklist.includes(window_wm_class))
  171. || (!enable_all && whitelist.includes(window_wm_class))
  172. )
  173. && [
  174. Meta.FrameType.NORMAL,
  175. Meta.FrameType.DIALOG,
  176. Meta.FrameType.MODAL_DIALOG
  177. ].includes(meta_window.get_frame_type())
  178. ) {
  179. // only blur the window if it is not already done
  180. if (!meta_window.blur_actor)
  181. this.create_blur_effect(meta_window);
  182. }
  183. // remove blur it is not explicitly whitelisted or un-blacklisted
  184. else if (meta_window.blur_actor)
  185. this.remove_blur(meta_window.bms_pid);
  186. }
  187. /// Add the blur effect to the window.
  188. /// Accepts only tracked meta window that is NOT already blurred.
  189. create_blur_effect(meta_window) {
  190. const pid = meta_window.bms_pid;
  191. const window_actor = meta_window.get_compositor_private();
  192. const pipeline = new DummyPipeline(this.effects_manager, this.settings.applications);
  193. let [blur_actor, bg_manager] = pipeline.create_background_with_effect(
  194. window_actor, 'bms-application-blurred-widget'
  195. );
  196. meta_window.blur_actor = blur_actor;
  197. meta_window.bg_manager = bg_manager;
  198. // if hacks are selected, force to repaint the window
  199. if (this.settings.HACKS_LEVEL === 1) {
  200. this._log("hack level 1");
  201. this.paint_signals.disconnect_all_for_actor(blur_actor);
  202. this.paint_signals.connect(blur_actor, pipeline.effect);
  203. } else {
  204. this.paint_signals.disconnect_all_for_actor(blur_actor);
  205. }
  206. // make sure window is blurred in overview
  207. if (this.settings.applications.BLUR_ON_OVERVIEW)
  208. this.enforce_window_visibility_on_overview_for(window_actor);
  209. // update the size
  210. this.update_size(pid);
  211. // set the window actor's opacity
  212. this.set_window_opacity(window_actor, this.settings.applications.OPACITY);
  213. // now set up the signals, for the window actor only: they are disconnected
  214. // in `remove_blur`, whereas the signals for the meta window are disconnected
  215. // only when the whole component is disabled
  216. // update the window opacity when it changes, else we don't control it fully
  217. this.connections.connect(
  218. window_actor, 'notify::opacity',
  219. _ => {
  220. if (this.focused_window_pid != pid)
  221. this.set_window_opacity(window_actor, this.settings.applications.OPACITY);
  222. }
  223. );
  224. // hide the blur if window becomes invisible
  225. if (!window_actor.visible)
  226. blur_actor.hide();
  227. this.connections.connect(
  228. window_actor,
  229. 'notify::visible',
  230. window_actor => {
  231. if (window_actor.visible)
  232. meta_window.blur_actor.show();
  233. else
  234. meta_window.blur_actor.hide();
  235. }
  236. );
  237. }
  238. /// With `focus=true`, tells us we are focused on said window (which can be null if
  239. /// we are not focused anymore). It automatically removes the ancient focus.
  240. /// With `focus=false`, just remove the focus from said window (which can still be null).
  241. set_focus_for_window(meta_window, focus = true) {
  242. let blur_actor = null;
  243. let window_actor = null;
  244. let new_pid = null;
  245. if (meta_window) {
  246. blur_actor = meta_window.blur_actor;
  247. window_actor = meta_window.get_compositor_private();
  248. new_pid = meta_window.bms_pid;
  249. }
  250. if (focus) {
  251. // remove old focused window if any
  252. if (this.focused_window_pid) {
  253. const old_focused_window = this.meta_window_map.get(this.focused_window_pid);
  254. if (old_focused_window)
  255. this.set_focus_for_window(old_focused_window, false);
  256. }
  257. // set new focused window pid
  258. this.focused_window_pid = new_pid;
  259. // if we have blur, hide it and make the window opaque
  260. if (this.settings.applications.DYNAMIC_OPACITY && blur_actor) {
  261. blur_actor.hide();
  262. this.set_window_opacity(window_actor, 255);
  263. }
  264. }
  265. // if we remove the focus and have blur, show it and make the window transparent
  266. else if (blur_actor) {
  267. blur_actor.show();
  268. this.set_window_opacity(window_actor, this.settings.applications.OPACITY);
  269. }
  270. }
  271. /// Makes sure that, when the overview is visible, the window actor will
  272. /// stay visible no matter what.
  273. /// We can instead hide the last child of the window actor, which will
  274. /// improve performances without hiding the blur effect.
  275. enforce_window_visibility_on_overview_for(window_actor) {
  276. this.connections.connect(window_actor, 'notify::visible',
  277. _ => {
  278. if (this.settings.applications.BLUR_ON_OVERVIEW) {
  279. if (
  280. !window_actor.visible
  281. && Main.overview.visible
  282. ) {
  283. window_actor.show();
  284. window_actor.get_last_child().hide();
  285. }
  286. else if (
  287. window_actor.visible
  288. )
  289. window_actor.get_last_child().show();
  290. }
  291. }
  292. );
  293. }
  294. /// Set the opacity of the window actor that sits on top of the blur effect.
  295. set_window_opacity(window_actor, opacity) {
  296. window_actor?.get_children().forEach(child => {
  297. if (child.name !== "blur-actor" && child.opacity != opacity)
  298. child.opacity = opacity;
  299. });
  300. }
  301. /// Update the opacity of all window actors.
  302. set_opacity() {
  303. let opacity = this.settings.applications.OPACITY;
  304. this.meta_window_map.forEach(((meta_window, pid) => {
  305. if (pid != this.focused_window_pid && meta_window.blur_actor) {
  306. let window_actor = meta_window.get_compositor_private();
  307. this.set_window_opacity(window_actor, opacity);
  308. }
  309. }));
  310. }
  311. /// Compute the size and position for a blur actor.
  312. /// If `scale-monitor-framebuffer` experimental feature if on, we don't need to manage scaling.
  313. /// Else, on wayland, we need to divide by the scale to get the correct result.
  314. compute_allocation(meta_window) {
  315. const scale_monitor_framebuffer = this.mutter_gsettings.get_strv('experimental-features')
  316. .includes('scale-monitor-framebuffer');
  317. const is_wayland = Meta.is_wayland_compositor();
  318. const monitor_index = meta_window.get_monitor();
  319. // check if the window is using wayland, or xwayland/xorg for rendering
  320. const scale = !scale_monitor_framebuffer && is_wayland && meta_window.get_client_type() == 0
  321. ? Main.layoutManager.monitors[monitor_index].geometry_scale
  322. : 1;
  323. let frame = meta_window.get_frame_rect();
  324. let buffer = meta_window.get_buffer_rect();
  325. return {
  326. x: (frame.x - buffer.x) / scale,
  327. y: (frame.y - buffer.y) / scale,
  328. width: frame.width / scale,
  329. height: frame.height / scale
  330. };
  331. }
  332. /// Removes the blur actor to make a blurred window become normal again.
  333. /// It however does not untrack the meta window itself.
  334. /// Accepts a pid corresponding (or not) to a blurred (or not) meta window.
  335. remove_blur(pid) {
  336. this._log(`removing blur for pid ${pid}`);
  337. let meta_window = this.meta_window_map.get(pid);
  338. if (meta_window) {
  339. let window_actor = meta_window.get_compositor_private();
  340. let blur_actor = meta_window.blur_actor;
  341. let bg_manager = meta_window.bg_manager;
  342. if (blur_actor && window_actor) {
  343. // reset the opacity
  344. this.set_window_opacity(window_actor, 255);
  345. // remove the blurred actor
  346. window_actor.remove_child(blur_actor);
  347. bg_manager._bms_pipeline.destroy();
  348. bg_manager.destroy();
  349. blur_actor.destroy();
  350. // kinda untrack the blurred actor, as its presence is how we know
  351. // whether we are blurred or not
  352. delete meta_window.blur_actor;
  353. delete meta_window.bg_manager;
  354. // disconnect the signals of the window actor
  355. this.paint_signals.disconnect_all_for_actor(blur_actor);
  356. this.connections.disconnect_all_for(window_actor);
  357. }
  358. }
  359. }
  360. /// Kinda the same as `remove_blur`, but better: it also untracks the window.
  361. /// This needs to be called when the component is being disabled, else it
  362. /// would cause havoc by having untracked windows during normal operations,
  363. /// which is not the point at all!
  364. /// Accepts a pid corresponding (or not) to a blurred (or not) meta window.
  365. untrack_meta_window(pid) {
  366. this.remove_blur(pid);
  367. let meta_window = this.meta_window_map.get(pid);
  368. if (meta_window) {
  369. this.connections.disconnect_all_for(meta_window);
  370. this.meta_window_map.delete(pid);
  371. }
  372. }
  373. disable() {
  374. this._log("removing blur from applications...");
  375. this.service?.unexport();
  376. delete this.mutter_gsettings;
  377. this.meta_window_map.forEach((_meta_window, pid) => {
  378. this.untrack_meta_window(pid);
  379. });
  380. this.connections.disconnect_all();
  381. this.paint_signals.disconnect_all();
  382. }
  383. _log(str) {
  384. if (this.settings.DEBUG)
  385. console.log(`[Blur my Shell > applications] ${str}`);
  386. }
  387. };