Skip to content

Commit

Permalink
Create Window Specific Orientation Extensions (#7338)
Browse files Browse the repository at this point in the history
* Create Window Specific Orientation Extensions

* Update src/Core/src/Platform/Tizen/WindowExtensions.cs

Co-authored-by: Kangho Hur <kangho.hur@samsung.com>

* Update src/Core/src/Platform/Tizen/WindowExtensions.cs

Co-authored-by: Kangho Hur <kangho.hur@samsung.com>

Co-authored-by: Kangho Hur <kangho.hur@samsung.com>
  • Loading branch information
PureWeen and rookiejava committed May 20, 2022
1 parent da75bff commit 7efa74c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
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;
}
}

0 comments on commit 7efa74c

Please sign in to comment.