Contents

overlayView(_:updatedMenuFor:for:at:)

Notifies your app before the framework presents a context menu.

Declaration

@MainActor func overlayView(_ overlayView: ImageAnalysisOverlayView, updatedMenuFor menu: NSMenu, for event: NSEvent, at point: CGPoint) -> NSMenu

Parameters

  • overlayView:

    The associated overlay view for the updated menu.

  • menu:

    The menu that appears.

  • event:

    The event associated with this menu.

  • point:

    The original location of the event for the menu.

Discussion

This callback enables your app to add custom context menu items or manage the framework-provided content menu items. For example, the following implementation changes the title of the framework-provided copySubject menu item from “Copy” to “Copy and remove background”.

func overlayView(_ overlayView: ImageAnalysisOverlayView, updateMenu menu: NSMenu, forEvent: NSEvent, atPoint point: CGPoint) -> NSMenu {

    let copySubjectItem = menu.item(withTag:ImageAnalysisOverlayView.MenuTag.copySubject)
    copySubjectItem.title = "Copy and remove background"

    return menu
}

The menu items don’t persist. However, your app can alter menu items and share them across different menus within the same menu session.

To add items to a menu, access the recommended index for insertion by using the recommendedAppItems menu tag, such as in the following code:

func overlayView(_ overlayView: ImageAnalysisOverlayView, updateMenu menu: NSMenu, forEvent: NSEvent, atPoint point: CGPoint) -> NSMenu {

    let item = NSMenuItem()
    let tag = ImageAnalysisOverlayView.MenuTag.recommendedAppItems
    let index = menu.indexOfItem(withTag:tag)
    menu.insertItem(item, at: index)

    return menu
}

See Also

Responding to key and menu events