Skip to content

Commit

Permalink
Implement canceling edits in FZE, fix crashes related to canceling. (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
ivan100sic committed Mar 18, 2020
1 parent ff0c021 commit 1c90107
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ private void UpdateZoneRects()
previewChildrenCount++;
}

while (previewChildrenCount > _model.Zones.Count)
{
Preview.Children.RemoveAt(previewChildrenCount - 1);
previewChildrenCount--;
}

for (int i = 0; i < previewChildrenCount; i++)
{
Int32Rect rect = _model.Zones[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public CanvasEditorWindow()
{
InitializeComponent();
_model = EditorOverlay.Current.DataContext as CanvasLayoutModel;
_stashedModel = (CanvasLayoutModel)_model.Clone();
}

private void OnAddZone(object sender, RoutedEventArgs e)
Expand All @@ -24,7 +25,14 @@ private void OnAddZone(object sender, RoutedEventArgs e)
_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;
}
}
12 changes: 10 additions & 2 deletions src/modules/fancyzones/editor/FancyZonesEditor/GridEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ public partial class GridEditor : UserControl
{
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)
Expand Down Expand Up @@ -73,7 +78,9 @@ private void GridEditor_Loaded(object sender, RoutedEventArgs e)
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);
}
Expand Down Expand Up @@ -495,7 +502,8 @@ private int AddZone()

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -12,6 +15,16 @@ public partial class GridEditorWindow : EditorWindow
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public override LayoutModel Clone()
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public void Reload(byte[] data)
public override LayoutModel Clone()
{
GridLayoutModel layout = new GridLayoutModel(Name);
RestoreTo(layout);
return layout;
}

public void RestoreTo(GridLayoutModel layout)
{
int rows = Rows;
int cols = Columns;

Expand Down Expand Up @@ -163,8 +169,6 @@ public override LayoutModel Clone()
}

layout.ColumnPercents = colPercents;

return layout;
}

// PersistData
Expand Down

0 comments on commit 1c90107

Please sign in to comment.