workspace: Make computeLayout() return a layout object

Start cleaning up the whole mess around the layout object a bit and
return a new object in the LayoutStrategies computeLayout()
implementation. This object is supposed to be opaque to the API user and
will only be passed to the layout strategy.

For now, keep setting a few things on that object from outside, we'll
clean that up later.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1617>
This commit is contained in:
Jonas Dreßler 2021-02-01 11:07:33 +01:00 committed by Marge Bot
parent ac7dc62da6
commit 8d9a92fe98

View File

@ -114,16 +114,11 @@ var LayoutStrategy = class {
this._columnSpacing = params.columnSpacing; this._columnSpacing = params.columnSpacing;
} }
// Compute strategy-specific window slots for each window in // Compute a strategy-specific overall layout given a list of WindowPreviews
// @windows, given the @layout. The strategy may also use @layout // @windows and the strategy-specific @layoutParams.
// as strategy-specific storage.
// //
// This must calculate: // Returns a strategy-specific layout object that is opaque to the user.
// * maxColumns - The maximum number of columns used by the layout. computeLayout(_windows, _layoutParams) {
// * gridWidth - The total width used by the grid, unscaled, unspaced.
// * gridHeight - The totial height used by the grid, unscaled, unspaced.
// * rows - A list of rows, which should be instantiated by _newRow.
computeLayout(_windows, _layout) {
throw new GObject.NotImplementedError(`computeLayout in ${this.constructor.name}`); throw new GObject.NotImplementedError(`computeLayout in ${this.constructor.name}`);
} }
@ -210,8 +205,15 @@ var UnalignedLayoutStrategy = class extends LayoutStrategy {
row.windows.sort((a, b) => a.windowCenter.x - b.windowCenter.x); row.windows.sort((a, b) => a.windowCenter.x - b.windowCenter.x);
} }
computeLayout(windows, layout) { computeLayout(windows, layoutParams) {
let numRows = layout.numRows; layoutParams = Params.parse(layoutParams, {
numRows: 0,
});
if (layoutParams.numRows === 0)
throw new Error(`${this.constructor.name}: No numRows given in layout params`);
const numRows = layoutParams.numRows;
let rows = []; let rows = [];
let totalWidth = 0; let totalWidth = 0;
@ -261,10 +263,13 @@ var UnalignedLayoutStrategy = class extends LayoutStrategy {
gridHeight += row.fullHeight; gridHeight += row.fullHeight;
} }
layout.rows = rows; return {
layout.maxColumns = maxRow.windows.length; numRows,
layout.gridWidth = maxRow.fullWidth; rows,
layout.gridHeight = gridHeight; maxColumns: maxRow.windows.length,
gridWidth: maxRow.fullWidth,
gridHeight,
};
} }
computeScaleAndSpace(layout) { computeScaleAndSpace(layout) {
@ -491,25 +496,31 @@ var WorkspaceLayout = GObject.registerClass({
columnSpacing, columnSpacing,
}); });
let lastLayout = {}; let lastLayout = null;
let lastNumColumns = -1;
for (let numRows = 1; ; numRows++) { for (let numRows = 1; ; numRows++) {
let numColumns = Math.ceil(this._sortedWindows.length / numRows); const numColumns = Math.ceil(this._sortedWindows.length / numRows);
// If adding a new row does not change column count just stop // If adding a new row does not change column count just stop
// (for instance: 9 windows, with 3 rows -> 3 columns, 4 rows -> // (for instance: 9 windows, with 3 rows -> 3 columns, 4 rows ->
// 3 columns as well => just use 3 rows then) // 3 columns as well => just use 3 rows then)
if (numColumns === lastLayout.numColumns) if (numColumns === lastNumColumns)
break; break;
let layout = { area, strategy, numRows, numColumns }; const layout = strategy.computeLayout(this._sortedWindows, {
strategy.computeLayout(this._sortedWindows, layout); numRows,
});
layout.area = area;
layout.strategy = strategy;
strategy.computeScaleAndSpace(layout); strategy.computeScaleAndSpace(layout);
if (!this._isBetterLayout(lastLayout, layout)) if (!this._isBetterLayout(lastLayout, layout))
break; break;
lastLayout = layout; lastLayout = layout;
lastNumColumns = numColumns;
} }
return lastLayout; return lastLayout;