From 1c90107571bdc5b9644dc79415ccaea2220ad48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Sto=C5=A1i=C4=87?= Date: Wed, 18 Mar 2020 14:13:25 +0100 Subject: [PATCH] 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 --- .../editor/FancyZonesEditor/CanvasEditor.xaml.cs | 6 ++++++ .../FancyZonesEditor/CanvasEditorWindow.xaml.cs | 8 ++++++++ .../editor/FancyZonesEditor/GridEditor.xaml.cs | 12 ++++++++++-- .../FancyZonesEditor/GridEditorWindow.xaml.cs | 13 +++++++++++++ .../FancyZonesEditor/Models/CanvasLayoutModel.cs | 9 +++++++++ .../FancyZonesEditor/Models/GridLayoutModel.cs | 8 ++++++-- 6 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditor.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditor.xaml.cs index 00ce0a98ca..857d023da7 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditor.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditor.xaml.cs @@ -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]; diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs index 26b3f786a8..07c1dfd3de 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs @@ -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; } } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/GridEditor.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/GridEditor.xaml.cs index a4d8b75953..b9678dd62d 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/GridEditor.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/GridEditor.xaml.cs @@ -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(); } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/GridEditorWindow.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/GridEditorWindow.xaml.cs index 32d5b7781e..bd49bd058c 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/GridEditorWindow.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/GridEditorWindow.xaml.cs @@ -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 { /// @@ -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; } } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs index b1af0d60e6..415656ac7b 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs @@ -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() diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs index 74501cac0a..89a5a26098 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs @@ -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