workspace: Refactor layout scale and space calculation
Make computeScaleAndSpace() return an array including scale and space so we no longer have to access the layout object from outside. With this we also no longer need to set layout.space, since only the scale property is needed in computeWindowSlots(). Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1617>
This commit is contained in:
parent
8d9a92fe98
commit
39e844ac0c
@ -122,14 +122,15 @@ var LayoutStrategy = class {
|
|||||||
throw new GObject.NotImplementedError(`computeLayout in ${this.constructor.name}`);
|
throw new GObject.NotImplementedError(`computeLayout in ${this.constructor.name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given @layout, compute the overall scale and space of the layout.
|
// Given @layout and @area, compute the overall scale of the layout and
|
||||||
|
// space occupied by the layout.
|
||||||
//
|
//
|
||||||
// This method does not return anything, but instead installs
|
// This method returns an array where the first element is the scale and
|
||||||
// the properties "scale" and "space" on @layout directly.
|
// the second element is the space.
|
||||||
//
|
//
|
||||||
// Make sure to call this methods before calling computeWindowSlots(),
|
// This method must be called before calling computeWindowSlots(), as it
|
||||||
// as it depends on the scale property installed in @layout here.
|
// sets the fixed overall scale of the layout.
|
||||||
computeScaleAndSpace(_layout) {
|
computeScaleAndSpace(_layout, _area) {
|
||||||
throw new GObject.NotImplementedError(`computeScaleAndSpace in ${this.constructor.name}`);
|
throw new GObject.NotImplementedError(`computeScaleAndSpace in ${this.constructor.name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,9 +273,7 @@ var UnalignedLayoutStrategy = class extends LayoutStrategy {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
computeScaleAndSpace(layout) {
|
computeScaleAndSpace(layout, area) {
|
||||||
let area = layout.area;
|
|
||||||
|
|
||||||
let hspacing = (layout.maxColumns - 1) * this._columnSpacing;
|
let hspacing = (layout.maxColumns - 1) * this._columnSpacing;
|
||||||
let vspacing = (layout.numRows - 1) * this._rowSpacing;
|
let vspacing = (layout.numRows - 1) * this._rowSpacing;
|
||||||
|
|
||||||
@ -293,7 +292,8 @@ var UnalignedLayoutStrategy = class extends LayoutStrategy {
|
|||||||
let space = (scaledLayoutWidth * scaledLayoutHeight) / (area.width * area.height);
|
let space = (scaledLayoutWidth * scaledLayoutHeight) / (area.width * area.height);
|
||||||
|
|
||||||
layout.scale = scale;
|
layout.scale = scale;
|
||||||
layout.space = space;
|
|
||||||
|
return [scale, space];
|
||||||
}
|
}
|
||||||
|
|
||||||
computeWindowSlots(layout, area) {
|
computeWindowSlots(layout, area) {
|
||||||
@ -436,20 +436,17 @@ var WorkspaceLayout = GObject.registerClass({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_isBetterLayout(oldLayout, newLayout) {
|
_isBetterScaleAndSpace(oldScale, oldSpace, scale, space) {
|
||||||
if (oldLayout.scale === undefined)
|
let spacePower = (space - oldSpace) * LAYOUT_SPACE_WEIGHT;
|
||||||
return true;
|
let scalePower = (scale - oldScale) * LAYOUT_SCALE_WEIGHT;
|
||||||
|
|
||||||
let spacePower = (newLayout.space - oldLayout.space) * LAYOUT_SPACE_WEIGHT;
|
if (scale > oldScale && space > oldSpace) {
|
||||||
let scalePower = (newLayout.scale - oldLayout.scale) * LAYOUT_SCALE_WEIGHT;
|
|
||||||
|
|
||||||
if (newLayout.scale > oldLayout.scale && newLayout.space > oldLayout.space) {
|
|
||||||
// Win win -- better scale and better space
|
// Win win -- better scale and better space
|
||||||
return true;
|
return true;
|
||||||
} else if (newLayout.scale > oldLayout.scale && newLayout.space <= oldLayout.space) {
|
} else if (scale > oldScale && space <= oldSpace) {
|
||||||
// Keep new layout only if scale gain outweighs aspect space loss
|
// Keep new layout only if scale gain outweighs aspect space loss
|
||||||
return scalePower > spacePower;
|
return scalePower > spacePower;
|
||||||
} else if (newLayout.scale <= oldLayout.scale && newLayout.space > oldLayout.space) {
|
} else if (scale <= oldScale && space > oldSpace) {
|
||||||
// Keep new layout only if aspect space gain outweighs scale loss
|
// Keep new layout only if aspect space gain outweighs scale loss
|
||||||
return spacePower > scalePower;
|
return spacePower > scalePower;
|
||||||
} else {
|
} else {
|
||||||
@ -498,6 +495,8 @@ var WorkspaceLayout = GObject.registerClass({
|
|||||||
|
|
||||||
let lastLayout = null;
|
let lastLayout = null;
|
||||||
let lastNumColumns = -1;
|
let lastNumColumns = -1;
|
||||||
|
let lastScale = 0;
|
||||||
|
let lastSpace = 0;
|
||||||
|
|
||||||
for (let numRows = 1; ; numRows++) {
|
for (let numRows = 1; ; numRows++) {
|
||||||
const numColumns = Math.ceil(this._sortedWindows.length / numRows);
|
const numColumns = Math.ceil(this._sortedWindows.length / numRows);
|
||||||
@ -511,16 +510,17 @@ var WorkspaceLayout = GObject.registerClass({
|
|||||||
const layout = strategy.computeLayout(this._sortedWindows, {
|
const layout = strategy.computeLayout(this._sortedWindows, {
|
||||||
numRows,
|
numRows,
|
||||||
});
|
});
|
||||||
layout.area = area;
|
|
||||||
layout.strategy = strategy;
|
layout.strategy = strategy;
|
||||||
|
|
||||||
strategy.computeScaleAndSpace(layout);
|
const [scale, space] = strategy.computeScaleAndSpace(layout, area);
|
||||||
|
|
||||||
if (!this._isBetterLayout(lastLayout, layout))
|
if (!this._isBetterScaleAndSpace(lastScale, lastSpace, scale, space))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
lastLayout = layout;
|
lastLayout = layout;
|
||||||
lastNumColumns = numColumns;
|
lastNumColumns = numColumns;
|
||||||
|
lastScale = scale;
|
||||||
|
lastSpace = space;
|
||||||
}
|
}
|
||||||
|
|
||||||
return lastLayout;
|
return lastLayout;
|
||||||
|
Loading…
Reference in New Issue
Block a user