-
Notifications
You must be signed in to change notification settings - Fork 424
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
osu.Framework.Tests.iOS/Visual/iOS/TestSceneLockScreenOrientation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
else | ||
UIViewController.AttemptRotationToDeviceOrientation(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}); | ||
} | ||
} | ||
|
||
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) | ||
{ | ||
mapLibraryNames(); | ||
|
@@ -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> | ||
|
@@ -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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.