MessagesViewController+Menu.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2019 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import Foundation
  21. internal extension MessagesViewController {
  22. // MARK: - Register / Unregister Observers
  23. internal func addMenuControllerObservers() {
  24. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.menuControllerWillShow(_:)), name: UIMenuController.willShowMenuNotification, object: nil)
  25. }
  26. internal func removeMenuControllerObservers() {
  27. NotificationCenter.default.removeObserver(self, name: UIMenuController.willShowMenuNotification, object: nil)
  28. }
  29. // MARK: - Notification Handlers
  30. /// Show menuController and set target rect to selected bubble
  31. @objc
  32. private func menuControllerWillShow(_ notification: Notification) {
  33. guard let currentMenuController = notification.object as? UIMenuController,
  34. let selectedIndexPath = selectedIndexPathForMenu else { return }
  35. NotificationCenter.default.removeObserver(self, name: UIMenuController.willShowMenuNotification, object: nil)
  36. defer {
  37. NotificationCenter.default.addObserver(self,
  38. selector: #selector(MessagesViewController.menuControllerWillShow(_:)),
  39. name: UIMenuController.willShowMenuNotification, object: nil)
  40. selectedIndexPathForMenu = nil
  41. }
  42. currentMenuController.setMenuVisible(false, animated: false)
  43. guard let selectedCell = messagesCollectionView.cellForItem(at: selectedIndexPath) as? MessageContentCell else { return }
  44. let selectedCellMessageBubbleFrame = selectedCell.convert(selectedCell.messageContainerView.frame, to: view)
  45. var messageInputBarFrame: CGRect = .zero
  46. if let messageInputBarSuperview = messageInputBar.superview {
  47. messageInputBarFrame = view.convert(messageInputBar.frame, from: messageInputBarSuperview)
  48. }
  49. var topNavigationBarFrame: CGRect = navigationBarFrame
  50. if navigationBarFrame != .zero, let navigationBarSuperview = navigationController?.navigationBar.superview {
  51. topNavigationBarFrame = view.convert(navigationController!.navigationBar.frame, from: navigationBarSuperview)
  52. }
  53. let menuHeight = currentMenuController.menuFrame.height
  54. let selectedCellMessageBubblePlusMenuFrame = CGRect(selectedCellMessageBubbleFrame.origin.x, selectedCellMessageBubbleFrame.origin.y - menuHeight, selectedCellMessageBubbleFrame.size.width, selectedCellMessageBubbleFrame.size.height + 2 * menuHeight)
  55. var targetRect: CGRect = selectedCellMessageBubbleFrame
  56. currentMenuController.arrowDirection = .default
  57. /// Message bubble intersects with navigationBar and keyboard
  58. if selectedCellMessageBubblePlusMenuFrame.intersects(topNavigationBarFrame) && selectedCellMessageBubblePlusMenuFrame.intersects(messageInputBarFrame) {
  59. let centerY = (selectedCellMessageBubblePlusMenuFrame.intersection(messageInputBarFrame).minY + selectedCellMessageBubblePlusMenuFrame.intersection(topNavigationBarFrame).maxY) / 2
  60. targetRect = CGRect(selectedCellMessageBubblePlusMenuFrame.midX, centerY, 1, 1)
  61. } /// Message bubble only intersects with navigationBar
  62. else if selectedCellMessageBubblePlusMenuFrame.intersects(topNavigationBarFrame) {
  63. currentMenuController.arrowDirection = .up
  64. }
  65. currentMenuController.setTargetRect(targetRect, in: view)
  66. currentMenuController.setMenuVisible(true, animated: true)
  67. }
  68. // MARK: - Helpers
  69. private var navigationBarFrame: CGRect {
  70. guard let navigationController = navigationController, !navigationController.navigationBar.isHidden else {
  71. return .zero
  72. }
  73. return navigationController.navigationBar.frame
  74. }
  75. }