From 069cedd357389d2caac13a71c2f3f9ff5c85fc3e Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Wed, 21 Jun 2023 21:43:23 -0700 Subject: [PATCH] Wpf: Optimize layout of parent controls when size of control is set --- src/Eto.Wpf/Forms/PixelLayoutHandler.cs | 8 ++++---- src/Eto.Wpf/Forms/TableLayoutHandler.cs | 6 +++--- src/Eto.Wpf/Forms/WpfContainer.cs | 2 +- src/Eto.Wpf/Forms/WpfFrameworkElement.cs | 8 +++++++- src/Eto.Wpf/Forms/WpfPanel.cs | 4 ++-- 5 files changed, 17 insertions(+), 11 deletions(-) mode change 100644 => 100755 src/Eto.Wpf/Forms/PixelLayoutHandler.cs mode change 100644 => 100755 src/Eto.Wpf/Forms/TableLayoutHandler.cs mode change 100644 => 100755 src/Eto.Wpf/Forms/WpfContainer.cs mode change 100644 => 100755 src/Eto.Wpf/Forms/WpfPanel.cs diff --git a/src/Eto.Wpf/Forms/PixelLayoutHandler.cs b/src/Eto.Wpf/Forms/PixelLayoutHandler.cs old mode 100644 new mode 100755 index 43e3264f96..8184dde668 --- a/src/Eto.Wpf/Forms/PixelLayoutHandler.cs +++ b/src/Eto.Wpf/Forms/PixelLayoutHandler.cs @@ -56,7 +56,7 @@ public void Add(Control child, int x, int y) swc.Canvas.SetLeft(element, x); swc.Canvas.SetTop(element, y); Control.Children.Add(element); - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } public void Move(Control child, int x, int y) @@ -64,20 +64,20 @@ public void Move(Control child, int x, int y) var element = child.GetContainerControl(); swc.Canvas.SetLeft(element, x); swc.Canvas.SetTop(element, y); - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } public void Remove(Control child) { var element = child.GetContainerControl(); Control.Children.Remove(element); - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } public override void Remove(sw.FrameworkElement child) { Control.Children.Remove(child); - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } int suspended; diff --git a/src/Eto.Wpf/Forms/TableLayoutHandler.cs b/src/Eto.Wpf/Forms/TableLayoutHandler.cs old mode 100644 new mode 100755 index 2ac02725d6..9495f6630d --- a/src/Eto.Wpf/Forms/TableLayoutHandler.cs +++ b/src/Eto.Wpf/Forms/TableLayoutHandler.cs @@ -395,7 +395,7 @@ public void Add(Control child, int x, int y) } Control.Children.Add(control); } - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } public void Move(Control child, int x, int y) @@ -420,7 +420,7 @@ public void Move(Control child, int x, int y) SetScale(handler, x, y); } controls[x, y] = child; - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } public void Remove(Control child) @@ -434,7 +434,7 @@ public override void Remove(sw.FrameworkElement child) var y = swc.Grid.GetRow(child); Control.Children.Remove(child); controls[x, y] = null; - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } } } diff --git a/src/Eto.Wpf/Forms/WpfContainer.cs b/src/Eto.Wpf/Forms/WpfContainer.cs old mode 100644 new mode 100755 index 2750d3bbd6..919b8f0ad9 --- a/src/Eto.Wpf/Forms/WpfContainer.cs +++ b/src/Eto.Wpf/Forms/WpfContainer.cs @@ -12,7 +12,7 @@ public interface IWpfContainer { void Remove(sw.FrameworkElement child); - void UpdatePreferredSize(); + void OnChildPreferredSizeUpdated(); } public abstract class WpfContainer : WpfFrameworkElement, Container.IHandler, IWpfContainer diff --git a/src/Eto.Wpf/Forms/WpfFrameworkElement.cs b/src/Eto.Wpf/Forms/WpfFrameworkElement.cs index fd2c414ec3..379bb6c24b 100755 --- a/src/Eto.Wpf/Forms/WpfFrameworkElement.cs +++ b/src/Eto.Wpf/Forms/WpfFrameworkElement.cs @@ -252,10 +252,16 @@ public virtual void UpdatePreferredSize() { if (Widget.Loaded) { - Widget.VisualParent.GetWpfContainer()?.UpdatePreferredSize(); + Widget.VisualParent.GetWpfContainer()?.OnChildPreferredSizeUpdated(); } } + public virtual void OnChildPreferredSizeUpdated() + { + if (double.IsNaN(UserPreferredSize.Width) || double.IsNaN(UserPreferredSize.Height)) + UpdatePreferredSize(); + } + public virtual void SetScale(bool xscale, bool yscale) { XScale = xscale; diff --git a/src/Eto.Wpf/Forms/WpfPanel.cs b/src/Eto.Wpf/Forms/WpfPanel.cs old mode 100644 new mode 100755 index 7775970d53..7eec695616 --- a/src/Eto.Wpf/Forms/WpfPanel.cs +++ b/src/Eto.Wpf/Forms/WpfPanel.cs @@ -113,7 +113,7 @@ public Control Content else border.Child = null; Control.InvalidateMeasure(); - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } } @@ -125,7 +125,7 @@ public override void Remove(sw.FrameworkElement child) { content = null; border.Child = null; - UpdatePreferredSize(); + OnChildPreferredSizeUpdated(); } } }