diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs index 64c49be11e..037df109eb 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs @@ -286,7 +286,11 @@ namespace FancyZonesEditor { _rowsModel.CellChildMap[i, 0] = i; _columnsModel.CellChildMap[0, i] = i; - _rowsModel.RowPercents[i] = _multiplier / ZoneCount; // _columnsModel is sharing the same array + + // Note: This is NOT equal to _multiplier / ZoneCount and is done like this to make + // the sum of all RowPercents exactly (_multiplier). + // _columnsModel is sharing the same array + _rowsModel.RowPercents[i] = ((_multiplier * (i + 1)) / ZoneCount) - ((_multiplier * i) / ZoneCount); } // Update the "Grid" Default Layout @@ -313,14 +317,16 @@ namespace FancyZonesEditor _gridModel.ColumnPercents = new int[cols]; _gridModel.CellChildMap = new int[rows, cols]; + // Note: The following are NOT equal to _multiplier divided by rows or columns and is + // done like this to make the sum of all RowPercents exactly (_multiplier). for (int row = 0; row < rows; row++) { - _gridModel.RowPercents[row] = _multiplier / rows; + _gridModel.RowPercents[row] = ((_multiplier * (row + 1)) / rows) - ((_multiplier * row) / rows); } for (int col = 0; col < cols; col++) { - _gridModel.ColumnPercents[col] = _multiplier / cols; + _gridModel.ColumnPercents[col] = ((_multiplier * (col + 1)) / cols) - ((_multiplier * col) / cols); } int index = 0; diff --git a/src/modules/fancyzones/lib/ZoneSet.cpp b/src/modules/fancyzones/lib/ZoneSet.cpp index 3977831f22..9a59fe5b55 100644 --- a/src/modules/fancyzones/lib/ZoneSet.cpp +++ b/src/modules/fancyzones/lib/ZoneSet.cpp @@ -373,36 +373,40 @@ bool ZoneSet::CalculateColumnsAndRowsLayout(Rect workArea, JSONHelpers::ZoneSetL { bool success = true; - int zonePercent = C_MULTIPLIER / zoneCount; - long totalWidth; long totalHeight; - long cellWidth; - long cellHeight; - if (type == JSONHelpers::ZoneSetLayoutType::Columns) { totalWidth = workArea.width() - (spacing * (zoneCount + 1)); totalHeight = workArea.height() - (spacing * 2); - cellWidth = totalWidth * zonePercent / C_MULTIPLIER; - cellHeight = totalHeight; } else { //Rows totalWidth = workArea.width() - (spacing * 2); totalHeight = workArea.height() - (spacing * (zoneCount + 1)); - cellWidth = totalWidth; - cellHeight = totalHeight * zonePercent / C_MULTIPLIER; } long top = spacing; long left = spacing; - long bottom = top + cellHeight; - long right = left + cellWidth; + long bottom; + long right; + // Note: The expressions below are NOT equal to total{Width|Height} / zoneCount and are done + // like this to make the sum of all zones' sizes exactly total{Width|Height}. for (int zone = 0; zone < zoneCount; zone++) { + if (type == JSONHelpers::ZoneSetLayoutType::Columns) + { + right = left + (zone + 1) * totalWidth / zoneCount - zone * totalWidth / zoneCount; + bottom = totalHeight - spacing; + } + else + { //Rows + right = totalWidth - spacing; + bottom = top + (zone + 1) * totalHeight / zoneCount - zone * totalHeight / zoneCount; + } + if (left >= right || top >= bottom || left < 0 || right < 0 || top < 0 || bottom < 0) { success = false; @@ -413,13 +417,11 @@ bool ZoneSet::CalculateColumnsAndRowsLayout(Rect workArea, JSONHelpers::ZoneSetL if (type == JSONHelpers::ZoneSetLayoutType::Columns) { - left += cellWidth + spacing; - right = left + cellWidth; + left = right + spacing; } else { //Rows - top += cellHeight + spacing; - bottom = top + cellHeight; + top = bottom + spacing; } } @@ -452,13 +454,15 @@ bool ZoneSet::CalculateGridLayout(Rect workArea, JSONHelpers::ZoneSetLayoutType JSONHelpers::GridLayoutInfo gridLayoutInfo(JSONHelpers::GridLayoutInfo::Minimal{ .rows = rows, .columns = columns }); + // Note: The expressions below are NOT equal to C_MULTIPLIER / {rows|columns} and are done + // like this to make the sum of all percents exactly C_MULTIPLIER for (int row = 0; row < rows; row++) { - gridLayoutInfo.rowsPercents()[row] = C_MULTIPLIER / rows; + gridLayoutInfo.rowsPercents()[row] = C_MULTIPLIER * (row + 1) / rows - C_MULTIPLIER * row / rows; } for (int col = 0; col < columns; col++) { - gridLayoutInfo.columnsPercents()[col] = C_MULTIPLIER / columns; + gridLayoutInfo.columnsPercents()[col] = C_MULTIPLIER * (col + 1) / columns - C_MULTIPLIER * col / columns; } for (int i = 0; i < rows; ++i) @@ -554,22 +558,24 @@ bool ZoneSet::CalculateGridZones(Rect workArea, JSONHelpers::GridLayoutInfo grid std::vector rowInfo(gridLayoutInfo.rows()); std::vector columnInfo(gridLayoutInfo.columns()); - long top = spacing; + // Note: The expressions below are carefully written to + // make the sum of all zones' sizes exactly total{Width|Height} + long long totalPercents = 0; for (int row = 0; row < gridLayoutInfo.rows(); row++) { - rowInfo[row].Start = top; - rowInfo[row].Extent = totalHeight * gridLayoutInfo.rowsPercents()[row] / C_MULTIPLIER; - rowInfo[row].End = rowInfo[row].Start + rowInfo[row].Extent; - top += rowInfo[row].Extent + spacing; + rowInfo[row].Start = totalPercents * totalHeight / C_MULTIPLIER + (row + 1) * spacing; + totalPercents += gridLayoutInfo.rowsPercents()[row]; + rowInfo[row].End = totalPercents * totalHeight / C_MULTIPLIER + (row + 1) * spacing; + rowInfo[row].Extent = rowInfo[row].End - rowInfo[row].Start; } - long left = spacing; + totalPercents = 0; for (int col = 0; col < gridLayoutInfo.columns(); col++) { - columnInfo[col].Start = left; - columnInfo[col].Extent = totalWidth * gridLayoutInfo.columnsPercents()[col] / C_MULTIPLIER; - columnInfo[col].End = columnInfo[col].Start + columnInfo[col].Extent; - left += columnInfo[col].Extent + spacing; + columnInfo[col].Start = totalPercents * totalWidth / C_MULTIPLIER + (col + 1) * spacing; + totalPercents += gridLayoutInfo.columnsPercents()[col]; + columnInfo[col].End = totalPercents * totalWidth / C_MULTIPLIER + (col + 1) * spacing; + columnInfo[col].Extent = columnInfo[col].End - columnInfo[col].Start; } for (int row = 0; row < gridLayoutInfo.rows(); row++) @@ -580,8 +586,8 @@ bool ZoneSet::CalculateGridZones(Rect workArea, JSONHelpers::GridLayoutInfo grid if (((row == 0) || (gridLayoutInfo.cellChildMap()[row - 1][col] != i)) && ((col == 0) || (gridLayoutInfo.cellChildMap()[row][col - 1] != i))) { - left = columnInfo[col].Start; - top = rowInfo[row].Start; + long left = columnInfo[col].Start; + long top = rowInfo[row].Start; int maxRow = row; while (((maxRow + 1) < gridLayoutInfo.rows()) && (gridLayoutInfo.cellChildMap()[maxRow + 1][col] == i))