resize.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import GLib from 'gi://GLib';
  2. import {TileWindowManager} from './tileWindowManager.js';
  3. export var resizeSourceId = null;
  4. /** Resize Tile with size of rect in the East
  5. *
  6. * @param {Tile} tile
  7. * @param {Mtk.Rectangle} rect
  8. * @returns
  9. */
  10. export function resizeE(tile, rect) {
  11. if (!tile.adjacents[1]) {
  12. if (resizeSourceId !== null)
  13. GLib.Source.remove(resizeSourceId);
  14. resizeSourceId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
  15. TileWindowManager.getMonitors()[tile.monitor].root?.update();
  16. resizeSourceId = null;
  17. return GLib.SOURCE_REMOVE;
  18. });
  19. return;
  20. }
  21. let p = tile.findParent(el => el.position.x + el.position.width === tile.position.x + tile.position.width);
  22. if (p) {
  23. let pos = p.position;
  24. let diff = rect.width - tile.position.width;
  25. pos.width += diff;
  26. p.resize(pos);
  27. p.update();
  28. let sibling = p.getSibling();
  29. if (sibling) {
  30. let posSib = sibling.position;
  31. posSib.width -= diff;
  32. posSib.x += diff;
  33. sibling.resize(posSib);
  34. sibling.update();
  35. }
  36. if (p.parent && p.parent.child2)
  37. p.parent.position.splitProportion = 0.5 * (p.parent.position.width - p.parent.child2.position.width) / p.parent.child2.position.width;
  38. }
  39. }
  40. /** Resize Tile with size of rect in the West
  41. *
  42. * @param {Tile} tile
  43. * @param {Mtk.Rectangle} rect
  44. * @returns
  45. */
  46. export function resizeW(tile, rect) {
  47. if (!tile.adjacents[0]) {
  48. if (resizeSourceId !== null)
  49. GLib.Source.remove(resizeSourceId);
  50. resizeSourceId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
  51. TileWindowManager.getMonitors()[tile.monitor].root?.update();
  52. resizeSourceId = null;
  53. return GLib.SOURCE_REMOVE;
  54. });
  55. return;
  56. }
  57. let p = tile.findParent(el => el.position.x === tile.position.x);
  58. if (p) {
  59. let pos = p.position;
  60. let diff = rect.width - tile.position.width;
  61. pos.width += diff;
  62. pos.x -= diff;
  63. p.resize(pos);
  64. p.update();
  65. let sibling = p.getSibling();
  66. if (sibling) {
  67. let posSib = sibling.position;
  68. posSib.width -= diff;
  69. sibling.resize(posSib);
  70. sibling.update();
  71. }
  72. if (p.parent && p.parent.child2)
  73. p.parent.position.splitProportion = 0.5 * (p.parent.position.width - p.parent.child2.position.width) / p.parent.child2.position.width;
  74. }
  75. }
  76. /** Resize Tile with size of rect in the South
  77. *
  78. * @param {Tile} tile
  79. * @param {Mtk.Rectangle} rect
  80. * @returns
  81. */
  82. export function resizeS(tile, rect) {
  83. if (!tile.adjacents[3]) {
  84. if (resizeSourceId !== null)
  85. GLib.Source.remove(resizeSourceId);
  86. resizeSourceId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
  87. TileWindowManager.getMonitors()[tile.monitor].root?.update();
  88. resizeSourceId = null;
  89. return GLib.SOURCE_REMOVE;
  90. });
  91. return;
  92. }
  93. let p = tile.findParent(el => el.position.y + el.position.height === tile.position.y + tile.position.height);
  94. if (p) {
  95. let pos = p.position;
  96. let diff = rect.height - pos.height;
  97. pos.height = rect.height;
  98. p.resize(pos);
  99. p.update();
  100. let sibling = p.getSibling();
  101. if (sibling) {
  102. let posSib = sibling.position;
  103. posSib.height -= diff;
  104. posSib.y += diff;
  105. sibling.resize(posSib);
  106. sibling.update();
  107. }
  108. if (p.parent && p.parent.child2)
  109. p.parent.position.splitProportion = 0.5 * (p.parent.position.height - p.parent.child2.position.height) / p.parent.child2.position.height;
  110. }
  111. }
  112. /** Resize Tile with size of rect in the North
  113. *
  114. * @param {Tile} tile
  115. * @param {Mtk.Rectangle} rect
  116. * @returns
  117. */
  118. export function resizeN(tile, rect) {
  119. if (!tile.adjacents[2]) {
  120. if (resizeSourceId !== null)
  121. GLib.Source.remove(resizeSourceId);
  122. resizeSourceId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
  123. TileWindowManager.getMonitors()[tile.monitor].root?.update();
  124. resizeSourceId = null;
  125. return GLib.SOURCE_REMOVE;
  126. });
  127. return;
  128. }
  129. let p = tile.findParent(el => el.position.y === tile.position.y);
  130. if (p) {
  131. let pos = p.position;
  132. let diff = rect.height - pos.height;
  133. pos.height += diff;
  134. pos.y -= diff;
  135. p.resize(pos);
  136. p.update();
  137. let sibling = p.getSibling();
  138. if (sibling) {
  139. let posSib = sibling.position;
  140. posSib.height -= diff;
  141. sibling.resize(posSib);
  142. sibling.update();
  143. }
  144. if (p.parent && p.parent.child2)
  145. p.parent.position.splitProportion = 0.5 * (p.parent.position.height - p.parent.child2.position.height) / p.parent.child2.position.height;
  146. }
  147. }