11/*
22 MIT License
3-
3+
44 Copyright (c) 2017-2019 MessageKit
5-
5+
66 Permission is hereby granted, free of charge, to any person obtaining a copy
77 of this software and associated documentation files (the "Software"), to deal
88 in the Software without restriction, including without limitation the rights
99 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010 copies of the Software, and to permit persons to whom the Software is
1111 furnished to do so, subject to the following conditions:
12-
12+
1313 The above copyright notice and this permission notice shall be included in all
1414 copies or substantial portions of the Software.
15-
15+
1616 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -26,56 +26,56 @@ import Foundation
2626import UIKit
2727
2828internal extension MessagesViewController {
29-
29+
3030 // MARK: - Register / Unregister Observers
31-
31+ #if os(iOS)
3232 func addMenuControllerObservers( ) {
3333 NotificationCenter . default. addObserver ( self , selector: #selector( MessagesViewController . menuControllerWillShow ( _: ) ) , name: UIMenuController . willShowMenuNotification, object: nil )
3434 }
35-
35+
3636 func removeMenuControllerObservers( ) {
3737 NotificationCenter . default. removeObserver ( self , name: UIMenuController . willShowMenuNotification, object: nil )
3838 }
39-
39+
4040 // MARK: - Notification Handlers
41-
41+
4242 /// Show menuController and set target rect to selected bubble
4343 @objc
4444 private func menuControllerWillShow( _ notification: Notification ) {
45-
45+
4646 guard let currentMenuController = notification. object as? UIMenuController ,
47- let selectedIndexPath = selectedIndexPathForMenu else { return }
48-
47+ let selectedIndexPath = selectedIndexPathForMenu else { return }
48+
4949 NotificationCenter . default. removeObserver ( self , name: UIMenuController . willShowMenuNotification, object: nil )
5050 defer {
5151 NotificationCenter . default. addObserver ( self ,
5252 selector: #selector( MessagesViewController . menuControllerWillShow ( _: ) ) ,
5353 name: UIMenuController . willShowMenuNotification, object: nil )
5454 selectedIndexPathForMenu = nil
5555 }
56-
56+
5757 currentMenuController. setMenuVisible ( false , animated: false )
58-
58+
5959 guard let selectedCell = messagesCollectionView. cellForItem ( at: selectedIndexPath) as? MessageContentCell else { return }
6060 let selectedCellMessageBubbleFrame = selectedCell. convert ( selectedCell. messageContainerView. frame, to: view)
61-
61+
6262 var messageInputBarFrame : CGRect = . zero
6363 if let messageInputBarSuperview = messageInputBar. superview {
6464 messageInputBarFrame = view. convert ( messageInputBar. frame, from: messageInputBarSuperview)
6565 }
66-
66+
6767 var topNavigationBarFrame : CGRect = navigationBarFrame
6868 if navigationBarFrame != . zero, let navigationBarSuperview = navigationController? . navigationBar. superview {
6969 topNavigationBarFrame = view. convert ( navigationController!. navigationBar. frame, from: navigationBarSuperview)
7070 }
71-
71+
7272 let menuHeight = currentMenuController. menuFrame. height
73-
73+
7474 let selectedCellMessageBubblePlusMenuFrame = CGRect ( selectedCellMessageBubbleFrame. origin. x, selectedCellMessageBubbleFrame. origin. y - menuHeight, selectedCellMessageBubbleFrame. size. width, selectedCellMessageBubbleFrame. size. height + 2 * menuHeight)
75-
75+
7676 var targetRect : CGRect = selectedCellMessageBubbleFrame
7777 currentMenuController. arrowDirection = . default
78-
78+
7979 /// Message bubble intersects with navigationBar and keyboard
8080 if selectedCellMessageBubblePlusMenuFrame. intersects ( topNavigationBarFrame) && selectedCellMessageBubblePlusMenuFrame. intersects ( messageInputBarFrame) {
8181 let centerY = ( selectedCellMessageBubblePlusMenuFrame. intersection ( messageInputBarFrame) . minY + selectedCellMessageBubblePlusMenuFrame. intersection ( topNavigationBarFrame) . maxY) / 2
@@ -84,17 +84,24 @@ internal extension MessagesViewController {
8484 else if selectedCellMessageBubblePlusMenuFrame. intersects ( topNavigationBarFrame) {
8585 currentMenuController. arrowDirection = . up
8686 }
87-
87+
8888 currentMenuController. setTargetRect ( targetRect, in: view)
8989 currentMenuController. setMenuVisible ( true , animated: true )
9090 }
91-
91+
9292 // MARK: - Helpers
93-
93+
9494 private var navigationBarFrame : CGRect {
9595 guard let navigationController = navigationController, !navigationController. navigationBar. isHidden else {
9696 return . zero
9797 }
9898 return navigationController. navigationBar. frame
9999 }
100+ #elseif os(visionOS)
101+ func addMenuControllerObservers( ) {
102+ }
103+
104+ func removeMenuControllerObservers( ) {
105+ }
106+ #endif
100107}
0 commit comments