From 5a65465035fa0c2e9653e2744f203e26ba09d8de Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 10 May 2022 13:48:58 +0200 Subject: [PATCH] feat: Window size for Mac Catalyst --- .../commandbar/CommandBarIntegrationTests.cs | 2 +- src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs | 29 +++++------------- .../Controls/Flyout/FlyoutBase.iOSmacOS.cs | 2 +- .../TimePicker/TimePickerFlyout.iOS.cs | 2 +- src/Uno.UI/UI/Xaml/Window.iOS.cs | 30 +++++++++---------- 5 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/IntegrationTests/dxaml/controls/commandbar/CommandBarIntegrationTests.cs b/src/Uno.UI.RuntimeTests/IntegrationTests/dxaml/controls/commandbar/CommandBarIntegrationTests.cs index d96eb80933a8..3a9b8b3ac022 100644 --- a/src/Uno.UI.RuntimeTests/IntegrationTests/dxaml/controls/commandbar/CommandBarIntegrationTests.cs +++ b/src/Uno.UI.RuntimeTests/IntegrationTests/dxaml/controls/commandbar/CommandBarIntegrationTests.cs @@ -2520,7 +2520,7 @@ public async Task ValidateFootprint() #if __IOS__ await RunOnUIThread(() => { - expectedCommandBarWidth = ViewHelper.GetScreenSize().Width; + expectedCommandBarWidth = ViewHelper.GetMainWindowSize().Width; }); #endif double expectedCommandBarCompactClosedHeight = 40; diff --git a/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs b/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs index 53a1e1082ef3..ace5b8125d3e 100644 --- a/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs +++ b/src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs @@ -16,6 +16,7 @@ #if __IOS__ using UIKit; using _View = UIKit.UIView; +using Windows.UI.Xaml; #elif __MACOS__ using AppKit; using _View = AppKit.NSView; @@ -224,31 +225,21 @@ public static nfloat StackSubViews (_View thisView, float topPadding, float spac /// Gets the orientation-dependent screen size /// /// - public static CGSize GetScreenSize() + public static CGSize GetMainWindowSize() { #if __IOS__ - var orientation = UIApplication.SharedApplication.StatusBarOrientation; - //In iOS versions prior to 8.0, the screen dimensions are based on the portrait orientation, so we might have to invert them. - //http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8 - var shouldInvertDimension = !UIDevice.CurrentDevice.CheckSystemVersion(8, 0) - && (orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight); - - return new CGSize(GetScreenWidth(shouldInvertDimension), GetScreenHeight(shouldInvertDimension)); + var width = Window.Current.NativeWindow.Frame.Width; + var height = Window.Current.NativeWindow.Frame.Height; + var windowSize = new CGSize(width, height); + return windowSize; #elif __MACOS__ return new CGSize(GetScreenWidth(false), GetScreenHeight(false)); #endif } +#if __MACOS__ private static nfloat GetScreenWidth(bool shouldInvertDimension) { -#if __IOS__ - //Starting with iOS 9 and the introduction of the SplitView, - //MainScreen.Bounds corresponds to the full screen size whereas the MainScreen.ApplicationFrame is the actual space the application is taking - var applicationFrameSize = UIScreen.MainScreen.ApplicationFrame.Size; -#elif __MACOS__ - var applicationFrameSize = NSScreen.MainScreen.VisibleFrame; -#endif - return shouldInvertDimension ? applicationFrameSize.Height : applicationFrameSize.Width; @@ -256,16 +247,12 @@ private static nfloat GetScreenWidth(bool shouldInvertDimension) private static nfloat GetScreenHeight(bool shouldInvertDimension) { -#if __IOS__ - //Since there cannot be any vertical split, we can use MainScreen.Bounds for the height to ignore the StatusBar height - var fullScreenSize = UIScreen.MainScreen.Bounds.Size; -#elif __MACOS__ var fullScreenSize = NSScreen.MainScreen.VisibleFrame.Size; -#endif return shouldInvertDimension ? fullScreenSize.Width : fullScreenSize.Height; } +#endif } } diff --git a/src/Uno.UI/UI/Xaml/Controls/Flyout/FlyoutBase.iOSmacOS.cs b/src/Uno.UI/UI/Xaml/Controls/Flyout/FlyoutBase.iOSmacOS.cs index da005315139b..ff39e32fd59f 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Flyout/FlyoutBase.iOSmacOS.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Flyout/FlyoutBase.iOSmacOS.cs @@ -34,7 +34,7 @@ partial void InitializePopupPanelPartial() NSViewResizingMask.MinYMargin | NSViewResizingMask.MaxYMargin, #endif - Frame = new CGRect(CGPoint.Empty, ViewHelper.GetScreenSize()) + Frame = new CGRect(CGPoint.Empty, ViewHelper.GetMainWindowSize()) }; } } diff --git a/src/Uno.UI/UI/Xaml/Controls/TimePicker/TimePickerFlyout.iOS.cs b/src/Uno.UI/UI/Xaml/Controls/TimePicker/TimePickerFlyout.iOS.cs index 0b8068019d55..ea5884417096 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TimePicker/TimePickerFlyout.iOS.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TimePicker/TimePickerFlyout.iOS.cs @@ -51,7 +51,7 @@ protected override void InitializePopupPanel() Visibility = Visibility.Collapsed, Background = SolidColorBrushHelper.Transparent, AutoresizingMask = UIViewAutoresizing.All, - Frame = new CGRect(CGPoint.Empty, ViewHelper.GetScreenSize()) + Frame = new CGRect(CGPoint.Empty, ViewHelper.GetMainWindowSize()) }; } diff --git a/src/Uno.UI/UI/Xaml/Window.iOS.cs b/src/Uno.UI/UI/Xaml/Window.iOS.cs index 02b7605362a9..45b6efded2bb 100644 --- a/src/Uno.UI/UI/Xaml/Window.iOS.cs +++ b/src/Uno.UI/UI/Xaml/Window.iOS.cs @@ -19,7 +19,7 @@ namespace Windows.UI.Xaml { public sealed partial class Window { - private Uno.UI.Controls.Window _window; + private Uno.UI.Controls.Window _nativeWindow; private static Window _current; private RootVisual _rootVisual; @@ -37,7 +37,7 @@ public sealed partial class Window public Window() { - _window = new Uno.UI.Controls.Window(); + _nativeWindow = new Uno.UI.Controls.Window(); _mainController = ViewControllerGenerator?.Invoke() ?? new RootViewController(); _mainController.View.BackgroundColor = UIColor.Clear; @@ -46,42 +46,42 @@ public Window() ObserveOrientationAndSize(); Dispatcher = CoreDispatcher.Main; - CoreWindow = new CoreWindow(_window); + CoreWindow = new CoreWindow(_nativeWindow); InitializeCommon(); } + internal Uno.UI.Controls.Window NativeWindow => _nativeWindow; + private void ObserveOrientationAndSize() { _orientationRegistration = UIApplication .Notifications .ObserveDidChangeStatusBarOrientation( - (sender, args) => RaiseNativeSizeChanged(ViewHelper.GetScreenSize()) + (sender, args) => RaiseNativeSizeChanged(ViewHelper.GetMainWindowSize()) ); _orientationRegistration = UIApplication .Notifications .ObserveDidChangeStatusBarFrame( - (sender, args) => RaiseNativeSizeChanged(ViewHelper.GetScreenSize()) + (sender, args) => RaiseNativeSizeChanged(ViewHelper.GetMainWindowSize()) ); - _window.FrameChanged += - () => RaiseNativeSizeChanged(ViewHelper.GetScreenSize()); + _nativeWindow.FrameChanged += + () => RaiseNativeSizeChanged(ViewHelper.GetMainWindowSize()); _mainController.VisibleBoundsChanged += - () => RaiseNativeSizeChanged(ViewHelper.GetScreenSize()); + () => RaiseNativeSizeChanged(ViewHelper.GetMainWindowSize()); var statusBar = StatusBar.GetForCurrentView(); - statusBar.Showing += (o, e) => RaiseNativeSizeChanged(ViewHelper.GetScreenSize()); - statusBar.Hiding += (o, e) => RaiseNativeSizeChanged(ViewHelper.GetScreenSize()); - - RaiseNativeSizeChanged(ViewHelper.GetScreenSize()); + statusBar.Showing += (o, e) => RaiseNativeSizeChanged(ViewHelper.GetMainWindowSize()); + statusBar.Hiding += (o, e) => RaiseNativeSizeChanged(ViewHelper.GetMainWindowSize()); } partial void InternalActivate() { - _window.RootViewController = _mainController; - _window.MakeKeyAndVisible(); + _nativeWindow.RootViewController = _mainController; + _nativeWindow.MakeKeyAndVisible(); } private void InternalSetContent(UIElement value) @@ -126,7 +126,7 @@ internal void RaiseNativeSizeChanged(CGSize size) { var newBounds = new Rect(0, 0, size.Width, size.Height); - ApplicationView.GetForCurrentView()?.SetVisibleBounds(_window, newBounds); + ApplicationView.GetForCurrentView()?.SetVisibleBounds(_nativeWindow, newBounds); if (Bounds != newBounds) {