NCMenu+FloatingPanel.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. class NCMenuFloatingPanelLayout: FloatingPanelLayout {
  26. var position: FloatingPanelPosition = .bottom
  27. var initialState: FloatingPanelState = .full
  28. var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] {
  29. [
  30. .full: FloatingPanelLayoutAnchor(absoluteInset: topInset, edge: .top, referenceGuide: .superview)
  31. ]
  32. }
  33. let topInset: CGFloat
  34. init(numberOfActions: Int) {
  35. // sometimes UIScreen.main.bounds.size.height is not updated correctly
  36. // this ensures we use the correct height value
  37. // can't use `layoutFor size` since menu is dieplayed on top of the whole screen not just the VC
  38. let screenHeight = UIApplication.shared.isLandscape
  39. ? min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  40. : max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
  41. let bottomInset = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0
  42. let panelHeight = CGFloat(numberOfActions * 60) + bottomInset
  43. topInset = max(48, screenHeight - panelHeight)
  44. }
  45. func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
  46. return [
  47. surfaceView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0),
  48. surfaceView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0)
  49. ]
  50. }
  51. func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
  52. return 0.2
  53. }
  54. }
  55. class NCMenuPanelController: FloatingPanelController {
  56. var parentPresenter: UIViewController?
  57. // MARK: - View Life Cycle
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. self.surfaceView.backgroundColor = NCBrandColor.shared.systemBackground
  61. self.isRemovalInteractionEnabled = true
  62. self.backdropView.dismissalTapGestureRecognizer.isEnabled = true
  63. self.surfaceView.layer.cornerRadius = 16
  64. }
  65. }