tree.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /*
  2. * This file is part of the Forge extension for GNOME
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. // Gnome imports
  19. import GObject from "gi://GObject";
  20. import Meta from "gi://Meta";
  21. import Shell from "gi://Shell";
  22. import St from "gi://St";
  23. // Shared state
  24. import { Logger } from "../shared/logger.js";
  25. // App imports
  26. import * as Utils from "./utils.js";
  27. import * as Window from "./window.js";
  28. export const NODE_TYPES = Utils.createEnum([
  29. "ROOT",
  30. "MONITOR", //Output in i3
  31. "CON", //Container in i3
  32. "WINDOW",
  33. "WORKSPACE",
  34. ]);
  35. export const LAYOUT_TYPES = Utils.createEnum([
  36. "STACKED",
  37. "TABBED",
  38. "ROOT",
  39. "HSPLIT",
  40. "VSPLIT",
  41. "PRESET",
  42. ]);
  43. export const ORIENTATION_TYPES = Utils.createEnum(["NONE", "HORIZONTAL", "VERTICAL"]);
  44. export const POSITION = Utils.createEnum(["BEFORE", "AFTER", "UNKNOWN"]);
  45. /**
  46. * The Node data representation of the following elements in the user's display:
  47. *
  48. * Monitor,
  49. * Window,
  50. * Container (generic),
  51. * Workspace
  52. *
  53. */
  54. export class Node extends GObject.Object {
  55. static {
  56. GObject.registerClass(this);
  57. }
  58. constructor(type, data) {
  59. super();
  60. // TODO - move to GObject property definitions?
  61. this._type = type; // see NODE_TYPES
  62. // _data: Meta.Window, unique id strings (Monitor,
  63. // Workspace or St.Bin - a representation of Container)
  64. this._data = data;
  65. this._parent = null;
  66. this._nodes = []; // Child elements of this node
  67. this.mode = Window.WINDOW_MODES.DEFAULT;
  68. this.percent = 0.0;
  69. this._rect = null;
  70. this.tab = null;
  71. this.decoration = null;
  72. this.app = null;
  73. if (this.isWindow()) {
  74. // When destroy() is called on Meta.Window, it might not be
  75. // available so we store it immediately
  76. this._initMetaWindow();
  77. this._actor = this._data.get_compositor_private();
  78. this._createWindowTab();
  79. }
  80. if (this.isCon()) {
  81. this._createDecoration();
  82. }
  83. }
  84. get windowActor() {
  85. return this._actor;
  86. }
  87. get actor() {
  88. switch (this.nodeType) {
  89. case NODE_TYPES.WINDOW:
  90. // A Meta.Window was assigned during creation
  91. // But obtain the Clutter.Actor
  92. return this._actor;
  93. case NODE_TYPES.CON:
  94. case NODE_TYPES.ROOT:
  95. // A St.Bin was assigned during creation
  96. return this.nodeValue;
  97. case NODE_TYPES.MONITOR:
  98. case NODE_TYPES.WORKSPACE:
  99. // A separate St.Bin was assigned on another attribute during creation
  100. return this.actorBin;
  101. }
  102. }
  103. set rect(rect) {
  104. this._rect = rect;
  105. switch (this.nodeType) {
  106. case NODE_TYPES.WINDOW:
  107. break;
  108. case NODE_TYPES.CON:
  109. case NODE_TYPES.MONITOR:
  110. case NODE_TYPES.ROOT:
  111. case NODE_TYPES.WORKSPACE:
  112. if (this.actor) {
  113. this.actor.set_size(rect.width, rect.height);
  114. this.actor.set_position(rect.x, rect.y);
  115. }
  116. break;
  117. }
  118. }
  119. get rect() {
  120. return this._rect;
  121. }
  122. get childNodes() {
  123. return this._nodes;
  124. }
  125. set childNodes(nodes) {
  126. this._nodes = nodes;
  127. }
  128. get firstChild() {
  129. if (this._nodes && this._nodes.length >= 1) {
  130. return this._nodes[0];
  131. }
  132. return null;
  133. }
  134. get level() {
  135. let _level = 0;
  136. let refNode = this.parentNode;
  137. while (refNode) {
  138. _level += 1;
  139. refNode = refNode.parentNode;
  140. }
  141. return _level;
  142. }
  143. /**
  144. * Find the index of this relative to the siblings
  145. */
  146. get index() {
  147. if (this.parentNode) {
  148. let childNodes = this.parentNode.childNodes;
  149. for (let i = 0; i < childNodes.length; i++) {
  150. if (childNodes[i] === this) {
  151. return i;
  152. }
  153. }
  154. }
  155. return null;
  156. }
  157. get lastChild() {
  158. if (this._nodes && this._nodes.length >= 1) {
  159. return this._nodes[this._nodes.length - 1];
  160. }
  161. return null;
  162. }
  163. get nextSibling() {
  164. if (this.parentNode) {
  165. if (this.parentNode.lastChild !== this) {
  166. return this.parentNode.childNodes[this.index + 1];
  167. }
  168. }
  169. return null;
  170. }
  171. get nodeType() {
  172. return this._type;
  173. }
  174. get nodeValue() {
  175. return this._data;
  176. }
  177. get parentNode() {
  178. return this._parent;
  179. }
  180. set parentNode(node) {
  181. this._parent = node;
  182. }
  183. get previousSibling() {
  184. if (this.parentNode) {
  185. if (this.parentNode.firstChild !== this) {
  186. return this.parentNode.childNodes[this.index - 1];
  187. }
  188. }
  189. return null;
  190. }
  191. appendChild(node) {
  192. if (!node) return null;
  193. if (node.parentNode) node.parentNode.removeChild(node);
  194. this.childNodes.push(node);
  195. node.parentNode = this;
  196. return node;
  197. }
  198. /**
  199. * Checks if node is a descendant of this,
  200. * or a descendant of its childNodes, etc
  201. */
  202. contains(node) {
  203. if (!node) return false;
  204. let searchNode = this.getNodeByValue(node.nodeValue);
  205. return searchNode ? true : false;
  206. }
  207. getNodeByLayout(layout) {
  208. let results = this._search(layout, "LAYOUT");
  209. return results;
  210. }
  211. getNodeByMode(mode) {
  212. let results = this._search(mode, "MODE");
  213. return results;
  214. }
  215. getNodeByValue(value) {
  216. let results = this._search(value, "VALUE");
  217. return results && results.length >= 1 ? results[0] : null;
  218. }
  219. getNodeByType(type) {
  220. let results = this._search(type, "TYPE");
  221. return results;
  222. }
  223. /**
  224. * @param childNode - is a child of this
  225. */
  226. insertBefore(newNode, childNode) {
  227. if (!newNode) return null;
  228. if (newNode === childNode) return null;
  229. if (!childNode) {
  230. this.appendChild(newNode);
  231. return newNode;
  232. }
  233. if (childNode.parentNode !== this) return null;
  234. if (newNode.parentNode) newNode.parentNode.removeChild(newNode);
  235. let index = childNode.index;
  236. if (childNode.index === 0) {
  237. this.childNodes.unshift(newNode);
  238. } else if (childNode.index > 0) {
  239. this.childNodes.splice(index, 0, newNode);
  240. }
  241. newNode.parentNode = this;
  242. return newNode;
  243. }
  244. isLayout(name) {
  245. let layout = this.layout;
  246. if (!layout) return false;
  247. return name === layout;
  248. }
  249. isHSplit() {
  250. return this.isLayout(LAYOUT_TYPES.HSPLIT);
  251. }
  252. isVSplit() {
  253. return this.isLayout(LAYOUT_TYPES.VSPLIT);
  254. }
  255. isStacked() {
  256. return this.isLayout(LAYOUT_TYPES.STACKED);
  257. }
  258. isTabbed() {
  259. return this.isLayout(LAYOUT_TYPES.TABBED);
  260. }
  261. isType(name) {
  262. const type = this.nodeType;
  263. if (!type) return false;
  264. return name === type;
  265. }
  266. isWindow() {
  267. return this.isType(NODE_TYPES.WINDOW);
  268. }
  269. isCon() {
  270. return this.isType(NODE_TYPES.CON);
  271. }
  272. isMonitor() {
  273. return this.isType(NODE_TYPES.MONITOR);
  274. }
  275. isWorkspace() {
  276. return this.isType(NODE_TYPES.WORKSPACE);
  277. }
  278. isRoot() {
  279. return this.isType(NODE_TYPES.ROOT);
  280. }
  281. isMode(name) {
  282. const mode = this.mode;
  283. if (!name) return false;
  284. return name === mode;
  285. }
  286. isFloat() {
  287. return this.isMode(Window.WINDOW_MODES.FLOAT);
  288. }
  289. isTile() {
  290. return this.isMode(Window.WINDOW_MODES.TILE);
  291. }
  292. isGrabTile() {
  293. return this.isMode(Window.WINDOW_MODES.GRAB_TILE);
  294. }
  295. removeChild(node) {
  296. if (node.isTabbed() && node.decoration) {
  297. node.decoration.hide();
  298. node.decoration.destroy_all_children();
  299. node.decoration.destroy();
  300. node.decoration = null;
  301. }
  302. let refNode;
  303. if (this.contains(node)) {
  304. // Since contains() tries to find node on all descendants,
  305. // detach only from the immediate parent
  306. let parentNode = node.parentNode;
  307. refNode = parentNode.childNodes.splice(node.index, 1);
  308. refNode.parentNode = null;
  309. }
  310. if (!refNode) {
  311. throw `NodeNotFound ${node}`;
  312. }
  313. return refNode;
  314. }
  315. /**
  316. * Backend for getNodeBy[attribute]. It is similar to DOM.getElementBy functions
  317. */
  318. _search(term, criteria) {
  319. let results = [];
  320. let searchFn = (candidate) => {
  321. if (criteria) {
  322. switch (criteria) {
  323. case "VALUE":
  324. if (candidate.nodeValue === term) {
  325. results.push(candidate);
  326. }
  327. break;
  328. case "TYPE":
  329. if (candidate.nodeType === term) {
  330. results.push(candidate);
  331. }
  332. break;
  333. case "MODE":
  334. if (candidate.mode === term) {
  335. results.push(candidate);
  336. }
  337. case "LAYOUT":
  338. if (candidate.layout && candidate.layout === term) {
  339. results.push(candidate);
  340. }
  341. }
  342. } else {
  343. if (candidate === term) {
  344. results.push(candidate);
  345. }
  346. }
  347. };
  348. this._walk(searchFn, this._traverseBreadthFirst);
  349. return results;
  350. }
  351. // start walking from root and all child nodes
  352. _traverseBreadthFirst(callback) {
  353. let queue = new Queue();
  354. queue.enqueue(this);
  355. let currentNode = queue.dequeue();
  356. while (currentNode) {
  357. for (let i = 0, length = currentNode.childNodes.length; i < length; i++) {
  358. queue.enqueue(currentNode.childNodes[i]);
  359. }
  360. callback(currentNode);
  361. currentNode = queue.dequeue();
  362. }
  363. }
  364. // start walking from bottom to root
  365. _traverseDepthFirst(callback) {
  366. let recurse = (currentNode) => {
  367. for (let i = 0, length = currentNode.childNodes.length; i < length; i++) {
  368. recurse(currentNode.childNodes[i]);
  369. }
  370. callback(currentNode);
  371. };
  372. recurse(this);
  373. }
  374. _walk(callback, traversal) {
  375. traversal.call(this, callback);
  376. }
  377. _initMetaWindow() {
  378. if (this.isWindow()) {
  379. let windowTracker = Shell.WindowTracker.get_default();
  380. let metaWin = this.nodeValue;
  381. let app = windowTracker.get_window_app(metaWin);
  382. this.app = app;
  383. }
  384. }
  385. _createWindowTab() {
  386. if (this.tab || !this.isWindow()) return;
  387. let tabContents = new St.BoxLayout({
  388. style_class: "window-tabbed-tab",
  389. x_expand: true,
  390. });
  391. let app = this.app;
  392. let labelText = this._getTitle();
  393. let metaWin = this.nodeValue;
  394. let titleButton = new St.Button({
  395. x_expand: true,
  396. label: `${labelText}`,
  397. });
  398. let iconBin = new St.Button({
  399. style_class: "window-tabbed-tab-icon",
  400. });
  401. let icon = app.create_icon_texture(24 * Utils.dpi());
  402. iconBin.child = icon;
  403. let closeButton = new St.Button({
  404. style_class: "window-tabbed-tab-close",
  405. child: new St.Icon({ icon_name: "window-close-symbolic" }),
  406. });
  407. tabContents.add(iconBin);
  408. tabContents.add(titleButton);
  409. tabContents.add(closeButton);
  410. let clickFn = () => {
  411. this.parentNode.childNodes.forEach((c) => {
  412. if (c.tab) {
  413. c.tab.remove_style_class_name("window-tabbed-tab-active");
  414. c.render();
  415. }
  416. });
  417. tabContents.add_style_class_name("window-tabbed-tab-active");
  418. metaWin.activate(global.display.get_current_time());
  419. };
  420. iconBin.connect("clicked", clickFn);
  421. titleButton.connect("clicked", clickFn);
  422. closeButton.connect("clicked", () => {
  423. metaWin.delete(global.get_current_time());
  424. });
  425. if (metaWin === global.display.get_focus_window()) {
  426. tabContents.add_style_class_name("window-tabbed-tab-active");
  427. }
  428. this.tab = tabContents;
  429. }
  430. _createDecoration() {
  431. if (this.decoration) return;
  432. let decoration = new St.BoxLayout();
  433. decoration.type = "forge-deco";
  434. decoration.parentNode = this;
  435. let globalWinGrp = global.window_group;
  436. decoration.style_class = "window-tabbed-bg";
  437. if (!globalWinGrp.contains(decoration)) {
  438. globalWinGrp.add_child(decoration);
  439. }
  440. decoration.hide();
  441. this.decoration = decoration;
  442. }
  443. _getTitle() {
  444. if (this.isWindow()) {
  445. return this.nodeValue.title ? this.nodeValue.title : this.app.get_name();
  446. }
  447. return null;
  448. }
  449. render() {
  450. // Always update the title for the tab
  451. if (this.tab !== null && this.tab !== undefined) {
  452. let titleLabel = this.tab.get_child_at_index(1);
  453. if (titleLabel) titleLabel.label = this._getTitle();
  454. }
  455. }
  456. set float(value) {
  457. if (this.isWindow()) {
  458. let metaWindow = this.nodeValue;
  459. let floatAlwaysOnTop = this.settings.get_boolean("float-always-on-top-enabled");
  460. if (value) {
  461. this.mode = Window.WINDOW_MODES.FLOAT;
  462. if (!metaWindow.is_above()) {
  463. floatAlwaysOnTop && metaWindow.make_above();
  464. }
  465. } else {
  466. this.mode = Window.WINDOW_MODES.TILE;
  467. if (metaWindow.is_above()) {
  468. metaWindow.unmake_above();
  469. }
  470. }
  471. }
  472. }
  473. set tile(value) {
  474. this.float = !value;
  475. }
  476. }
  477. /**
  478. * An implementation of Queue using arrays
  479. */
  480. export class Queue extends GObject.Object {
  481. static {
  482. GObject.registerClass(this);
  483. }
  484. constructor() {
  485. super();
  486. this._elements = [];
  487. }
  488. get length() {
  489. return this._elements.length;
  490. }
  491. enqueue(item) {
  492. this._elements.push(item);
  493. }
  494. dequeue() {
  495. return this._elements.shift();
  496. }
  497. }
  498. export class Tree extends Node {
  499. static {
  500. GObject.registerClass(this);
  501. }
  502. /** @param {Window.WindowManager} extWm */
  503. constructor(extWm) {
  504. let rootBin = new St.Bin();
  505. super(NODE_TYPES.ROOT, rootBin);
  506. this._extWm = extWm;
  507. this.defaultStackHeight = 35;
  508. this.settings = this.extWm.ext.settings;
  509. this.layout = LAYOUT_TYPES.ROOT;
  510. if (!global.window_group.contains(rootBin)) global.window_group.add_child(rootBin);
  511. this._initWorkspaces();
  512. }
  513. /** @type {Window.WindowManager} */
  514. get extWm() {
  515. return this._extWm;
  516. }
  517. /**
  518. * Handles new and existing workspaces in the tree
  519. */
  520. _initWorkspaces() {
  521. let wsManager = global.display.get_workspace_manager();
  522. let workspaces = wsManager.get_n_workspaces();
  523. for (let i = 0; i < workspaces; i++) {
  524. this.addWorkspace(i);
  525. }
  526. }
  527. // TODO move to monitor.js
  528. addMonitor(wsIndex) {
  529. let monitors = global.display.get_n_monitors();
  530. for (let mi = 0; mi < monitors; mi++) {
  531. let monitorWsNode = this.createNode(
  532. `ws${wsIndex}`,
  533. NODE_TYPES.MONITOR,
  534. `mo${mi}ws${wsIndex}`
  535. );
  536. monitorWsNode.layout = this.extWm.determineSplitLayout();
  537. monitorWsNode.actorBin = new St.Bin();
  538. if (!global.window_group.contains(monitorWsNode.actorBin))
  539. global.window_group.add_child(monitorWsNode.actorBin);
  540. }
  541. }
  542. // TODO move to workspace.js
  543. addWorkspace(wsIndex) {
  544. let wsManager = global.display.get_workspace_manager();
  545. let workspaceNodeValue = `ws${wsIndex}`;
  546. let existingWsNode = this.findNode(workspaceNodeValue);
  547. if (existingWsNode) {
  548. return false;
  549. }
  550. let newWsNode = this.createNode(this.nodeValue, NODE_TYPES.WORKSPACE, workspaceNodeValue);
  551. let workspace = wsManager.get_workspace_by_index(wsIndex);
  552. newWsNode.layout = LAYOUT_TYPES.HSPLIT;
  553. newWsNode.actorBin = new St.Bin({ style_class: "workspace-actor-bg" });
  554. if (!global.window_group.contains(newWsNode.actorBin))
  555. global.window_group.add_child(newWsNode.actorBin);
  556. this.extWm.bindWorkspaceSignals(workspace);
  557. this.addMonitor(wsIndex);
  558. return true;
  559. }
  560. // TODO move to workspace.js
  561. removeWorkspace(wsIndex) {
  562. let workspaceNodeData = `ws${wsIndex}`;
  563. let existingWsNode = this.findNode(workspaceNodeData);
  564. if (!existingWsNode) {
  565. return false;
  566. }
  567. if (global.window_group.contains(existingWsNode.actorBin))
  568. global.window_group.remove_child(existingWsNode.actorBin);
  569. this.removeChild(existingWsNode);
  570. return true;
  571. }
  572. get nodeWorkpaces() {
  573. let nodeWorkspaces = this.getNodeByType(NODE_TYPES.WORKSPACE);
  574. return nodeWorkspaces;
  575. }
  576. get nodeWindows() {
  577. let nodeWindows = this.getNodeByType(NODE_TYPES.WINDOW);
  578. return nodeWindows;
  579. }
  580. /**
  581. * Creates a new Node and attaches it to a parent toData.
  582. * Parent can be MONITOR or CON types only.
  583. */
  584. createNode(parentObj, type, value, mode = Window.WINDOW_MODES.TILE) {
  585. let parentNode = this.findNode(parentObj);
  586. let child;
  587. if (parentNode) {
  588. child = new Node(type, value);
  589. child.settings = this.settings;
  590. if (child.isWindow()) child.mode = mode;
  591. // Append after a window
  592. if (parentNode.isWindow()) {
  593. const grandParentNode = parentNode.parentNode;
  594. grandParentNode.insertBefore(child, parentNode.nextSibling);
  595. Logger.debug(
  596. `Parent is a window, attaching to this window's parent ${grandParentNode.nodeType}`
  597. );
  598. } else {
  599. // Append as the last item of the container
  600. parentNode.appendChild(child);
  601. }
  602. }
  603. return child;
  604. }
  605. /**
  606. * Finds any Node in the tree using data
  607. * Data types can be in the form of Meta.Window or unique id strings
  608. * for Workspace, Monitor and Container
  609. *
  610. * Workspace id strings takes the form `ws{n}`.
  611. * Monitor id strings takes the form `mo{m}ws{n}`
  612. * Container id strings takes the form `mo{m}ws{n}c{x}`
  613. *
  614. */
  615. findNode(data) {
  616. let searchNode = this.getNodeByValue(data);
  617. return searchNode;
  618. }
  619. /**
  620. * Find the NodeWindow using the Meta.WindowActor
  621. */
  622. findNodeByActor(windowActor) {
  623. let searchNode;
  624. let criteriaMatchFn = (node) => {
  625. if (node.isWindow() && node.actor === windowActor) {
  626. searchNode = node;
  627. }
  628. };
  629. this._walk(criteriaMatchFn, this._traverseDepthFirst);
  630. return searchNode;
  631. }
  632. /**
  633. * Focuses on the next node, if metaWindow and tiled, raise it
  634. */
  635. focus(node, direction) {
  636. if (!node) return null;
  637. let next = this.next(node, direction);
  638. if (!next) return null;
  639. let type = next.nodeType;
  640. let position = Utils.positionFromDirection(direction);
  641. const previous = position === POSITION.BEFORE;
  642. switch (type) {
  643. case NODE_TYPES.WINDOW:
  644. break;
  645. case NODE_TYPES.CON:
  646. const tiledConWindows = next.getNodeByType(NODE_TYPES.WINDOW).filter((w) => w.isTile());
  647. if (next.layout === LAYOUT_TYPES.STACKED) {
  648. next = next.lastChild;
  649. } else {
  650. if (tiledConWindows.length > 1) {
  651. if (previous) {
  652. next = tiledConWindows[tiledConWindows.length - 1];
  653. } else {
  654. next = tiledConWindows[0];
  655. }
  656. } else {
  657. next = tiledConWindows[0];
  658. }
  659. }
  660. break;
  661. case NODE_TYPES.MONITOR:
  662. if (next.layout === LAYOUT_TYPES.STACKED) {
  663. next = next.lastChild;
  664. } else {
  665. if (previous) {
  666. next = next.lastChild;
  667. } else {
  668. next = next.firstChild;
  669. }
  670. }
  671. if (next && next.nodeType === NODE_TYPES.CON) {
  672. const tiledConWindows = next.getNodeByType(NODE_TYPES.WINDOW).filter((w) => w.isTile());
  673. if (next.layout === LAYOUT_TYPES.STACKED) {
  674. next = next.lastChild;
  675. } else {
  676. if (tiledConWindows.length > 1) {
  677. if (previous) {
  678. next = tiledConWindows[tiledConWindows.length - 1];
  679. } else {
  680. next = tiledConWindows[0];
  681. }
  682. } else {
  683. next = tiledConWindows[0];
  684. }
  685. }
  686. }
  687. break;
  688. }
  689. if (!next) return null;
  690. let metaWindow = next.nodeValue;
  691. if (!metaWindow) return null;
  692. if (metaWindow.minimized) {
  693. next = this.focus(next, direction);
  694. } else {
  695. metaWindow.raise();
  696. metaWindow.focus(global.display.get_current_time());
  697. metaWindow.activate(global.display.get_current_time());
  698. if (this.settings.get_boolean("move-pointer-focus-enabled")) {
  699. this.extWm.movePointerWith(next);
  700. } else {
  701. let monitorArea = metaWindow.get_work_area_current_monitor();
  702. let ptr = this.extWm.getPointer();
  703. if (!Utils.rectContainsPoint(monitorArea, [ptr[0], ptr[1]])) {
  704. this.extWm.movePointerWith(next);
  705. }
  706. }
  707. }
  708. return next;
  709. }
  710. /**
  711. * Obtains the non-floating, non-minimized list of nodes
  712. * Useful for calculating the rect areas
  713. */
  714. getTiledChildren(items) {
  715. let filterFn = (node) => {
  716. if (node.isWindow()) {
  717. let floating = node.isFloat();
  718. let grabTiling = node.isGrabTile();
  719. // A Node[Window]._data is a Meta.Window
  720. if (!node.nodeValue.minimized && !(floating || grabTiling)) {
  721. return true;
  722. }
  723. }
  724. // handle split containers
  725. if (node.isCon()) {
  726. return this.getTiledChildren(node.childNodes).length > 0;
  727. }
  728. return false;
  729. };
  730. return items ? items.filter(filterFn) : [];
  731. }
  732. /**
  733. * Move a given node into a direction
  734. *
  735. * TODO, handle minimized or floating windows
  736. *
  737. */
  738. move(node, direction) {
  739. let next = this.next(node, direction);
  740. let position = Utils.positionFromDirection(direction);
  741. if (!next || next === -1) {
  742. if (next === -1) {
  743. // TODO - update appending or prepending on the same monitor
  744. const currMonWsNode = this.extWm.currentMonWsNode;
  745. if (currMonWsNode) {
  746. if (position === POSITION.AFTER) {
  747. currMonWsNode.appendChild(node);
  748. } else {
  749. currMonWsNode.insertBefore(node, next.firstChild);
  750. }
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. let parentNode = node.parentNode;
  757. let parentTarget;
  758. switch (next.nodeType) {
  759. case NODE_TYPES.WINDOW:
  760. // If same parent, swap
  761. if (next === node.previousSibling || next === node.nextSibling) {
  762. parentTarget = next.parentNode;
  763. this.swapPairs(node, next);
  764. if (this.settings.get_boolean("move-pointer-focus-enabled")) {
  765. this.extWm.movePointerWith(node);
  766. }
  767. // do not reset percent when swapped
  768. return true;
  769. } else {
  770. parentTarget = next.parentNode;
  771. if (parentTarget) {
  772. if (position === POSITION.AFTER) {
  773. parentTarget.insertBefore(node, next);
  774. } else {
  775. parentTarget.insertBefore(node, next.nextSibling);
  776. }
  777. }
  778. }
  779. break;
  780. case NODE_TYPES.CON:
  781. parentTarget = next;
  782. if (next.isStacked()) {
  783. next.appendChild(node);
  784. } else {
  785. if (position === POSITION.AFTER) {
  786. next.insertBefore(node, next.firstChild);
  787. } else {
  788. next.appendChild(node);
  789. }
  790. }
  791. break;
  792. case NODE_TYPES.MONITOR:
  793. parentTarget = next;
  794. const currMonWsNode = this.extWm.currentMonWsNode;
  795. if (
  796. !next.contains(node) &&
  797. (node === currMonWsNode.firstChild || node === currMonWsNode.lastChild)
  798. ) {
  799. let targetMonRect = this.extWm.rectForMonitor(node, Utils.monitorIndex(next.nodeValue));
  800. if (!targetMonRect) return false;
  801. if (position === POSITION.AFTER) {
  802. next.insertBefore(node, next.firstChild);
  803. } else {
  804. next.appendChild(node);
  805. }
  806. let rect = targetMonRect;
  807. this.extWm.move(node.nodeValue, rect);
  808. this.extWm.movePointerWith(node);
  809. } else {
  810. if (position === POSITION.AFTER) {
  811. currMonWsNode.appendChild(node);
  812. } else {
  813. currMonWsNode.insertBefore(node, currMonWsNode.firstChild);
  814. }
  815. }
  816. break;
  817. default:
  818. break;
  819. }
  820. this.resetSiblingPercent(parentNode);
  821. this.resetSiblingPercent(parentTarget);
  822. return true;
  823. }
  824. /**
  825. * Give the next sibling/parent/descendant on the tree based
  826. * on a given Meta.MotionDirection
  827. *
  828. * @param {Tree.Node} node
  829. * @param {Meta.MotionDirection} direction
  830. *
  831. * Credits: borrowed logic from tree.c of i3
  832. */
  833. next(node, direction) {
  834. if (!node) return null;
  835. let orientation = Utils.orientationFromDirection(direction);
  836. let position = Utils.positionFromDirection(direction);
  837. let previous = position === POSITION.BEFORE;
  838. const type = node.nodeType;
  839. switch (type) {
  840. case NODE_TYPES.ROOT:
  841. // Root is the top of the tree
  842. if (node.childNodes.length > 1) {
  843. if (previous) {
  844. return node.firstChild;
  845. } else {
  846. return node.lastChild;
  847. }
  848. } else {
  849. return node.firstChild;
  850. }
  851. case NODE_TYPES.WORKSPACE:
  852. // Let gnome-shell handle this?
  853. break;
  854. case NODE_TYPES.MONITOR:
  855. // Find the next monitor
  856. const nodeWindow = this.findFirstNodeWindowFrom(node);
  857. return this.nextMonitor(nodeWindow, position, orientation);
  858. }
  859. while (node.nodeType !== NODE_TYPES.WORKSPACE) {
  860. if (node.nodeType === NODE_TYPES.MONITOR) {
  861. return this.next(node, direction);
  862. }
  863. const parentNode = node.parentNode;
  864. const parentOrientation = Utils.orientationFromLayout(parentNode.layout);
  865. if (parentNode.childNodes.length > 1 && orientation === parentOrientation) {
  866. const next = previous ? node.previousSibling : node.nextSibling;
  867. if (next) {
  868. return next;
  869. }
  870. }
  871. node = node.parentNode;
  872. }
  873. }
  874. nextMonitor(nodeWindow, position, orientation) {
  875. if (!nodeWindow) return null;
  876. // Use the built in logic to determine adjacent monitors
  877. let monitorNode = null;
  878. let monitorDirection = Utils.directionFrom(position, orientation);
  879. let targetMonitor = -1;
  880. targetMonitor = global.display.get_monitor_neighbor_index(
  881. nodeWindow.nodeValue.get_monitor(),
  882. monitorDirection
  883. );
  884. if (targetMonitor < 0) return targetMonitor;
  885. let monWs = `mo${targetMonitor}ws${nodeWindow.nodeValue.get_workspace().index()}`;
  886. monitorNode = this.findNode(monWs);
  887. return monitorNode;
  888. }
  889. findAncestorMonitor(node) {
  890. return this.findAncestor(node, NODE_TYPES.MONITOR);
  891. }
  892. findAncestor(node, ancestorType) {
  893. let ancestorNode;
  894. while (node && ancestorType && !node.isRoot()) {
  895. if (node.isType(ancestorType)) {
  896. ancestorNode = node;
  897. break;
  898. } else {
  899. node = node.parentNode;
  900. }
  901. }
  902. return ancestorNode;
  903. }
  904. nextVisible(node, direction) {
  905. if (!node) return null;
  906. let next = this.next(node, direction);
  907. if (next && next.nodeType === NODE_TYPES.WINDOW && next.nodeValue && next.nodeValue.minimized) {
  908. next = this.nextVisible(next, direction);
  909. }
  910. return next;
  911. }
  912. /**
  913. * Credits: i3-like split
  914. */
  915. split(node, orientation, forceSplit = false) {
  916. if (!node) return;
  917. let type = node.nodeType;
  918. if (type === NODE_TYPES.WINDOW && node.mode === Window.WINDOW_MODES.FLOAT) {
  919. return;
  920. }
  921. if (!(type === NODE_TYPES.MONITOR || type === NODE_TYPES.CON || type === NODE_TYPES.WINDOW)) {
  922. return;
  923. }
  924. let parentNode = node.parentNode;
  925. let numChildren = parentNode.childNodes.length;
  926. // toggle the split
  927. if (
  928. !forceSplit &&
  929. numChildren === 1 &&
  930. (parentNode.layout === LAYOUT_TYPES.HSPLIT || parentNode.layout === LAYOUT_TYPES.VSPLIT)
  931. ) {
  932. parentNode.layout =
  933. orientation === ORIENTATION_TYPES.HORIZONTAL ? LAYOUT_TYPES.HSPLIT : LAYOUT_TYPES.VSPLIT;
  934. this.attachNode = parentNode;
  935. return;
  936. }
  937. // Push down the Meta.Window into a new Container
  938. let currentIndex = node.index;
  939. let container = new St.Bin();
  940. let newConNode = new Node(NODE_TYPES.CON, container);
  941. newConNode.settings = this.settings;
  942. // Take the direction of the parent
  943. newConNode.layout =
  944. orientation === ORIENTATION_TYPES.HORIZONTAL ? LAYOUT_TYPES.HSPLIT : LAYOUT_TYPES.VSPLIT;
  945. newConNode.rect = node.rect;
  946. newConNode.percent = node.percent;
  947. newConNode.parentNode = parentNode;
  948. parentNode.childNodes[currentIndex] = newConNode;
  949. this.createNode(container, node.nodeType, node.nodeValue);
  950. node.parentNode = newConNode;
  951. this.attachNode = newConNode;
  952. }
  953. swap(node, direction) {
  954. let nextSwapNode = this.next(node, direction);
  955. if (!nextSwapNode) {
  956. return;
  957. }
  958. let nodeSwapType = nextSwapNode.nodeType;
  959. switch (nodeSwapType) {
  960. case NODE_TYPES.WINDOW:
  961. break;
  962. case NODE_TYPES.CON:
  963. case NODE_TYPES.MONITOR:
  964. let childWindowNodes = nextSwapNode
  965. .getNodeByMode(Window.WINDOW_MODES.TILE)
  966. .filter((t) => t.nodeType === NODE_TYPES.WINDOW);
  967. if (nextSwapNode.layout === LAYOUT_TYPES.STACKED) {
  968. nextSwapNode = childWindowNodes[childWindowNodes.length - 1];
  969. } else {
  970. nextSwapNode = childWindowNodes[0];
  971. }
  972. break;
  973. }
  974. let isNextNodeWin =
  975. nextSwapNode && nextSwapNode.nodeValue && nextSwapNode.nodeType === NODE_TYPES.WINDOW;
  976. if (isNextNodeWin) {
  977. if (!this.extWm.sameParentMonitor(node, nextSwapNode)) {
  978. // TODO, there is a freeze bug if there are not in same monitor.
  979. return;
  980. }
  981. this.swapPairs(node, nextSwapNode);
  982. }
  983. return nextSwapNode;
  984. }
  985. swapPairs(fromNode, toNode, focus = true) {
  986. if (!(this._swappable(fromNode) && this._swappable(toNode))) return;
  987. // Swap the items in the array
  988. let parentForFrom = fromNode ? fromNode.parentNode : undefined;
  989. let parentForTo = toNode.parentNode;
  990. if (parentForTo && parentForFrom) {
  991. let nextIndex = toNode.index;
  992. let focusIndex = fromNode.index;
  993. let transferMode = fromNode.mode;
  994. fromNode.mode = toNode.mode;
  995. toNode.mode = transferMode;
  996. let transferRect = fromNode.nodeValue.get_frame_rect();
  997. let transferToRect = toNode.nodeValue.get_frame_rect();
  998. let transferPercent = fromNode.percent;
  999. fromNode.percent = toNode.percent;
  1000. toNode.percent = transferPercent;
  1001. parentForTo.childNodes[nextIndex] = fromNode;
  1002. fromNode.parentNode = parentForTo;
  1003. parentForFrom.childNodes[focusIndex] = toNode;
  1004. toNode.parentNode = parentForFrom;
  1005. this.extWm.move(fromNode.nodeValue, transferToRect);
  1006. this.extWm.move(toNode.nodeValue, transferRect);
  1007. if (focus) {
  1008. // The fromNode is now on the parent-target
  1009. fromNode.nodeValue.raise();
  1010. fromNode.nodeValue.focus(global.get_current_time());
  1011. }
  1012. }
  1013. }
  1014. _swappable(node) {
  1015. if (!node) return false;
  1016. if (node.nodeType === NODE_TYPES.WINDOW && !node.nodeValue.minimized) {
  1017. return true;
  1018. }
  1019. return false;
  1020. }
  1021. /**
  1022. * Performs cleanup of dangling parents in addition to removing the
  1023. * node from the parent.
  1024. */
  1025. removeNode(node) {
  1026. let oldChild;
  1027. let cleanUpParent = (existParent) => {
  1028. if (this.getTiledChildren(existParent.childNodes).length === 0) {
  1029. existParent.percent = 0.0;
  1030. this.resetSiblingPercent(existParent.parentNode);
  1031. }
  1032. this.resetSiblingPercent(existParent);
  1033. };
  1034. let parentNode = node.parentNode;
  1035. // If parent has only this window, remove the parent instead
  1036. if (parentNode.childNodes.length === 1 && parentNode.nodeType !== NODE_TYPES.MONITOR) {
  1037. let existParent = parentNode.parentNode;
  1038. oldChild = existParent.removeChild(parentNode);
  1039. cleanUpParent(existParent);
  1040. } else {
  1041. let existParent = node.parentNode;
  1042. oldChild = existParent.removeChild(node);
  1043. if (!this.extWm.floatingWindow(node)) cleanUpParent(existParent);
  1044. }
  1045. if (node === this.attachNode) {
  1046. this.attachNode = null;
  1047. } else {
  1048. // Find the next focus node as attachNode
  1049. this.attachNode = this.findNode(this.extWm.focusMetaWindow);
  1050. }
  1051. return oldChild ? true : false;
  1052. }
  1053. render(from) {
  1054. Logger.debug(`render tree ${from ? "from " + from : ""}`);
  1055. this.processNode(this);
  1056. this.apply(this);
  1057. this.cleanTree();
  1058. let debugMode = true;
  1059. if (debugMode) {
  1060. this.debugTree();
  1061. }
  1062. Logger.debug(`*********************************************`);
  1063. }
  1064. apply(node) {
  1065. if (!node) return;
  1066. let tiledChildren = node
  1067. .getNodeByMode(Window.WINDOW_MODES.TILE)
  1068. .filter((t) => t.nodeType === NODE_TYPES.WINDOW);
  1069. tiledChildren.forEach((w) => {
  1070. if (w.renderRect) {
  1071. if (w.renderRect.width > 0 && w.renderRect.height > 0) {
  1072. let metaWin = w.nodeValue;
  1073. this.extWm.move(metaWin, w.renderRect);
  1074. } else {
  1075. Logger.debug(`ignoring apply for ${w.renderRect.width}x${w.renderRect.height}`);
  1076. }
  1077. }
  1078. if (w.nodeValue.firstRender) w.nodeValue.firstRender = false;
  1079. });
  1080. }
  1081. cleanTree() {
  1082. // Phase 1: remove any cons with empty children
  1083. const orphanCons = this.getNodeByType(NODE_TYPES.CON).filter((c) => c.childNodes.length === 0);
  1084. const hasOrphanCons = orphanCons.length > 0;
  1085. orphanCons.forEach((o) => {
  1086. this.removeNode(o);
  1087. });
  1088. const invalidWindows = this.getNodeByType(NODE_TYPES.WINDOW).filter((w) => {
  1089. const metaWindow = w.nodeValue;
  1090. const title = metaWindow.title;
  1091. const wmClass = metaWindow.wm_class;
  1092. return wmClass === "gjs";
  1093. });
  1094. invalidWindows.forEach((w) => {
  1095. this.removeNode(w);
  1096. });
  1097. // Phase 2: remove any empty parent cons up to the single intermediate parent-window level
  1098. // Basically, flatten them?
  1099. // [con[con[con[con[window]]]]] --> [con[window]]
  1100. // TODO: help :)
  1101. const grandParentCons = this.getNodeByType(NODE_TYPES.CON).filter(
  1102. (c) => c.childNodes.length === 1 && c.childNodes[0].nodeType === NODE_TYPES.CON
  1103. );
  1104. grandParentCons.forEach((c) => {
  1105. c.layout = LAYOUT_TYPES.HSPLIT;
  1106. });
  1107. if (hasOrphanCons || invalidWindows.length > 0) {
  1108. this.processNode(this);
  1109. this.apply(this);
  1110. }
  1111. }
  1112. /**
  1113. *
  1114. * Credits: Do the i3-like calculations
  1115. *
  1116. */
  1117. processNode(node) {
  1118. if (!node) return;
  1119. // Render the Root, Workspace and Monitor
  1120. // For now, we let them render their children recursively
  1121. if (node.nodeType === NODE_TYPES.ROOT) {
  1122. node.childNodes.forEach((child) => {
  1123. this.processNode(child);
  1124. });
  1125. }
  1126. if (node.nodeType === NODE_TYPES.WORKSPACE) {
  1127. node.childNodes.forEach((child) => {
  1128. this.processNode(child);
  1129. });
  1130. }
  1131. let params = {};
  1132. if (node.nodeType === NODE_TYPES.MONITOR || node.nodeType === NODE_TYPES.CON) {
  1133. // The workarea from Meta.Window's assigned monitor
  1134. // is important so it computes to `remove` the panel size
  1135. // really well. However, this type of workarea would only
  1136. // appear if there is window present on the monitor.
  1137. if (node.childNodes.length === 0) {
  1138. return;
  1139. }
  1140. // If monitor, get the workarea
  1141. if (node.nodeType === NODE_TYPES.MONITOR) {
  1142. let monitorIndex = Utils.monitorIndex(node.nodeValue);
  1143. let monitorArea = global.display
  1144. .get_workspace_manager()
  1145. .get_active_workspace()
  1146. .get_work_area_for_monitor(monitorIndex);
  1147. if (!monitorArea) return; // there is no visible child window
  1148. node.rect = monitorArea;
  1149. node.rect = this.processGap(node);
  1150. }
  1151. let tiledChildren = this.getTiledChildren(node.childNodes);
  1152. let sizes = this.computeSizes(node, tiledChildren);
  1153. params.sizes = sizes;
  1154. let showTabs = this.settings.get_boolean("showtab-decoration-enabled");
  1155. params.stackedHeight = showTabs ? this.defaultStackHeight * Utils.dpi() : 0;
  1156. params.tiledChildren = tiledChildren;
  1157. let decoration = node.decoration;
  1158. if (decoration) {
  1159. let decoChildren = decoration.get_children();
  1160. decoChildren.forEach((decoChild) => {
  1161. decoration.remove_child(decoChild);
  1162. });
  1163. }
  1164. tiledChildren.forEach((child, index) => {
  1165. // A monitor can contain a window or container child
  1166. if (node.layout === LAYOUT_TYPES.HSPLIT || node.layout === LAYOUT_TYPES.VSPLIT) {
  1167. this.processSplit(node, child, params, index);
  1168. } else if (node.layout === LAYOUT_TYPES.STACKED) {
  1169. this.processStacked(node, child, params, index);
  1170. } else if (node.layout === LAYOUT_TYPES.TABBED) {
  1171. this.processTabbed(node, child, params, index);
  1172. }
  1173. this.processNode(child);
  1174. });
  1175. }
  1176. if (node.isWindow()) {
  1177. if (!node.rect) node.rect = node.nodeValue.get_work_area_current_monitor();
  1178. node.renderRect = this.processGap(node);
  1179. }
  1180. }
  1181. /**
  1182. * Forge processes both non-Window and Window gaps
  1183. */
  1184. processGap(node) {
  1185. let nodeWidth = node.rect.width;
  1186. let nodeHeight = node.rect.height;
  1187. let nodeX = node.rect.x;
  1188. let nodeY = node.rect.y;
  1189. let gap = this.extWm.calculateGaps(node);
  1190. if (nodeWidth > gap * 2 && nodeHeight > gap * 2) {
  1191. nodeX += gap;
  1192. nodeY += gap;
  1193. // TODO - detect inbetween windows and adjust accordingly
  1194. // Also adjust depending on display scaling
  1195. nodeWidth -= gap * 2;
  1196. nodeHeight -= gap * 2;
  1197. }
  1198. return { x: nodeX, y: nodeY, width: nodeWidth, height: nodeHeight };
  1199. }
  1200. processSplit(node, child, params, index) {
  1201. let layout = node.layout;
  1202. let nodeRect = node.rect;
  1203. let nodeWidth;
  1204. let nodeHeight;
  1205. let nodeX;
  1206. let nodeY;
  1207. if (layout === LAYOUT_TYPES.HSPLIT) {
  1208. // Divide the parent container's width
  1209. // depending on number of children. And use this
  1210. // to setup each child window's width.
  1211. nodeWidth = params.sizes[index];
  1212. nodeHeight = nodeRect.height;
  1213. nodeX = nodeRect.x;
  1214. if (index != 0) {
  1215. let i = 1;
  1216. while (i <= index) {
  1217. nodeX += params.sizes[i - 1];
  1218. i++;
  1219. }
  1220. }
  1221. nodeY = nodeRect.y;
  1222. } else if (layout === LAYOUT_TYPES.VSPLIT) {
  1223. // split vertically
  1224. // Conversely for vertical split, divide the parent container's height
  1225. // depending on number of children. And use this
  1226. // to setup each child window's height.
  1227. nodeWidth = nodeRect.width;
  1228. nodeHeight = params.sizes[index];
  1229. nodeX = nodeRect.x;
  1230. nodeY = nodeRect.y;
  1231. if (index != 0) {
  1232. let i = 1;
  1233. while (i <= index) {
  1234. nodeY += params.sizes[i - 1];
  1235. i++;
  1236. }
  1237. }
  1238. }
  1239. child.rect = {
  1240. x: nodeX,
  1241. y: nodeY,
  1242. width: nodeWidth,
  1243. height: nodeHeight,
  1244. };
  1245. }
  1246. /**
  1247. * Process the child node here for the dimensions of the child stack/window,
  1248. * It will be moved to the Node class in the future as Node.render()
  1249. *
  1250. */
  1251. processStacked(node, child, params, index) {
  1252. let layout = node.layout;
  1253. let nodeWidth = node.rect.width;
  1254. let nodeHeight = node.rect.height;
  1255. let nodeX = node.rect.x;
  1256. let nodeY = node.rect.y;
  1257. let stackHeight = this.defaultStackHeight;
  1258. if (layout === LAYOUT_TYPES.STACKED) {
  1259. if (node.childNodes.length > 1) {
  1260. nodeY += stackHeight * index;
  1261. nodeHeight -= stackHeight * index;
  1262. }
  1263. child.rect = {
  1264. x: nodeX,
  1265. y: nodeY,
  1266. width: nodeWidth,
  1267. height: nodeHeight,
  1268. };
  1269. }
  1270. }
  1271. /**
  1272. * Process the child node here for the dimensions of the child tab/window,
  1273. * It will be moved to the Node class in the future as Node.render()
  1274. *
  1275. */
  1276. processTabbed(node, child, params, _index) {
  1277. let layout = node.layout;
  1278. let nodeRect = node.rect;
  1279. let nodeWidth;
  1280. let nodeHeight;
  1281. let nodeX;
  1282. let nodeY;
  1283. if (layout === LAYOUT_TYPES.TABBED) {
  1284. nodeWidth = nodeRect.width;
  1285. nodeX = nodeRect.x;
  1286. nodeY = nodeRect.y;
  1287. nodeHeight = nodeRect.height;
  1288. let alwaysShowDecorationTab = true;
  1289. if (node.childNodes.length > 1 || alwaysShowDecorationTab) {
  1290. nodeY = nodeRect.y + params.stackedHeight;
  1291. nodeHeight = nodeRect.height - params.stackedHeight;
  1292. if (node.decoration && child.isWindow()) {
  1293. let gap = this.extWm.calculateGaps(node);
  1294. let renderRect = this.processGap(node);
  1295. let borderWidth = child.actor.border.get_theme_node().get_border_width(St.Side.TOP);
  1296. // Make adjustments to the gaps
  1297. let adjust = 4 * Utils.dpi();
  1298. let adjustWidth = renderRect.width + (borderWidth * 2 + gap) / adjust;
  1299. let adjustX = renderRect.x - (gap + borderWidth * 2) / (adjust * 2);
  1300. let adjustY = renderRect.y - adjust;
  1301. if (gap === 0) {
  1302. adjustY = renderRect.y;
  1303. nodeY = renderRect.y + params.stackedHeight + adjust / 4;
  1304. }
  1305. let decoration = node.decoration;
  1306. if (decoration !== null && decoration !== undefined) {
  1307. decoration.set_size(adjustWidth, params.stackedHeight);
  1308. decoration.set_position(adjustX, adjustY);
  1309. if (params.tiledChildren.length > 0 && params.stackedHeight !== 0) {
  1310. decoration.show();
  1311. } else {
  1312. decoration.hide();
  1313. }
  1314. if (!decoration.contains(child.tab)) decoration.add(child.tab);
  1315. }
  1316. child.render();
  1317. }
  1318. }
  1319. child.rect = {
  1320. x: nodeX,
  1321. y: nodeY,
  1322. width: nodeWidth,
  1323. height: nodeHeight,
  1324. };
  1325. }
  1326. }
  1327. computeSizes(node, childItems) {
  1328. let sizes = [];
  1329. let orientation = Utils.orientationFromLayout(node.layout);
  1330. let totalSize =
  1331. orientation === ORIENTATION_TYPES.HORIZONTAL ? node.rect.width : node.rect.height;
  1332. let grabTiled = node.getNodeByMode(Window.WINDOW_MODES.GRAB_TILE).length > 0;
  1333. childItems.forEach((childNode, index) => {
  1334. let percent =
  1335. childNode.percent && childNode.percent > 0.0 && !grabTiled
  1336. ? childNode.percent
  1337. : 1.0 / childItems.length;
  1338. sizes[index] = Math.floor(percent * totalSize);
  1339. });
  1340. // TODO - make sure the totalSize = the sizes total
  1341. return sizes;
  1342. }
  1343. findFirstNodeWindowFrom(node) {
  1344. let results = node.getNodeByType(NODE_TYPES.WINDOW);
  1345. if (results.length > 0) {
  1346. return results[0];
  1347. }
  1348. return null;
  1349. }
  1350. resetSiblingPercent(parentNode) {
  1351. if (!parentNode) return;
  1352. let children = parentNode.childNodes;
  1353. children.forEach((n) => {
  1354. n.percent = 0.0;
  1355. });
  1356. }
  1357. debugTree() {
  1358. this.debugNode(this);
  1359. }
  1360. debugNode(node) {
  1361. let spacing = "";
  1362. let dashes = "-->";
  1363. let level = node.level;
  1364. for (let i = 0; i < level; i++) {
  1365. let parentSpacing = i === 0 ? " " : "|";
  1366. spacing += `${parentSpacing} `;
  1367. }
  1368. let rootSpacing = level === 0 ? "#" : "*";
  1369. let attributes = "";
  1370. if (node.isWindow()) {
  1371. let metaWindow = node.nodeValue;
  1372. attributes += `class:'${metaWindow.get_wm_class()}',title:'${
  1373. metaWindow.title
  1374. }',string:'${metaWindow}'${metaWindow === this.extWm.focusMetaWindow ? " FOCUS" : ""}`;
  1375. } else if (node.isCon() || node.isMonitor() || node.isWorkspace()) {
  1376. attributes += `${node.nodeValue}`;
  1377. if (node.isCon() || node.isMonitor()) {
  1378. attributes += `,layout:${node.layout}`;
  1379. }
  1380. }
  1381. if (node.rect) {
  1382. attributes += `,rect:${node.rect.width}x${node.rect.height}+${node.rect.x}+${node.rect.y}`;
  1383. }
  1384. if (level !== 0) Logger.debug(`${spacing}|`);
  1385. Logger.debug(
  1386. `${spacing}${rootSpacing}${dashes} ${node.nodeType}#${
  1387. node.index !== null ? node.index : "-"
  1388. } @${attributes}`
  1389. );
  1390. node.childNodes.forEach((child) => {
  1391. this.debugNode(child);
  1392. });
  1393. }
  1394. findParent(childNode, parentNodeType) {
  1395. let parents = this.getNodeByType(parentNodeType);
  1396. // Only get the first parent
  1397. return parents.filter((p) => p.contains(childNode))[0];
  1398. }
  1399. }