Skip to content

Commit

Permalink
feat: Window size for Mac Catalyst
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 11, 2022
1 parent 0db0498 commit 5a65465
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2520,7 +2520,7 @@ public async Task ValidateFootprint()
#if __IOS__
await RunOnUIThread(() =>
{
expectedCommandBarWidth = ViewHelper.GetScreenSize().Width;
expectedCommandBarWidth = ViewHelper.GetMainWindowSize().Width;
});
#endif
double expectedCommandBarCompactClosedHeight = 40;
Expand Down
29 changes: 8 additions & 21 deletions src/Uno.UI/Extensions/ViewHelper.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#if __IOS__
using UIKit;
using _View = UIKit.UIView;
using Windows.UI.Xaml;
#elif __MACOS__
using AppKit;
using _View = AppKit.NSView;
Expand Down Expand Up @@ -224,48 +225,34 @@ public static nfloat StackSubViews (_View thisView, float topPadding, float spac
/// Gets the orientation-dependent screen size
/// </summary>
/// <returns></returns>
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;
}

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
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Flyout/FlyoutBase.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
};
}

Expand Down
30 changes: 15 additions & 15 deletions src/Uno.UI/UI/Xaml/Window.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 5a65465

Please sign in to comment.