MessagesViewController+Menu.swift 5.0 KB

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