window.js 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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 GLib from "gi://GLib";
  20. import Clutter from "gi://Clutter";
  21. import GObject from "gi://GObject";
  22. import Meta from "gi://Meta";
  23. import St from "gi://St";
  24. // Gnome Shell imports
  25. import { gettext as _ } from "resource:///org/gnome/shell/extensions/extension.js";
  26. import * as Main from "resource:///org/gnome/shell/ui/main.js";
  27. import { PACKAGE_VERSION } from "resource:///org/gnome/shell/misc/config.js";
  28. // Shared state
  29. import { Logger } from "../shared/logger.js";
  30. // App imports
  31. import * as Utils from "./utils.js";
  32. import { Keybindings } from "./keybindings.js";
  33. import {
  34. Tree,
  35. Queue,
  36. Node,
  37. POSITION,
  38. LAYOUT_TYPES,
  39. ORIENTATION_TYPES,
  40. NODE_TYPES,
  41. } from "./tree.js";
  42. import { production } from "../shared/settings.js";
  43. /** @typedef {import('../../extension.js').default} ForgeExtension */
  44. export const WINDOW_MODES = Utils.createEnum(["FLOAT", "TILE", "GRAB_TILE", "DEFAULT"]);
  45. // Simplify the grab modes
  46. export const GRAB_TYPES = Utils.createEnum(["RESIZING", "MOVING", "UNKNOWN"]);
  47. export class WindowManager extends GObject.Object {
  48. static {
  49. GObject.registerClass(this);
  50. }
  51. /** @type {ForgeExtension} */
  52. ext;
  53. /** @param {ForgeExtension} ext */
  54. constructor(ext) {
  55. super();
  56. this.ext = ext;
  57. this.prefsTitle = `Forge ${_("Settings")} - ${
  58. !production ? "DEV" : `${PACKAGE_VERSION}-${ext.metadata.version}`
  59. }`;
  60. this.windowProps = this.ext.configMgr.windowProps;
  61. this._kbd = this.ext.keybindings;
  62. this._tree = new Tree(this);
  63. this.eventQueue = new Queue();
  64. this.theme = this.ext.theme;
  65. Logger.info("forge initialized");
  66. }
  67. addFloatOverride(metaWindow, byClass = true) {
  68. let overrides = this.windowProps.overrides;
  69. let wmTitle = metaWindow.get_title();
  70. let wmClass = metaWindow.get_wm_class();
  71. for (let override in overrides) {
  72. if (!byClass) {
  73. if (override.wmClass === wmClass && override.wmTitle === wmTitle) return;
  74. } else {
  75. if (override.wmClass === wmClass && !override.wmTitle) return;
  76. }
  77. }
  78. overrides.push({
  79. wmClass: wmClass,
  80. wmTitle: !byClass ? wmTitle : undefined,
  81. mode: "float",
  82. });
  83. this.windowProps.overrides = overrides;
  84. this.ext.configMgr.windowProps = this.windowProps;
  85. }
  86. removeFloatOverride(metaWindow, byClass = true) {
  87. let overrides = this.windowProps.overrides;
  88. let wmTitle = metaWindow.get_title();
  89. let wmClass = metaWindow.get_wm_class();
  90. if (byClass) {
  91. // remove purely wmClass - by checking also if override title exists
  92. overrides = overrides.filter(
  93. (override) => !(override.wmClass === wmClass) && !override.wmTitle
  94. );
  95. } else {
  96. overrides = overrides.filter(
  97. (override) => !(override.wmClass === wmClass && override.wmTitle === wmTitle)
  98. );
  99. }
  100. this.windowProps.overrides = overrides;
  101. this.ext.configMgr.windowProps = this.windowProps;
  102. }
  103. toggleFloatingMode(action, metaWindow) {
  104. let nodeWindow = this.findNodeWindow(metaWindow);
  105. if (!nodeWindow || !(action || action.mode)) return;
  106. if (nodeWindow.nodeType !== NODE_TYPES.WINDOW) return;
  107. let floatToggle = action.name === "FloatToggle";
  108. let floatClassToggle = action.name === "FloatClassToggle";
  109. let floatingExempt = this.isFloatingExempt(metaWindow);
  110. if (floatingExempt) {
  111. if (floatClassToggle) {
  112. this.removeFloatOverride(metaWindow);
  113. } else if (floatToggle) {
  114. this.removeFloatOverride(metaWindow, false);
  115. }
  116. nodeWindow.mode = WINDOW_MODES.TILE;
  117. if (!this.isActiveWindowWorkspaceTiled(metaWindow)) {
  118. nodeWindow.mode = WINDOW_MODES.FLOAT;
  119. }
  120. } else {
  121. if (floatClassToggle) {
  122. this.addFloatOverride(metaWindow);
  123. } else if (floatToggle) {
  124. this.addFloatOverride(metaWindow, false);
  125. }
  126. nodeWindow.mode = WINDOW_MODES.FLOAT;
  127. }
  128. }
  129. queueEvent(eventObj, interval = 220) {
  130. this.eventQueue.enqueue(eventObj);
  131. if (!this._queueSourceId) {
  132. this._queueSourceId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, interval, () => {
  133. const currEventObj = this.eventQueue.dequeue();
  134. if (currEventObj) {
  135. currEventObj.callback();
  136. }
  137. const result = this.eventQueue.length !== 0;
  138. if (!result) {
  139. this._queueSourceId = 0;
  140. }
  141. return result;
  142. });
  143. }
  144. }
  145. /**
  146. * This is the central place to bind all the non-window signals.
  147. */
  148. _bindSignals() {
  149. if (this._signalsBound) return;
  150. const display = global.display;
  151. const shellWm = global.window_manager;
  152. this._displaySignals = [
  153. display.connect("window-created", this.trackWindow.bind(this)),
  154. display.connect("grab-op-begin", this._handleGrabOpBegin.bind(this)),
  155. display.connect("window-entered-monitor", (_, monitor, metaWindow) => {
  156. this.updateMetaWorkspaceMonitor("window-entered-monitor", monitor, metaWindow);
  157. this.trackCurrentMonWs();
  158. }),
  159. display.connect("grab-op-end", this._handleGrabOpEnd.bind(this)),
  160. display.connect("showing-desktop-changed", () => {
  161. this.hideWindowBorders();
  162. this.updateDecorationLayout();
  163. }),
  164. display.connect("in-fullscreen-changed", () => {
  165. this.renderTree("full-screen-changed");
  166. }),
  167. display.connect("workareas-changed", (_display) => {
  168. if (global.display.get_n_monitors() == 0) {
  169. Logger.debug(`workareas-changed: no monitors, ignoring signal`);
  170. return;
  171. }
  172. if (this.tree.getNodeByType("WINDOW").length > 0) {
  173. let workspaceReload = this.workspaceAdded || this.workspaceRemoved;
  174. if (workspaceReload) {
  175. this.trackCurrentWindows();
  176. this.workspaceRemoved = false;
  177. this.workspaceAdded = false;
  178. } else {
  179. this.renderTree("workareas-changed");
  180. }
  181. }
  182. }),
  183. ];
  184. this._windowManagerSignals = [
  185. shellWm.connect("minimize", () => {
  186. this.hideWindowBorders();
  187. let focusNodeWindow = this.tree.findNode(this.focusMetaWindow);
  188. if (focusNodeWindow) {
  189. if (this.tree.getTiledChildren(focusNodeWindow.parentNode.childNodes).length === 0) {
  190. this.tree.resetSiblingPercent(focusNodeWindow.parentNode.parentNode);
  191. }
  192. this.tree.resetSiblingPercent(focusNodeWindow.parentNode);
  193. }
  194. let prevFrozen = this._freezeRender;
  195. if (prevFrozen) this.unfreezeRender();
  196. this.renderTree("minimize");
  197. if (prevFrozen) this.freezeRender();
  198. }),
  199. shellWm.connect("unminimize", () => {
  200. let focusNodeWindow = this.tree.findNode(this.focusMetaWindow);
  201. if (focusNodeWindow) {
  202. this.tree.resetSiblingPercent(focusNodeWindow.parentNode);
  203. }
  204. let prevFrozen = this._freezeRender;
  205. if (prevFrozen) this.unfreezeRender();
  206. this.renderTree("unminimize");
  207. if (prevFrozen) this.freezeRender();
  208. }),
  209. shellWm.connect("show-tile-preview", (_, _metaWindow, _rect, _num) => {
  210. // Empty
  211. }),
  212. ];
  213. const globalWsm = global.workspace_manager;
  214. this._workspaceManagerSignals = [
  215. globalWsm.connect("showing-desktop-changed", () => {
  216. this.hideWindowBorders();
  217. this.updateDecorationLayout();
  218. }),
  219. globalWsm.connect("workspace-added", (_, wsIndex) => {
  220. this.tree.addWorkspace(wsIndex);
  221. this.trackCurrentMonWs();
  222. this.workspaceAdded = true;
  223. this.renderTree("workspace-added");
  224. }),
  225. globalWsm.connect("workspace-removed", (_, wsIndex) => {
  226. this.tree.removeWorkspace(wsIndex);
  227. this.trackCurrentMonWs();
  228. this.workspaceRemoved = true;
  229. this.updateDecorationLayout();
  230. this.renderTree("workspace-removed");
  231. }),
  232. globalWsm.connect("active-workspace-changed", () => {
  233. this.hideWindowBorders();
  234. this.trackCurrentMonWs();
  235. this.updateDecorationLayout();
  236. this.renderTree("active-workspace-changed");
  237. }),
  238. ];
  239. let numberOfWorkspaces = globalWsm.get_n_workspaces();
  240. for (let i = 0; i < numberOfWorkspaces; i++) {
  241. let workspace = globalWsm.get_workspace_by_index(i);
  242. this.bindWorkspaceSignals(workspace);
  243. }
  244. let settings = this.ext.settings;
  245. settings.connect("changed", (_, settingName) => {
  246. switch (settingName) {
  247. case "focus-border-toggle":
  248. this.renderTree(settingName);
  249. break;
  250. case "tiling-mode-enabled":
  251. this.renderTree(settingName);
  252. break;
  253. case "window-gap-size-increment":
  254. case "window-gap-size":
  255. case "window-gap-hidden-on-single":
  256. case "workspace-skip-tile":
  257. this.renderTree(settingName, true);
  258. break;
  259. case "stacked-tiling-mode-enabled":
  260. if (!settings.get_boolean(settingName)) {
  261. let stackedNodes = this.tree.getNodeByLayout(LAYOUT_TYPES.STACKED);
  262. stackedNodes.forEach((node) => {
  263. node.prevLayout = node.layout;
  264. node.layout = this.determineSplitLayout();
  265. });
  266. } else {
  267. let hSplitNodes = this.tree.getNodeByLayout(LAYOUT_TYPES.HSPLIT);
  268. let vSplitNodes = this.tree.getNodeByLayout(LAYOUT_TYPES.VSPLIT);
  269. Array.prototype.push.apply(hSplitNodes, vSplitNodes);
  270. hSplitNodes.forEach((node) => {
  271. if (node.prevLayout && node.prevLayout === LAYOUT_TYPES.STACKED) {
  272. node.layout = LAYOUT_TYPES.STACKED;
  273. }
  274. });
  275. }
  276. this.renderTree(settingName);
  277. break;
  278. case "tabbed-tiling-mode-enabled":
  279. if (!settings.get_boolean(settingName)) {
  280. let tabbedNodes = this.tree.getNodeByLayout(LAYOUT_TYPES.TABBED);
  281. tabbedNodes.forEach((node) => {
  282. node.prevLayout = node.layout;
  283. node.layout = this.determineSplitLayout();
  284. });
  285. } else {
  286. let hSplitNodes = this.tree.getNodeByLayout(LAYOUT_TYPES.HSPLIT);
  287. let vSplitNodes = this.tree.getNodeByLayout(LAYOUT_TYPES.VSPLIT);
  288. Array.prototype.push.apply(hSplitNodes, vSplitNodes);
  289. hSplitNodes.forEach((node) => {
  290. if (node.prevLayout && node.prevLayout === LAYOUT_TYPES.TABBED) {
  291. node.layout = LAYOUT_TYPES.TABBED;
  292. }
  293. });
  294. }
  295. this.renderTree(settingName);
  296. break;
  297. case "css-updated":
  298. this.theme.reloadStylesheet();
  299. break;
  300. case "float-always-on-top-enabled":
  301. if (!settings.get_boolean(settingName)) {
  302. this.cleanupAlwaysFloat();
  303. } else {
  304. this.restoreAlwaysFloat();
  305. }
  306. break;
  307. default:
  308. break;
  309. }
  310. });
  311. this._overviewSignals = [
  312. Main.overview.connect("hiding", () => {
  313. this.fromOverview = true;
  314. const eventObj = {
  315. name: "focus-after-overview",
  316. callback: () => {
  317. const focusNodeWindow = this.tree.findNode(this.focusMetaWindow);
  318. this.updateStackedFocus(focusNodeWindow);
  319. this.updateTabbedFocus(focusNodeWindow);
  320. },
  321. };
  322. this.queueEvent(eventObj);
  323. }),
  324. Main.overview.connect("showing", () => {
  325. this.toOverview = true;
  326. }),
  327. ];
  328. this._signalsBound = true;
  329. }
  330. cleanupAlwaysFloat() {
  331. // remove the setting for each node window
  332. this.allNodeWindows.forEach((w) => {
  333. if (w.mode === WINDOW_MODES.FLOAT) {
  334. w.nodeValue.is_above() && w.nodeValue.unmake_above();
  335. }
  336. });
  337. }
  338. restoreAlwaysFloat() {
  339. this.allNodeWindows.forEach((w) => {
  340. if (w.mode === WINDOW_MODES.FLOAT) {
  341. !w.nodeValue.is_above() && w.nodeValue.make_above();
  342. }
  343. });
  344. }
  345. trackCurrentMonWs() {
  346. let metaWindow = this.focusMetaWindow;
  347. if (!metaWindow) return;
  348. const currentMonitor = global.display.get_current_monitor();
  349. const currentWorkspace = global.display.get_workspace_manager().get_active_workspace_index();
  350. let currentMonWs = `mo${currentMonitor}ws${currentWorkspace}`;
  351. let activeMetaMonWs = `mo${metaWindow.get_monitor()}ws${metaWindow.get_workspace().index()}`;
  352. let currentWsNode = this.tree.findNode(`ws${currentWorkspace}`);
  353. if (!currentWsNode) {
  354. return;
  355. }
  356. // Search for all the valid windows on the workspace
  357. const monWindows = currentWsNode.getNodeByType(NODE_TYPES.WORKSPACE).flatMap((ws) => {
  358. return ws
  359. .getNodeByType(NODE_TYPES.WINDOW)
  360. .filter(
  361. (w) =>
  362. !w.nodeValue.minimized &&
  363. w.isTile() &&
  364. w.nodeValue !== metaWindow &&
  365. // The searched window should be on the same monitor workspace
  366. // This ensures that Forge already updated the workspace node tree:
  367. currentMonWs === activeMetaMonWs
  368. )
  369. .map((w) => w.nodeValue);
  370. });
  371. this.sortedWindows = global.display.sort_windows_by_stacking(monWindows).reverse();
  372. }
  373. // TODO move this to workspace.js
  374. bindWorkspaceSignals(metaWorkspace) {
  375. if (metaWorkspace) {
  376. if (!metaWorkspace.workspaceSignals) {
  377. let workspaceSignals = [
  378. metaWorkspace.connect("window-added", (_, metaWindow) => {
  379. if (!this._wsWindowAddSrcId) {
  380. this._wsWindowAddSrcId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 200, () => {
  381. this.updateMetaWorkspaceMonitor(
  382. "window-added",
  383. metaWindow.get_monitor(),
  384. metaWindow
  385. );
  386. this._wsWindowAddSrcId = 0;
  387. return false;
  388. });
  389. }
  390. }),
  391. ];
  392. metaWorkspace.workspaceSignals = workspaceSignals;
  393. }
  394. }
  395. }
  396. // TODO move this in command.js
  397. command(action) {
  398. let focusWindow = this.focusMetaWindow;
  399. // Do not check if the node window is null, some of the commands do not need the focus window
  400. let focusNodeWindow = this.findNodeWindow(focusWindow);
  401. let currentLayout;
  402. switch (action.name) {
  403. case "FloatToggle":
  404. case "FloatClassToggle":
  405. this.toggleFloatingMode(action, focusWindow);
  406. const rectRequest = {
  407. x: action.x,
  408. y: action.y,
  409. width: action.width,
  410. height: action.height,
  411. };
  412. let moveRect = {
  413. x: Utils.resolveX(rectRequest, focusWindow),
  414. y: Utils.resolveY(rectRequest, focusWindow),
  415. width: Utils.resolveWidth(rectRequest, focusWindow),
  416. height: Utils.resolveHeight(rectRequest, focusWindow),
  417. };
  418. this.move(focusWindow, moveRect);
  419. let existParent = focusNodeWindow.parentNode;
  420. if (this.tree.getTiledChildren(existParent.childNodes).length <= 1) {
  421. existParent.percent = 0.0;
  422. this.tree.resetSiblingPercent(existParent.parentNode);
  423. }
  424. this.tree.resetSiblingPercent(existParent);
  425. this.renderTree("float-toggle", true);
  426. break;
  427. case "Move":
  428. this.unfreezeRender();
  429. let moveDirection = Utils.resolveDirection(action.direction);
  430. let prev = focusNodeWindow;
  431. let moved = this.tree.move(focusNodeWindow, moveDirection);
  432. if (!focusNodeWindow) {
  433. focusNodeWindow = this.findNodeWindow(this.focusMetaWindow);
  434. }
  435. this.queueEvent({
  436. name: "move",
  437. callback: () => {
  438. if (this.eventQueue.length <= 0) {
  439. this.unfreezeRender();
  440. if (focusNodeWindow.parentNode.layout === LAYOUT_TYPES.STACKED) {
  441. focusNodeWindow.parentNode.appendChild(focusNodeWindow);
  442. focusNodeWindow.nodeValue.raise();
  443. focusNodeWindow.nodeValue.activate(global.display.get_current_time());
  444. this.renderTree("move-stacked-queue");
  445. }
  446. if (focusNodeWindow.parentNode.layout === LAYOUT_TYPES.TABBED) {
  447. focusNodeWindow.nodeValue.raise();
  448. focusNodeWindow.nodeValue.activate(global.display.get_current_time());
  449. if (prev) prev.parentNode.lastTabFocus = prev.nodeValue;
  450. this.renderTree("move-tabbed-queue");
  451. }
  452. }
  453. },
  454. });
  455. if (moved) {
  456. if (prev) prev.parentNode.lastTabFocus = prev.nodeValue;
  457. this.renderTree("move-window");
  458. }
  459. break;
  460. case "Focus":
  461. let focusDirection = Utils.resolveDirection(action.direction);
  462. focusNodeWindow = this.tree.focus(focusNodeWindow, focusDirection);
  463. if (!focusNodeWindow) {
  464. focusNodeWindow = this.findNodeWindow(this.focusMetaWindow);
  465. }
  466. break;
  467. case "Swap":
  468. if (!focusNodeWindow) return;
  469. this.unfreezeRender();
  470. let swapDirection = Utils.resolveDirection(action.direction);
  471. this.tree.swap(focusNodeWindow, swapDirection);
  472. focusNodeWindow.nodeValue.raise();
  473. this.updateTabbedFocus(focusNodeWindow);
  474. this.updateStackedFocus(focusNodeWindow);
  475. this.renderTree("swap", true);
  476. break;
  477. case "Split":
  478. if (!focusNodeWindow) return;
  479. currentLayout = focusNodeWindow.parentNode.layout;
  480. if (currentLayout === LAYOUT_TYPES.STACKED || currentLayout === LAYOUT_TYPES.TABBED) {
  481. return;
  482. }
  483. let orientation = action.orientation
  484. ? action.orientation.toUpperCase()
  485. : ORIENTATION_TYPES.NONE;
  486. this.tree.split(focusNodeWindow, orientation);
  487. this.renderTree("split");
  488. break;
  489. case "LayoutToggle":
  490. if (!focusNodeWindow) return;
  491. currentLayout = focusNodeWindow.parentNode.layout;
  492. if (currentLayout === LAYOUT_TYPES.HSPLIT) {
  493. focusNodeWindow.parentNode.layout = LAYOUT_TYPES.VSPLIT;
  494. } else if (currentLayout === LAYOUT_TYPES.VSPLIT) {
  495. focusNodeWindow.parentNode.layout = LAYOUT_TYPES.HSPLIT;
  496. }
  497. this.tree.attachNode = focusNodeWindow.parentNode;
  498. this.renderTree("layout-split-toggle");
  499. break;
  500. case "FocusBorderToggle":
  501. let focusBorderEnabled = this.ext.settings.get_boolean("focus-border-toggle");
  502. this.ext.settings.set_boolean("focus-border-toggle", !focusBorderEnabled);
  503. break;
  504. case "TilingModeToggle":
  505. // FIXME, not sure if this toggle is still needed from a use case
  506. // perspective, since Extension.disable also should do the same thing.
  507. let tilingModeEnabled = this.ext.settings.get_boolean("tiling-mode-enabled");
  508. this.ext.settings.set_boolean("tiling-mode-enabled", !tilingModeEnabled);
  509. if (tilingModeEnabled) {
  510. this.floatAllWindows();
  511. } else {
  512. this.unfloatAllWindows();
  513. }
  514. this.renderTree(`tiling-mode-toggle ${!tilingModeEnabled}`);
  515. break;
  516. case "GapSize":
  517. let gapIncrement = this.ext.settings.get_uint("window-gap-size-increment");
  518. let amount = action.amount;
  519. gapIncrement = gapIncrement + amount;
  520. if (gapIncrement < 0) gapIncrement = 0;
  521. if (gapIncrement > 8) gapIncrement = 8;
  522. this.ext.settings.set_uint("window-gap-size-increment", gapIncrement);
  523. break;
  524. case "WorkspaceActiveTileToggle":
  525. let activeWorkspace = global.workspace_manager.get_active_workspace_index();
  526. let skippedWorkspaces = this.ext.settings.get_string("workspace-skip-tile");
  527. let workspaceSkipped = false;
  528. let skippedArr = [];
  529. if (skippedWorkspaces.length === 0) {
  530. skippedArr.push(`${activeWorkspace}`);
  531. this.floatWorkspace(activeWorkspace);
  532. } else {
  533. skippedArr = skippedWorkspaces.split(",");
  534. for (let i = 0; i < skippedArr.length; i++) {
  535. if (`${skippedArr[i]}` === `${activeWorkspace}`) {
  536. workspaceSkipped = true;
  537. break;
  538. }
  539. }
  540. if (workspaceSkipped) {
  541. // tile this workspace
  542. let indexWs = skippedArr.indexOf(`${activeWorkspace}`);
  543. skippedArr.splice(indexWs, 1);
  544. this.unfloatWorkspace(activeWorkspace);
  545. } else {
  546. // skip tiling workspace
  547. skippedArr.push(`${activeWorkspace}`);
  548. this.floatWorkspace(activeWorkspace);
  549. }
  550. }
  551. this.ext.settings.set_string("workspace-skip-tile", skippedArr.toString());
  552. this.renderTree("workspace-toggle");
  553. break;
  554. case "LayoutStackedToggle":
  555. if (!focusNodeWindow) return;
  556. if (!this.ext.settings.get_boolean("stacked-tiling-mode-enabled")) return;
  557. if (focusNodeWindow.parentNode.isMonitor()) {
  558. this.tree.split(focusNodeWindow, ORIENTATION_TYPES.HORIZONTAL, true);
  559. }
  560. currentLayout = focusNodeWindow.parentNode.layout;
  561. if (currentLayout === LAYOUT_TYPES.STACKED) {
  562. focusNodeWindow.parentNode.layout = this.determineSplitLayout();
  563. this.tree.resetSiblingPercent(focusNodeWindow.parentNode);
  564. } else {
  565. if (currentLayout === LAYOUT_TYPES.TABBED) {
  566. focusNodeWindow.parentNode.lastTabFocus = null;
  567. }
  568. focusNodeWindow.parentNode.layout = LAYOUT_TYPES.STACKED;
  569. let lastChild = focusNodeWindow.parentNode.lastChild;
  570. if (lastChild.nodeType === NODE_TYPES.WINDOW) {
  571. lastChild.nodeValue.activate(global.display.get_current_time());
  572. }
  573. }
  574. this.unfreezeRender();
  575. this.tree.attachNode = focusNodeWindow.parentNode;
  576. this.renderTree("layout-stacked-toggle");
  577. break;
  578. case "LayoutTabbedToggle":
  579. if (!focusNodeWindow) return;
  580. if (!this.ext.settings.get_boolean("tabbed-tiling-mode-enabled")) return;
  581. if (focusNodeWindow.parentNode.isMonitor()) {
  582. this.tree.split(focusNodeWindow, ORIENTATION_TYPES.HORIZONTAL, true);
  583. }
  584. currentLayout = focusNodeWindow.parentNode.layout;
  585. if (currentLayout === LAYOUT_TYPES.TABBED) {
  586. focusNodeWindow.parentNode.layout = this.determineSplitLayout();
  587. this.tree.resetSiblingPercent(focusNodeWindow.parentNode);
  588. focusNodeWindow.parentNode.lastTabFocus = null;
  589. } else {
  590. focusNodeWindow.parentNode.layout = LAYOUT_TYPES.TABBED;
  591. focusNodeWindow.parentNode.lastTabFocus = focusNodeWindow.nodeValue;
  592. }
  593. this.unfreezeRender();
  594. this.tree.attachNode = focusNodeWindow.parentNode;
  595. this.renderTree("layout-tabbed-toggle");
  596. break;
  597. case "CancelOperation":
  598. if (focusNodeWindow.mode === WINDOW_MODES.GRAB_TILE) {
  599. this.cancelGrab = true;
  600. }
  601. break;
  602. case "PrefsOpen":
  603. let existWindow = Utils.findWindowWith(this.prefsTitle);
  604. if (existWindow && existWindow.get_workspace()) {
  605. existWindow
  606. .get_workspace()
  607. .activate_with_focus(existWindow, global.display.get_current_time());
  608. this.moveCenter(existWindow);
  609. } else {
  610. this.ext.openPreferences();
  611. }
  612. break;
  613. case "WindowSwapLastActive":
  614. if (focusNodeWindow) {
  615. let lastActiveWindow = global.display.get_tab_next(
  616. Meta.TabList.NORMAL,
  617. global.display.get_workspace_manager().get_active_workspace(),
  618. focusNodeWindow.nodeValue,
  619. false
  620. );
  621. let lastActiveNodeWindow = this.tree.findNode(lastActiveWindow);
  622. this.tree.swapPairs(lastActiveNodeWindow, focusNodeWindow);
  623. this.renderTree("swap-last-active");
  624. }
  625. break;
  626. case "SnapLayoutMove":
  627. if (focusNodeWindow) {
  628. let workareaRect = focusNodeWindow.nodeValue.get_work_area_current_monitor();
  629. let layoutAmount = action.amount;
  630. let layoutDirection = action.direction.toUpperCase();
  631. let layout = {};
  632. let processGap = false;
  633. switch (layoutDirection) {
  634. case "LEFT":
  635. layout.width = layoutAmount * workareaRect.width;
  636. layout.height = workareaRect.height;
  637. layout.x = workareaRect.x;
  638. layout.y = workareaRect.y;
  639. processGap = true;
  640. break;
  641. case "RIGHT":
  642. layout.width = layoutAmount * workareaRect.width;
  643. layout.height = workareaRect.height;
  644. layout.x = workareaRect.x + (workareaRect.width - layout.width);
  645. layout.y = workareaRect.y;
  646. processGap = true;
  647. break;
  648. case "CENTER":
  649. let metaRect = this.focusMetaWindow.get_frame_rect();
  650. layout.x = "center";
  651. layout.y = "center";
  652. layout = {
  653. x: Utils.resolveX(layout, this.focusMetaWindow),
  654. y: Utils.resolveY(layout, this.focusMetaWindow),
  655. width: metaRect.width,
  656. height: metaRect.height,
  657. };
  658. break;
  659. default:
  660. break;
  661. }
  662. focusNodeWindow.rect = layout;
  663. if (processGap) {
  664. focusNodeWindow.rect = this.tree.processGap(focusNodeWindow);
  665. }
  666. if (!focusNodeWindow.isFloat()) {
  667. this.addFloatOverride(focusNodeWindow.nodeValue, false);
  668. }
  669. this.move(focusNodeWindow.nodeValue, focusNodeWindow.rect);
  670. this.queueEvent({
  671. name: "snap-layout-move",
  672. callback: () => {
  673. this.renderTree("snap-layout-move");
  674. },
  675. });
  676. break;
  677. }
  678. case "ShowTabDecorationToggle":
  679. if (!focusNodeWindow) return;
  680. if (!this.ext.settings.get_boolean("tabbed-tiling-mode-enabled")) return;
  681. let showTabs = this.ext.settings.get_boolean("showtab-decoration-enabled");
  682. this.ext.settings.set_boolean("showtab-decoration-enabled", !showTabs);
  683. this.unfreezeRender();
  684. this.tree.attachNode = focusNodeWindow.parentNode;
  685. this.renderTree("showtab-decoration-enabled");
  686. break;
  687. case "WindowResizeRight":
  688. this.resize(Meta.GrabOp.KEYBOARD_RESIZING_E, action.amount);
  689. break;
  690. case "WindowResizeLeft":
  691. this.resize(Meta.GrabOp.KEYBOARD_RESIZING_W, action.amount);
  692. break;
  693. case "WindowResizeTop":
  694. this.resize(Meta.GrabOp.KEYBOARD_RESIZING_N, action.amount);
  695. break;
  696. case "WindowResizeBottom":
  697. this.resize(Meta.GrabOp.KEYBOARD_RESIZING_S, action.amount);
  698. break;
  699. default:
  700. break;
  701. }
  702. }
  703. resize(grabOp, amount) {
  704. let metaWindow = this.focusMetaWindow;
  705. let display = global.display;
  706. this._handleGrabOpBegin(display, metaWindow, grabOp);
  707. let rect = metaWindow.get_frame_rect();
  708. let direction = Utils.directionFromGrab(grabOp);
  709. switch (direction) {
  710. case Meta.MotionDirection.RIGHT:
  711. rect.width = rect.width + amount;
  712. break;
  713. case Meta.MotionDirection.LEFT:
  714. rect.width = rect.width + amount;
  715. rect.x = rect.x - amount;
  716. break;
  717. case Meta.MotionDirection.UP:
  718. rect.height = rect.height + amount;
  719. break;
  720. case Meta.MotionDirection.DOWN:
  721. rect.height = rect.height + amount;
  722. rect.y = rect.y - amount;
  723. break;
  724. }
  725. this.move(metaWindow, rect);
  726. this.queueEvent(
  727. {
  728. name: "manual-resize",
  729. callback: () => {
  730. if (this.eventQueue.length === 0) {
  731. this._handleGrabOpEnd(display, metaWindow, grabOp);
  732. }
  733. },
  734. },
  735. 50
  736. );
  737. }
  738. disable() {
  739. Utils._disableDecorations();
  740. this._removeSignals();
  741. this.disabled = true;
  742. Logger.debug(`extension:disable`);
  743. }
  744. enable() {
  745. this._bindSignals();
  746. this.reloadTree("enable");
  747. Logger.debug(`extension:enable`);
  748. }
  749. findNodeWindow(metaWindow) {
  750. return this.tree.findNode(metaWindow);
  751. }
  752. get focusMetaWindow() {
  753. return global.display.get_focus_window();
  754. }
  755. get tree() {
  756. if (!this._tree) {
  757. this._tree = new Tree(this);
  758. }
  759. return this._tree;
  760. }
  761. get kbd() {
  762. if (!this._kbd) {
  763. this._kbd = new Keybindings(this.ext);
  764. this.ext.keybindings = this._kbd;
  765. }
  766. return this._kbd;
  767. }
  768. get windowsActiveWorkspace() {
  769. let wsManager = global.workspace_manager;
  770. return global.display.get_tab_list(Meta.TabList.NORMAL_ALL, wsManager.get_active_workspace());
  771. }
  772. get windowsAllWorkspaces() {
  773. let wsManager = global.workspace_manager;
  774. let windowsAll = [];
  775. for (let i = 0; i < wsManager.get_n_workspaces(); i++) {
  776. Array.prototype.push.apply(
  777. windowsAll,
  778. global.display.get_tab_list(Meta.TabList.NORMAL_ALL, wsManager.get_workspace_by_index(i))
  779. );
  780. }
  781. windowsAll.sort((w1, w2) => {
  782. return w1.get_stable_sequence() - w2.get_stable_sequence();
  783. });
  784. return windowsAll;
  785. }
  786. getWindowsOnWorkspace(workspaceIndex) {
  787. const workspaceNode = this.tree.findNode(`ws${workspaceIndex}`);
  788. const workspaceWindows = workspaceNode.getNodeByType(NODE_TYPES.WINDOW);
  789. return workspaceWindows;
  790. }
  791. determineSplitLayout() {
  792. // if the monitor width is less than height, the monitor could be vertical orientation;
  793. let monitorRect = global.display.get_monitor_geometry(global.display.get_current_monitor());
  794. if (monitorRect.width < monitorRect.height) {
  795. return LAYOUT_TYPES.VSPLIT;
  796. }
  797. return LAYOUT_TYPES.HSPLIT;
  798. }
  799. floatWorkspace(workspaceIndex) {
  800. const workspaceWindows = this.getWindowsOnWorkspace(workspaceIndex);
  801. if (!workspaceWindows) return;
  802. workspaceWindows.forEach((w) => {
  803. w.float = true;
  804. });
  805. }
  806. unfloatWorkspace(workspaceIndex) {
  807. const workspaceWindows = this.getWindowsOnWorkspace(workspaceIndex);
  808. if (!workspaceWindows) return;
  809. workspaceWindows.forEach((w) => {
  810. w.tile = true;
  811. });
  812. }
  813. hideWindowBorders() {
  814. this.tree.nodeWindows.forEach((nodeWindow) => {
  815. let actor = nodeWindow.windowActor;
  816. if (actor) {
  817. if (actor.border) {
  818. actor.border.hide();
  819. }
  820. if (actor.splitBorder) {
  821. actor.splitBorder.hide();
  822. }
  823. }
  824. if (nodeWindow.parentNode.isTabbed()) {
  825. if (nodeWindow.tab) {
  826. // TODO: review the cleanup of the tab:St.Widget variable
  827. try {
  828. nodeWindow.tab.remove_style_class_name("window-tabbed-tab-active");
  829. } catch (e) {
  830. Logger.warn(e);
  831. }
  832. }
  833. }
  834. });
  835. }
  836. // Window movement API
  837. move(metaWindow, rect) {
  838. if (!metaWindow) return;
  839. if (metaWindow.grabbed) return;
  840. metaWindow.unmaximize(Meta.MaximizeFlags.HORIZONTAL);
  841. metaWindow.unmaximize(Meta.MaximizeFlags.VERTICAL);
  842. metaWindow.unmaximize(Meta.MaximizeFlags.BOTH);
  843. let windowActor = metaWindow.get_compositor_private();
  844. if (!windowActor) return;
  845. windowActor.remove_all_transitions();
  846. metaWindow.move_frame(true, rect.x, rect.y);
  847. metaWindow.move_resize_frame(true, rect.x, rect.y, rect.width, rect.height);
  848. }
  849. moveCenter(metaWindow) {
  850. if (!metaWindow) return;
  851. let frameRect = metaWindow.get_frame_rect();
  852. const rectRequest = {
  853. x: "center",
  854. y: "center",
  855. width: frameRect.width,
  856. height: frameRect.height,
  857. };
  858. let moveRect = {
  859. x: Utils.resolveX(rectRequest, metaWindow),
  860. y: Utils.resolveY(rectRequest, metaWindow),
  861. width: Utils.resolveWidth(rectRequest, metaWindow),
  862. height: Utils.resolveHeight(rectRequest, metaWindow),
  863. };
  864. this.move(metaWindow, moveRect);
  865. }
  866. rectForMonitor(node, targetMonitor) {
  867. if (!node || (node && node.nodeType !== NODE_TYPES.WINDOW)) return null;
  868. if (targetMonitor < 0) return null;
  869. let currentWorkArea = node.nodeValue.get_work_area_current_monitor();
  870. let nextWorkArea = node.nodeValue.get_work_area_for_monitor(targetMonitor);
  871. if (currentWorkArea && nextWorkArea) {
  872. let rect = node.rect;
  873. if (!rect && node.mode === WINDOW_MODES.FLOAT) {
  874. rect = node.nodeValue.get_frame_rect();
  875. }
  876. let hRatio = 1;
  877. let wRatio = 1;
  878. hRatio = nextWorkArea.height / currentWorkArea.height;
  879. wRatio = nextWorkArea.width / currentWorkArea.width;
  880. rect.height *= hRatio;
  881. rect.width *= wRatio;
  882. if (nextWorkArea.y < currentWorkArea.y) {
  883. rect.y =
  884. ((nextWorkArea.y + rect.y - currentWorkArea.y) / currentWorkArea.height) *
  885. nextWorkArea.height;
  886. } else if (nextWorkArea.y > currentWorkArea.y) {
  887. rect.y = (rect.y / currentWorkArea.height) * nextWorkArea.height + nextWorkArea.y;
  888. }
  889. if (nextWorkArea.x < currentWorkArea.x) {
  890. rect.x =
  891. ((nextWorkArea.x + rect.x - currentWorkArea.x) / currentWorkArea.width) *
  892. nextWorkArea.width;
  893. } else if (nextWorkArea.x > currentWorkArea.x) {
  894. rect.x = (rect.x / currentWorkArea.width) * nextWorkArea.width + nextWorkArea.x;
  895. }
  896. return rect;
  897. }
  898. return null;
  899. }
  900. _removeSignals() {
  901. if (!this._signalsBound) return;
  902. if (this._displaySignals) {
  903. for (const displaySignal of this._displaySignals) {
  904. global.display.disconnect(displaySignal);
  905. }
  906. this._displaySignals.length = 0;
  907. this._displaySignals = undefined;
  908. }
  909. if (this._windowManagerSignals) {
  910. for (const windowManagerSignal of this._windowManagerSignals) {
  911. global.window_manager.disconnect(windowManagerSignal);
  912. }
  913. this._windowManagerSignals.length = 0;
  914. this._windowManagerSignals = undefined;
  915. }
  916. const globalWsm = global.workspace_manager;
  917. if (this._workspaceManagerSignals) {
  918. for (const workspaceManagerSignal of this._workspaceManagerSignals) {
  919. globalWsm.disconnect(workspaceManagerSignal);
  920. }
  921. this._workspaceManagerSignals.length = 0;
  922. this._workspaceManagerSignals = undefined;
  923. }
  924. let numberOfWorkspaces = globalWsm.get_n_workspaces();
  925. for (let i = 0; i < numberOfWorkspaces; i++) {
  926. let workspace = globalWsm.get_workspace_by_index(i);
  927. if (workspace.workspaceSignals) {
  928. for (const workspaceSignal of workspace.workspaceSignals) {
  929. workspace.disconnect(workspaceSignal);
  930. }
  931. workspace.workspaceSignals.length = 0;
  932. workspace.workspaceSignals = undefined;
  933. }
  934. }
  935. let allWindows = this.windowsAllWorkspaces;
  936. if (allWindows) {
  937. for (let metaWindow of allWindows) {
  938. if (metaWindow.windowSignals !== undefined) {
  939. for (const windowSignal of metaWindow.windowSignals) {
  940. metaWindow.disconnect(windowSignal);
  941. }
  942. metaWindow.windowSignals.length = 0;
  943. metaWindow.windowSignals = undefined;
  944. }
  945. let windowActor = metaWindow.get_compositor_private();
  946. if (windowActor && windowActor.actorSignals) {
  947. for (const actorSignal of windowActor.actorSignals) {
  948. windowActor.disconnect(actorSignal);
  949. }
  950. windowActor.actorSignals.length = 0;
  951. windowActor.actorSignals = undefined;
  952. }
  953. if (windowActor && windowActor.border) {
  954. windowActor.border.hide();
  955. if (global.window_group) {
  956. global.window_group.remove_child(windowActor.border);
  957. }
  958. windowActor.border = undefined;
  959. }
  960. if (windowActor && windowActor.splitBorder) {
  961. windowActor.splitBorder.hide();
  962. if (global.window_group) {
  963. global.window_group.remove_child(windowActor.splitBorder);
  964. }
  965. windowActor.splitBorder = undefined;
  966. }
  967. }
  968. }
  969. if (this._renderTreeSrcId) {
  970. GLib.Source.remove(this._renderTreeSrcId);
  971. this._renderTreeSrcId = 0;
  972. }
  973. if (this._reloadTreeSrcId) {
  974. GLib.Source.remove(this._reloadTreeSrcId);
  975. this._reloadTreeSrcId = 0;
  976. }
  977. if (this._wsWindowAddSrcId) {
  978. GLib.Source.remove(this._wsWindowAddSrcId);
  979. this._wsWindowAddSrcId = 0;
  980. }
  981. if (this._queueSourceId) {
  982. GLib.Source.remove(this._queueSourceId);
  983. this._queueSourceId = 0;
  984. }
  985. if (this._prefsOpenSrcId) {
  986. GLib.Source.remove(this._prefsOpenSrcId);
  987. this._prefsOpenSrcId = 0;
  988. }
  989. if (this._overviewSignals) {
  990. for (const overviewSignal of this._overviewSignals) {
  991. Main.overview.disconnect(overviewSignal);
  992. }
  993. this._overviewSignals.length = 0;
  994. this._overviewSignals = null;
  995. }
  996. this._signalsBound = false;
  997. }
  998. renderTree(from, force = false) {
  999. let wasFrozen = this._freezeRender;
  1000. if (force && wasFrozen) this.unfreezeRender();
  1001. if (this._freezeRender || !this.ext.settings.get_boolean("tiling-mode-enabled")) {
  1002. this.updateDecorationLayout();
  1003. this.updateBorderLayout();
  1004. } else {
  1005. if (!this._renderTreeSrcId) {
  1006. this._renderTreeSrcId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
  1007. this.processFloats();
  1008. this.tree.render(from);
  1009. this._renderTreeSrcId = 0;
  1010. this.updateDecorationLayout();
  1011. this.updateBorderLayout();
  1012. if (wasFrozen) this.freezeRender();
  1013. return false;
  1014. });
  1015. }
  1016. }
  1017. }
  1018. processFloats() {
  1019. this.allNodeWindows.forEach((nodeWindow) => {
  1020. let metaWindow = nodeWindow.nodeValue;
  1021. if (this.isFloatingExempt(metaWindow) || !this.isActiveWindowWorkspaceTiled(metaWindow)) {
  1022. nodeWindow.float = true;
  1023. } else {
  1024. nodeWindow.float = false;
  1025. }
  1026. });
  1027. }
  1028. get allNodeWindows() {
  1029. return this.tree.getNodeByType(NODE_TYPES.WINDOW);
  1030. }
  1031. /**
  1032. * Reloads the tree. This is an expensive operation.
  1033. * Useful when using dynamic workspaces in GNOME-shell.
  1034. *
  1035. * TODO: add support to reload the tree from a JSON dump file.
  1036. * TODO: move this to tree.js
  1037. */
  1038. reloadTree(from) {
  1039. if (!this._reloadTreeSrcId) {
  1040. this._reloadTreeSrcId = GLib.idle_add(GLib.PRIORITY_LOW, () => {
  1041. Utils._disableDecorations();
  1042. let treeWorkspaces = this.tree.nodeWorkpaces;
  1043. let wsManager = global.workspace_manager;
  1044. let globalWsNum = wsManager.get_n_workspaces();
  1045. // empty out the root children nodes
  1046. this.tree.childNodes.length = 0;
  1047. this.tree.attachNode = undefined;
  1048. // initialize the workspaces and monitors id strings
  1049. this.tree._initWorkspaces();
  1050. this.trackCurrentWindows();
  1051. this.renderTree(from);
  1052. this._reloadTreeSrcId = 0;
  1053. return false;
  1054. });
  1055. }
  1056. }
  1057. sameParentMonitor(firstNode, secondNode) {
  1058. if (!firstNode || !secondNode) return false;
  1059. if (!firstNode.nodeValue || !secondNode.nodeValue) return false;
  1060. if (!firstNode.nodeValue.get_workspace()) return false;
  1061. if (!secondNode.nodeValue.get_workspace()) return false;
  1062. let firstMonWs = `mo${firstNode.nodeValue.get_monitor()}ws${firstNode.nodeValue
  1063. .get_workspace()
  1064. .index()}`;
  1065. let secondMonWs = `mo${secondNode.nodeValue.get_monitor()}ws${secondNode.nodeValue
  1066. .get_workspace()
  1067. .index()}`;
  1068. return firstMonWs === secondMonWs;
  1069. }
  1070. showWindowBorders() {
  1071. let metaWindow = this.focusMetaWindow;
  1072. if (!metaWindow) return;
  1073. let windowActor = metaWindow.get_compositor_private();
  1074. if (!windowActor) return;
  1075. let nodeWindow = this.findNodeWindow(metaWindow);
  1076. if (!nodeWindow) return;
  1077. if (metaWindow.get_wm_class() === null) return;
  1078. let borders = [];
  1079. let focusBorderEnabled = this.ext.settings.get_boolean("focus-border-toggle");
  1080. let splitBorderEnabled = this.ext.settings.get_boolean("split-border-toggle");
  1081. let tilingModeEnabled = this.ext.settings.get_boolean("tiling-mode-enabled");
  1082. let gap = this.calculateGaps(nodeWindow);
  1083. let maximized = () => {
  1084. return metaWindow.get_maximized() === 3 || metaWindow.is_fullscreen() || gap === 0;
  1085. };
  1086. let monitorCount = global.display.get_n_monitors();
  1087. let tiledChildren = this.tree.getTiledChildren(nodeWindow.parentNode.childNodes);
  1088. let inset = 3;
  1089. let parentNode = nodeWindow.parentNode;
  1090. const floatingWindow = nodeWindow.isFloat();
  1091. const tiledBorder = windowActor.border;
  1092. if (parentNode.isTabbed()) {
  1093. if (nodeWindow.tab) {
  1094. nodeWindow.tab.add_style_class_name("window-tabbed-tab-active");
  1095. }
  1096. }
  1097. if (tiledBorder && focusBorderEnabled) {
  1098. if (
  1099. !maximized() ||
  1100. (gap === 0 && tiledChildren.length === 1 && monitorCount > 1) ||
  1101. (gap === 0 && tiledChildren.length > 1)
  1102. ) {
  1103. if (tilingModeEnabled) {
  1104. if (parentNode.isStacked()) {
  1105. if (!floatingWindow) {
  1106. tiledBorder.set_style_class_name("window-stacked-border");
  1107. } else {
  1108. tiledBorder.set_style_class_name("window-floated-border");
  1109. }
  1110. } else if (parentNode.isTabbed()) {
  1111. if (!floatingWindow) {
  1112. tiledBorder.set_style_class_name("window-tabbed-border");
  1113. if (nodeWindow.backgroundTab) {
  1114. tiledBorder.add_style_class_name("window-tabbed-bg");
  1115. }
  1116. } else {
  1117. tiledBorder.set_style_class_name("window-floated-border");
  1118. }
  1119. } else {
  1120. if (!floatingWindow) {
  1121. tiledBorder.set_style_class_name("window-tiled-border");
  1122. } else {
  1123. tiledBorder.set_style_class_name("window-floated-border");
  1124. }
  1125. }
  1126. } else {
  1127. tiledBorder.set_style_class_name("window-floated-border");
  1128. }
  1129. borders.push(tiledBorder);
  1130. }
  1131. }
  1132. if (gap === 0 || metaWindow.get_maximized() === 1 || metaWindow.get_maximized() === 2) {
  1133. inset = 0;
  1134. }
  1135. // handle the split border
  1136. // It should only show when V or H-Split and with single child CONs
  1137. if (
  1138. splitBorderEnabled &&
  1139. focusBorderEnabled &&
  1140. tilingModeEnabled &&
  1141. !nodeWindow.isFloat() &&
  1142. !maximized &&
  1143. parentNode.childNodes.length === 1 &&
  1144. (parentNode.isCon() || parentNode.isMonitor()) &&
  1145. !(parentNode.isTabbed() || parentNode.isStacked())
  1146. ) {
  1147. if (!windowActor.splitBorder) {
  1148. let splitBorder = new St.Bin({ style_class: "window-split-border" });
  1149. global.window_group.add_child(splitBorder);
  1150. windowActor.splitBorder = splitBorder;
  1151. }
  1152. let splitBorder = windowActor.splitBorder;
  1153. splitBorder.remove_style_class_name("window-split-vertical");
  1154. splitBorder.remove_style_class_name("window-split-horizontal");
  1155. if (parentNode.isVSplit()) {
  1156. splitBorder.add_style_class_name("window-split-vertical");
  1157. } else if (parentNode.isHSplit()) {
  1158. splitBorder.add_style_class_name("window-split-horizontal");
  1159. }
  1160. borders.push(splitBorder);
  1161. }
  1162. let rect = metaWindow.get_frame_rect();
  1163. borders.forEach((border) => {
  1164. border.set_size(rect.width + inset * 2, rect.height + inset * 2);
  1165. border.set_position(rect.x - inset, rect.y - inset);
  1166. if (metaWindow.appears_focused && !metaWindow.minimized) {
  1167. border.show();
  1168. }
  1169. if (global.window_group && global.window_group.contains(border)) {
  1170. // TODO - sort the borders with split border being on top
  1171. global.window_group.remove_child(border);
  1172. // Add the border just above the focused window
  1173. global.window_group.insert_child_above(border, metaWindow.get_compositor_private());
  1174. }
  1175. });
  1176. }
  1177. updateBorderLayout() {
  1178. this.hideWindowBorders();
  1179. this.showWindowBorders();
  1180. }
  1181. calculateGaps(node) {
  1182. if (!node) return 0;
  1183. let settings = this.ext.settings;
  1184. let gapSize = settings.get_uint("window-gap-size");
  1185. let gapIncrement = settings.get_uint("window-gap-size-increment");
  1186. let gap = gapSize * gapIncrement;
  1187. if (!node.isRoot()) {
  1188. let hideGapWhenSingle = settings.get_boolean("window-gap-hidden-on-single");
  1189. let parentNode = this.tree.findParent(node, NODE_TYPES.MONITOR);
  1190. if (parentNode) {
  1191. let tiled = parentNode
  1192. .getNodeByMode(WINDOW_MODES.TILE)
  1193. .filter((t) => t.isWindow() && !t.nodeValue.minimized);
  1194. if (tiled.length == 1 && hideGapWhenSingle) gap = 0;
  1195. }
  1196. }
  1197. return gap;
  1198. }
  1199. /**
  1200. * Track meta/mutter windows and append them to the tree.
  1201. * Windows can be attached on any of the following Node Types:
  1202. * MONITOR, CONTAINER
  1203. *
  1204. */
  1205. trackWindow(_display, metaWindow) {
  1206. let autoSplit = this.ext.settings.get_boolean("auto-split-enabled");
  1207. if (autoSplit && this.focusMetaWindow) {
  1208. let currentFocusNode = this.tree.findNode(this.focusMetaWindow);
  1209. if (currentFocusNode) {
  1210. let currentParentFocusNode = currentFocusNode.parentNode;
  1211. let layout = currentParentFocusNode.layout;
  1212. if (layout === LAYOUT_TYPES.HSPLIT || layout === LAYOUT_TYPES.VSPLIT) {
  1213. let frameRect = this.focusMetaWindow.get_frame_rect();
  1214. let splitHorizontal = frameRect.width > frameRect.height;
  1215. let orientation = splitHorizontal ? "horizontal" : "vertical";
  1216. this.command({ name: "Split", orientation: orientation });
  1217. }
  1218. }
  1219. }
  1220. // Make window types configurable
  1221. if (this._validWindow(metaWindow)) {
  1222. let existNodeWindow = this.tree.findNode(metaWindow);
  1223. Logger.debug(`Meta Window ${metaWindow.get_title()} ${metaWindow.get_window_type()}`);
  1224. if (!existNodeWindow) {
  1225. let attachTarget;
  1226. const activeMonitor = global.display.get_current_monitor();
  1227. const activeWorkspace = global.display.get_workspace_manager().get_active_workspace_index();
  1228. let metaMonWs = `mo${activeMonitor}ws${activeWorkspace}`;
  1229. // Check if the active monitor / workspace has windows
  1230. let metaMonWsNode = this.tree.findNode(metaMonWs);
  1231. if (!metaMonWsNode) {
  1232. // Reload the tree as a last resort
  1233. this.reloadTree("no-meta-monws");
  1234. return;
  1235. }
  1236. let windowNodes = metaMonWsNode.getNodeByType(NODE_TYPES.WINDOW);
  1237. let hasWindows = windowNodes.length > 0;
  1238. attachTarget = this.tree.attachNode;
  1239. attachTarget = attachTarget ? this.tree.findNode(attachTarget.nodeValue) : null;
  1240. if (!attachTarget) {
  1241. attachTarget = metaMonWsNode;
  1242. } else {
  1243. if (hasWindows) {
  1244. if (attachTarget && metaMonWsNode.contains(attachTarget)) {
  1245. // Use the attach target
  1246. } else {
  1247. // Find the first window
  1248. attachTarget = windowNodes[0];
  1249. }
  1250. } else {
  1251. attachTarget = metaMonWsNode;
  1252. }
  1253. }
  1254. let nodeWindow = this.tree.createNode(
  1255. attachTarget.nodeValue,
  1256. NODE_TYPES.WINDOW,
  1257. metaWindow,
  1258. WINDOW_MODES.FLOAT
  1259. );
  1260. metaWindow.firstRender = true;
  1261. let windowActor = metaWindow.get_compositor_private();
  1262. if (!metaWindow.windowSignals) {
  1263. let windowSignals = [
  1264. metaWindow.connect("position-changed", (_metaWindow) => {
  1265. let from = "position-changed";
  1266. this.updateMetaPositionSize(_metaWindow, from);
  1267. }),
  1268. metaWindow.connect("size-changed", (_metaWindow) => {
  1269. let from = "size-changed";
  1270. this.updateMetaPositionSize(_metaWindow, from);
  1271. }),
  1272. metaWindow.connect("focus", (_metaWindowFocus) => {
  1273. this.queueEvent({
  1274. name: "focus-update",
  1275. callback: () => {
  1276. this.unfreezeRender();
  1277. this.updateBorderLayout();
  1278. this.updateDecorationLayout();
  1279. this.updateStackedFocus();
  1280. this.updateTabbedFocus();
  1281. },
  1282. });
  1283. let focusNodeWindow = this.tree.findNode(this.focusMetaWindow);
  1284. if (focusNodeWindow) {
  1285. // handle the attach node
  1286. this.tree.attachNode = focusNodeWindow._parent;
  1287. if (this.floatingWindow(focusNodeWindow)) {
  1288. this.queueEvent({
  1289. name: "raise-float",
  1290. callback: () => {
  1291. this.renderTree("raise-float-queue");
  1292. },
  1293. });
  1294. }
  1295. this.tree.attachNode = focusNodeWindow;
  1296. }
  1297. this.renderTree("focus", true);
  1298. }),
  1299. metaWindow.connect("workspace-changed", (_metaWindow) => {
  1300. this.updateMetaWorkspaceMonitor("metawindow-workspace-changed", null, _metaWindow);
  1301. this.trackCurrentMonWs();
  1302. }),
  1303. ];
  1304. metaWindow.windowSignals = windowSignals;
  1305. }
  1306. if (!windowActor.actorSignals) {
  1307. let actorSignals = [windowActor.connect("destroy", this.windowDestroy.bind(this))];
  1308. windowActor.actorSignals = actorSignals;
  1309. }
  1310. if (!windowActor.border) {
  1311. let border = new St.Bin({ style_class: "window-tiled-border" });
  1312. if (global.window_group) global.window_group.add_child(border);
  1313. windowActor.border = border;
  1314. border.show();
  1315. }
  1316. this.postProcessWindow(nodeWindow);
  1317. this.queueEvent(
  1318. {
  1319. name: "window-create-queue",
  1320. callback: () => {
  1321. metaWindow.unmaximize(Meta.MaximizeFlags.HORIZONTAL);
  1322. metaWindow.unmaximize(Meta.MaximizeFlags.VERTICAL);
  1323. metaWindow.unmaximize(Meta.MaximizeFlags.BOTH);
  1324. this.renderTree("window-create", true);
  1325. },
  1326. },
  1327. 200
  1328. );
  1329. let childNodes = this.tree.getTiledChildren(nodeWindow.parentNode.childNodes);
  1330. childNodes.forEach((n) => {
  1331. n.percent = 0.0;
  1332. });
  1333. }
  1334. }
  1335. }
  1336. postProcessWindow(nodeWindow) {
  1337. let metaWindow = nodeWindow.nodeValue;
  1338. if (metaWindow) {
  1339. if (metaWindow.get_title() === this.prefsTitle) {
  1340. metaWindow
  1341. .get_workspace()
  1342. .activate_with_focus(metaWindow, global.display.get_current_time());
  1343. this.moveCenter(metaWindow);
  1344. }
  1345. }
  1346. }
  1347. updateStackedFocus(focusNodeWindow) {
  1348. if (!focusNodeWindow) return;
  1349. const parentNode = focusNodeWindow.parentNode;
  1350. if (parentNode.layout === LAYOUT_TYPES.STACKED && !this._freezeRender) {
  1351. parentNode.appendChild(focusNodeWindow);
  1352. parentNode.childNodes
  1353. .filter((child) => child.isWindow())
  1354. .forEach((child) => child.nodeValue.raise());
  1355. this.queueEvent({
  1356. name: "render-focus-stack",
  1357. callback: () => {
  1358. this.renderTree("focus-stacked");
  1359. },
  1360. });
  1361. }
  1362. }
  1363. updateTabbedFocus(focusNodeWindow) {
  1364. if (!focusNodeWindow) return;
  1365. if (focusNodeWindow.parentNode.layout === LAYOUT_TYPES.TABBED && !this._freezeRender) {
  1366. const metaWindow = focusNodeWindow.nodeValue;
  1367. metaWindow.raise();
  1368. }
  1369. }
  1370. /**
  1371. * Check if a Meta Window's workspace is skipped for tiling.
  1372. */
  1373. isActiveWindowWorkspaceTiled(metaWindow) {
  1374. if (!metaWindow) return true;
  1375. let skipWs = this.ext.settings.get_string("workspace-skip-tile");
  1376. let skipArr = skipWs.split(",");
  1377. let skipThisWs = false;
  1378. for (let i = 0; i < skipArr.length; i++) {
  1379. let activeWorkspaceForWin = metaWindow.get_workspace();
  1380. if (activeWorkspaceForWin) {
  1381. let wsIndex = activeWorkspaceForWin.index();
  1382. if (skipArr[i].trim() === `${wsIndex}`) {
  1383. skipThisWs = true;
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. return !skipThisWs;
  1389. }
  1390. /**
  1391. * Check the current active workspace's tiling mode
  1392. */
  1393. isCurrentWorkspaceTiled() {
  1394. let skipWs = this.ext.settings.get_string("workspace-skip-tile");
  1395. let skipArr = skipWs.split(",");
  1396. let skipThisWs = false;
  1397. let wsMgr = global.workspace_manager;
  1398. let wsIndex = wsMgr.get_active_workspace_index();
  1399. for (let i = 0; i < skipArr.length; i++) {
  1400. if (skipArr[i].trim() === `${wsIndex}`) {
  1401. skipThisWs = true;
  1402. break;
  1403. }
  1404. }
  1405. return !skipThisWs;
  1406. }
  1407. trackCurrentWindows() {
  1408. this.tree.attachNode = null;
  1409. let windowsAll = this.windowsAllWorkspaces;
  1410. for (let i = 0; i < windowsAll.length; i++) {
  1411. let metaWindow = windowsAll[i];
  1412. this.trackWindow(global.display, metaWindow);
  1413. // This updates and handles dynamic workspaces
  1414. this.updateMetaWorkspaceMonitor(
  1415. "track-current-windows",
  1416. metaWindow.get_monitor(),
  1417. metaWindow
  1418. );
  1419. }
  1420. this.updateDecorationLayout();
  1421. }
  1422. _validWindow(metaWindow) {
  1423. let windowType = metaWindow.get_window_type();
  1424. return (
  1425. windowType === Meta.WindowType.NORMAL ||
  1426. windowType === Meta.WindowType.MODAL_DIALOG ||
  1427. windowType === Meta.WindowType.DIALOG
  1428. );
  1429. }
  1430. windowDestroy(actor) {
  1431. // Release any resources on the window
  1432. let border = actor.border;
  1433. if (border) {
  1434. if (global.window_group) {
  1435. global.window_group.remove_child(border);
  1436. border.hide();
  1437. border = null;
  1438. }
  1439. }
  1440. let splitBorder = actor.splitBorder;
  1441. if (splitBorder) {
  1442. if (global.window_group) {
  1443. global.window_group.remove_child(splitBorder);
  1444. splitBorder.hide();
  1445. splitBorder = null;
  1446. }
  1447. }
  1448. let nodeWindow;
  1449. nodeWindow = this.tree.findNodeByActor(actor);
  1450. if (nodeWindow) {
  1451. this.tree.removeNode(nodeWindow);
  1452. this.renderTree("window-destroy-quick", true);
  1453. }
  1454. // find the next attachNode here
  1455. let focusNodeWindow = this.tree.findNode(this.focusMetaWindow);
  1456. if (focusNodeWindow) {
  1457. this.tree.attachNode = focusNodeWindow.parentNode;
  1458. }
  1459. this.queueEvent({
  1460. name: "window-destroy",
  1461. callback: () => {
  1462. this.renderTree("window-destroy", true);
  1463. },
  1464. });
  1465. }
  1466. /**
  1467. * Handles any workspace/monitor update for the Meta.Window.
  1468. */
  1469. updateMetaWorkspaceMonitor(from, _monitor, metaWindow) {
  1470. if (this._validWindow(metaWindow)) {
  1471. if (metaWindow.get_workspace() === null) return;
  1472. let existNodeWindow = this.tree.findNode(metaWindow);
  1473. let metaMonWs = `mo${metaWindow.get_monitor()}ws${metaWindow.get_workspace().index()}`;
  1474. let metaMonWsNode = this.tree.findNode(metaMonWs);
  1475. if (existNodeWindow) {
  1476. if (existNodeWindow.parentNode && metaMonWsNode) {
  1477. // Uses the existing workspace, monitor that the metaWindow
  1478. // belongs to.
  1479. let containsWindow = metaMonWsNode.contains(existNodeWindow);
  1480. if (!containsWindow) {
  1481. // handle cleanup of resize percentages
  1482. let existParent = existNodeWindow.parentNode;
  1483. this.tree.resetSiblingPercent(existParent);
  1484. metaMonWsNode.appendChild(existNodeWindow);
  1485. // Ensure that the workspace tiling is honored
  1486. if (this.isActiveWindowWorkspaceTiled(metaWindow)) {
  1487. if (!this.grabOp === Meta.GrabOp.WINDOW_BASE) this.updateTabbedFocus(existNodeWindow);
  1488. this.updateStackedFocus(existNodeWindow);
  1489. } else {
  1490. if (this.floatingWindow(existNodeWindow)) {
  1491. existNodeWindow.nodeValue.raise();
  1492. }
  1493. }
  1494. }
  1495. }
  1496. }
  1497. this.renderTree(from);
  1498. }
  1499. }
  1500. /**
  1501. * Handle any updates to the current focused window's position.
  1502. * Useful for updating the active window border, etc.
  1503. */
  1504. updateMetaPositionSize(_metaWindow, from) {
  1505. let focusMetaWindow = this.focusMetaWindow;
  1506. if (!focusMetaWindow) return;
  1507. let focusNodeWindow = this.findNodeWindow(focusMetaWindow);
  1508. if (!focusNodeWindow) return;
  1509. let tilingModeEnabled = this.ext.settings.get_boolean("tiling-mode-enabled");
  1510. if (focusNodeWindow.grabMode && tilingModeEnabled) {
  1511. if (focusNodeWindow.grabMode === GRAB_TYPES.RESIZING) {
  1512. this._handleResizing(focusNodeWindow);
  1513. } else if (focusNodeWindow.grabMode === GRAB_TYPES.MOVING) {
  1514. this._handleMoving(focusNodeWindow);
  1515. }
  1516. } else {
  1517. if (focusMetaWindow.get_maximized() === 0) {
  1518. this.renderTree(from);
  1519. }
  1520. }
  1521. this.updateBorderLayout();
  1522. this.updateDecorationLayout();
  1523. }
  1524. updateDecorationLayout() {
  1525. if (this._freezeRender) return;
  1526. let activeWsNode = this.currentWsNode;
  1527. let allCons = this.tree.getNodeByType(NODE_TYPES.CON);
  1528. // First, hide all decorations:
  1529. allCons.forEach((con) => {
  1530. if (con.decoration) {
  1531. con.decoration.hide();
  1532. }
  1533. });
  1534. // Next, handle showing-desktop usually by Super + D
  1535. if (!activeWsNode) return;
  1536. let allWindows = activeWsNode.getNodeByType(NODE_TYPES.WINDOW);
  1537. let allHiddenWindows = allWindows.filter((w) => {
  1538. let metaWindow = w.nodeValue;
  1539. return !metaWindow.showing_on_its_workspace() || metaWindow.minimized;
  1540. });
  1541. // Then if all hidden, do not proceed showing the decorations at all;
  1542. if (allWindows.length === allHiddenWindows.length) return;
  1543. // Show the decoration where on all monitors of active workspace
  1544. // But not on the monitor where there is a maximized or fullscreen window
  1545. // Note, that when multi-display, user can have multi maximized windows,
  1546. // So it needs to be fully filtered:
  1547. let monWsNoMaxWindows = activeWsNode.getNodeByType(NODE_TYPES.MONITOR).filter((monitor) => {
  1548. return (
  1549. monitor.getNodeByType(NODE_TYPES.WINDOW).filter((w) => {
  1550. return (
  1551. w.nodeValue.get_maximized() === Meta.MaximizeFlags.BOTH || w.nodeValue.is_fullscreen()
  1552. );
  1553. }).length === 0
  1554. );
  1555. });
  1556. monWsNoMaxWindows.forEach((monitorWs) => {
  1557. let activeMonWsCons = monitorWs.getNodeByType(NODE_TYPES.CON);
  1558. activeMonWsCons.forEach((con) => {
  1559. let tiled = this.tree.getTiledChildren(con.childNodes);
  1560. let showTabs = this.ext.settings.get_boolean("showtab-decoration-enabled");
  1561. if (con.decoration && tiled.length > 0 && showTabs) {
  1562. con.decoration.show();
  1563. if (global.window_group.contains(con.decoration) && this.focusMetaWindow) {
  1564. global.window_group.remove_child(con.decoration);
  1565. // Show it below the focused window
  1566. global.window_group.insert_child_below(
  1567. con.decoration,
  1568. this.focusMetaWindow.get_compositor_private()
  1569. );
  1570. }
  1571. con.childNodes.forEach((cn) => {
  1572. cn.render();
  1573. });
  1574. }
  1575. });
  1576. });
  1577. }
  1578. freezeRender() {
  1579. this._freezeRender = true;
  1580. }
  1581. unfreezeRender() {
  1582. this._freezeRender = false;
  1583. }
  1584. floatingWindow(node) {
  1585. if (!node) return false;
  1586. return node.nodeType === NODE_TYPES.WINDOW && node.mode === WINDOW_MODES.FLOAT;
  1587. }
  1588. /**
  1589. * Moves the pointer along with the nodeWindow's meta
  1590. *
  1591. * This is useful for making sure that Forge calculates the attachNode
  1592. * properly
  1593. */
  1594. movePointerWith(nodeWindow) {
  1595. if (!nodeWindow || !nodeWindow._data) return;
  1596. let rect = nodeWindow._data.get_frame_rect();
  1597. global.get_pointer();
  1598. const seat = Clutter.get_default_backend().get_default_seat();
  1599. seat.warp_pointer(rect.x, rect.y);
  1600. }
  1601. getPointer() {
  1602. return global.get_pointer();
  1603. }
  1604. minimizedWindow(node) {
  1605. if (!node) return false;
  1606. return node._type === NODE_TYPES.WINDOW && node._data && node._data.minimized;
  1607. }
  1608. swapWindowsUnderPointer(focusNodeWindow) {
  1609. if (this.cancelGrab) {
  1610. return;
  1611. }
  1612. let nodeWinAtPointer = this.findNodeWindowAtPointer(focusNodeWindow);
  1613. if (nodeWinAtPointer) this.tree.swapPairs(focusNodeWindow, nodeWinAtPointer);
  1614. }
  1615. /**
  1616. *
  1617. * Handle previewing and applying where a drag-drop window is going to be tiled
  1618. *
  1619. */
  1620. moveWindowToPointer(focusNodeWindow, preview = false) {
  1621. if (this.cancelGrab) {
  1622. return;
  1623. }
  1624. if (!focusNodeWindow || focusNodeWindow.mode !== WINDOW_MODES.GRAB_TILE) return;
  1625. let nodeWinAtPointer = this.nodeWinAtPointer;
  1626. if (nodeWinAtPointer) {
  1627. const targetRect = nodeWinAtPointer.nodeValue.get_frame_rect();
  1628. const parentNodeTarget = nodeWinAtPointer.parentNode;
  1629. const currPointer = this.getPointer();
  1630. const horizontal = parentNodeTarget.isHSplit() || parentNodeTarget.isTabbed();
  1631. const isMonParent = parentNodeTarget.nodeType === NODE_TYPES.MONITOR;
  1632. const isConParent = parentNodeTarget.nodeType === NODE_TYPES.CON;
  1633. const stacked = parentNodeTarget.isStacked();
  1634. const tabbed = parentNodeTarget.isTabbed();
  1635. const stackedOrTabbed = stacked || tabbed;
  1636. const updatePreview = (focusNodeWindow, previewParams) => {
  1637. let previewHint = focusNodeWindow.previewHint;
  1638. let previewHintEnabled = this.ext.settings.get_boolean("preview-hint-enabled");
  1639. const previewRect = previewParams.targetRect;
  1640. if (previewHint && previewHintEnabled) {
  1641. if (!previewRect) {
  1642. previewHint.hide();
  1643. return;
  1644. }
  1645. previewHint.set_style_class_name(previewParams.className);
  1646. previewHint.set_position(previewRect.x, previewRect.y);
  1647. previewHint.set_size(previewRect.width, previewRect.height);
  1648. previewHint.show();
  1649. }
  1650. };
  1651. const regions = (targetRect, regionWidth) => {
  1652. leftRegion = {
  1653. x: targetRect.x,
  1654. y: targetRect.y,
  1655. width: targetRect.width * regionWidth,
  1656. height: targetRect.height,
  1657. };
  1658. rightRegion = {
  1659. x: targetRect.x + targetRect.width * (1 - regionWidth),
  1660. y: targetRect.y,
  1661. width: targetRect.width * regionWidth,
  1662. height: targetRect.height,
  1663. };
  1664. topRegion = {
  1665. x: targetRect.x,
  1666. y: targetRect.y,
  1667. width: targetRect.width,
  1668. height: targetRect.height * regionWidth,
  1669. };
  1670. bottomRegion = {
  1671. x: targetRect.x,
  1672. y: targetRect.y + targetRect.height * (1 - regionWidth),
  1673. width: targetRect.width,
  1674. height: targetRect.height * regionWidth,
  1675. };
  1676. centerRegion = {
  1677. x: targetRect.x + targetRect.width * regionWidth,
  1678. y: targetRect.y + targetRect.height * regionWidth,
  1679. width: targetRect.width - targetRect.width * regionWidth * 2,
  1680. height: targetRect.height - targetRect.height * regionWidth * 2,
  1681. };
  1682. return {
  1683. left: leftRegion,
  1684. right: rightRegion,
  1685. top: topRegion,
  1686. bottom: bottomRegion,
  1687. center: centerRegion,
  1688. };
  1689. };
  1690. let referenceNode = null;
  1691. let containerNode;
  1692. let childNode = focusNodeWindow;
  1693. let previewParams = {
  1694. className: "",
  1695. targetRect: null,
  1696. };
  1697. let leftRegion;
  1698. let rightRegion;
  1699. let topRegion;
  1700. let bottomRegion;
  1701. let centerRegion;
  1702. let previewWidth = 0.5;
  1703. let hoverWidth = 0.3;
  1704. // Hover region detects where the pointer is on the target drop window
  1705. const hoverRegions = regions(targetRect, hoverWidth);
  1706. // Preview region interprets the hover intersect where the focus window
  1707. // would go when dropped
  1708. const previewRegions = regions(targetRect, previewWidth);
  1709. leftRegion = hoverRegions.left;
  1710. rightRegion = hoverRegions.right;
  1711. topRegion = hoverRegions.top;
  1712. bottomRegion = hoverRegions.bottom;
  1713. centerRegion = hoverRegions.center;
  1714. const isLeft = Utils.rectContainsPoint(leftRegion, currPointer);
  1715. const isRight = Utils.rectContainsPoint(rightRegion, currPointer);
  1716. const isTop = Utils.rectContainsPoint(topRegion, currPointer);
  1717. const isBottom = Utils.rectContainsPoint(bottomRegion, currPointer);
  1718. const isCenter = Utils.rectContainsPoint(centerRegion, currPointer);
  1719. if (isCenter) {
  1720. if (stackedOrTabbed) {
  1721. containerNode = parentNodeTarget;
  1722. referenceNode = null;
  1723. previewParams = {
  1724. className: stacked ? "window-tilepreview-stacked" : "window-tilepreview-tabbed",
  1725. targetRect: targetRect,
  1726. };
  1727. } else {
  1728. if (isMonParent) {
  1729. childNode.createCon = true;
  1730. containerNode = parentNodeTarget;
  1731. referenceNode = nodeWinAtPointer;
  1732. previewParams = {
  1733. targetRect: targetRect,
  1734. };
  1735. } else {
  1736. containerNode = parentNodeTarget;
  1737. referenceNode = null;
  1738. const parentTargetRect = this.tree.processGap(parentNodeTarget);
  1739. previewParams = {
  1740. targetRect: parentTargetRect,
  1741. };
  1742. }
  1743. }
  1744. } else if (isLeft) {
  1745. previewParams = {
  1746. targetRect: previewRegions.left,
  1747. };
  1748. if (stackedOrTabbed) {
  1749. // treat any windows on stacked or tabbed layouts to be
  1750. // a single node unit: the con itself and then
  1751. // split left, top, right or bottom accordingly (subsequent if conditions):
  1752. childNode.detachWindow = true;
  1753. if (!isMonParent) {
  1754. referenceNode = parentNodeTarget;
  1755. containerNode = parentNodeTarget.parentNode;
  1756. } else {
  1757. // It is a monitor that's a stack/tab
  1758. // TODO: update the stacked/tabbed toggles to not
  1759. // change layout if the parent is a monitor?
  1760. }
  1761. } else {
  1762. if (horizontal) {
  1763. containerNode = parentNodeTarget;
  1764. referenceNode = nodeWinAtPointer;
  1765. } else {
  1766. // vertical orientation
  1767. childNode.createCon = true;
  1768. containerNode = parentNodeTarget;
  1769. referenceNode = nodeWinAtPointer;
  1770. }
  1771. }
  1772. } else if (isRight) {
  1773. previewParams = {
  1774. targetRect: previewRegions.right,
  1775. };
  1776. if (stackedOrTabbed) {
  1777. // treat any windows on stacked or tabbed layouts to be
  1778. // a single node unit: the con itself and then
  1779. // split left, top, right or bottom accordingly (subsequent if conditions):
  1780. childNode.detachWindow = true;
  1781. if (!isMonParent) {
  1782. referenceNode = parentNodeTarget.nextSibling;
  1783. containerNode = parentNodeTarget.parentNode;
  1784. } else {
  1785. // It is a monitor that's a stack/tab
  1786. // TODO: update the stacked/tabbed toggles to not
  1787. // change layout if the parent is a monitor?
  1788. }
  1789. } else {
  1790. if (horizontal) {
  1791. containerNode = parentNodeTarget;
  1792. referenceNode = nodeWinAtPointer.nextSibling;
  1793. } else {
  1794. childNode.createCon = true;
  1795. containerNode = parentNodeTarget;
  1796. referenceNode = nodeWinAtPointer.nextSibling;
  1797. }
  1798. }
  1799. } else if (isTop) {
  1800. previewParams = {
  1801. targetRect: previewRegions.top,
  1802. };
  1803. if (stackedOrTabbed) {
  1804. // treat any windows on stacked or tabbed layouts to be
  1805. // a single node unit: the con itself and then
  1806. // split left, top, right or bottom accordingly (subsequent if conditions):
  1807. if (!isMonParent) {
  1808. containerNode = parentNodeTarget;
  1809. referenceNode = null;
  1810. previewParams = {
  1811. className: stacked ? "window-tilepreview-stacked" : "window-tilepreview-tabbed",
  1812. targetRect: targetRect,
  1813. };
  1814. } else {
  1815. // It is a monitor that's a stack/tab
  1816. // TODO: update the stacked/tabbed toggles to not
  1817. // change layout if the parent is a monitor?
  1818. }
  1819. } else {
  1820. if (horizontal) {
  1821. childNode.createCon = true;
  1822. containerNode = parentNodeTarget;
  1823. referenceNode = nodeWinAtPointer;
  1824. } else {
  1825. containerNode = parentNodeTarget;
  1826. referenceNode = nodeWinAtPointer;
  1827. }
  1828. }
  1829. } else if (isBottom) {
  1830. previewParams = {
  1831. targetRect: previewRegions.bottom,
  1832. };
  1833. if (stackedOrTabbed) {
  1834. // treat any windows on stacked or tabbed layouts to be
  1835. // a single node unit: the con itself and then
  1836. // split left, top, right or bottom accordingly (subsequent if conditions):
  1837. if (!isMonParent) {
  1838. containerNode = parentNodeTarget;
  1839. referenceNode = null;
  1840. previewParams = {
  1841. className: stacked ? "window-tilepreview-stacked" : "window-tilepreview-tabbed",
  1842. targetRect: targetRect,
  1843. };
  1844. } else {
  1845. // It is a monitor that's a stack/tab
  1846. // TODO: update the stacked/tabbed toggles to not
  1847. // change layout if the parent is a monitor?
  1848. }
  1849. } else {
  1850. if (horizontal) {
  1851. childNode = focusNodeWindow;
  1852. childNode.createCon = true;
  1853. containerNode = parentNodeTarget;
  1854. referenceNode = nodeWinAtPointer.nextSibling;
  1855. } else {
  1856. childNode = focusNodeWindow;
  1857. containerNode = parentNodeTarget;
  1858. referenceNode = nodeWinAtPointer.nextSibling;
  1859. }
  1860. }
  1861. }
  1862. if (!isCenter) {
  1863. if (stackedOrTabbed) {
  1864. if (isLeft || isRight) {
  1865. previewParams.className = "window-tilepreview-tiled";
  1866. } else if (isTop || isBottom) {
  1867. previewParams.className = stacked
  1868. ? "window-tilepreview-stacked"
  1869. : "window-tilepreview-tabbed";
  1870. }
  1871. } else {
  1872. previewParams.className = "window-tilepreview-tiled";
  1873. }
  1874. } else if (isCenter) {
  1875. if (!stackedOrTabbed) previewParams.className = this._getDragDropCenterPreviewStyle();
  1876. }
  1877. if (!preview) {
  1878. const previousParent = focusNodeWindow.parentNode;
  1879. this.tree.resetSiblingPercent(containerNode);
  1880. this.tree.resetSiblingPercent(previousParent);
  1881. if (focusNodeWindow.tab) {
  1882. let decoParent = focusNodeWindow.tab.get_parent();
  1883. if (decoParent) decoParent.remove_child(focusNodeWindow.tab);
  1884. }
  1885. if (childNode.createCon) {
  1886. const numWin = parentNodeTarget.childNodes.filter(
  1887. (c) => c.nodeType === NODE_TYPES.WINDOW
  1888. ).length;
  1889. const numChild = parentNodeTarget.childNodes.length;
  1890. const sameNumChild = numWin === numChild;
  1891. // Child Node will still be created
  1892. if (
  1893. !isCenter &&
  1894. ((isConParent && numWin === 1 && sameNumChild) ||
  1895. (isMonParent && numWin == 2 && sameNumChild))
  1896. ) {
  1897. childNode = parentNodeTarget;
  1898. } else {
  1899. childNode = new Node(NODE_TYPES.CON, new St.Bin());
  1900. containerNode.insertBefore(childNode, referenceNode);
  1901. childNode.appendChild(nodeWinAtPointer);
  1902. }
  1903. if (isLeft || isTop) {
  1904. childNode.insertBefore(focusNodeWindow, nodeWinAtPointer);
  1905. } else if (isRight || isBottom || isCenter) {
  1906. childNode.insertBefore(focusNodeWindow, null);
  1907. }
  1908. if (isLeft || isRight) {
  1909. childNode.layout = LAYOUT_TYPES.HSPLIT;
  1910. } else if (isTop || isBottom) {
  1911. childNode.layout = LAYOUT_TYPES.VSPLIT;
  1912. } else if (isCenter) {
  1913. const centerLayout = this.ext.settings.get_string("dnd-center-layout").toUpperCase();
  1914. childNode.layout = LAYOUT_TYPES[centerLayout];
  1915. }
  1916. } else if (childNode.detachWindow) {
  1917. const orientation =
  1918. isLeft || isRight ? ORIENTATION_TYPES.HORIZONTAL : ORIENTATION_TYPES.VERTICAL;
  1919. this.tree.split(childNode, orientation);
  1920. containerNode.insertBefore(childNode.parentNode, referenceNode);
  1921. } else {
  1922. // Child Node is a WINDOW
  1923. containerNode.insertBefore(childNode, referenceNode);
  1924. if (isLeft || isRight) {
  1925. containerNode.layout = LAYOUT_TYPES.HSPLIT;
  1926. } else if (isTop || isBottom) {
  1927. if (!stackedOrTabbed) containerNode.layout = LAYOUT_TYPES.VSPLIT;
  1928. } else if (isCenter) {
  1929. if (containerNode.isHSplit() || containerNode.isVSplit()) {
  1930. const centerLayout = this.ext.settings.get_string("dnd-center-layout").toUpperCase();
  1931. containerNode.layout = LAYOUT_TYPES[centerLayout];
  1932. }
  1933. }
  1934. }
  1935. } else {
  1936. updatePreview(focusNodeWindow, previewParams);
  1937. }
  1938. childNode.createCon = false;
  1939. childNode.detachWindow = false;
  1940. }
  1941. }
  1942. findNodeWindowAtPointer(focusNodeWindow) {
  1943. let pointerCoord = global.get_pointer();
  1944. let nodeWinAtPointer = this._findNodeWindowAtPointer(focusNodeWindow.nodeValue, pointerCoord);
  1945. return nodeWinAtPointer;
  1946. }
  1947. /**
  1948. * Finds the NodeWindow under the Meta.Window and the
  1949. * current pointer coordinates;
  1950. */
  1951. _findNodeWindowAtPointer(metaWindow, pointer) {
  1952. if (!metaWindow) return undefined;
  1953. let sortedWindows = this.sortedWindows;
  1954. if (!sortedWindows) {
  1955. Logger.warn("No sorted windows");
  1956. return;
  1957. }
  1958. for (let i = 0, n = sortedWindows.length; i < n; i++) {
  1959. const w = sortedWindows[i];
  1960. const metaRect = w.get_frame_rect();
  1961. const atPointer = Utils.rectContainsPoint(metaRect, pointer);
  1962. if (atPointer) return this.tree.getNodeByValue(w);
  1963. }
  1964. return null;
  1965. }
  1966. _handleGrabOpBegin(_display, _metaWindow, grabOp) {
  1967. this.grabOp = grabOp;
  1968. this.trackCurrentMonWs();
  1969. let focusMetaWindow = this.focusMetaWindow;
  1970. if (focusMetaWindow) {
  1971. let focusNodeWindow = this.findNodeWindow(focusMetaWindow);
  1972. if (!focusNodeWindow) return;
  1973. const frameRect = focusMetaWindow.get_frame_rect();
  1974. const gaps = this.calculateGaps(focusNodeWindow);
  1975. focusNodeWindow.grabMode = Utils.grabMode(grabOp);
  1976. if (
  1977. focusNodeWindow.grabMode === GRAB_TYPES.MOVING &&
  1978. focusNodeWindow.mode === WINDOW_MODES.TILE
  1979. ) {
  1980. this.freezeRender();
  1981. focusNodeWindow.mode = WINDOW_MODES.GRAB_TILE;
  1982. }
  1983. focusNodeWindow.initGrabOp = grabOp;
  1984. focusNodeWindow.initRect = Utils.removeGapOnRect(frameRect, gaps);
  1985. }
  1986. }
  1987. _handleGrabOpEnd(_display, _metaWindow, grabOp) {
  1988. this.unfreezeRender();
  1989. let focusMetaWindow = this.focusMetaWindow;
  1990. if (!focusMetaWindow) return;
  1991. let focusNodeWindow = this.findNodeWindow(focusMetaWindow);
  1992. if (focusNodeWindow && !this.cancelGrab) {
  1993. // WINDOW_BASE is when grabbing the window decoration
  1994. // COMPOSITOR is when something like Overview requesting a grab, especially when Super is pressed.
  1995. if (
  1996. grabOp === Meta.GrabOp.WINDOW_BASE ||
  1997. grabOp === Meta.GrabOp.COMPOSITOR ||
  1998. grabOp === Meta.GrabOp.MOVING_UNCONSTRAINED
  1999. ) {
  2000. if (this.allowDragDropTile()) {
  2001. this.moveWindowToPointer(focusNodeWindow);
  2002. }
  2003. }
  2004. }
  2005. this._grabCleanup(focusNodeWindow);
  2006. if (focusMetaWindow.get_maximized() === 0) {
  2007. this.renderTree("grab-op-end");
  2008. }
  2009. this.updateStackedFocus(focusNodeWindow);
  2010. this.updateTabbedFocus(focusNodeWindow);
  2011. this.nodeWinAtPointer = null;
  2012. }
  2013. _grabCleanup(focusNodeWindow) {
  2014. this.cancelGrab = false;
  2015. if (!focusNodeWindow) return;
  2016. focusNodeWindow.initRect = null;
  2017. focusNodeWindow.grabMode = null;
  2018. focusNodeWindow.initGrabOp = null;
  2019. if (focusNodeWindow.previewHint) {
  2020. focusNodeWindow.previewHint.hide();
  2021. global.window_group.remove_child(focusNodeWindow.previewHint);
  2022. focusNodeWindow.previewHint.destroy();
  2023. focusNodeWindow.previewHint = null;
  2024. }
  2025. if (focusNodeWindow.mode === WINDOW_MODES.GRAB_TILE) {
  2026. focusNodeWindow.mode = WINDOW_MODES.TILE;
  2027. }
  2028. }
  2029. allowDragDropTile() {
  2030. return this.kbd.allowDragDropTile();
  2031. }
  2032. _handleResizing(focusNodeWindow) {
  2033. if (!focusNodeWindow || focusNodeWindow.isFloat()) return;
  2034. let grabOp = this.grabOp;
  2035. let initGrabOp = focusNodeWindow.initGrabOp;
  2036. let direction = Utils.directionFromGrab(grabOp);
  2037. let orientation = Utils.orientationFromGrab(grabOp);
  2038. let parentNodeForFocus = focusNodeWindow.parentNode;
  2039. let position = Utils.positionFromGrabOp(grabOp);
  2040. // normalize the rect without gaps
  2041. let frameRect = this.focusMetaWindow.get_frame_rect();
  2042. let gaps = this.calculateGaps(focusNodeWindow);
  2043. let currentRect = Utils.removeGapOnRect(frameRect, gaps);
  2044. let firstRect;
  2045. let secondRect;
  2046. let parentRect;
  2047. let resizePairForWindow;
  2048. if (initGrabOp === Meta.GrabOp.RESIZING_UNKNOWN) {
  2049. // the direction is null so do not process yet below.
  2050. return;
  2051. } else {
  2052. resizePairForWindow = this.tree.nextVisible(focusNodeWindow, direction);
  2053. }
  2054. let sameParent = resizePairForWindow
  2055. ? resizePairForWindow.parentNode === focusNodeWindow.parentNode
  2056. : false;
  2057. if (orientation === ORIENTATION_TYPES.HORIZONTAL) {
  2058. if (sameParent) {
  2059. // use the window or con pairs
  2060. if (this.tree.getTiledChildren(parentNodeForFocus.childNodes).length <= 1) {
  2061. return;
  2062. }
  2063. firstRect = focusNodeWindow.initRect;
  2064. if (resizePairForWindow) {
  2065. if (
  2066. !this.floatingWindow(resizePairForWindow) &&
  2067. !this.minimizedWindow(resizePairForWindow)
  2068. ) {
  2069. secondRect = resizePairForWindow.rect;
  2070. } else {
  2071. // TODO try to get the next resize pair?
  2072. }
  2073. }
  2074. if (!firstRect || !secondRect) {
  2075. return;
  2076. }
  2077. parentRect = parentNodeForFocus.rect;
  2078. let changePx = currentRect.width - firstRect.width;
  2079. let firstPercent = (firstRect.width + changePx) / parentRect.width;
  2080. let secondPercent = (secondRect.width - changePx) / parentRect.width;
  2081. focusNodeWindow.percent = firstPercent;
  2082. resizePairForWindow.percent = secondPercent;
  2083. } else {
  2084. // use the parent pairs (con to another con or window)
  2085. if (resizePairForWindow && resizePairForWindow.parentNode) {
  2086. if (this.tree.getTiledChildren(resizePairForWindow.parentNode.childNodes).length <= 1) {
  2087. return;
  2088. }
  2089. let firstWindowRect = focusNodeWindow.initRect;
  2090. let index = resizePairForWindow.index;
  2091. if (position === POSITION.BEFORE) {
  2092. // Find the opposite node
  2093. index = index + 1;
  2094. } else {
  2095. index = index - 1;
  2096. }
  2097. parentNodeForFocus = resizePairForWindow.parentNode.childNodes[index];
  2098. firstRect = parentNodeForFocus.rect;
  2099. secondRect = resizePairForWindow.rect;
  2100. if (!firstRect || !secondRect) {
  2101. return;
  2102. }
  2103. parentRect = parentNodeForFocus.parentNode.rect;
  2104. let changePx = currentRect.width - firstWindowRect.width;
  2105. let firstPercent = (firstRect.width + changePx) / parentRect.width;
  2106. let secondPercent = (secondRect.width - changePx) / parentRect.width;
  2107. parentNodeForFocus.percent = firstPercent;
  2108. resizePairForWindow.percent = secondPercent;
  2109. }
  2110. }
  2111. } else if (orientation === ORIENTATION_TYPES.VERTICAL) {
  2112. if (sameParent) {
  2113. // use the window or con pairs
  2114. if (this.tree.getTiledChildren(parentNodeForFocus.childNodes).length <= 1) {
  2115. return;
  2116. }
  2117. firstRect = focusNodeWindow.initRect;
  2118. if (resizePairForWindow) {
  2119. if (
  2120. !this.floatingWindow(resizePairForWindow) &&
  2121. !this.minimizedWindow(resizePairForWindow)
  2122. ) {
  2123. secondRect = resizePairForWindow.rect;
  2124. } else {
  2125. // TODO try to get the next resize pair?
  2126. }
  2127. }
  2128. if (!firstRect || !secondRect) {
  2129. return;
  2130. }
  2131. parentRect = parentNodeForFocus.rect;
  2132. let changePx = currentRect.height - firstRect.height;
  2133. let firstPercent = (firstRect.height + changePx) / parentRect.height;
  2134. let secondPercent = (secondRect.height - changePx) / parentRect.height;
  2135. focusNodeWindow.percent = firstPercent;
  2136. resizePairForWindow.percent = secondPercent;
  2137. } else {
  2138. // use the parent pairs (con to another con or window)
  2139. if (resizePairForWindow && resizePairForWindow.parentNode) {
  2140. if (this.tree.getTiledChildren(resizePairForWindow.parentNode.childNodes).length <= 1) {
  2141. return;
  2142. }
  2143. let firstWindowRect = focusNodeWindow.initRect;
  2144. let index = resizePairForWindow.index;
  2145. if (position === POSITION.BEFORE) {
  2146. // Find the opposite node
  2147. index = index + 1;
  2148. } else {
  2149. index = index - 1;
  2150. }
  2151. parentNodeForFocus = resizePairForWindow.parentNode.childNodes[index];
  2152. firstRect = parentNodeForFocus.rect;
  2153. secondRect = resizePairForWindow.rect;
  2154. if (!firstRect || !secondRect) {
  2155. return;
  2156. }
  2157. parentRect = parentNodeForFocus.parentNode.rect;
  2158. let changePx = currentRect.height - firstWindowRect.height;
  2159. let firstPercent = (firstRect.height + changePx) / parentRect.height;
  2160. let secondPercent = (secondRect.height - changePx) / parentRect.height;
  2161. parentNodeForFocus.percent = firstPercent;
  2162. resizePairForWindow.percent = secondPercent;
  2163. }
  2164. }
  2165. }
  2166. }
  2167. _handleMoving(focusNodeWindow) {
  2168. if (!focusNodeWindow || focusNodeWindow.mode !== WINDOW_MODES.GRAB_TILE) return;
  2169. const nodeWinAtPointer = this.findNodeWindowAtPointer(focusNodeWindow);
  2170. this.nodeWinAtPointer = nodeWinAtPointer;
  2171. const hidePreview = () => {
  2172. if (focusNodeWindow.previewHint) {
  2173. focusNodeWindow.previewHint.hide();
  2174. }
  2175. };
  2176. if (nodeWinAtPointer) {
  2177. if (!focusNodeWindow.previewHint) {
  2178. let previewHint = new St.Bin();
  2179. global.window_group.add_child(previewHint);
  2180. focusNodeWindow.previewHint = previewHint;
  2181. }
  2182. if (this.allowDragDropTile()) {
  2183. this.moveWindowToPointer(focusNodeWindow, true);
  2184. } else {
  2185. hidePreview();
  2186. }
  2187. } else {
  2188. hidePreview();
  2189. }
  2190. }
  2191. isFloatingExempt(metaWindow) {
  2192. if (!metaWindow) return true;
  2193. let windowTitle = metaWindow.get_title();
  2194. let windowType = metaWindow.get_window_type();
  2195. let floatByType =
  2196. windowType === Meta.WindowType.DIALOG ||
  2197. windowType === Meta.WindowType.MODAL_DIALOG ||
  2198. metaWindow.get_transient_for() !== null ||
  2199. metaWindow.get_wm_class() === null ||
  2200. windowTitle === null ||
  2201. windowTitle === "" ||
  2202. windowTitle.length === 0 ||
  2203. !metaWindow.allows_resize();
  2204. const knownFloats = this.windowProps.overrides.filter((wprop) => wprop.mode === "float");
  2205. let floatOverride =
  2206. knownFloats.filter((kf) => {
  2207. let matchTitle = false;
  2208. let matchClass = false;
  2209. if (kf.wmTitle) {
  2210. if (kf.wmTitle === " ") {
  2211. matchTitle = kf.wmTitle === windowTitle;
  2212. } else {
  2213. let titles = kf.wmTitle.split(",");
  2214. matchTitle = titles.filter((t) => {
  2215. if (windowTitle) {
  2216. if (t.startsWith('!')) {
  2217. return !windowTitle.includes(t.slice(1))
  2218. } else {
  2219. return windowTitle.includes(t)
  2220. }
  2221. }
  2222. return false
  2223. }).length > 0;
  2224. }
  2225. }
  2226. if (kf.wmClass) {
  2227. matchClass = kf.wmClass.includes(metaWindow.get_wm_class());
  2228. }
  2229. if (kf.wmClass && kf.wmTitle) {
  2230. return matchTitle && matchClass;
  2231. } else {
  2232. if (kf.wmTitle) {
  2233. return matchTitle;
  2234. } else {
  2235. return matchClass;
  2236. }
  2237. }
  2238. }).length > 0;
  2239. return floatByType || floatOverride;
  2240. }
  2241. _getDragDropCenterPreviewStyle() {
  2242. const centerLayout = this.ext.settings.get_string("dnd-center-layout");
  2243. return `window-tilepreview-${centerLayout}`;
  2244. }
  2245. get currentMonWsNode() {
  2246. const monWs = this.currentMonWs;
  2247. if (monWs) {
  2248. return this.tree.findNode(monWs);
  2249. }
  2250. return null;
  2251. }
  2252. get currentWsNode() {
  2253. const ws = this.currentWs;
  2254. if (ws) {
  2255. return this.tree.findNode(ws);
  2256. }
  2257. return null;
  2258. }
  2259. get currentMonWs() {
  2260. const monWs = `${this.currentMon}${this.currentWs}`;
  2261. return monWs;
  2262. }
  2263. get currentWs() {
  2264. const display = global.display;
  2265. const wsMgr = display.get_workspace_manager();
  2266. return `ws${wsMgr.get_active_workspace_index()}`;
  2267. }
  2268. get currentMon() {
  2269. const display = global.display;
  2270. return `mo${display.get_current_monitor()}`;
  2271. }
  2272. floatAllWindows() {
  2273. this.tree.getNodeByType(NODE_TYPES.WINDOW).forEach((w) => {
  2274. if (w.isFloat()) {
  2275. w.prevFloat = true;
  2276. }
  2277. w.mode = WINDOW_MODES.FLOAT;
  2278. });
  2279. }
  2280. unfloatAllWindows() {
  2281. this.tree.getNodeByType(NODE_TYPES.WINDOW).forEach((w) => {
  2282. if (!w.prevFloat) {
  2283. w.mode = WINDOW_MODES.TILE;
  2284. } else {
  2285. // Reset the float marker
  2286. w.prevFloat = false;
  2287. }
  2288. });
  2289. }
  2290. }