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

Add support for locking screen orientation on iOS #6461

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Framework.iOS;
using osu.Framework.Tests.Visual;
using UIKit;

namespace osu.Framework.Tests.iOS
{
public partial class TestSceneLockScreenOrientation : FrameworkTestScene
{
[Test]
public void TestToggle()
{
AddToggleStep("lock orientation", v =>
{
var appDelegate = (GameApplicationDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.LockScreenOrientation = v;
});
}
}
}
57 changes: 57 additions & 0 deletions osu.Framework.iOS/GameApplicationDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ public abstract class GameApplicationDelegate : UIApplicationDelegate

private IOSGameHost host = null!;

private bool lockScreenOrientation { get; set; }

/// <summary>
/// Whether the screen orientation should be locked from rotation.
/// </summary>
public bool LockScreenOrientation
{
get => lockScreenOrientation;
set
{
if (lockScreenOrientation == value)
return;

lockScreenOrientation = value;

InvokeOnMainThread(() =>
{
if (OperatingSystem.IsIOSVersionAtLeast(16))
((IOSWindow)host.Window).UIWindow.RootViewController!.SetNeedsUpdateOfSupportedInterfaceOrientations();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ugly line will be improved somewhat once the window API in 753ab06 gets in. Should've been in a separate PR but alas.

else
UIViewController.AttemptRotationToDeviceOrientation();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to assume this is the only function to call as the old counterpart of setNeedsUpdateOfSupportedInterfaceOrientations for iOS -16, I don't have an old iOS device to test this with but documentation and few stackoverflow threads show that this should do the job:

CleanShot 2024-12-16 at 06 46 59

});
}
}

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
mapLibraryNames();
Expand Down Expand Up @@ -55,6 +80,19 @@ public override bool OpenUrl(UIApplication application, NSUrl url, string source
return true;
}

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow? forWindow)
{
if (forWindow == null)
// although not documented anywhere for some reason, this may be called with forWindow = null during initialisation.
// return this random mask just to continue execution without nullrefs anywhere.
// this will be called again with a valid forWindow when it's available anyway.
return UIInterfaceOrientationMask.All;

var allOrientations = application.SupportedInterfaceOrientationsForWindow(forWindow);
var currentOrientation = getOrientationMask(forWindow.WindowScene!.InterfaceOrientation);
return LockScreenOrientation ? currentOrientation : allOrientations;
}

/// <summary>
/// Creates the <see cref="Game"/> class to launch.
/// </summary>
Expand All @@ -68,6 +106,25 @@ private static void mapLibraryNames()
NativeLibrary.SetDllImportResolver(typeof(SDL3).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/SDL3.framework/SDL3", assembly, path));
}

private static UIInterfaceOrientationMask getOrientationMask(UIInterfaceOrientation orientation)
{
switch (orientation)
{
case UIInterfaceOrientation.Portrait:
return UIInterfaceOrientationMask.Portrait;

case UIInterfaceOrientation.PortraitUpsideDown:
return UIInterfaceOrientationMask.PortraitUpsideDown;

default:
case UIInterfaceOrientation.LandscapeRight:
return UIInterfaceOrientationMask.LandscapeRight;

case UIInterfaceOrientation.LandscapeLeft:
return UIInterfaceOrientationMask.LandscapeLeft;
}
}

private class OutputVolumeObserver : NSObject
{
public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, nint context)
Expand Down
Loading