Skip to content

Commit

Permalink
Create Window Specific Orientation Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed May 19, 2022
1 parent 4b833b0 commit dc2c2ec
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controls/src/Core/FlyoutPage.cs
Expand Up @@ -166,7 +166,7 @@ bool IFlyoutPageController.ShouldShowSplitMode
return false;

FlyoutLayoutBehavior behavior = FlyoutLayoutBehavior;
var orientation = DeviceDisplay.MainDisplayInfo.Orientation;
var orientation = this.Window?.GetOrientation() ?? DisplayOrientation.Unknown;

bool isSplitOnLandscape = (behavior == FlyoutLayoutBehavior.SplitOnLandscape || behavior == FlyoutLayoutBehavior.Default) && orientation.IsLandscape();
bool isSplitOnPortrait = behavior == FlyoutLayoutBehavior.SplitOnPortrait && orientation.IsPortrait();
Expand Down
19 changes: 19 additions & 0 deletions src/Core/src/Platform/Android/WindowExtensions.cs
@@ -0,0 +1,19 @@
using Android.App;
using Android.Content.Res;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Platform;

namespace Microsoft.Maui
{
public static partial class WindowExtensions
{
internal static DisplayOrientation GetOrientation(this IWindow window) =>
window.Handler?.MauiContext?.GetPlatformWindow()?.Resources?.Configuration?.Orientation switch
{
Orientation.Landscape => DisplayOrientation.Landscape,
Orientation.Portrait => DisplayOrientation.Portrait,
Orientation.Square => DisplayOrientation.Portrait,
_ => DisplayOrientation.Unknown
};
}
}
10 changes: 10 additions & 0 deletions src/Core/src/Platform/Standard/WindowExtensions.cs
@@ -0,0 +1,10 @@
using Microsoft.Maui.Devices;

namespace Microsoft.Maui
{
public static partial class WindowExtensions
{
internal static DisplayOrientation GetOrientation(this IWindow window) =>
DisplayOrientation.Unknown;
}
}
15 changes: 15 additions & 0 deletions src/Core/src/Platform/Tizen/WindowExtensions.cs
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Threading.Tasks;
using ElmSharp;
using Microsoft.Maui.ApplicationModel;
using Tizen.UIExtensions.Common;
using Tizen.UIExtensions.ElmSharp;
using ELayout = ElmSharp.Layout;
Expand Down Expand Up @@ -110,5 +111,19 @@ static void OnBackButtonPressed(Window window)
s_windowCloseRequestHandler[window].Invoke();
}

internal static Devices.DisplayOrientation GetOrientation(this IWindow window)
{
int displayWidth = PlatformUtils.GetFeatureInfo<int>("screen.width");
int displayHeight = PlatformUtils.GetFeatureInfo<int>("screen.height");

if (displayHeight >= displayWidth)
{
return Devices.DisplayOrientation.Portrait;
}
else
{
return Devices.DisplayOrientation.Landscape;
}
}
}
}
21 changes: 21 additions & 0 deletions src/Core/src/Platform/Windows/WindowExtensions.cs
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.Maui.Media;
using WinRT.Interop;
using Windows.Graphics.Display;

namespace Microsoft.Maui.Platform
{
Expand Down Expand Up @@ -60,5 +61,25 @@ public static float GetDisplayDensity(this UI.Xaml.Window platformWindow)
var windowId = UI.Win32Interop.GetWindowIdFromWindow(hwnd);
return UI.Windowing.AppWindow.GetFromWindowId(windowId);
}

internal static DisplayOrientation GetOrientation(this IWindow window)
{
var appWindow = window.Handler?.MauiContext?.GetPlatformWindow()?.GetAppWindow();

if (appWindow == null)
return DisplayOrientation.Unknown;

DisplayOrientations orientationEnum;
int theScreenWidth = appWindow.Size.Width;
int theScreenHeight = appWindow.Size.Height;
if (theScreenWidth > theScreenHeight)
orientationEnum = DisplayOrientations.Landscape;
else
orientationEnum = DisplayOrientations.Portrait;

return orientationEnum == DisplayOrientations.Landscape
? DisplayOrientation.Landscape
: DisplayOrientation.Portrait;
}
}
}
8 changes: 8 additions & 0 deletions src/Core/src/Platform/iOS/WindowExtensions.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Devices;
using UIKit;

namespace Microsoft.Maui.Platform
Expand Down Expand Up @@ -29,5 +30,12 @@ public static partial class WindowExtensions

public static float GetDisplayDensity(this UIWindow uiWindow) =>
(float)(uiWindow.Screen?.Scale ?? new nfloat(1.0f));

#pragma warning disable CA1416 // UIApplication.StatusBarOrientation has [UnsupportedOSPlatform("ios9.0")]. (Deprecated but still works)
internal static DisplayOrientation GetOrientation(this IWindow window) =>
UIApplication.SharedApplication.StatusBarOrientation.IsLandscape()
? DisplayOrientation.Landscape
: DisplayOrientation.Portrait;
#pragma warning restore CA1416
}
}

0 comments on commit dc2c2ec

Please sign in to comment.