NSControl API should allow for semantic target-action API

Originator:joeljfischer
Number:rdar://17627577 Date Originated:7/10/2014
Status:Open Resolved:
Product:OSX SDK Product Version:10.10 beta3
Classification:Enhancement Reproducible:Always
 
Summary:
Currently NSControl’s API for receiving clicks requires the developer to set multiple sendActionOn: masks, which resolves to sending an action when any of the optioned actions occur. This requires the developer to then differentiate within the action method, to find which option mask occurred. Like below:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	self.statusItem.action = @selector(clickStatusItem:);
	[self.statusItem sendActionOn:(NSLeftMouseDownMask|NSRightMouseDownMask)];
}

- (void)clickStatusItem:(id)sender {
	BOOL rightClick = (([[NSApp currentEvent] modifierFlags] & NSControlKeyMask) == NSControlKeyMask) || ([[NSApp currentEvent] type] == NSRightMouseDown);
	if (rightClick) {
		[self singleRightClickedStatusItem:sender];
	} else {
		[self singleLeftClickedStatusItem:sender];
	}
}

Instead, the API could be similar to iOS’ UIControl, which uses an API like so

addTarget:action:forControlEvents:

allowing the above code to be transformed into the much more readable:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	[self.statusItem addTarget:self action:@selector(clickStatusItem) forControlEvents:NSLeftMouseDownMask];
	[self.statusItem addTarget:self action:@selector(rightClickStatusItem) forControlEvents:NSRightMouseDownMask];
}

- (void)clickStatusItem:(id)sender {
	[self singleLeftClickedStatusItem:sender];
}

- (void)rightClickStatusItem:(id)sender {
	[self singleRightClickedStatusItem:sender];
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!