From 5fb4c0ccf614e58be75a6ecfba112547eacef37d Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Sat, 3 Aug 2024 22:11:25 +0200 Subject: [PATCH 01/12] mac catalyst - window - dimensions --- src/Core/src/Platform/iOS/WindowExtensions.cs | 49 ++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index 67c9607deee3..011d8f5b615a 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using CoreGraphics; using Microsoft.Maui.Devices; using UIKit; @@ -15,23 +16,55 @@ internal static void UpdateTitle(this UIWindow platformWindow, IWindow window) // If you set it to an empty string the title reverts back to the // default app title. if (OperatingSystem.IsIOSVersionAtLeast(13) && platformWindow.WindowScene is not null) + { platformWindow.WindowScene.Title = window.Title ?? String.Empty; + } } internal static void UpdateX(this UIWindow platformWindow, IWindow window) => - platformWindow.UpdateUnsupportedCoordinate(window); + platformWindow.UpdateCoordinates(window); internal static void UpdateY(this UIWindow platformWindow, IWindow window) => - platformWindow.UpdateUnsupportedCoordinate(window); + platformWindow.UpdateCoordinates(window); internal static void UpdateWidth(this UIWindow platformWindow, IWindow window) => - platformWindow.UpdateUnsupportedCoordinate(window); + platformWindow.UpdateCoordinates(window); internal static void UpdateHeight(this UIWindow platformWindow, IWindow window) => - platformWindow.UpdateUnsupportedCoordinate(window); + platformWindow.UpdateCoordinates(window); + + internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow window) + { + var rectangle = platformWindow.Bounds.ToRectangle(); + + if (OperatingSystem.IsIOSVersionAtLeast(13)) + { + var windowScene = platformWindow.WindowScene; + + if (windowScene is not null) + { + var frame = windowScene.EffectiveGeometry.SystemFrame; + + // frame.X = (float)window.X; + // frame.Y = (float)window.Y; + // frame.Width = (float)window.Width; + // frame.Height = (float)window.Height; + + System.Console.WriteLine($"frame = {frame}, window=[X={window.X},Y={window.Y},Width={window.Width},Height={window.Height}]"); - internal static void UpdateUnsupportedCoordinate(this UIWindow platformWindow, IWindow window) => - window.FrameChanged(platformWindow.Bounds.ToRectangle()); + var preferences = new UIWindowSceneGeometryPreferencesMac + { + SystemFrame = new CGRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height) + }; + + windowScene.RequestGeometryUpdate(preferences, (error) => { + System.Diagnostics.Debug.WriteLine("" + error); + }); + } + } + + //window.FrameChanged(rectangle); + } public static void UpdateMaximumWidth(this UIWindow platformWindow, IWindow window) => platformWindow.UpdateMaximumSize(window.MaximumWidth, window.MaximumHeight); @@ -45,7 +78,9 @@ public static void UpdateMaximumSize(this UIWindow platformWindow, IWindow windo internal static void UpdateMaximumSize(this UIWindow platformWindow, double width, double height) { if (!OperatingSystem.IsIOSVersionAtLeast(13)) + { return; + } var restrictions = platformWindow.WindowScene?.SizeRestrictions; if (restrictions is null) @@ -71,7 +106,9 @@ public static void UpdateMinimumSize(this UIWindow platformWindow, IWindow windo internal static void UpdateMinimumSize(this UIWindow platformWindow, double width, double height) { if (!OperatingSystem.IsIOSVersionAtLeast(13)) + { return; + } var restrictions = platformWindow.WindowScene?.SizeRestrictions; if (restrictions is null) From 9b72481623bd9386dad9c07ba84a51cbddf130b7 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Sun, 4 Aug 2024 09:49:08 +0200 Subject: [PATCH 02/12] WIP --- src/Core/src/Platform/iOS/WindowExtensions.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index 011d8f5b615a..ace9526d97bf 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -37,21 +37,12 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win { var rectangle = platformWindow.Bounds.ToRectangle(); - if (OperatingSystem.IsIOSVersionAtLeast(13)) + if (OperatingSystem.IsIOSVersionAtLeast(16)) { var windowScene = platformWindow.WindowScene; if (windowScene is not null) { - var frame = windowScene.EffectiveGeometry.SystemFrame; - - // frame.X = (float)window.X; - // frame.Y = (float)window.Y; - // frame.Width = (float)window.Width; - // frame.Height = (float)window.Height; - - System.Console.WriteLine($"frame = {frame}, window=[X={window.X},Y={window.Y},Width={window.Width},Height={window.Height}]"); - var preferences = new UIWindowSceneGeometryPreferencesMac { SystemFrame = new CGRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height) @@ -62,8 +53,11 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win }); } } - - //window.FrameChanged(rectangle); + else + { + // TODO: When should this be called exactly? + window.FrameChanged(rectangle); + } } public static void UpdateMaximumWidth(this UIWindow platformWindow, IWindow window) => From f4ff908de35011f4088ecd242a5f6d2c77e9e25f Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Tue, 6 Aug 2024 20:09:46 +0200 Subject: [PATCH 03/12] WIP --- .../src/Handlers/Window/WindowHandler.iOS.cs | 27 +++++++++++++++++-- src/Core/src/Platform/iOS/WindowExtensions.cs | 10 ++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index 4025e0b61e58..1bc1a79bc173 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -1,4 +1,5 @@ using System; +using Foundation; using ObjCRuntime; using UIKit; @@ -6,12 +7,35 @@ namespace Microsoft.Maui.Handlers { public partial class WindowHandler : ElementHandler { + private IDisposable? effectiveGeometryObserver; + protected override void ConnectHandler(UIWindow platformView) { base.ConnectHandler(platformView); UpdateVirtualViewFrame(platformView); + + effectiveGeometryObserver = platformView.WindowScene?.AddObserver("effectiveGeometry", NSKeyValueObservingOptions.OldNew, HandleEffectiveGeometryObserved); + } + +#pragma warning disable RS0016 // Add public types and members to the declared API + protected override void DisconnectHandler(UIWindow platformView) +#pragma warning restore RS0016 // Add public types and members to the declared API + { + base.DisconnectHandler(platformView); + + effectiveGeometryObserver?.Dispose(); } + + void HandleEffectiveGeometryObserved(NSObservedChange obj) + { + if (obj is not null && obj.OldValue is UIWindowSceneGeometry oldGeometry && obj.NewValue is UIWindowSceneGeometry newGeometry) + { + Console.WriteLine($" oldValue={oldGeometry.SystemFrame}, newValue={newGeometry.SystemFrame}"); + VirtualView.FrameChanged(newGeometry.SystemFrame.ToRectangle()); + } + } + public static void MapTitle(IWindowHandler handler, IWindow window) => handler.PlatformView.UpdateTitle(window); @@ -23,8 +47,7 @@ public static void MapContent(IWindowHandler handler, IWindow window) handler.PlatformView.RootViewController = nativeContent; - if (window.VisualDiagnosticsOverlay != null) - window.VisualDiagnosticsOverlay.Initialize(); + window.VisualDiagnosticsOverlay?.Initialize(); } public static void MapX(IWindowHandler handler, IWindow view) => diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index ace9526d97bf..862e0fab4648 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -35,7 +35,9 @@ internal static void UpdateHeight(this UIWindow platformWindow, IWindow window) internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow window) { + var callFrameChanged = true; var rectangle = platformWindow.Bounds.ToRectangle(); + Console.WriteLine($"UpdateCoordinates: platformWindow={rectangle}, window=[X={window.X}, Y={window.Y}, Width={window.Width}, Height Height={window.Height}]"); if (OperatingSystem.IsIOSVersionAtLeast(16)) { @@ -45,17 +47,19 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win { var preferences = new UIWindowSceneGeometryPreferencesMac { - SystemFrame = new CGRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height) + SystemFrame = new CGRect(window.X, window.Y, window.Width, window.Height) }; windowScene.RequestGeometryUpdate(preferences, (error) => { System.Diagnostics.Debug.WriteLine("" + error); }); + + callFrameChanged = false; } } - else + + if (callFrameChanged) { - // TODO: When should this be called exactly? window.FrameChanged(rectangle); } } From bbf4c5c3e5c0773d542d86289e7a91ef2ca09998 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:33:01 +0200 Subject: [PATCH 04/12] cleanup --- .../src/Handlers/Window/WindowHandler.iOS.cs | 16 +++++++--------- src/Core/src/Platform/iOS/WindowExtensions.cs | 9 +++------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index 1bc1a79bc173..eecc0751b46c 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -1,13 +1,12 @@ using System; using Foundation; -using ObjCRuntime; using UIKit; namespace Microsoft.Maui.Handlers { public partial class WindowHandler : ElementHandler { - private IDisposable? effectiveGeometryObserver; + private IDisposable? _effectiveGeometryObserver; protected override void ConnectHandler(UIWindow platformView) { @@ -15,23 +14,20 @@ protected override void ConnectHandler(UIWindow platformView) UpdateVirtualViewFrame(platformView); - effectiveGeometryObserver = platformView.WindowScene?.AddObserver("effectiveGeometry", NSKeyValueObservingOptions.OldNew, HandleEffectiveGeometryObserved); + _effectiveGeometryObserver = platformView.WindowScene?.AddObserver("effectiveGeometry", NSKeyValueObservingOptions.OldNew, HandleEffectiveGeometryObserved); } -#pragma warning disable RS0016 // Add public types and members to the declared API protected override void DisconnectHandler(UIWindow platformView) -#pragma warning restore RS0016 // Add public types and members to the declared API { - base.DisconnectHandler(platformView); + _effectiveGeometryObserver?.Dispose(); - effectiveGeometryObserver?.Dispose(); + base.DisconnectHandler(platformView); } void HandleEffectiveGeometryObserved(NSObservedChange obj) { - if (obj is not null && obj.OldValue is UIWindowSceneGeometry oldGeometry && obj.NewValue is UIWindowSceneGeometry newGeometry) + if (obj is not null && obj.NewValue is UIWindowSceneGeometry newGeometry) { - Console.WriteLine($" oldValue={oldGeometry.SystemFrame}, newValue={newGeometry.SystemFrame}"); VirtualView.FrameChanged(newGeometry.SystemFrame.ToRectangle()); } } @@ -105,7 +101,9 @@ public static void MapMenuBar(IWindowHandler handler, IWindow view) public static void MapRequestDisplayDensity(IWindowHandler handler, IWindow window, object? args) { if (args is DisplayDensityRequest request) + { request.SetResult(handler.PlatformView.GetDisplayDensity()); + } } void UpdateVirtualViewFrame(UIWindow window) diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index 862e0fab4648..18149f90435b 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -37,7 +37,6 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win { var callFrameChanged = true; var rectangle = platformWindow.Bounds.ToRectangle(); - Console.WriteLine($"UpdateCoordinates: platformWindow={rectangle}, window=[X={window.X}, Y={window.Y}, Width={window.Width}, Height Height={window.Height}]"); if (OperatingSystem.IsIOSVersionAtLeast(16)) { @@ -45,15 +44,13 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win if (windowScene is not null) { - var preferences = new UIWindowSceneGeometryPreferencesMac + UIWindowSceneGeometryPreferencesMac preferences = new() { SystemFrame = new CGRect(window.X, window.Y, window.Width, window.Height) }; - windowScene.RequestGeometryUpdate(preferences, (error) => { - System.Diagnostics.Debug.WriteLine("" + error); - }); - + // TODO: Log errors. + windowScene.RequestGeometryUpdate(preferences, (error) => { }); callFrameChanged = false; } } From 46ddc99f19f2f7779f0b883ddd1130977ae094b4 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:56:31 +0200 Subject: [PATCH 05/12] Fix reseting of window dimensions --- .../LifecycleEvents/AppHostBuilderExtensions.iOS.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs index a7044fcb97a5..10d8ef63a945 100644 --- a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs +++ b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs @@ -105,21 +105,30 @@ static void OnConfigureWindow(IiOSLifecycleBuilder iOS) { // Pre iOS 13 doesn't support scenes if (!OperatingSystem.IsIOSVersionAtLeast(13)) + { return; + } iOS = iOS .WindowSceneDidUpdateCoordinateSpace((windowScene, _, _, _) => { - if (!OperatingSystem.IsIOSVersionAtLeast(13)) + // iOS 16+ supports effectiveGeometry property on window scenes. + if (!OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsIOSVersionAtLeast(16)) + { return; + } if (windowScene.Delegate is not IUIWindowSceneDelegate wsd || wsd.GetWindow() is not UIWindow platformWindow) + { return; + } var window = platformWindow.GetWindow(); if (window is null) + { return; + } window.FrameChanged(platformWindow.Frame.ToRectangle()); }); From 2b7fb4407a8d299dde7ec2eea3a87f8b751379ad Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:22:32 +0200 Subject: [PATCH 06/12] Update src/Core/src/Platform/iOS/WindowExtensions.cs Co-authored-by: Matthew Leibowitz --- src/Core/src/Platform/iOS/WindowExtensions.cs | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index 18149f90435b..d7ea2ac68795 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -35,28 +35,19 @@ internal static void UpdateHeight(this UIWindow platformWindow, IWindow window) internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow window) { - var callFrameChanged = true; - var rectangle = platformWindow.Bounds.ToRectangle(); - - if (OperatingSystem.IsIOSVersionAtLeast(16)) + if (OperatingSystem.IsIOSVersionAtLeast(16) && platformWindow.WindowScene is {} windowScene)) { - var windowScene = platformWindow.WindowScene; - - if (windowScene is not null) + var preferences = new UIWindowSceneGeometryPreferencesMac() { - UIWindowSceneGeometryPreferencesMac preferences = new() - { - SystemFrame = new CGRect(window.X, window.Y, window.Width, window.Height) - }; - - // TODO: Log errors. - windowScene.RequestGeometryUpdate(preferences, (error) => { }); - callFrameChanged = false; - } - } + SystemFrame = new CGRect(window.X, window.Y, window.Width, window.Height) + }; - if (callFrameChanged) + // TODO: Log errors. + windowScene.RequestGeometryUpdate(preferences, (error) => { }); + } + else { + var rectangle = platformWindow.Bounds.ToRectangle(); window.FrameChanged(rectangle); } } From a2c5dd6eb120ec8a3a6630f43002ec0756fb2b9f Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:47:55 +0200 Subject: [PATCH 07/12] Feedback --- .../src/Handlers/Window/WindowHandler.iOS.cs | 43 +++++++++++++------ src/Core/src/Platform/iOS/WindowExtensions.cs | 2 +- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index eecc0751b46c..5fc9b8009828 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -6,32 +6,22 @@ namespace Microsoft.Maui.Handlers { public partial class WindowHandler : ElementHandler { - private IDisposable? _effectiveGeometryObserver; + readonly WindowProxy _proxy = new(); protected override void ConnectHandler(UIWindow platformView) { base.ConnectHandler(platformView); UpdateVirtualViewFrame(platformView); - - _effectiveGeometryObserver = platformView.WindowScene?.AddObserver("effectiveGeometry", NSKeyValueObservingOptions.OldNew, HandleEffectiveGeometryObserved); + _proxy.Connect(VirtualView, platformView); } protected override void DisconnectHandler(UIWindow platformView) { - _effectiveGeometryObserver?.Dispose(); - + _proxy.Disconnect(); base.DisconnectHandler(platformView); } - void HandleEffectiveGeometryObserved(NSObservedChange obj) - { - if (obj is not null && obj.NewValue is UIWindowSceneGeometry newGeometry) - { - VirtualView.FrameChanged(newGeometry.SystemFrame.ToRectangle()); - } - } - public static void MapTitle(IWindowHandler handler, IWindow window) => handler.PlatformView.UpdateTitle(window); @@ -110,5 +100,32 @@ void UpdateVirtualViewFrame(UIWindow window) { VirtualView.FrameChanged(window.Bounds.ToRectangle()); } + + class WindowProxy + { + WeakReference? _virtualView; + + IWindow? VirtualView => _virtualView is not null && _virtualView.TryGetTarget(out var v) ? v : null; + IDisposable? _effectiveGeometryObserver; + + public void Connect(IWindow virtualView, UIWindow platformView) + { + _virtualView = new(virtualView); + _effectiveGeometryObserver = platformView.WindowScene?.AddObserver("effectiveGeometry", NSKeyValueObservingOptions.OldNew, HandleEffectiveGeometryObserved); + } + + public void Disconnect() + { + _effectiveGeometryObserver?.Dispose(); + } + + void HandleEffectiveGeometryObserved(NSObservedChange obj) + { + if (obj is not null && VirtualView is IWindow virtualView && obj.NewValue is UIWindowSceneGeometry newGeometry) + { + virtualView.FrameChanged(newGeometry.SystemFrame.ToRectangle()); + } + } + } } } \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index d7ea2ac68795..f62d67eb4cf8 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -35,7 +35,7 @@ internal static void UpdateHeight(this UIWindow platformWindow, IWindow window) internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow window) { - if (OperatingSystem.IsIOSVersionAtLeast(16) && platformWindow.WindowScene is {} windowScene)) + if (OperatingSystem.IsIOSVersionAtLeast(16) && platformWindow.WindowScene is {} windowScene) { var preferences = new UIWindowSceneGeometryPreferencesMac() { From 6cbf4eef14576ab521f71b15b4b32fc19938b36f Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:21:45 +0200 Subject: [PATCH 08/12] Handle initial NaNs --- .../src/Handlers/Window/WindowHandler.iOS.cs | 16 ++++++++++++++-- src/Core/src/Platform/iOS/WindowExtensions.cs | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index 5fc9b8009828..9db0c0b68c0f 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -12,7 +12,12 @@ protected override void ConnectHandler(UIWindow platformView) { base.ConnectHandler(platformView); - UpdateVirtualViewFrame(platformView); + // For newer iOS versions, we want to wait until we get effective window dimensions from the platform. + if (!OperatingSystem.IsIOSVersionAtLeast(16)) + { + UpdateVirtualViewFrame(platformView); + } + _proxy.Connect(VirtualView, platformView); } @@ -123,7 +128,14 @@ void HandleEffectiveGeometryObserved(NSObservedChange obj) { if (obj is not null && VirtualView is IWindow virtualView && obj.NewValue is UIWindowSceneGeometry newGeometry) { - virtualView.FrameChanged(newGeometry.SystemFrame.ToRectangle()); + var newRectangle = newGeometry.SystemFrame.ToRectangle(); + + if (double.IsNaN(newRectangle.X) || double.IsNaN(newRectangle.Y) || double.IsNaN(newRectangle.Width) || double.IsNaN(newRectangle.Height)) + { + return; + } + + virtualView.FrameChanged(newRectangle); } } } diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index f62d67eb4cf8..bef2a5f1481a 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -37,6 +37,11 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win { if (OperatingSystem.IsIOSVersionAtLeast(16) && platformWindow.WindowScene is {} windowScene) { + if (double.IsNaN(window.X) || double.IsNaN(window.Y) || double.IsNaN(window.Width) || double.IsNaN(window.Height)) + { + return; + } + var preferences = new UIWindowSceneGeometryPreferencesMac() { SystemFrame = new CGRect(window.X, window.Y, window.Width, window.Height) From 9fbb53a9ee63d22f948a592e9feb3b2deb95a774 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:39:07 +0200 Subject: [PATCH 09/12] Guard --- src/Core/src/Handlers/Window/WindowHandler.iOS.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index 9db0c0b68c0f..9be795c82c0a 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -16,14 +16,20 @@ protected override void ConnectHandler(UIWindow platformView) if (!OperatingSystem.IsIOSVersionAtLeast(16)) { UpdateVirtualViewFrame(platformView); + } + else + { + _proxy.Connect(VirtualView, platformView); } - - _proxy.Connect(VirtualView, platformView); } protected override void DisconnectHandler(UIWindow platformView) { - _proxy.Disconnect(); + if (OperatingSystem.IsIOSVersionAtLeast(16)) + { + _proxy.Disconnect(); + } + base.DisconnectHandler(platformView); } From dab9393683ebc16bf284b1692cd4da774409db3b Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:46:51 +0200 Subject: [PATCH 10/12] Modify PublicAPI.Unshipped.txt files --- src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt | 1 + src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt b/src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt index 4b24efde1786..79f367a6711d 100644 --- a/src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt +++ b/src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt @@ -77,3 +77,4 @@ static Microsoft.Maui.ViewExtensions.DisconnectHandlers(this Microsoft.Maui.IVie override Microsoft.Maui.Platform.MauiCALayer.AddAnimation(CoreAnimation.CAAnimation! animation, string? key) -> void *REMOVED*override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void override Microsoft.Maui.Handlers.BorderHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect rect) -> void +override Microsoft.Maui.Handlers.WindowHandler.DisconnectHandler(UIKit.UIWindow! platformView) -> void diff --git a/src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt b/src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt index 4b24efde1786..79f367a6711d 100644 --- a/src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt +++ b/src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt @@ -77,3 +77,4 @@ static Microsoft.Maui.ViewExtensions.DisconnectHandlers(this Microsoft.Maui.IVie override Microsoft.Maui.Platform.MauiCALayer.AddAnimation(CoreAnimation.CAAnimation! animation, string? key) -> void *REMOVED*override Microsoft.Maui.Handlers.BorderHandler.ConnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void override Microsoft.Maui.Handlers.BorderHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect rect) -> void +override Microsoft.Maui.Handlers.WindowHandler.DisconnectHandler(UIKit.UIWindow! platformView) -> void From 1650bee04598a0ce3cac3a7265bdff6337590bd4 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:20:20 +0100 Subject: [PATCH 11/12] Fix tests by applying new code only to Mac Catalyst and not iOS --- src/Core/src/Handlers/Window/WindowHandler.iOS.cs | 10 +++++----- .../LifecycleEvents/AppHostBuilderExtensions.iOS.cs | 4 ++-- src/Core/src/Platform/iOS/WindowExtensions.cs | 5 ++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index 9be795c82c0a..35fb4b322f2d 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -12,20 +12,20 @@ protected override void ConnectHandler(UIWindow platformView) { base.ConnectHandler(platformView); - // For newer iOS versions, we want to wait until we get effective window dimensions from the platform. - if (!OperatingSystem.IsIOSVersionAtLeast(16)) + // For newer Mac Catalyst versions, we want to wait until we get effective window dimensions from the platform. + if (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16)) { - UpdateVirtualViewFrame(platformView); + _proxy.Connect(VirtualView, platformView); } else { - _proxy.Connect(VirtualView, platformView); + UpdateVirtualViewFrame(platformView); } } protected override void DisconnectHandler(UIWindow platformView) { - if (OperatingSystem.IsIOSVersionAtLeast(16)) + if (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16)) { _proxy.Disconnect(); } diff --git a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs index 10d8ef63a945..51019783e81d 100644 --- a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs +++ b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs @@ -112,8 +112,8 @@ static void OnConfigureWindow(IiOSLifecycleBuilder iOS) iOS = iOS .WindowSceneDidUpdateCoordinateSpace((windowScene, _, _, _) => { - // iOS 16+ supports effectiveGeometry property on window scenes. - if (!OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsIOSVersionAtLeast(16)) + // Mac Catalyst version 16+ supports effectiveGeometry property on window scenes. + if (!OperatingSystem.IsIOSVersionAtLeast(13) || (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16))) { return; } diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index bef2a5f1481a..77ff7219a2eb 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -35,7 +35,7 @@ internal static void UpdateHeight(this UIWindow platformWindow, IWindow window) internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow window) { - if (OperatingSystem.IsIOSVersionAtLeast(16) && platformWindow.WindowScene is {} windowScene) + if (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16) && platformWindow.WindowScene is {} windowScene) { if (double.IsNaN(window.X) || double.IsNaN(window.Y) || double.IsNaN(window.Width) || double.IsNaN(window.Height)) { @@ -52,8 +52,7 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win } else { - var rectangle = platformWindow.Bounds.ToRectangle(); - window.FrameChanged(rectangle); + window.FrameChanged(platformWindow.Bounds.ToRectangle()); } } From 6b719b1d87febeac69dfe25a5c0979503ac6408d Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:50:37 +0100 Subject: [PATCH 12/12] review --- src/Core/src/Handlers/Window/WindowHandler.iOS.cs | 9 +++++++-- .../LifecycleEvents/AppHostBuilderExtensions.iOS.cs | 2 +- src/Core/src/Platform/iOS/WindowExtensions.cs | 6 ++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs index 35fb4b322f2d..13e2eedcc629 100644 --- a/src/Core/src/Handlers/Window/WindowHandler.iOS.cs +++ b/src/Core/src/Handlers/Window/WindowHandler.iOS.cs @@ -13,7 +13,7 @@ protected override void ConnectHandler(UIWindow platformView) base.ConnectHandler(platformView); // For newer Mac Catalyst versions, we want to wait until we get effective window dimensions from the platform. - if (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16)) + if (OperatingSystem.IsMacCatalystVersionAtLeast(16)) { _proxy.Connect(VirtualView, platformView); } @@ -25,7 +25,7 @@ protected override void ConnectHandler(UIWindow platformView) protected override void DisconnectHandler(UIWindow platformView) { - if (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16)) + if (OperatingSystem.IsMacCatalystVersionAtLeast(16)) { _proxy.Disconnect(); } @@ -122,6 +122,11 @@ class WindowProxy public void Connect(IWindow virtualView, UIWindow platformView) { _virtualView = new(virtualView); + + // https://developer.apple.com/documentation/uikit/uiwindowscene/effectivegeometry?language=objc#Discussion mentions: + // > This property is key-value observing (KVO) compliant. Observing effectiveGeometry is the recommended way + // > to receive notifications of changes to the window scene’s geometry. These changes can occur because of + // > user interaction or as a result of the system resolving a geometry request. _effectiveGeometryObserver = platformView.WindowScene?.AddObserver("effectiveGeometry", NSKeyValueObservingOptions.OldNew, HandleEffectiveGeometryObserved); } diff --git a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs index 51019783e81d..3cd1e1d3bf45 100644 --- a/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs +++ b/src/Core/src/Hosting/LifecycleEvents/AppHostBuilderExtensions.iOS.cs @@ -113,7 +113,7 @@ static void OnConfigureWindow(IiOSLifecycleBuilder iOS) .WindowSceneDidUpdateCoordinateSpace((windowScene, _, _, _) => { // Mac Catalyst version 16+ supports effectiveGeometry property on window scenes. - if (!OperatingSystem.IsIOSVersionAtLeast(13) || (OperatingSystem.IsMacCatalyst() && OperatingSystem.IsIOSVersionAtLeast(16))) + if (!OperatingSystem.IsIOSVersionAtLeast(13) || (OperatingSystem.IsMacCatalystVersionAtLeast(16))) { return; } diff --git a/src/Core/src/Platform/iOS/WindowExtensions.cs b/src/Core/src/Platform/iOS/WindowExtensions.cs index 77ff7219a2eb..65e2f745358b 100644 --- a/src/Core/src/Platform/iOS/WindowExtensions.cs +++ b/src/Core/src/Platform/iOS/WindowExtensions.cs @@ -3,6 +3,7 @@ using System.Text; using System.Threading.Tasks; using CoreGraphics; +using Microsoft.Extensions.Logging; using Microsoft.Maui.Devices; using UIKit; @@ -47,8 +48,9 @@ internal static void UpdateCoordinates(this UIWindow platformWindow, IWindow win SystemFrame = new CGRect(window.X, window.Y, window.Width, window.Height) }; - // TODO: Log errors. - windowScene.RequestGeometryUpdate(preferences, (error) => { }); + windowScene.RequestGeometryUpdate(preferences, (error) => { + window.Handler?.MauiContext?.CreateLogger()?.LogError("Requesting geometry update failed with error '{error}'.", error); + }); } else {