From 9146d7b37c41f2384994a33def79dee10e2a4965 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 18 Apr 2018 15:31:56 -0300 Subject: [PATCH 1/7] First implementation of GetGridWidthAfterResize --- .../GridSplitter/GridSplitter.Events.cs | 1 + .../GridSplitter/GridSplitter.Helper.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs index ce8295c06f0..4fef0b9d456 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs @@ -158,6 +158,7 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) if (_resizeDirection == GridResizeDirection.Columns) { + System.Diagnostics.Debug.WriteLine($"Previous: {Resizable.DesiredSize.Width}; Future: {GetGridWidthAfterResize(horizontalChange)}"); if (HorizontalMove(horizontalChange)) { return; diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs index d17b6037ab5..d3c03593581 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs @@ -10,6 +10,7 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** +using System.Collections.Generic; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -264,5 +265,32 @@ private GridResizeBehavior GetResizeBehavior() return resizeBehavior; } + + private double GetGridWidthAfterResize(double change) + { + // Backup column definition sizes + var columnWidths = new List(); + foreach (var columnDefinition in Resizable.ColumnDefinitions) + { + columnWidths.Add(new GridLength(columnDefinition.Width.Value, columnDefinition.Width.GridUnitType)); + } + + // Resize + HorizontalMove(change); + + // Get the size of the grid after resizing + Resizable.Measure(new Windows.Foundation.Size(double.MaxValue, double.MaxValue)); + var size = Resizable.DesiredSize; + Resizable.InvalidateArrange(); + Resizable.UpdateLayout(); + + // Restore column definition sizes + for (int i = 0; i < columnWidths.Count; i++) + { + Resizable.ColumnDefinitions[i].Width = columnWidths[i]; + } + + return size.Width; + } } } From a4d07df61aee51532a60003d6b0974ca5251d451 Mon Sep 17 00:00:00 2001 From: Kevin Date: Fri, 20 Apr 2018 19:04:49 -0300 Subject: [PATCH 2/7] Implemented fix to stop the resize when the min width of any column has been reached --- .../GridSplitter/GridSplitter.Events.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs index 4fef0b9d456..c94df2900f9 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs @@ -247,6 +247,17 @@ private bool HorizontalMove(double horizontalChange) return true; } + if (horizontalChange < 0) + { + foreach (var column in Resizable.ColumnDefinitions) + { + if (column.ActualWidth == column.MinWidth) + { + return true; + } + } + } + // if current column has fixed width then resize it if (!IsStarColumn(CurrentColumn)) { From 1b75284cf8a6e24d6d7995370d7179c0ff030341 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 24 Apr 2018 15:08:38 -0300 Subject: [PATCH 3/7] Improved solution by checking only required columns and using the right horizontalChange value --- .../GridSplitter/GridSplitter.Events.cs | 103 ++++++++++++++++-- 1 file changed, 94 insertions(+), 9 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs index c94df2900f9..dfd1e182235 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs @@ -10,6 +10,7 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** +using System.Linq; using Windows.System; using Windows.UI.Core; using Windows.UI.Xaml; @@ -86,11 +87,15 @@ protected override void OnKeyDown(KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Left) { + LogColumnStates(); HorizontalMove(-step); + LogColumnStates(); } else if (e.Key == VirtualKey.Right) { + LogColumnStates(); HorizontalMove(step); + LogColumnStates(); } else { @@ -158,11 +163,27 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) if (_resizeDirection == GridResizeDirection.Columns) { - System.Diagnostics.Debug.WriteLine($"Previous: {Resizable.DesiredSize.Width}; Future: {GetGridWidthAfterResize(horizontalChange)}"); + //Resizable.Measure(new Windows.Foundation.Size(double.MaxValue, double.MaxValue)); + //var size = Resizable.DesiredSize; + //Resizable.InvalidateArrange(); + //Resizable.UpdateLayout(); + //System.Diagnostics.Debug.WriteLine($"Previous: {size.Width}; Future: {GetGridWidthAfterResize(horizontalChange)}; Grid Parent: {GetElementWidth((FrameworkElement)Resizable.Parent as UIElement)}"); + + LogColumnStates(); + SaveProperties(Resizable.ColumnDefinitions); if (HorizontalMove(horizontalChange)) { + SaveProperties(Resizable.ColumnDefinitions); + LogColumnStates(); return; } + + foreach (var column in Resizable.ColumnDefinitions) + { + SaveProperties(column); + } + + LogColumnStates(); } else if (_resizeDirection == GridResizeDirection.Rows) { @@ -240,6 +261,61 @@ private bool VerticalMove(double verticalChange) return false; } + private void LogColumnStates() + { + //foreach (var columndefinition in resizable.columndefinitions) + //{ + // string columnname = "other"; + // if (columndefinition == currentcolumn) + // { + // columnname = "current"; + // } + // else if (columndefinition == siblingcolumn) + // { + // columnname = "sibling"; + // } + + // system.diagnostics.debug.writeline($"column {columnname}, size: {columndefinition.width}; hash: {columndefinition.gethashcode()}"); + //} + } + + private void SetInitialColumnSizes() + { + //foreach (var column in Resizable.ColumnDefinitions) + //{ + // columnWidths.Add(column.GetHashCode(), column.Width); + // column.Width = new GridLength(column.ActualWidth, GridUnitType.Pixel); + //} + + //Window.Current.SizeChanged += (sender, e) => + //{ + // foreach (var column in Resizable.ColumnDefinitions) + // { + // column.Width = columnWidths[column.GetHashCode()]; + // } + + // Resizable.InvalidateArrange(); + // Resizable.UpdateLayout(); + + // foreach (var column in Resizable.ColumnDefinitions) + // { + // columnWidths.Remove(column.GetHashCode()); + // columnWidths.Add(column.GetHashCode(), column.Width); + // column.Width = new GridLength(column.ActualWidth, GridUnitType.Pixel); + // } + //}; + } + + //private bool HorizontalMoveRefactor(double horizontalChange) + //{ + // if (CurrentColumn == null || SiblingColumn == null) + // { + // return true; + // } + + + //} + private bool HorizontalMove(double horizontalChange) { if (CurrentColumn == null || SiblingColumn == null) @@ -247,20 +323,19 @@ private bool HorizontalMove(double horizontalChange) return true; } - if (horizontalChange < 0) + var columnsWithAutomaticWidth = Resizable.ColumnDefinitions.Where(x => (x.Width.IsStar || x.Width.IsAuto) && x != CurrentColumn); + + // if current column has fixed width then resize it + if (!IsStarColumn(CurrentColumn)) { - foreach (var column in Resizable.ColumnDefinitions) + foreach (var column in columnsWithAutomaticWidth) { - if (column.ActualWidth == column.MinWidth) + if (!IsValidColumnWidth(column, horizontalChange * -1)) { - return true; + return false; } } - } - // if current column has fixed width then resize it - if (!IsStarColumn(CurrentColumn)) - { // No need to check for the Column Min width because it is automatically respected if (!SetColumnWidth(CurrentColumn, horizontalChange, GridUnitType.Pixel)) { @@ -277,6 +352,16 @@ private bool HorizontalMove(double horizontalChange) return false; } + columnsWithAutomaticWidth = columnsWithAutomaticWidth.Where(x => x != SiblingColumn); + + foreach (var column in columnsWithAutomaticWidth) + { + if (!IsValidColumnWidth(column, horizontalChange)) + { + return false; + } + } + if (!SetColumnWidth(SiblingColumn, horizontalChange * -1, GridUnitType.Pixel)) { return true; From 072268e93a28d526e64962856becf14218854500 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 24 Apr 2018 15:28:16 -0300 Subject: [PATCH 4/7] Removed unused function call --- .../GridSplitter/GridSplitter.Events.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs index dfd1e182235..873d6bc0967 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs @@ -170,19 +170,12 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) //System.Diagnostics.Debug.WriteLine($"Previous: {size.Width}; Future: {GetGridWidthAfterResize(horizontalChange)}; Grid Parent: {GetElementWidth((FrameworkElement)Resizable.Parent as UIElement)}"); LogColumnStates(); - SaveProperties(Resizable.ColumnDefinitions); if (HorizontalMove(horizontalChange)) { - SaveProperties(Resizable.ColumnDefinitions); LogColumnStates(); return; } - foreach (var column in Resizable.ColumnDefinitions) - { - SaveProperties(column); - } - LogColumnStates(); } else if (_resizeDirection == GridResizeDirection.Rows) From a5a8f2d9c0165a0a7915ac11ff6621d12bf08a4d Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 25 Apr 2018 13:10:58 -0300 Subject: [PATCH 5/7] Removed unused code --- .../GridSplitter/GridSplitter.Events.cs | 69 ------------------- .../GridSplitter/GridSplitter.Helper.cs | 27 -------- 2 files changed, 96 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs index 873d6bc0967..690b0d5f983 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs @@ -87,15 +87,11 @@ protected override void OnKeyDown(KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Left) { - LogColumnStates(); HorizontalMove(-step); - LogColumnStates(); } else if (e.Key == VirtualKey.Right) { - LogColumnStates(); HorizontalMove(step); - LogColumnStates(); } else { @@ -163,20 +159,10 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) if (_resizeDirection == GridResizeDirection.Columns) { - //Resizable.Measure(new Windows.Foundation.Size(double.MaxValue, double.MaxValue)); - //var size = Resizable.DesiredSize; - //Resizable.InvalidateArrange(); - //Resizable.UpdateLayout(); - //System.Diagnostics.Debug.WriteLine($"Previous: {size.Width}; Future: {GetGridWidthAfterResize(horizontalChange)}; Grid Parent: {GetElementWidth((FrameworkElement)Resizable.Parent as UIElement)}"); - - LogColumnStates(); if (HorizontalMove(horizontalChange)) { - LogColumnStates(); return; } - - LogColumnStates(); } else if (_resizeDirection == GridResizeDirection.Rows) { @@ -254,61 +240,6 @@ private bool VerticalMove(double verticalChange) return false; } - private void LogColumnStates() - { - //foreach (var columndefinition in resizable.columndefinitions) - //{ - // string columnname = "other"; - // if (columndefinition == currentcolumn) - // { - // columnname = "current"; - // } - // else if (columndefinition == siblingcolumn) - // { - // columnname = "sibling"; - // } - - // system.diagnostics.debug.writeline($"column {columnname}, size: {columndefinition.width}; hash: {columndefinition.gethashcode()}"); - //} - } - - private void SetInitialColumnSizes() - { - //foreach (var column in Resizable.ColumnDefinitions) - //{ - // columnWidths.Add(column.GetHashCode(), column.Width); - // column.Width = new GridLength(column.ActualWidth, GridUnitType.Pixel); - //} - - //Window.Current.SizeChanged += (sender, e) => - //{ - // foreach (var column in Resizable.ColumnDefinitions) - // { - // column.Width = columnWidths[column.GetHashCode()]; - // } - - // Resizable.InvalidateArrange(); - // Resizable.UpdateLayout(); - - // foreach (var column in Resizable.ColumnDefinitions) - // { - // columnWidths.Remove(column.GetHashCode()); - // columnWidths.Add(column.GetHashCode(), column.Width); - // column.Width = new GridLength(column.ActualWidth, GridUnitType.Pixel); - // } - //}; - } - - //private bool HorizontalMoveRefactor(double horizontalChange) - //{ - // if (CurrentColumn == null || SiblingColumn == null) - // { - // return true; - // } - - - //} - private bool HorizontalMove(double horizontalChange) { if (CurrentColumn == null || SiblingColumn == null) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs index d3c03593581..2058097705c 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs @@ -265,32 +265,5 @@ private GridResizeBehavior GetResizeBehavior() return resizeBehavior; } - - private double GetGridWidthAfterResize(double change) - { - // Backup column definition sizes - var columnWidths = new List(); - foreach (var columnDefinition in Resizable.ColumnDefinitions) - { - columnWidths.Add(new GridLength(columnDefinition.Width.Value, columnDefinition.Width.GridUnitType)); - } - - // Resize - HorizontalMove(change); - - // Get the size of the grid after resizing - Resizable.Measure(new Windows.Foundation.Size(double.MaxValue, double.MaxValue)); - var size = Resizable.DesiredSize; - Resizable.InvalidateArrange(); - Resizable.UpdateLayout(); - - // Restore column definition sizes - for (int i = 0; i < columnWidths.Count; i++) - { - Resizable.ColumnDefinitions[i].Width = columnWidths[i]; - } - - return size.Width; - } } } From 65c5633ee86e45c4cf518ab1bb6910b8e0768a67 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 25 Apr 2018 13:14:30 -0300 Subject: [PATCH 6/7] Removed old using --- .../GridSplitter/GridSplitter.Helper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs index 2058097705c..d17b6037ab5 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Helper.cs @@ -10,7 +10,6 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** -using System.Collections.Generic; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; From 9cf6d857f90cd6114911c9bc5380578aef4d2f9b Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 25 Apr 2018 13:37:54 -0300 Subject: [PATCH 7/7] Moved the code to another function, added comments --- .../GridSplitter/GridSplitter.Events.cs | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs index 690b0d5f983..a60f9fcc259 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/GridSplitter/GridSplitter.Events.cs @@ -10,6 +10,7 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** +using System.Collections.Generic; using System.Linq; using Windows.System; using Windows.UI.Core; @@ -252,12 +253,10 @@ private bool HorizontalMove(double horizontalChange) // if current column has fixed width then resize it if (!IsStarColumn(CurrentColumn)) { - foreach (var column in columnsWithAutomaticWidth) + // Check if all automatic width columns will respect min and max width after resize + if (!AreValidColumnWidths(columnsWithAutomaticWidth, horizontalChange * -1)) { - if (!IsValidColumnWidth(column, horizontalChange * -1)) - { - return false; - } + return false; } // No need to check for the Column Min width because it is automatically respected @@ -276,14 +275,13 @@ private bool HorizontalMove(double horizontalChange) return false; } + // Remove the Sibling as it will be checked during resize columnsWithAutomaticWidth = columnsWithAutomaticWidth.Where(x => x != SiblingColumn); - foreach (var column in columnsWithAutomaticWidth) + // Check if all automatic width columns will respect min and max width after resize + if (!AreValidColumnWidths(columnsWithAutomaticWidth, horizontalChange)) { - if (!IsValidColumnWidth(column, horizontalChange)) - { - return false; - } + return false; } if (!SetColumnWidth(SiblingColumn, horizontalChange * -1, GridUnitType.Pixel)) @@ -325,5 +323,18 @@ private bool HorizontalMove(double horizontalChange) return false; } + + private bool AreValidColumnWidths(IEnumerable columns, double horizontalChange) + { + foreach (var column in columns) + { + if (!IsValidColumnWidth(column, horizontalChange)) + { + return false; + } + } + + return true; + } } }