Skip to content

Commit

Permalink
Reposition the share sheet after a rotation on iPad.
Browse files Browse the repository at this point in the history
iOS does not automatically handle reposition of popovers after a
rotation (except when presented from a UIBarButtonItem).

This patch fixes the issue by retaining the displayed popover controller
and a configuration block. These will be used at rotation events to
dismiss the controller then re-present it after the rotation is finished
and after calling the `updateDisplayedPopoverProperties` block to reset
the popover anchor point.
  • Loading branch information
pbernery committed Nov 24, 2015
1 parent 0824db3 commit 6e6e3cd
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions Client/Frontend/Browser/BrowserViewController.swift
Expand Up @@ -50,6 +50,10 @@ class BrowserViewController: UIViewController {
private let snackBars = UIView()
private let webViewContainerToolbar = UIView()

// popover rotation handling
private var displayedPopoverController: UIViewController?
private var updateDisplayedPopoverProperties: (() -> ())?

private var openInHelper: OpenInHelper?

// location label actions
Expand Down Expand Up @@ -107,6 +111,17 @@ class BrowserViewController: UIViewController {
}
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
displayedPopoverController?.dismissViewControllerAnimated(true, completion: nil)

coordinator.animateAlongsideTransition(nil) { context in
if let displayedPopoverController = self.displayedPopoverController {
self.updateDisplayedPopoverProperties?()
self.presentViewController(displayedPopoverController, animated: true, completion: nil)
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
log.debug("BVC received memory warning")
Expand Down Expand Up @@ -182,6 +197,8 @@ class BrowserViewController: UIViewController {
updateToolbarStateForTraitCollection(newCollection)
}

displayedPopoverController?.dismissViewControllerAnimated(true, completion: nil)

// WKWebView looks like it has a bug where it doesn't invalidate it's visible area when the user
// performs a device rotation. Since scrolling calls
// _updateVisibleContentRects (https://github.com/WebKit/webkit/blob/master/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm#L1430)
Expand All @@ -202,7 +219,7 @@ class BrowserViewController: UIViewController {
}

func SELappWillResignActiveNotification() {
// If we are displying a private tab, hide any elements in the browser that we wouldn't want shown
// If we are displying a private tab, hide any elements in the browser that we wouldn't want shown
// when the app is in the home switcher
guard let privateTab = tabManager.selectedTab where privateTab.isPrivate else {
return
Expand Down Expand Up @@ -1097,13 +1114,24 @@ extension BrowserViewController: BrowserToolbarDelegate {
self.updateReaderModeBar()
})

if let popoverPresentationController = activityViewController.popoverPresentationController {
// Using the button for the sourceView here results in this not showing on iPads.
popoverPresentationController.sourceView = self.toolbar ?? self.urlBar
popoverPresentationController.sourceRect = button.frame ?? button.frame
popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Up
popoverPresentationController.delegate = self
let setupPopover = { [unowned self] in
if let popoverPresentationController = activityViewController.popoverPresentationController {
let sourceView = self.navigationToolbar.shareButton
popoverPresentationController.sourceView = sourceView.superview
popoverPresentationController.sourceRect = sourceView.frame
popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Up
popoverPresentationController.delegate = self
}
}

setupPopover()
presentViewController(activityViewController, animated: true, completion: nil)

if activityViewController.popoverPresentationController != nil {
displayedPopoverController = activityViewController
updateDisplayedPopoverProperties = setupPopover
}

self.presentViewController(activityViewController, animated: true, completion: nil)
}

Expand Down Expand Up @@ -1143,7 +1171,7 @@ extension BrowserViewController: BrowserDelegate {

let favicons = FaviconManager(browser: browser, profile: profile)
browser.addHelper(favicons, name: FaviconManager.name())

// only add the logins helper if the tab is not a private browsing tab
if !browser.isPrivate {
let logins = LoginsHelper(browser: browser, profile: profile)
Expand Down Expand Up @@ -1783,7 +1811,7 @@ extension BrowserViewController: WKUIDelegate {
}
}

extension BrowserViewController: ReaderModeDelegate, UIPopoverPresentationControllerDelegate {
extension BrowserViewController: ReaderModeDelegate {
func readerMode(readerMode: ReaderMode, didChangeReaderModeState state: ReaderModeState, forBrowser browser: Browser) {
// If this reader mode availability state change is for the tab that we currently show, then update
// the button. Otherwise do nothing and the button will be updated when the tab is made active.
Expand All @@ -1804,6 +1832,17 @@ extension BrowserViewController: ReaderModeDelegate, UIPopoverPresentationContro
}
}

// MARK: - UIPopoverPresentationControllerDelegate

extension BrowserViewController: UIPopoverPresentationControllerDelegate {
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
displayedPopoverController = nil
updateDisplayedPopoverProperties = nil
}
}

// MARK: - ReaderModeStyleViewControllerDelegate

extension BrowserViewController: ReaderModeStyleViewControllerDelegate {
func readerModeStyleViewController(readerModeStyleViewController: ReaderModeStyleViewController, didConfigureStyle style: ReaderModeStyle) {
// Persist the new style to the profile
Expand Down Expand Up @@ -1933,19 +1972,19 @@ extension BrowserViewController: ReaderModeBarViewDelegate {
readerModeStyle = style
}
}

let readerModeStyleViewController = ReaderModeStyleViewController()
readerModeStyleViewController.delegate = self
readerModeStyleViewController.readerModeStyle = readerModeStyle
readerModeStyleViewController.modalPresentationStyle = UIModalPresentationStyle.Popover

let popoverPresentationController = readerModeStyleViewController.popoverPresentationController
popoverPresentationController?.backgroundColor = UIColor.whiteColor()
popoverPresentationController?.delegate = self
popoverPresentationController?.sourceView = readerModeBar
popoverPresentationController?.sourceRect = CGRect(x: readerModeBar.frame.width/2, y: UIConstants.ToolbarHeight, width: 1, height: 1)
popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up

self.presentViewController(readerModeStyleViewController, animated: true, completion: nil)
}

Expand Down

0 comments on commit 6e6e3cd

Please sign in to comment.