From 6cb16dddc4f5aaf5779fd6d6d6951e19f90b2304 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Wed, 22 Nov 2023 19:26:18 -0500 Subject: [PATCH 1/4] Disallow NaNs in Rect --- src/Controls/src/Core/Window/Window.cs | 2 +- src/Graphics/src/Graphics/Rect.cs | 29 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Controls/src/Core/Window/Window.cs b/src/Controls/src/Core/Window/Window.cs index 392fa4be5507..29ad1d1f0b8e 100644 --- a/src/Controls/src/Core/Window/Window.cs +++ b/src/Controls/src/Core/Window/Window.cs @@ -206,7 +206,7 @@ void IWindow.FrameChanged(Rect frame) var y = Y; var width = Width; var height = Height; - if (new Rect(x, y, width, height) == frame) + if (!double.IsNaN(width) && !double.IsNaN(height) && new Rect(x, y, width, height) == frame) return; _batchFrameUpdate++; diff --git a/src/Graphics/src/Graphics/Rect.cs b/src/Graphics/src/Graphics/Rect.cs index 9fe25894d6c0..74e9ac796e31 100644 --- a/src/Graphics/src/Graphics/Rect.cs +++ b/src/Graphics/src/Graphics/Rect.cs @@ -9,13 +9,34 @@ namespace Microsoft.Maui.Graphics [TypeConverter(typeof(Converters.RectTypeConverter))] public partial struct Rect { + double _Height; + double _Width; + public double X { get; set; } public double Y { get; set; } - public double Width { get; set; } + public double Width + { + get => _Width; + set + { + if (double.IsNaN(value)) + throw new ArgumentOutOfRangeException(nameof(value), value, "Cannot be NaN"); + _Width = value; + } + } - public double Height { get; set; } + public double Height + { + get => _Height; + set + { + if (double.IsNaN(value)) + throw new ArgumentOutOfRangeException(nameof(value), value, "Cannot be NaN"); + _Height = value; + } + } public static Rect Zero = new Rect(); @@ -27,6 +48,10 @@ public override string ToString() // constructors public Rect(double x, double y, double width, double height) : this() { + if (double.IsNaN(width)) + throw new ArgumentOutOfRangeException(nameof(width), width, "Cannot be NaN"); + if (double.IsNaN(height)) + throw new ArgumentOutOfRangeException(nameof(height), height, "Cannot be NaN"); X = x; Y = y; Width = width; From 3aea7ea43be538d4e9b2fbc7abef2de4010f81f6 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Fri, 1 Dec 2023 19:43:00 -0500 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: E.Z. Hart --- src/Graphics/src/Graphics/Rect.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Graphics/src/Graphics/Rect.cs b/src/Graphics/src/Graphics/Rect.cs index 74e9ac796e31..873666ebb29e 100644 --- a/src/Graphics/src/Graphics/Rect.cs +++ b/src/Graphics/src/Graphics/Rect.cs @@ -9,8 +9,8 @@ namespace Microsoft.Maui.Graphics [TypeConverter(typeof(Converters.RectTypeConverter))] public partial struct Rect { - double _Height; - double _Width; + double _height; + double _width; public double X { get; set; } From 4e17d0c36c250f8abfc802fa7ddf5d37053a02ef Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Fri, 1 Dec 2023 19:48:04 -0500 Subject: [PATCH 3/4] Rely on setters for checks --- src/Graphics/src/Graphics/Rect.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Graphics/src/Graphics/Rect.cs b/src/Graphics/src/Graphics/Rect.cs index 74e9ac796e31..4a0b07c0da53 100644 --- a/src/Graphics/src/Graphics/Rect.cs +++ b/src/Graphics/src/Graphics/Rect.cs @@ -48,10 +48,6 @@ public override string ToString() // constructors public Rect(double x, double y, double width, double height) : this() { - if (double.IsNaN(width)) - throw new ArgumentOutOfRangeException(nameof(width), width, "Cannot be NaN"); - if (double.IsNaN(height)) - throw new ArgumentOutOfRangeException(nameof(height), height, "Cannot be NaN"); X = x; Y = y; Width = width; From 5b84b7654975845e6ac8f5cbf37aee05d736f419 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Fri, 1 Dec 2023 20:00:56 -0500 Subject: [PATCH 4/4] Don't allocate the Rect just for an equality check, and fix properties --- src/Controls/src/Core/Window/Window.cs | 2 +- src/Graphics/src/Graphics/Rect.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Controls/src/Core/Window/Window.cs b/src/Controls/src/Core/Window/Window.cs index 29ad1d1f0b8e..a53a9e5e4324 100644 --- a/src/Controls/src/Core/Window/Window.cs +++ b/src/Controls/src/Core/Window/Window.cs @@ -206,7 +206,7 @@ void IWindow.FrameChanged(Rect frame) var y = Y; var width = Width; var height = Height; - if (!double.IsNaN(width) && !double.IsNaN(height) && new Rect(x, y, width, height) == frame) + if (frame.X == x && frame.Y == y && frame.Width == width && frame.Height == height) return; _batchFrameUpdate++; diff --git a/src/Graphics/src/Graphics/Rect.cs b/src/Graphics/src/Graphics/Rect.cs index 06e7c22316a3..916743a8beda 100644 --- a/src/Graphics/src/Graphics/Rect.cs +++ b/src/Graphics/src/Graphics/Rect.cs @@ -18,23 +18,23 @@ public partial struct Rect public double Width { - get => _Width; + get => _width; set { if (double.IsNaN(value)) throw new ArgumentOutOfRangeException(nameof(value), value, "Cannot be NaN"); - _Width = value; + _width = value; } } public double Height { - get => _Height; + get => _height; set { if (double.IsNaN(value)) throw new ArgumentOutOfRangeException(nameof(value), value, "Cannot be NaN"); - _Height = value; + _height = value; } }