NCMenu+FloatingPanel.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NCMenu+FloatingPanel.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 16.12.21.
  6. // Copyright © 2021 Henrik Storch All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import FloatingPanel
  25. import UIKit
  26. class NCMenuFloatingPanelLayout: FloatingPanelLayout {
  27. var position: FloatingPanelPosition = .bottom
  28. var initialState: FloatingPanelState = .full
  29. var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] {
  30. [
  31. .full: FloatingPanelLayoutAnchor(absoluteInset: topInset, edge: .top, referenceGuide: .superview)
  32. ]
  33. }
  34. let topInset: CGFloat
  35. init(actionsHeight: CGFloat) {
  36. // sometimes UIScreen.main.bounds.size.height is not updated correctly
  37. // this ensures we use the correct height value
  38. // can't use `layoutFor size` since menu is dieplayed on top of the whole screen not just the VC
  39. let screenHeight = UIDevice.current.orientation.isLandscapeHardCheck
  40. ? min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  41. : max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  42. let window = UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
  43. let bottomInset = window?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  44. let panelHeight = CGFloat(actionsHeight) + bottomInset
  45. topInset = max(48, screenHeight - panelHeight)
  46. }
  47. func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  48. return [
  49. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  50. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0)
  51. ]
  52. }
  53. func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
  54. return 0.2
  55. }
  56. }
  57. class NCMenuPanelController: FloatingPanelController {
  58. var parentPresenter: UIViewController?
  59. // MARK: - View Life Cycle
  60. override func viewDidLoad() {
  61. super.viewDidLoad()
  62. self.surfaceView.backgroundColor = .systemBackground
  63. self.isRemovalInteractionEnabled = true
  64. self.backdropView.dismissalTapGestureRecognizer.isEnabled = true
  65. self.surfaceView.layer.cornerRadius = 16
  66. self.surfaceView.clipsToBounds = true
  67. surfaceView.grabberHandle.accessibilityLabel = NSLocalizedString("_cart_controller_", comment: "")
  68. let collapseName = NSLocalizedString("_dismiss_menu_", comment: "")
  69. let collapseAction = UIAccessibilityCustomAction(name: collapseName, target: self, selector: #selector(accessibilityActionCollapsePanel))
  70. surfaceView.grabberHandle.accessibilityCustomActions = [collapseAction]
  71. surfaceView.grabberHandle.isAccessibilityElement = true
  72. }
  73. @objc private func accessibilityActionCollapsePanel() {
  74. self.dismiss(animated: true)
  75. }
  76. }