|
| 1 | +// Copyright © 2021 Saleem Abdulrasool < [email protected]> |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +/// A set of methods that, in conjunction with a presentation controller, |
| 5 | +/// determine how to respond to trait changes in your application. |
| 6 | +public protocol AdaptivePresentationControllerDelegate: AnyObject { |
| 7 | + // MARK - Adapting the Presentation Style |
| 8 | + |
| 9 | + /// Asks the delegate for the presentation style to use when the specified set |
| 10 | + /// of traits are active. |
| 11 | + /// |
| 12 | + /// The presentation controller calls this method when the traits of the |
| 13 | + /// current environment are about to change. Your implementation of this |
| 14 | + /// method can return the preferred presentation style to use for the |
| 15 | + /// specified traits. If you do not return one of the allowed styles, the |
| 16 | + /// presentation controller uses its preferred default style. |
| 17 | + /// |
| 18 | + /// If you do not implement this method in your delegate, the framework calls |
| 19 | + /// the `adaptivePresentationStyle(for:)` method instead. |
| 20 | + func adaptivePresentationStyle(for controller: PresentationController, |
| 21 | + traitCollection: TraitCollection) |
| 22 | + -> ModalPresentationStyle |
| 23 | + |
| 24 | + /// Asks the delegate for the new presentation style to use. |
| 25 | + /// |
| 26 | + /// Use the `adaptivePresentationStyle(for:traitCollection:)` method to handle |
| 27 | + /// all trait changes instead of this method. If you do not implement that |
| 28 | + /// method, you can use this method to change the presentation style when |
| 29 | + /// transitioning to a horizontally compact environment. |
| 30 | + /// |
| 31 | + /// If you do not implement this method or if you return an invalid style, the |
| 32 | + /// current presentation controller returns its preferred default style. |
| 33 | + func adaptivePresentationStyle(for controller: PresentationController) |
| 34 | + -> ModalPresentationStyle |
| 35 | + |
| 36 | + // MARK - Adapting the View Controller |
| 37 | + |
| 38 | + /// Asks the delegate for the view controller to display when adapting to the |
| 39 | + /// specified presentation style. |
| 40 | + /// |
| 41 | + /// When a size class change causes a change to the underlying presentation |
| 42 | + /// style, the presentation controller calls this method to ask for the view |
| 43 | + /// controller to display in that new style. This method is your opportunity |
| 44 | + /// to replace the current view controller with one that is better suited for |
| 45 | + /// the new presentation style. For example, you might use this method to |
| 46 | + /// insert a navigation controller into your view hierarchy to facilitate |
| 47 | + /// pushing new view controllers more easily in the compact environment. In |
| 48 | + /// that instance, you would return a navigation controller whose root view |
| 49 | + /// controller is the currently presented view controller. You could also |
| 50 | + /// return an entirely different view controller if you prefer. |
| 51 | + /// |
| 52 | + /// If you do not implement this method or your implementation returns `nil`, |
| 53 | + /// the presentation controller uses its existing presented view controller. |
| 54 | + func presentationController(_ controller: PresentationController, |
| 55 | + viewControllerForAdaptivePresentationStyle style: ModalPresentationStyle) |
| 56 | + -> ViewController? |
| 57 | + |
| 58 | + // MARK - Responding to Adaptive Transitions |
| 59 | + |
| 60 | + /// Notifies the delegate that an adaptivity-related transition is about to |
| 61 | + /// occur. |
| 62 | + /// |
| 63 | + /// When a size class change occurs, the framework calls this method to let |
| 64 | + /// you know how the presentation controller will adapt. Use this method to |
| 65 | + /// make any additional changes. For example, you might use the transition |
| 66 | + /// coordinator object to create additional animations for the transition. |
| 67 | + func presentationController(_ presentationController: PresentationController, |
| 68 | + willPresentWithAdaptiveStyle style: ModalPresentationStyle, |
| 69 | + transitionCoordinator: ViewControllerTransitionCoordinator?) |
| 70 | + |
| 71 | + /// Notifies the delegate that a user-initiated attempt to dismiss a view was |
| 72 | + /// prevented. |
| 73 | + /// |
| 74 | + /// The framework supports refusing to dismiss a presentation when the |
| 75 | + /// `presentationController.isModalInPresentation` returns `true` or |
| 76 | + /// `presentationControllerShouldDismiss(_:)` returns `false`. |
| 77 | + /// |
| 78 | + /// Use this method to inform the user why the presentation can't be |
| 79 | + /// dismissed, for example, by presenting an instance of `AlertController`. |
| 80 | + func presentationControllerDidAttemptToDismiss(_ presentationController: PresentationController) |
| 81 | + |
| 82 | + /// Asks the delegate for permission to dismiss the presentation. |
| 83 | + /// |
| 84 | + /// The system may call this method at any time. This method isn't guaranteed |
| 85 | + /// to be followed by a call to `presentationControllerWillDismiss(_:)` or |
| 86 | + /// `presentationControllerDidDismiss(_:)`. Make sure that your implementation |
| 87 | + /// of this method returns quickly. |
| 88 | + func presentationControllerShouldDismiss(_ presentationController: PresentationController) |
| 89 | + -> Bool |
| 90 | + |
| 91 | + /// Notifies the delegate after a presentation is dismissed. |
| 92 | + /// |
| 93 | + /// This method is not called if the presentation is dismissed |
| 94 | + /// programmatically. |
| 95 | + func presentationControllerDidDismiss(_ presentationController: PresentationController) |
| 96 | + |
| 97 | + /// Notifies the delegate before a presentation is dismissed. |
| 98 | + /// |
| 99 | + /// You can use this method to set up animations or interaction notifications |
| 100 | + /// with the `presentationController`'s `transitionCoordinator`. |
| 101 | + /// |
| 102 | + /// This method is not called if the presentation is dismissed |
| 103 | + /// programmatically. |
| 104 | + func presentationControllerWillDismiss(_ presentationController: PresentationController) |
| 105 | + |
| 106 | + // MARK - Preparing the Adaptive Presentation Controller |
| 107 | + |
| 108 | + /// Provides an opportunity to configure the adaptive presentation controller |
| 109 | + /// after an adaptivity change. |
| 110 | + /// |
| 111 | + /// The system calls this method during adaptation so the delegate can |
| 112 | + /// configure properties of the adaptive presentation controller before it |
| 113 | + /// presents. |
| 114 | + /// |
| 115 | + /// For example, the system automatically adapts a view controller that |
| 116 | + /// presents as a popover in standard size classes to a sheet in compact size |
| 117 | + /// classes. You can implement this method to customize the sheet's |
| 118 | + /// properties before it presents. |
| 119 | + func presentationController(_ presentationController: PresentationController, |
| 120 | + prepare adaptivePresentationController: PresentationController) |
| 121 | +} |
| 122 | + |
| 123 | +extension AdaptivePresentationControllerDelegate { |
| 124 | + public func adaptivePresentationStyle(for controller: PresentationController, |
| 125 | + traitCollection: TraitCollection) |
| 126 | + -> ModalPresentationStyle { |
| 127 | + return self.adaptivePresentationStyle(for: controller) |
| 128 | + } |
| 129 | + |
| 130 | + public func adaptivePresentationStyle(for controller: PresentationController) |
| 131 | + -> ModalPresentationStyle { |
| 132 | + return controller.presentationStyle |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +extension AdaptivePresentationControllerDelegate { |
| 137 | + public func presentationController(controller: PresentationController, |
| 138 | + viewControllerForAdaptivePresentationStyle style: ModalPresentationStyle) |
| 139 | + -> ViewController? { |
| 140 | + return controller.presentedViewController |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +extension AdaptivePresentationControllerDelegate { |
| 145 | + public func presentationController(_ controller: PresentationController, |
| 146 | + willPresentWithAdaptiveStyle style: ModalPresentationStyle, |
| 147 | + transitionCoordinator: ViewControllerTransitionCoordinator?) { |
| 148 | + } |
| 149 | + |
| 150 | + public func presentationControllerDidAttemptToDismiss(_ presentationController: PresentationController) { |
| 151 | + } |
| 152 | + |
| 153 | + public func presentationControllerShouldDismiss(_ presentationController: PresentationController) -> Bool { |
| 154 | + return true |
| 155 | + } |
| 156 | + |
| 157 | + public func presentationControllerDidDismiss(_ presentationController: PresentationController) { |
| 158 | + } |
| 159 | + |
| 160 | + public func presentationControllerWillDismiss(_ presentationController: PresentationController) { |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +extension AdaptivePresentationControllerDelegate { |
| 165 | + public func presentationController(_ presentationController: PresentationController, |
| 166 | + prepare adaptivePresentationController: PresentationController) { |
| 167 | + } |
| 168 | +} |
0 commit comments