iOS: Support a helper bar button to lock the current orientation (#15633)

* iOS: Support a helper bar button to lock the current orientation

* Moved init of shouldLockCurrentInterfaceOrientation property to iOS specific #if
This commit is contained in:
yoshisuga 2023-08-21 20:05:34 -10:00 committed by GitHub
parent cf9947b7c7
commit cb2cc58f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 3 deletions

View File

@ -10,8 +10,10 @@ protocol HelperBarActionDelegate: AnyObject {
func keyboardButtonTapped()
func mouseButtonTapped()
func helpButtonTapped()
func orientationLockButtonTapped()
var isKeyboardEnabled: Bool { get }
var isMouseEnabled: Bool { get }
var isOrientationLocked: Bool { get }
}
extension CocoaView {
@ -47,6 +49,17 @@ extension CocoaView: HelperBarActionDelegate {
func helpButtonTapped() {
}
func orientationLockButtonTapped() {
shouldLockCurrentInterfaceOrientation.toggle()
if shouldLockCurrentInterfaceOrientation {
let currentOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? UIInterfaceOrientation.portrait
self.lockInterfaceOrientation = currentOrientation
}
if #available(iOS 16, *) {
setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
var isKeyboardEnabled: Bool {
!keyboardController.view.isHidden
}
@ -54,4 +67,8 @@ extension CocoaView: HelperBarActionDelegate {
var isMouseEnabled: Bool {
mouseHandler.enabled
}
var isOrientationLocked: Bool {
shouldLockCurrentInterfaceOrientation
}
}

View File

@ -9,12 +9,17 @@
protocol HelperBarItem {
var image: UIImage? { get }
var selectedImage: UIImage? { get }
var tintColorOnSelection: UIColor? { get }
var isSelected: Bool { get }
var shortDescription: String { get }
var longDescription: String? { get }
func action()
}
extension HelperBarItem {
var tintColorOnSelection: UIColor? { nil }
}
struct KeyboardBarItem: HelperBarItem {
let image = UIImage(systemName: "keyboard")
let selectedImage = UIImage(systemName: "keyboard.fill")
@ -53,3 +58,21 @@ struct MouseBarItem: HelperBarItem {
actionDelegate?.mouseButtonTapped()
}
}
struct LockOrientationBarItem: HelperBarItem {
let image = UIImage(systemName: "lock.rotation")
let selectedImage = UIImage(systemName: "lock.rotation")
var tintColorOnSelection: UIColor? { .red }
var isSelected: Bool { actionDelegate?.isOrientationLocked ?? false }
let shortDescription = NSLocalizedString("Lock the current screen orientation", comment: "Description for orientation lock item on helper bar")
var longDescription: String? { nil }
weak var actionDelegate: HelperBarActionDelegate?
init(actionDelegate: HelperBarActionDelegate?) {
self.actionDelegate = actionDelegate
}
func action() {
actionDelegate?.orientationLockButtonTapped()
}
}

View File

@ -88,8 +88,14 @@ class HelperBarViewController: UIViewController {
}
if helperBarItem.isSelected {
barButtonItem.image = helperBarItem.selectedImage
if let tintColor = helperBarItem.tintColorOnSelection {
barButtonItem.tintColor = tintColor
} else {
barButtonItem.tintColor = nil
}
} else {
barButtonItem.image = helperBarItem.image
barButtonItem.tintColor = nil
}
}
}

View File

@ -23,7 +23,8 @@ class HelperBarViewModel {
lazy var barItems: [HelperBarItem] = [
KeyboardBarItem(actionDelegate: actionDelegate),
MouseBarItem(actionDelegate: actionDelegate)
MouseBarItem(actionDelegate: actionDelegate),
LockOrientationBarItem(actionDelegate: actionDelegate)
]
var barItemMapping = [UIBarButtonItem: HelperBarItem]()

View File

@ -68,6 +68,9 @@
@property(nonatomic,strong) UIView *helperBarView;
#endif
@property(readwrite) BOOL shouldLockCurrentInterfaceOrientation;
@property(readwrite) UIInterfaceOrientation lockInterfaceOrientation;
+ (CocoaView*)get;
@end

View File

@ -53,7 +53,6 @@ void *glkitview_init(void);
,EmulatorTouchMouseHandlerDelegate
#endif
>
@end
#endif
@ -106,6 +105,10 @@ void *glkitview_init(void);
*/
self.controllerUserInteractionEnabled = YES;
#endif
#if TARGET_OS_IOS
self.shouldLockCurrentInterfaceOrientation = NO;
#endif
return self;
}
@ -417,7 +420,23 @@ void *glkitview_init(void);
/* NOTE: This version runs on iOS6+. */
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMask)apple_frontend_settings.orientation_flags;
if (@available(iOS 16, *)) {
if (self.shouldLockCurrentInterfaceOrientation) {
return 1 << self.lockInterfaceOrientation;
} else {
return (UIInterfaceOrientationMask)apple_frontend_settings.orientation_flags;
}
} else {
return (UIInterfaceOrientationMask)apple_frontend_settings.orientation_flags;
}
}
/* NOTE: This does not run on iOS 16+ */
-(BOOL)shouldAutorotate {
if (self.shouldLockCurrentInterfaceOrientation) {
return NO;
}
return YES;
}
/* NOTE: This version runs on iOS2-iOS5, but not iOS6+. */