Implement canceling edits in FZE, fix crashes related to canceling. (#1610)

* Implemented proper canceling for CanvasEditor

* Implemented proper canceling for GridEditor

* Possible fix for a crash in my implementation of canceling

* Fixed a crash in FZE/Grid editor
This commit is contained in:
Ivan Stošić 2020-03-18 14:13:25 +01:00 committed by GitHub
parent ff0c021162
commit 1c90107571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 4 deletions

View File

@ -55,6 +55,12 @@ namespace FancyZonesEditor
previewChildrenCount++;
}
while (previewChildrenCount > _model.Zones.Count)
{
Preview.Children.RemoveAt(previewChildrenCount - 1);
previewChildrenCount--;
}
for (int i = 0; i < previewChildrenCount; i++)
{
Int32Rect rect = _model.Zones[i];

View File

@ -16,6 +16,7 @@ namespace FancyZonesEditor
{
InitializeComponent();
_model = EditorOverlay.Current.DataContext as CanvasLayoutModel;
_stashedModel = (CanvasLayoutModel)_model.Clone();
}
private void OnAddZone(object sender, RoutedEventArgs e)
@ -24,7 +25,14 @@ namespace FancyZonesEditor
_offset += 100;
}
protected new void OnCancel(object sender, RoutedEventArgs e)
{
base.OnCancel(sender, e);
_stashedModel.RestoreTo(_model);
}
private int _offset = 100;
private CanvasLayoutModel _model;
private CanvasLayoutModel _stashedModel;
}
}

View File

@ -18,11 +18,16 @@ namespace FancyZonesEditor
{
public static readonly DependencyProperty ModelProperty = DependencyProperty.Register("Model", typeof(GridLayoutModel), typeof(GridEditor), new PropertyMetadata(null, OnGridDimensionsChanged));
private static int gridEditorUniqueIdCounter = 0;
private int gridEditorUniqueId;
public GridEditor()
{
InitializeComponent();
Loaded += GridEditor_Loaded;
((App)Application.Current).ZoneSettings.PropertyChanged += ZoneSettings_PropertyChanged;
gridEditorUniqueId = ++gridEditorUniqueIdCounter;
}
private void GridEditor_Loaded(object sender, RoutedEventArgs e)
@ -73,7 +78,9 @@ namespace FancyZonesEditor
private void ZoneSettings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
Size actualSize = new Size(ActualWidth, ActualHeight);
if (actualSize.Width > 0)
// Only enter if this is the newest instance
if (actualSize.Width > 0 && gridEditorUniqueId == gridEditorUniqueIdCounter)
{
ArrangeGridRects(actualSize);
}
@ -495,7 +502,8 @@ namespace FancyZonesEditor
private void OnGridDimensionsChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if ((e.PropertyName == "Rows") || (e.PropertyName == "Columns"))
// Only enter if this is the newest instance
if (((e.PropertyName == "Rows") || (e.PropertyName == "Columns")) && gridEditorUniqueId == gridEditorUniqueIdCounter)
{
OnGridDimensionsChanged();
}

View File

@ -2,6 +2,9 @@
// 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.Windows;
using FancyZonesEditor.Models;
namespace FancyZonesEditor
{
/// <summary>
@ -12,6 +15,16 @@ namespace FancyZonesEditor
public GridEditorWindow()
{
InitializeComponent();
_stashedModel = (GridLayoutModel)(EditorOverlay.Current.DataContext as GridLayoutModel).Clone();
}
protected new void OnCancel(object sender, RoutedEventArgs e)
{
base.OnCancel(sender, e);
GridLayoutModel model = EditorOverlay.Current.DataContext as GridLayoutModel;
_stashedModel.RestoreTo(model);
}
private GridLayoutModel _stashedModel;
}
}

View File

@ -113,6 +113,15 @@ namespace FancyZonesEditor.Models
return layout;
}
public void RestoreTo(CanvasLayoutModel other)
{
other.Zones.Clear();
foreach (Int32Rect zone in Zones)
{
other.Zones.Add(zone);
}
}
// PersistData
// Implements the LayoutModel.PersistData abstract method
protected override void PersistData()

View File

@ -131,6 +131,12 @@ namespace FancyZonesEditor.Models
public override LayoutModel Clone()
{
GridLayoutModel layout = new GridLayoutModel(Name);
RestoreTo(layout);
return layout;
}
public void RestoreTo(GridLayoutModel layout)
{
int rows = Rows;
int cols = Columns;
@ -163,8 +169,6 @@ namespace FancyZonesEditor.Models
}
layout.ColumnPercents = colPercents;
return layout;
}
// PersistData