@@ -218,23 +218,32 @@ final class MainWindowController: NSWindowController {
218218 @IBAction private func invokeSharingServicePicker( _ sender: Any ) {
219219 // The sender is either the menu item in the main menu, the menu-form
220220 // representation of the toolbar item in label-only mode or the toolbar
221- // item itself.
221+ // item itself when its isBordered property is enabled .
222222 if sender is NSMenuItem || sender is NSToolbarItem ,
223223 let window,
224224 let contentView = window. contentView
225225 {
226+ let layoutRect = window. contentLayoutRect
227+ // The menu or toolbar item does not have a view to which the picker
228+ // could be attached. The window's content view is used instead, but
229+ // a location is still needed.
230+ let xCoordinate : CGFloat
231+ // If the action was sent from within the window (e.g. label-only
232+ // mode), an approximate horizontal location can be retrieved from
233+ // the current NSEvent, otherwise the midpoint of the window content
234+ // layout rect is used.
235+ if let event = NSApp . currentEvent, event. window == window {
236+ xCoordinate = event. locationInWindow. x
237+ } else {
238+ xCoordinate = layoutRect. midX - 1
239+ }
240+ // Subtract 1 point from the Y coordinate and make the rect 1 point
241+ // in size, so that it fits within the coordinates of the view.
242+ let origin = NSPoint ( x: xCoordinate, y: layoutRect. maxY - 1 )
243+ let topEdge = NSRect ( origin: origin, size: NSSize ( width: 1 , height: 1 ) )
226244 let picker = NSSharingServicePicker ( items: shareableItems)
227245 picker. delegate = self
228- // The menu item does not have a view to which the picker could be
229- // attached. The window's content view is used instead. The picker
230- // should attach to the top middle point of the content view.
231- let layoutRect = window. contentLayoutRect
232- // Subtract 1 point from the coordinates and make the rect 1 point in
233- // size, so that it fits within the coordinates of the view.
234- let xCoordinate = layoutRect. midX - 1
235- let yCoordinate = layoutRect. maxY - 1
236- let topEdgeRect = NSRect ( x: xCoordinate, y: yCoordinate, width: 1 , height: 1 )
237- picker. show ( relativeTo: topEdgeRect, of: contentView, preferredEdge: . minY)
246+ picker. show ( relativeTo: topEdge, of: contentView, preferredEdge: . minY)
238247 return
239248 }
240249
0 commit comments