A robot friend says this - may help, may not:
To fix unexpected autofill or contacts in macOS context menus using C++ and Objective-C++, check these areas:
Context Menu Implementation:
Ensure menus only show relevant items for the context.
Event Handling:
Verify correct handling of right-click events.
Input Field Attributes:
Disable unwanted autofill attributes (e.g., autocomplete="off").
Custom Menu Items:
Remove unwanted system items and ensure custom items are properly defined.
Debugging and Logging:
Add logs to trace unwanted menu additions.
Use Xcode Instruments for profiling.
Frameworks and Libraries:
Check third-party libraries for side effects.
Update to latest versions.
Here is an example of how to implement a context menu in C++ with Objective-C++:
// ViewController.h
#import <Cocoa/Cocoa.h>
@interface ViewController : NSViewController <NSMenuDelegate>
@end
// ViewController.mm
#import "ViewController.h"
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Context Menu"];
[menu setDelegate:self];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Custom Action"
action:@selector(customAction)
keyEquivalent:@""];
[menu addItem:menuItem];
[self.view setMenu:menu];
}
(void)customAction {
// Handle custom action
}
(void)menuNeedsUpdate:(NSMenu *)menu {
[menu removeAllItems];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Dynamic Action"
action:@selector(customAction)
keyEquivalent:@""];
[menu addItem:menuItem];
}
@end
// main.cpp
#include <Cocoa/Cocoa.h>
int main(int argc, const char * argv[]) {
return NSApplicationMain(argc, argv);
}
In this example, ViewController implements the NSMenuDelegate protocol to dynamically update the context menu. The menuNeedsUpdate: method ensures that the context menu only contains relevant items. This example combines C++ (in main.cpp) with Objective-C++ (in ViewController.mm), which is commonly used in macOS development.