NCMenu+FloatingPanel.swift 3.4 KB

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