MessagesViewController+Menu.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 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. import MessageInputBar
  22. extension MessagesViewController {
  23. // MARK: - Register / Unregister Observers
  24. internal func addMenuControllerObservers() {
  25. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.menuControllerWillShow(_:)), name: UIMenuController.willShowMenuNotification, object: nil)
  26. }
  27. internal func removeMenuControllerObservers() {
  28. NotificationCenter.default.removeObserver(self, name: UIMenuController.willShowMenuNotification, object: nil)
  29. }
  30. // MARK: - Notification Handlers
  31. /// Show menuController and set target rect to selected bubble
  32. @objc
  33. private func menuControllerWillShow(_ notification: Notification) {
  34. guard let currentMenuController = notification.object as? UIMenuController,
  35. let selectedIndexPath = selectedIndexPathForMenu else { return }
  36. NotificationCenter.default.removeObserver(self, name: UIMenuController.willShowMenuNotification, object: nil)
  37. defer {
  38. NotificationCenter.default.addObserver(self,
  39. selector: #selector(MessagesViewController.menuControllerWillShow(_:)),
  40. name: UIMenuController.willShowMenuNotification, object: nil)
  41. selectedIndexPathForMenu = nil
  42. }
  43. currentMenuController.setMenuVisible(false, animated: false)
  44. guard let selectedCell = messagesCollectionView.cellForItem(at: selectedIndexPath) as? MessageContentCell else { return }
  45. let selectedCellMessageBubbleFrame = selectedCell.convert(selectedCell.messageContainerView.frame, to: view)
  46. var messageInputBarFrame: CGRect = .zero
  47. if let messageInputBarSuperview = messageInputBar.superview {
  48. messageInputBarFrame = view.convert(messageInputBar.frame, from: messageInputBarSuperview)
  49. }
  50. var topNavigationBarFrame: CGRect = navigationBarFrame
  51. if navigationBarFrame != .zero, let navigationBarSuperview = navigationController?.navigationBar.superview {
  52. topNavigationBarFrame = view.convert(navigationController!.navigationBar.frame, from: navigationBarSuperview)
  53. }
  54. let menuHeight = currentMenuController.menuFrame.height
  55. let selectedCellMessageBubblePlusMenuFrame = CGRect(selectedCellMessageBubbleFrame.origin.x, selectedCellMessageBubbleFrame.origin.y - menuHeight, selectedCellMessageBubbleFrame.size.width, selectedCellMessageBubbleFrame.size.height + 2 * menuHeight)
  56. var targetRect: CGRect = selectedCellMessageBubbleFrame
  57. currentMenuController.arrowDirection = .default
  58. /// Message bubble intersects with navigationBar and keyboard
  59. if selectedCellMessageBubblePlusMenuFrame.intersects(topNavigationBarFrame) && selectedCellMessageBubblePlusMenuFrame.intersects(messageInputBarFrame) {
  60. let centerY = (selectedCellMessageBubblePlusMenuFrame.intersection(messageInputBarFrame).minY + selectedCellMessageBubblePlusMenuFrame.intersection(topNavigationBarFrame).maxY) / 2
  61. targetRect = CGRect(selectedCellMessageBubblePlusMenuFrame.midX, centerY, 1, 1)
  62. } /// Message bubble only intersects with navigationBar
  63. else if selectedCellMessageBubblePlusMenuFrame.intersects(topNavigationBarFrame) {
  64. currentMenuController.arrowDirection = .up
  65. }
  66. currentMenuController.setTargetRect(targetRect, in: view)
  67. currentMenuController.setMenuVisible(true, animated: true)
  68. }
  69. // MARK: - Helpers
  70. private var navigationBarFrame: CGRect {
  71. guard let navigationController = navigationController, !navigationController.navigationBar.isHidden else {
  72. return .zero
  73. }
  74. return navigationController.navigationBar.frame
  75. }
  76. }