dash_to_dock.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import Meta from 'gi://Meta';
  2. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  3. import * as Signals from 'resource:///org/gnome/shell/misc/signals.js';
  4. import { PaintSignals } from '../conveniences/paint_signals.js';
  5. import { Pipeline } from '../conveniences/pipeline.js';
  6. import { DummyPipeline } from '../conveniences/dummy_pipeline.js';
  7. const DASH_STYLES = [
  8. "transparent-dash",
  9. "light-dash",
  10. "dark-dash"
  11. ];
  12. /// This type of object is created for every dash found, and talks to the main
  13. /// DashBlur thanks to signals.
  14. ///
  15. /// This allows to dynamically track the created dashes for each screen.
  16. class DashInfos {
  17. constructor(
  18. dash_blur, dash, dash_container, dash_background,
  19. background, background_group, bg_manager
  20. ) {
  21. // the parent DashBlur object, to communicate
  22. this.dash_blur = dash_blur;
  23. this.dash = dash;
  24. this.dash_container = dash_container;
  25. this.dash_background = dash_background;
  26. this.background = background;
  27. this.background_group = background_group;
  28. this.bg_manager = bg_manager;
  29. this.settings = dash_blur.settings;
  30. this.old_style = this.dash._background.style;
  31. this.dash_destroy_id = dash.connect('destroy', () => this.remove_dash_blur(false));
  32. this.dash_blur_connections_ids = [];
  33. this.dash_blur_connections_ids.push(
  34. this.dash_blur.connect('remove-dashes', () => this.remove_dash_blur()),
  35. this.dash_blur.connect('override-style', () => this.override_style()),
  36. this.dash_blur.connect('remove-style', () => this.remove_style()),
  37. this.dash_blur.connect('show', () => this.background_group.show()),
  38. this.dash_blur.connect('hide', () => this.background_group.hide()),
  39. this.dash_blur.connect('update-size', () => this.update_size()),
  40. this.dash_blur.connect('change-blur-type', () => this.change_blur_type()),
  41. this.dash_blur.connect('update-pipeline', () => this.update_pipeline())
  42. );
  43. }
  44. // IMPORTANT: do never call this in a mutable `this.dash_blur.forEach`
  45. remove_dash_blur(dash_not_already_destroyed = true) {
  46. // remove the style and destroy the effects
  47. this.remove_style();
  48. this.destroy_dash(dash_not_already_destroyed);
  49. // remove the dash infos from their list
  50. const dash_infos_index = this.dash_blur.dashes.indexOf(this);
  51. if (dash_infos_index >= 0)
  52. this.dash_blur.dashes.splice(dash_infos_index, 1);
  53. // disconnect everything
  54. this.dash_blur_connections_ids.forEach(id => { if (id) this.dash_blur.disconnect(id); });
  55. this.dash_blur_connections_ids = [];
  56. if (this.dash_destroy_id)
  57. this.dash.disconnect(this.dash_destroy_id);
  58. this.dash_destroy_id = null;
  59. }
  60. override_style() {
  61. this.remove_style();
  62. this.dash.set_style_class_name(
  63. DASH_STYLES[this.settings.dash_to_dock.STYLE_DASH_TO_DOCK]
  64. );
  65. }
  66. remove_style() {
  67. this.dash._background.style = this.old_style;
  68. DASH_STYLES.forEach(
  69. style => this.dash.remove_style_class_name(style)
  70. );
  71. }
  72. destroy_dash(dash_not_already_destroyed = true) {
  73. if (!dash_not_already_destroyed)
  74. this.bg_manager.backgroundActor = null;
  75. this.paint_signals?.disconnect_all();
  76. this.dash.get_parent().remove_child(this.background_group);
  77. this.bg_manager._bms_pipeline.destroy();
  78. this.bg_manager.destroy();
  79. this.background_group.destroy();
  80. }
  81. change_blur_type() {
  82. this.destroy_dash();
  83. let [
  84. background, background_group, bg_manager, paint_signals
  85. ] = this.dash_blur.add_blur(this.dash);
  86. this.background = background;
  87. this.background_group = background_group;
  88. this.bg_manager = bg_manager;
  89. this.paint_signals = paint_signals;
  90. this.dash.get_parent().insert_child_at_index(this.background_group, 0);
  91. this.update_size();
  92. }
  93. update_pipeline() {
  94. this.bg_manager._bms_pipeline.change_pipeline_to(
  95. this.settings.dash_to_dock.PIPELINE
  96. );
  97. }
  98. update_size() {
  99. if (this.dash_blur.is_static) {
  100. let [x, y] = this.get_dash_position(this.dash_container, this.dash_background);
  101. this.background.x = -x;
  102. this.background.y = -y;
  103. if (this.dash_container.get_style_class_name().includes("top"))
  104. this.background.set_clip(
  105. x,
  106. y + this.dash.y + this.dash_background.y,
  107. this.dash_background.width,
  108. this.dash_background.height
  109. );
  110. else if (this.dash_container.get_style_class_name().includes("bottom"))
  111. this.background.set_clip(
  112. x,
  113. y + this.dash.y + this.dash_background.y,
  114. this.dash_background.width,
  115. this.dash_background.height
  116. );
  117. else if (this.dash_container.get_style_class_name().includes("left"))
  118. this.background.set_clip(
  119. x + this.dash.x + this.dash_background.x,
  120. y + this.dash.y + this.dash_background.y,
  121. this.dash_background.width,
  122. this.dash_background.height
  123. );
  124. else if (this.dash_container.get_style_class_name().includes("right"))
  125. this.background.set_clip(
  126. x + this.dash.x + this.dash_background.x,
  127. y + this.dash.y + this.dash_background.y,
  128. this.dash_background.width,
  129. this.dash_background.height
  130. );
  131. } else {
  132. this.background.width = this.dash_background.width;
  133. this.background.height = this.dash_background.height;
  134. this.background.x = this.dash_background.x;
  135. this.background.y = this.dash_background.y + this.dash.y;
  136. }
  137. }
  138. get_dash_position(dash_container, dash_background) {
  139. var x, y;
  140. let monitor = Main.layoutManager.findMonitorForActor(dash_container);
  141. let dash_box = dash_container._slider.get_child();
  142. if (dash_container.get_style_class_name().includes("top")) {
  143. x = (monitor.width - dash_background.width) / 2;
  144. y = dash_box.y;
  145. } else if (dash_container.get_style_class_name().includes("bottom")) {
  146. x = (monitor.width - dash_background.width) / 2;
  147. y = monitor.height - dash_container.height;
  148. } else if (dash_container.get_style_class_name().includes("left")) {
  149. x = dash_box.x;
  150. y = dash_container.y + (dash_container.height - dash_background.height) / 2 - dash_background.y;
  151. } else if (dash_container.get_style_class_name().includes("right")) {
  152. x = monitor.width - dash_container.width;
  153. y = dash_container.y + (dash_container.height - dash_background.height) / 2 - dash_background.y;
  154. }
  155. return [x, y];
  156. }
  157. _log(str) {
  158. if (this.settings.DEBUG)
  159. console.log(`[Blur my Shell > dash] ${str}`);
  160. }
  161. _warn(str) {
  162. console.warn(`[Blur my Shell > dash] ${str}`);
  163. }
  164. }
  165. export const DashBlur = class DashBlur extends Signals.EventEmitter {
  166. constructor(connections, settings, _) {
  167. super();
  168. this.dashes = [];
  169. this.connections = connections;
  170. this.settings = settings;
  171. this.paint_signals = new PaintSignals(connections);
  172. this.is_static = this.settings.dash_to_dock.STATIC_BLUR;
  173. this.enabled = false;
  174. }
  175. enable() {
  176. this.connections.connect(Main.uiGroup, 'child-added', (_, actor) => {
  177. if (
  178. (actor.get_name() === "dashtodockContainer") &&
  179. (actor.constructor.name === 'DashToDock')
  180. )
  181. this.try_blur(actor);
  182. });
  183. this.blur_existing_dashes();
  184. this.connect_to_overview();
  185. this.update_size();
  186. this.enabled = true;
  187. }
  188. // Finds all existing dashes on every monitor, and call `try_blur` on them
  189. // We cannot only blur `Main.overview.dash`, as there could be several
  190. blur_existing_dashes() {
  191. this._log("searching for dash");
  192. // blur every dash found, filtered by name
  193. Main.uiGroup.get_children().filter((child) => {
  194. return (child.get_name() === "dashtodockContainer") &&
  195. (child.constructor.name === 'DashToDock');
  196. }).forEach(dash_container => this.try_blur(dash_container));
  197. }
  198. // Tries to blur the dash contained in the given actor
  199. try_blur(dash_container) {
  200. let dash_box = dash_container._slider.get_child();
  201. // verify that we did not already blur that dash
  202. if (!dash_box.get_children().some(child =>
  203. child.get_name() === "bms-dash-backgroundgroup"
  204. )) {
  205. this._log("dash to dock found, blurring it");
  206. // finally blur the dash
  207. let dash = dash_box.get_children().find(child => {
  208. return child.get_name() === 'dash';
  209. });
  210. this.dashes.push(this.blur_dash_from(dash, dash_container));
  211. }
  212. }
  213. // Blurs the dash and returns a `DashInfos` containing its information
  214. blur_dash_from(dash, dash_container) {
  215. let [background, background_group, bg_manager, paint_signals] = this.add_blur(dash);
  216. // insert the background group to the right element
  217. dash.get_parent().insert_child_at_index(background_group, 0);
  218. // updates size and position on change
  219. this.connections.connect(
  220. dash,
  221. ['notify::width', 'notify::height'],
  222. _ => this.update_size()
  223. );
  224. this.connections.connect(
  225. dash_container,
  226. ['notify::width', 'notify::height', 'notify::y', 'notify::x'],
  227. _ => this.update_size()
  228. );
  229. const dash_background = dash.get_children().find(child => {
  230. return child.get_style_class_name() === 'dash-background';
  231. });
  232. // create infos
  233. let infos = new DashInfos(
  234. this,
  235. dash,
  236. dash_container,
  237. dash_background,
  238. background,
  239. background_group,
  240. bg_manager,
  241. paint_signals
  242. );
  243. this.update_size();
  244. this.update_background();
  245. // returns infos
  246. return infos;
  247. }
  248. add_blur(dash) {
  249. const monitor = Main.layoutManager.findMonitorForActor(dash);
  250. if (!monitor)
  251. return;
  252. const background_group = new Meta.BackgroundGroup({
  253. name: 'bms-dash-backgroundgroup', width: 0, height: 0
  254. });
  255. let background, bg_manager, paint_signals;
  256. let static_blur = this.settings.dash_to_dock.STATIC_BLUR;
  257. if (static_blur) {
  258. let bg_manager_list = [];
  259. const pipeline = new Pipeline(
  260. global.blur_my_shell._effects_manager,
  261. global.blur_my_shell._pipelines_manager,
  262. this.settings.dash_to_dock.PIPELINE
  263. );
  264. background = pipeline.create_background_with_effects(
  265. monitor.index, bg_manager_list,
  266. background_group, 'bms-dash-blurred-widget'
  267. );
  268. bg_manager = bg_manager_list[0];
  269. }
  270. else {
  271. const pipeline = new DummyPipeline(
  272. global.blur_my_shell._effects_manager,
  273. this.settings.dash_to_dock
  274. );
  275. [background, bg_manager] = pipeline.create_background_with_effect(
  276. background_group, 'bms-dash-blurred-widget'
  277. );
  278. paint_signals = new PaintSignals(this.connections);
  279. // HACK
  280. //
  281. //`Shell.BlurEffect` does not repaint when shadows are under it. [1]
  282. //
  283. // This does not entirely fix this bug (shadows caused by windows
  284. // still cause artifacts), but it prevents the shadows of the dash
  285. // buttons to cause artifacts on the dash itself
  286. //
  287. // [1]: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/2857
  288. if (this.settings.HACKS_LEVEL === 1) {
  289. this._log("hack level 1");
  290. paint_signals.disconnect_all();
  291. paint_signals.connect(background, pipeline.effect);
  292. } else {
  293. paint_signals.disconnect_all();
  294. }
  295. }
  296. return [background, background_group, bg_manager, paint_signals];
  297. }
  298. change_blur_type() {
  299. this.is_static = this.settings.dash_to_dock.STATIC_BLUR;
  300. this.emit('change-blur-type');
  301. this.update_background();
  302. }
  303. /// Connect when overview if opened/closed to hide/show the blur accordingly
  304. connect_to_overview() {
  305. this.connections.disconnect_all_for(Main.overview);
  306. if (this.settings.dash_to_dock.UNBLUR_IN_OVERVIEW) {
  307. this.connections.connect(
  308. Main.overview, 'showing', _ => this.hide()
  309. );
  310. this.connections.connect(
  311. Main.overview, 'hidden', _ => this.show()
  312. );
  313. }
  314. };
  315. /// Updates the background to either remove it or not, according to the
  316. /// user preferences.
  317. update_background() {
  318. this._log("updating background");
  319. if (this.settings.dash_to_dock.OVERRIDE_BACKGROUND)
  320. this.emit('override-style');
  321. else
  322. this.emit('remove-style');
  323. }
  324. update_pipeline() {
  325. this.emit('update-pipeline');
  326. }
  327. update_size() {
  328. this.emit('update-size');
  329. }
  330. show() {
  331. this.emit('show');
  332. }
  333. hide() {
  334. this.emit('hide');
  335. }
  336. disable() {
  337. this._log("removing blur from dashes");
  338. this.emit('remove-dashes');
  339. this.dashes = [];
  340. this.connections.disconnect_all();
  341. this.enabled = false;
  342. }
  343. _log(str) {
  344. if (this.settings.DEBUG)
  345. console.log(`[Blur my Shell > dash manager] ${str}`);
  346. }
  347. _warn(str) {
  348. console.warn(`[Blur my Shell > dash manager] ${str}`);
  349. }
  350. };