[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 30 deletions

View File

@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using FancyZonesEditor.Models;
@ -121,7 +120,7 @@ namespace FancyZonesEditor
if (zoneCount > rows * cols)
{
throw new ArgumentException("Invalid index found in model.CellChildMap");
return;
}
var indexCount = Enumerable.Repeat(0, zoneCount).ToList();
@ -147,33 +146,18 @@ namespace FancyZonesEditor
{
if (indexCount[index] == 0)
{
throw new ArgumentException("Indices in model.CellChildMap are not contiguous");
return;
}
if (indexCount[index] != (indexRowHigh[index] - indexRowLow[index] + 1) * (indexColHigh[index] - indexColLow[index] + 1))
{
throw new ArgumentException("One or more indices in model.CellChildMap don't form a rectangle");
return;
}
}
if (model.RowPercents.Count != rows)
if (model.RowPercents.Count != model.Rows || model.ColumnPercents.Count != model.Columns || model.RowPercents.Exists((x) => (x < 1)) || model.ColumnPercents.Exists((x) => (x < 1)))
{
throw new ArgumentException("model.RowPercents has invalid size");
}
if (model.ColumnPercents.Count != cols)
{
throw new ArgumentException("model.ColumnPercents has invalid size");
}
if (model.RowPercents.Exists((x) => (x < 1)))
{
throw new ArgumentException("Invalid value in model.RowPercents");
}
if (model.ColumnPercents.Exists((x) => (x < 1)))
{
throw new ArgumentException("Invalid value in model.ColumnPercents");
return;
}
var rowPrefixSum = PrefixSum(model.RowPercents);
@ -181,7 +165,7 @@ namespace FancyZonesEditor
if (rowPrefixSum[rows] != Multiplier || colPrefixSum[cols] != Multiplier)
{
throw new ArgumentException();
return;
}
_zones = new List<Zone>(zoneCount);

View File

@ -97,7 +97,7 @@ namespace FancyZonesEditor
{
Size actualSize = WorkAreaSize();
if (actualSize.Width < 1 || _data == null || Model == null)
if (actualSize.Width < 1 || _data == null || _data.Zones == null || Model == null)
{
return;
}

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();

View File

@ -916,17 +916,19 @@ namespace FancyZonesEditor.Utils
}
// Check if percentage is valid. Otherwise, Editor could crash on layout rendering.
foreach (int percent in info.RowsPercentage)
if (info.RowsPercentage.Exists((x) => (x < 1)) || info.ColumnsPercentage.Exists((x) => (x < 1)))
{
if (percent < 0)
{
return null;
}
return null;
}
foreach (int percent in info.ColumnsPercentage)
if (info.CellChildMap.Length != info.Rows)
{
if (percent < 0)
return null;
}
foreach (var col in info.CellChildMap)
{
if (col.Length != info.Columns)
{
return null;
}
@ -942,6 +944,11 @@ namespace FancyZonesEditor.Utils
}
var layout = new GridLayoutModel(wrapper.Uuid, wrapper.Name, LayoutType.Custom, info.Rows, info.Columns, info.RowsPercentage, info.ColumnsPercentage, cells);
if (!layout.IsModelValid())
{
return null;
}
layout.SensitivityRadius = info.SensitivityRadius;
layout.ShowSpacing = info.ShowSpacing;
layout.Spacing = info.Spacing;