Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Window Specific Orientation Extensions #7338

Merged
merged 4 commits into from May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = Window.GetOrientation();

bool isSplitOnLandscape = (behavior == FlyoutLayoutBehavior.SplitOnLandscape || behavior == FlyoutLayoutBehavior.Default) && orientation.IsLandscape();
bool isSplitOnPortrait = behavior == FlyoutLayoutBehavior.SplitOnPortrait && orientation.IsPortrait();
Expand Down
3 changes: 1 addition & 2 deletions src/Controls/tests/Core.UnitTests/FlyoutPageUnitTests.cs
Expand Up @@ -281,7 +281,7 @@ public void ThrowsInSetIsPresentOnSplitModeOnTablet()
}

[Test]
public void ThorwsInSetIsPresentOnSplitPortraitModeOnTablet()
public void ThrowsInSetIsPresentOnSplitPortraitModeOnTablet()
{
mockDeviceInfo.Idiom = DeviceIdiom.Tablet;
mockDeviceDisplay.SetMainDisplayOrientation(DisplayOrientation.Portrait);
Expand All @@ -290,7 +290,6 @@ public void ThorwsInSetIsPresentOnSplitPortraitModeOnTablet()
{
Flyout = new ContentPage { Content = new View(), IsPlatformEnabled = true, Title = "Foo" },
Detail = new ContentPage { Content = new View(), IsPlatformEnabled = true },
IsPlatformEnabled = true,
FlyoutLayoutBehavior = FlyoutLayoutBehavior.SplitOnPortrait
};

Expand Down
24 changes: 24 additions & 0 deletions src/Core/src/Platform/Android/WindowExtensions.cs
@@ -0,0 +1,24 @@
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)
{
if (window == null)
return DeviceDisplay.Current.MainDisplayInfo.Orientation;

return 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) =>
DeviceDisplay.Current.MainDisplayInfo.Orientation;
}
}
16 changes: 16 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,20 @@ static void OnBackButtonPressed(Window window)
s_windowCloseRequestHandler[window].Invoke();
}

internal static Devices.DisplayOrientation GetOrientation(this IWindow? window)
{
if (window == null)
return Devices.DeviceDisplay.Current.MainDisplayInfo.Orientation;

bool isTV = Elementary.GetProfile() == "tv";
return window.Handler?.MauiContext?.GetPlatformWindow()?.Rotation switch
{
0 => isTV ? Devices.DisplayOrientation.Landscape : Devices.DisplayOrientation.Portrait,
90 => isTV ? Devices.DisplayOrientation.Portrait : Devices.DisplayOrientation.Landscape,
180 => isTV ? Devices.DisplayOrientation.Landscape : Devices.DisplayOrientation.Portrait,
270 => isTV ? Devices.DisplayOrientation.Portrait : Devices.DisplayOrientation.Landscape,
_ => Devices.DisplayOrientation.Unknown
};
}
}
}
24 changes: 24 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,28 @@ 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)
{
if (window == null)
return DeviceDisplay.Current.MainDisplayInfo.Orientation;

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;
}
}
}
4 changes: 4 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,8 @@ public static partial class WindowExtensions

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

internal static DisplayOrientation GetOrientation(this IWindow? window) =>
DeviceDisplay.Current.MainDisplayInfo.Orientation;
}
}