NCMenu+FloatingPanel.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. let screenHeight = UIDevice.current.orientation.isLandscape
  37. ? min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  38. : max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  39. let window = UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
  40. let bottomInset = window?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  41. let panelHeight = CGFloat(actionsHeight) + bottomInset
  42. topInset = max(48, screenHeight - panelHeight)
  43. }
  44. func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  45. return [
  46. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  47. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0)
  48. ]
  49. }
  50. func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
  51. return 0.2
  52. }
  53. }
  54. class NCMenuPanelController: FloatingPanelController {
  55. var parentPresenter: UIViewController?
  56. // MARK: - View Life Cycle
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. self.surfaceView.backgroundColor = .systemBackground
  60. self.isRemovalInteractionEnabled = true
  61. self.backdropView.dismissalTapGestureRecognizer.isEnabled = true
  62. self.surfaceView.layer.cornerRadius = 16
  63. self.surfaceView.clipsToBounds = true
  64. surfaceView.grabberHandle.accessibilityLabel = NSLocalizedString("_cart_controller_", comment: "")
  65. let collapseName = NSLocalizedString("_dismiss_menu_", comment: "")
  66. let collapseAction = UIAccessibilityCustomAction(name: collapseName, target: self, selector: #selector(accessibilityActionCollapsePanel))
  67. surfaceView.grabberHandle.accessibilityCustomActions = [collapseAction]
  68. surfaceView.grabberHandle.isAccessibilityElement = true
  69. contentInsetAdjustmentBehavior = .never
  70. }
  71. @objc private func accessibilityActionCollapsePanel() {
  72. self.dismiss(animated: true)
  73. }
  74. }