Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #8484: Portrait Orientation Locking on Phone is not working #8485

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 8 additions & 8 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1497,15 +1497,15 @@ public class BrowserViewController: UIViewController {
}

guard let vc = BraveVPN.vpnState.enableVPNDestinationVC else { return }
let nav = SettingsNavigationController(rootViewController: vc)
nav.navigationBar.topItem?.leftBarButtonItem =
.init(barButtonSystemItem: .cancel, target: nav, action: #selector(nav.done))
let navigationController = SettingsNavigationController(rootViewController: vc)
navigationController.navigationBar.topItem?.leftBarButtonItem =
.init(barButtonSystemItem: .cancel, target: navigationController, action: #selector(navigationController.done))
let idiom = UIDevice.current.userInterfaceIdiom

UIDevice.current.forcePortraitIfIphone(for: UIApplication.shared)

nav.modalPresentationStyle = idiom == .phone ? .pageSheet : .formSheet
present(nav, animated: true)
DeviceOrientation.shared.changeOrientationToPortraitOnPhone()
navigationController.modalPresentationStyle = idiom == .phone ? .pageSheet : .formSheet
present(navigationController, animated: true)
}

func updateInContentHomePanel(_ url: URL?) {
Expand Down Expand Up @@ -2085,7 +2085,7 @@ public class BrowserViewController: UIViewController {
UIBarButtonItem(barButtonSystemItem: .done, target: settingsNavigationController, action: #selector(settingsNavigationController.done))

// All menu views should be opened in portrait on iPhones.
UIDevice.current.forcePortraitIfIphone(for: UIApplication.shared)
DeviceOrientation.shared.changeOrientationToPortraitOnPhone()

present(settingsNavigationController, animated: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extension BrowserViewController: TopToolbarDelegate {
$0.toolbarUrlActionsDelegate = self
}
let container = UINavigationController(rootViewController: tabTrayController)
container.delegate = self

if !UIAccessibility.isReduceMotionEnabled {
if !isExternallyPresented {
Expand Down Expand Up @@ -1009,3 +1010,15 @@ extension BrowserViewController: UIContextMenuInteractionDelegate {
return UIMenu(options: .displayInline, children: children)
}
}

// MARK: UINavigationControllerDelegate

extension BrowserViewController: UINavigationControllerDelegate {
public func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return navigationController.visibleViewController?.supportedInterfaceOrientations ?? navigationController.supportedInterfaceOrientations
}

public func navigationControllerPreferredInterfaceOrientationForPresentation(_ navigationController: UINavigationController) -> UIInterfaceOrientation {
return navigationController.visibleViewController?.preferredInterfaceOrientationForPresentation ?? navigationController.preferredInterfaceOrientationForPresentation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,14 @@ class TabTrayController: AuthenticationController {
return
}

let syncSettingsScreen = SyncSettingsTableViewController(
isModallyPresented: true,
syncAPI: braveCore.syncAPI,
syncProfileService: braveCore.syncProfileService,
tabManager: tabManager,
windowProtection: windowProtection)

syncSettingsScreen.syncStatusDelegate = self
let syncSettingsScreen = SyncSettingsTableViewController(
isModallyPresented: true,
syncAPI: braveCore.syncAPI,
syncProfileService: braveCore.syncProfileService,
tabManager: tabManager,
windowProtection: windowProtection)
syncSettingsScreen.syncStatusDelegate = self

openInsideSettingsNavigation(with: syncSettingsScreen)
default:
Expand All @@ -772,7 +772,7 @@ class TabTrayController: AuthenticationController {
settingsNavigationController.navigationBar.topItem?.leftBarButtonItem =
UIBarButtonItem(barButtonSystemItem: .done, target: settingsNavigationController, action: #selector(settingsNavigationController.done))

UIDevice.current.forcePortraitIfIphone(for: UIApplication.shared)
DeviceOrientation.shared.changeOrientationToPortraitOnPhone()

present(settingsNavigationController, animated: true)
}
Expand All @@ -793,6 +793,19 @@ class TabTrayController: AuthenticationController {
}
}

// MARK: InterfaceOrientation

extension TabTrayController {

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
Brandon-T marked this conversation as resolved.
Show resolved Hide resolved
return self.presentedViewController?.preferredInterfaceOrientationForPresentation ?? super.preferredInterfaceOrientationForPresentation
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.presentedViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
}
}

// MARK: PresentingModalViewControllerDelegate

extension TabTrayController: PresentingModalViewControllerDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ class MenuViewController: UINavigationController, UIPopoverPresentationControlle
}
}

// MARK: PanModalPresentable

extension MenuViewController: PanModalPresentable {
var panScrollable: UIScrollView? {
// For SwiftUI:
Expand Down Expand Up @@ -337,6 +339,8 @@ private class MenuHostingController<MenuContent: View>: UIHostingController<Menu
}
}

// MARK: MenuNavigationControllerDelegate

private class MenuNavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
weak var panModal: (UIViewController & PanModalPresentable)?
init(panModal: UIViewController & PanModalPresentable) {
Expand All @@ -350,6 +354,14 @@ private class MenuNavigationControllerDelegate: NSObject, UINavigationController
) {
panModal?.panModalSetNeedsLayoutUpdate()
}

public func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return navigationController.visibleViewController?.supportedInterfaceOrientations ?? navigationController.supportedInterfaceOrientations
}

public func navigationControllerPreferredInterfaceOrientationForPresentation(_ navigationController: UINavigationController) -> UIInterfaceOrientation {
return navigationController.visibleViewController?.preferredInterfaceOrientationForPresentation ?? navigationController.preferredInterfaceOrientationForPresentation
}
}

private class InnerMenuNavigationController: UINavigationController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import UIKit
class SettingsNavigationController: UINavigationController {
var popoverDelegate: PresentingModalViewControllerDelegate?

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

if #available(iOS 16.0, *) {
self.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}

@objc func done() {
if let delegate = popoverDelegate {
delegate.dismissPresentedModalViewController(self, animated: true)
Expand All @@ -29,10 +37,6 @@ class SettingsNavigationController: UINavigationController {
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}

override var shouldAutorotate: Bool {
return false
}
}

protocol PresentingModalViewControllerDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class SettingsViewController: TableViewController {
syncProfileService:
syncProfileServices,
tabManager: tabManager,
windowProtection: windowProtection)
windowProtection: windowProtection)

self.navigationController?
.pushViewController(syncSettingsViewController, animated: true)
Expand Down
18 changes: 18 additions & 0 deletions Sources/Brave/Frontend/Sync/SyncViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ class SyncViewController: AuthenticationController {
}
}
}

// MARK: - InterfaceOrientation

extension SyncViewController {

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}

override var shouldAutorotate: Bool {
return false
}
}

7 changes: 6 additions & 1 deletion Sources/BraveWallet/WalletHostingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SwiftUI
import BraveCore
import Combine
import BraveUI
import Shared

/// Methods for handling actions that occur while the user is interacting with Brave Wallet that require
/// some integration with the browser
Expand Down Expand Up @@ -120,7 +121,11 @@ public class WalletHostingViewController: UIHostingController<CryptoView> {
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.window?.addGestureRecognizer(gesture)
UIDevice.current.forcePortraitIfIphone(for: UIApplication.shared)

DeviceOrientation.shared.changeOrientationToPortraitOnPhone()
if #available(iOS 16.0, *) {
self.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}

public override func viewDidLoad() {
Expand Down
1 change: 1 addition & 0 deletions Sources/BraveWallet/WalletPanelHostingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BraveCore
import SwiftUI
import Combine
import BraveUI
import Shared

/// Displays a summary of the users wallet when they are visiting a webpage that wants to connect with the
/// users wallet
Expand Down
62 changes: 62 additions & 0 deletions Sources/Shared/DeviceOrientation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 The Brave Authors. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to move this to BraveShared? We shouldn't be adding anything to Shared anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh sorry I just merged this before your comment, let me do this in a different PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR created #8499

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import UIKit

public final class DeviceOrientation {

private var windowScene: UIWindowScene? {
return UIApplication.shared.connectedScenes.first as? UIWindowScene
}

public static let shared: DeviceOrientation = DeviceOrientation()

public func changeOrientation(_ orientationMask: UIInterfaceOrientationMask) {
if #available(iOS 16.0, *) {
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientationMask))
} else {
var orientation: UIInterfaceOrientation?

switch orientationMask {
case .portrait:
orientation = UIInterfaceOrientation.portrait
case .portraitUpsideDown:
orientation = UIInterfaceOrientation.portraitUpsideDown
case .landscapeRight:
orientation = UIInterfaceOrientation.landscapeRight
case .landscapeLeft:
orientation = UIInterfaceOrientation.landscapeLeft
default:
orientation = UIInterfaceOrientation.unknown
}

if let orientation = orientation {
UIDevice.current.setValue(orientation.rawValue, forKey: "orientation")
}
}
}

public var isLandscape: Bool {
if #available(iOS 16.0, *) {
return windowScene?.interfaceOrientation.isLandscape ?? false
}

return UIDevice.current.orientation.isLandscape
}

public var isPortrait: Bool {
soner-yuksel marked this conversation as resolved.
Show resolved Hide resolved
if #available(iOS 16.0, *) {
return windowScene?.interfaceOrientation.isPortrait ?? false
}
return UIDevice.current.orientation.isPortrait
}

public func changeOrientationToPortraitOnPhone() {
if UIDevice.current.userInterfaceIdiom != .pad && isLandscape {
changeOrientation(.portrait)
}
}
}

9 changes: 0 additions & 9 deletions Sources/Shared/Extensions/UIDeviceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,4 @@ public extension UIDevice {
static var isPhone: Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}

// Dev note: UIApplication.shared cannot be used in application extensions.
func forcePortraitIfIphone(for application: UIApplication) {
if userInterfaceIdiom != .pad && application.statusBarOrientation.isLandscape {
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
}

}