window.js 88 KB

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