| 1 |
// SPDX-FileCopyrightText: 2011 Giovanni Campagna <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2011 Stefano Facchini <[email protected]> |
| 3 |
// SPDX-FileCopyrightText: 2011 Wepmaschda <[email protected]> |
| 4 |
// SPDX-FileCopyrightText: 2015 Florian Müllner <[email protected]> |
| 5 |
// |
| 6 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 7 |
|
| 8 |
import Clutter from 'gi://Clutter'; |
| 9 |
|
| 10 |
import {Extension, InjectionManager} from 'resource:///org/gnome/shell/extensions/extension.js'; |
| 11 |
|
| 12 |
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; |
| 13 |
import {WindowPreview} from 'resource:///org/gnome/shell/ui/windowPreview.js'; |
| 14 |
import * as Workspace from 'resource:///org/gnome/shell/ui/workspace.js'; |
| 15 |
|
| 16 |
// testing settings for natural window placement strategy: |
| 17 |
const WINDOW_PLACEMENT_NATURAL_ACCURACY = 20; // accuracy of window translate moves (KDE-default: 20) |
| 18 |
const WINDOW_PLACEMENT_NATURAL_GAPS = 5; // half of the minimum gap between windows |
| 19 |
const WINDOW_PLACEMENT_NATURAL_MAX_TRANSLATIONS = 5000; // safety limit for preventing endless loop if something is wrong in the algorithm |
| 20 |
|
| 21 |
class Rect { |
| 22 |
constructor(x, y, width, height) { |
| 23 |
[this.x, this.y, this.width, this.height] = [x, y, width, height]; |
| 24 |
} |
| 25 |
|
| 26 |
// used in _calculateWindowTransformationsNatural to replace Meta.Rectangle that is too slow. |
| 27 |
copy() { |
| 28 |
return new Rect(this.x, this.y, this.width, this.height); |
| 29 |
} |
| 30 |
|
| 31 |
union(rect2) { |
| 32 |
const dest = this.copy(); |
| 33 |
if (rect2.x < dest.x) { |
| 34 |
dest.width += dest.x - rect2.x; |
| 35 |
dest.x = rect2.x; |
| 36 |
} |
| 37 |
if (rect2.y < dest.y) { |
| 38 |
dest.height += dest.y - rect2.y; |
| 39 |
dest.y = rect2.y; |
| 40 |
} |
| 41 |
if (rect2.x + rect2.width > dest.x + dest.width) |
| 42 |
dest.width = rect2.x + rect2.width - dest.x; |
| 43 |
if (rect2.y + rect2.height > dest.y + dest.height) |
| 44 |
dest.height = rect2.y + rect2.height - dest.y; |
| 45 |
|
| 46 |
return dest; |
| 47 |
} |
| 48 |
|
| 49 |
adjusted(dx, dy, dx2, dy2) { |
| 50 |
const dest = this.copy(); |
| 51 |
dest.x += dx; |
| 52 |
dest.y += dy; |
| 53 |
dest.width += -dx + dx2; |
| 54 |
dest.height += -dy + dy2; |
| 55 |
return dest; |
| 56 |
} |
| 57 |
|
| 58 |
overlap(rect2) { |
| 59 |
return !(this.x + this.width <= rect2.x || |
| 60 |
rect2.x + rect2.width <= this.x || |
| 61 |
this.y + this.height <= rect2.y || |
| 62 |
rect2.y + rect2.height <= this.y); |
| 63 |
} |
| 64 |
|
| 65 |
center() { |
| 66 |
return [this.x + this.width / 2, this.y + this.height / 2]; |
| 67 |
} |
| 68 |
|
| 69 |
translate(dx, dy) { |
| 70 |
this.x += dx; |
| 71 |
this.y += dy; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
class NaturalLayoutStrategy extends Workspace.LayoutStrategy { |
| 76 |
constructor(params, settings) { |
| 77 |
super(params); |
| 78 |
this._settings = settings; |
| 79 |
} |
| 80 |
|
| 81 |
computeLayout(windows, _params) { |
| 82 |
return { |
| 83 |
windows, |
| 84 |
}; |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
* Returns clones with matching target coordinates and scales to arrange windows in a natural way that no overlap exists and relative window size is preserved. |
| 89 |
* This function is almost a 1:1 copy of the function |
| 90 |
* PresentWindowsEffect::calculateWindowTransformationsNatural() from KDE, see: |
| 91 |
* https://projects.kde.org/projects/kde/kdebase/kde-workspace/repository/revisions/master/entry/kwin/effects/presentwindows/presentwindows.cpp |
| 92 |
*/ |
| 93 |
computeWindowSlots(layout, area) { |
| 94 |
// As we are using pseudo-random movement (See "slot") we need to make sure the list |
| 95 |
// is always sorted the same way no matter which window is currently active. |
| 96 |
|
| 97 |
const areaRect = new Rect(area.x, area.y, area.width, area.height); |
| 98 |
let bounds = areaRect.copy(); |
| 99 |
const clones = layout.windows; |
| 100 |
|
| 101 |
let direction = 0; |
| 102 |
const directions = []; |
| 103 |
const rects = []; |
| 104 |
for (let i = 0; i < clones.length; i++) { |
| 105 |
// save rectangles into 4-dimensional arrays representing two corners of the rectangular: [left_x, top_y, right_x, bottom_y] |
| 106 |
const rect = clones[i].boundingBox; |
| 107 |
rects[i] = new Rect(rect.x, rect.y, rect.width, rect.height); |
| 108 |
bounds = bounds.union(rects[i]); |
| 109 |
|
| 110 |
// This is used when the window is on the edge of the screen to try to use as much screen real estate as possible. |
| 111 |
directions[i] = direction; |
| 112 |
direction++; |
| 113 |
if (direction === 4) |
| 114 |
direction = 0; |
| 115 |
} |
| 116 |
|
| 117 |
let loopCounter = 0; |
| 118 |
let overlap; |
| 119 |
do { |
| 120 |
overlap = false; |
| 121 |
for (let i = 0; i < rects.length; i++) { |
| 122 |
for (let j = 0; j < rects.length; j++) { |
| 123 |
const adjustments = [-1, -1, 1, 1] |
| 124 |
.map(v => (v *= WINDOW_PLACEMENT_NATURAL_GAPS)); |
| 125 |
const iAdjusted = rects[i].adjusted(...adjustments); |
| 126 |
const jAdjusted = rects[j].adjusted(...adjustments); |
| 127 |
if (i !== j && iAdjusted.overlap(jAdjusted)) { |
| 128 |
loopCounter++; |
| 129 |
overlap = true; |
| 130 |
|
| 131 |
// TODO: something like a Point2D would be nicer here: |
| 132 |
|
| 133 |
// Determine pushing direction |
| 134 |
let iCenter = rects[i].center(); |
| 135 |
const jCenter = rects[j].center(); |
| 136 |
const diff = [jCenter[0] - iCenter[0], jCenter[1] - iCenter[1]]; |
| 137 |
|
| 138 |
// Prevent dividing by zero and non-movement |
| 139 |
if (diff[0] === 0 && diff[1] === 0) |
| 140 |
diff[0] = 1; |
| 141 |
// Try to keep screen/workspace aspect ratio |
| 142 |
if (bounds.height / bounds.width > areaRect.height / areaRect.width) |
| 143 |
diff[0] *= 2; |
| 144 |
else |
| 145 |
diff[1] *= 2; |
| 146 |
|
| 147 |
// Approximate a vector of between 10px and 20px in magnitude in the same direction |
| 148 |
let length = Math.sqrt(diff[0] * diff[0] + diff[1] * diff[1]); |
| 149 |
diff[0] = diff[0] * WINDOW_PLACEMENT_NATURAL_ACCURACY / length; |
| 150 |
diff[1] = diff[1] * WINDOW_PLACEMENT_NATURAL_ACCURACY / length; |
| 151 |
|
| 152 |
// Move both windows apart |
| 153 |
rects[i].translate(-diff[0], -diff[1]); |
| 154 |
rects[j].translate(diff[0], diff[1]); |
| 155 |
|
| 156 |
|
| 157 |
if (this._settings.get_boolean('use-more-screen')) { |
| 158 |
// Try to keep the bounding rect the same aspect as the screen so that more |
| 159 |
// screen real estate is utilised. We do this by splitting the screen into nine |
| 160 |
// equal sections, if the window center is in any of the corner sections pull the |
| 161 |
// window towards the outer corner. If it is in any of the other edge sections |
| 162 |
// alternate between each corner on that edge. We don't want to determine it |
| 163 |
// randomly as it will not produce consistant locations when using the filter. |
| 164 |
// Only move one window so we don't cause large amounts of unnecessary zooming |
| 165 |
// in some situations. We need to do this even when expanding later just in case |
| 166 |
// all windows are the same size. |
| 167 |
// (We are using an old bounding rect for this, hopefully it doesn't matter) |
| 168 |
let xSection = Math.round((rects[i].x - bounds.x) / (bounds.width / 3)); |
| 169 |
let ySection = Math.round((rects[i].y - bounds.y) / (bounds.height / 3)); |
| 170 |
|
| 171 |
iCenter = rects[i].center(); |
| 172 |
diff[0] = 0; |
| 173 |
diff[1] = 0; |
| 174 |
if (xSection !== 1 || ySection !== 1) { // Remove this if you want the center to pull as well |
| 175 |
if (xSection === 1) |
| 176 |
xSection = directions[i] / 2 ? 2 : 0; |
| 177 |
if (ySection === 1) |
| 178 |
ySection = directions[i] % 2 ? 2 : 0; |
| 179 |
} |
| 180 |
if (xSection === 0 && ySection === 0) { |
| 181 |
diff[0] = bounds.x - iCenter[0]; |
| 182 |
diff[1] = bounds.y - iCenter[1]; |
| 183 |
} |
| 184 |
if (xSection === 2 && ySection === 0) { |
| 185 |
diff[0] = bounds.x + bounds.width - iCenter[0]; |
| 186 |
diff[1] = bounds.y - iCenter[1]; |
| 187 |
} |
| 188 |
if (xSection === 2 && ySection === 2) { |
| 189 |
diff[0] = bounds.x + bounds.width - iCenter[0]; |
| 190 |
diff[1] = bounds.y + bounds.height - iCenter[1]; |
| 191 |
} |
| 192 |
if (xSection === 0 && ySection === 2) { |
| 193 |
diff[0] = bounds.x - iCenter[0]; |
| 194 |
diff[1] = bounds.y + bounds.height - iCenter[1]; |
| 195 |
} |
| 196 |
if (diff[0] !== 0 || diff[1] !== 0) { |
| 197 |
length = Math.sqrt(diff[0] * diff[0] + diff[1] * diff[1]); |
| 198 |
diff[0] *= WINDOW_PLACEMENT_NATURAL_ACCURACY / length / 2; // /2 to make it less influencing than the normal center-move above |
| 199 |
diff[1] *= WINDOW_PLACEMENT_NATURAL_ACCURACY / length / 2; |
| 200 |
rects[i].translate(diff[0], diff[1]); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// Update bounding rect |
| 205 |
bounds = bounds.union(rects[i]); |
| 206 |
bounds = bounds.union(rects[j]); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} while (overlap && loopCounter < WINDOW_PLACEMENT_NATURAL_MAX_TRANSLATIONS); |
| 211 |
|
| 212 |
// Work out scaling by getting the most top-left and most bottom-right window coords. |
| 213 |
const scale = Math.min( |
| 214 |
areaRect.width / bounds.width, |
| 215 |
areaRect.height / bounds.height, |
| 216 |
1.0); |
| 217 |
|
| 218 |
// Make bounding rect fill the screen size for later steps |
| 219 |
bounds.x -= (areaRect.width - bounds.width * scale) / 2; |
| 220 |
bounds.y -= (areaRect.height - bounds.height * scale) / 2; |
| 221 |
bounds.width = areaRect.width / scale; |
| 222 |
bounds.height = areaRect.height / scale; |
| 223 |
|
| 224 |
// Move all windows back onto the screen and set their scale |
| 225 |
for (let i = 0; i < rects.length; i++) |
| 226 |
rects[i].translate(-bounds.x, -bounds.y); |
| 227 |
|
| 228 |
|
| 229 |
// rescale to workspace |
| 230 |
const slots = []; |
| 231 |
for (let i = 0; i < rects.length; i++) { |
| 232 |
rects[i].x = rects[i].x * scale + areaRect.x; |
| 233 |
rects[i].y = rects[i].y * scale + areaRect.y; |
| 234 |
rects[i].width *= scale; |
| 235 |
rects[i].height *= scale; |
| 236 |
|
| 237 |
slots.push([rects[i].x, rects[i].y, rects[i].width, rects[i].height, clones[i]]); |
| 238 |
} |
| 239 |
|
| 240 |
return slots; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
export default class NativeWindowPlacementExtension extends Extension { |
| 245 |
enable() { |
| 246 |
const settings = this.getSettings(); |
| 247 |
|
| 248 |
const layoutProto = Workspace.WorkspaceLayout.prototype; |
| 249 |
const previewProto = WindowPreview.prototype; |
| 250 |
|
| 251 |
this._injectionManager = new InjectionManager(); |
| 252 |
this._injectionManager.overrideMethod(layoutProto, '_createBestLayout', () => { |
| 253 |
/* eslint-disable no-invalid-this */ |
| 254 |
return function () { |
| 255 |
this._layoutStrategy = new NaturalLayoutStrategy({ |
| 256 |
monitor: Main.layoutManager.monitors[this._monitorIndex], |
| 257 |
}, settings); |
| 258 |
return this._layoutStrategy.computeLayout(this._sortedWindows); |
| 259 |
}; |
| 260 |
/* eslint-enable no-invalid-this */ |
| 261 |
}); |
| 262 |
|
| 263 |
// position window titles on top of windows in overlay |
| 264 |
this._injectionManager.overrideMethod(previewProto, '_init', originalMethod => { |
| 265 |
/* eslint-disable no-invalid-this */ |
| 266 |
return function (...args) { |
| 267 |
originalMethod.call(this, ...args); |
| 268 |
|
| 269 |
if (!settings.get_boolean('window-captions-on-top')) |
| 270 |
return; |
| 271 |
|
| 272 |
const alignConstraint = this._title.get_constraints().find( |
| 273 |
c => c.align_axis && c.align_axis === Clutter.AlignAxis.Y_AXIS); |
| 274 |
alignConstraint.factor = 0; |
| 275 |
|
| 276 |
const bindConstraint = this._title.get_constraints().find( |
| 277 |
c => c.coordinate && c.coordinate === Clutter.BindCoordinate.Y); |
| 278 |
bindConstraint.offset = 0; |
| 279 |
}; |
| 280 |
/* eslint-enable no-invalid-this */ |
| 281 |
}); |
| 282 |
|
| 283 |
this._injectionManager.overrideMethod(previewProto, '_adjustOverlayOffsets', originalMethod => { |
| 284 |
/* eslint-disable no-invalid-this */ |
| 285 |
return function (...args) { |
| 286 |
originalMethod.call(this, ...args); |
| 287 |
|
| 288 |
if (settings.get_boolean('window-captions-on-top')) |
| 289 |
this._title.translation_y = -this._title.translation_y; |
| 290 |
}; |
| 291 |
/* eslint-enable no-invalid-this */ |
| 292 |
}); |
| 293 |
} |
| 294 |
|
| 295 |
disable() { |
| 296 |
this._injectionManager.clear(); |
| 297 |
this._injectionManager = null; |
| 298 |
|
| 299 |
global.stage.queue_relayout(); |
| 300 |
} |
| 301 |
} |