[FancyZones Editor] Fix FZ Editor crash on opening custom layout for editing (#10911)

This commit is contained in:
Seraphima Zykova
2021-04-26 16:32:18 +03:00
committed by GitHub
parent b2f47b9073
commit e9a0b58796
4 changed files with 69 additions and 30 deletions

View File

@@ -2,6 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
namespace FancyZonesEditor.Models
@@ -173,6 +174,53 @@ namespace FancyZonesEditor.Models
}
}
public bool IsModelValid()
{
// Check if rows and columns are valid
if (Rows <= 0 || Columns <= 0)
{
return false;
}
// Check if percentage is valid.
if (RowPercents.Count != Rows || ColumnPercents.Count != Columns || RowPercents.Exists((x) => (x < 1)) || ColumnPercents.Exists((x) => (x < 1)))
{
return false;
}
// Check if cells map is valid
if (CellChildMap.Length != Rows * Columns)
{
return false;
}
int zoneCount = 0;
for (int row = 0; row < Rows; row++)
{
for (int col = 0; col < Columns; col++)
{
zoneCount = Math.Max(zoneCount, CellChildMap[row, col]);
}
}
zoneCount++;
if (zoneCount > Rows * Columns)
{
return false;
}
var rowPrefixSum = GridData.PrefixSum(RowPercents);
var colPrefixSum = GridData.PrefixSum(ColumnPercents);
if (rowPrefixSum[Rows] != GridData.Multiplier || colPrefixSum[Columns] != GridData.Multiplier)
{
return false;
}
return true;
}
public void UpdatePreview()
{
FirePropertyChanged();