Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I discovered that if we observe <code>color</code> keypath of <code>NSColorPanel</code> we get called one extra time on mouse up events. This allows us to ignore action messages from <code>NSColorWell</code> when the left mouse button is down and to get the final color from keypath observer.</p> <p>In this application delegate example code <code>colorChanged:</code> is a <code>NSColorWell</code> action method.</p> <pre><code>void* const ColorPanelColorContext = (void*)1001; @interface AppDelegate() @property (weak) NSColorWell *updatingColorWell; @end @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)notification { NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel]; [colorPanel addObserver:self forKeyPath:@"color" options:0 context:ColorPanelColorContext]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == ColorPanelColorContext) { if (![self isLeftMouseButtonDown]) { if (self.updatingColorWell) { NSColorWell *colorWell = self.updatingColorWell; [colorWell sendAction:[colorWell action] to:[colorWell target]]; } self.updatingColorWell = nil; } } } - (IBAction)colorChanged:(id)sender { if ([self isLeftMouseButtonDown]) { self.updatingColorWell = sender; } else { NSColorWell *colorWell = sender; [self updateFinalColor:[colorWell color]]; self.updatingColorWell = nil; } } - (void)updateFinalColor:(NSColor*)color { // Do something with the final color... } - (BOOL)isLeftMouseButtonDown { return ([NSEvent pressedMouseButtons] &amp; 1) == 1; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload