Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, a better web search term (<em>NSColorWell "first responder"</em>) and I see others have struggled with <code>NSColorWell</code> and this same problem for a long time. Many old mailing list threads have covered this and saw 3 solutions:</p> <ol> <li><p><a href="http://www.cocoabuilder.com/archive/cocoa/82832-nscolorwell-changecolor-and-first-responder.html" rel="nofollow">http://www.cocoabuilder.com/archive/cocoa/82832-nscolorwell-changecolor-and-first-responder.html</a> Douglas Davidson suggests subclassing the potential first responder(s) so they ignore <code>changeColor:</code> (probably what I'll do)</p></li> <li><p><a href="http://www.cocoabuilder.com/archive/cocoa/3263-your-nscolorwell-got-in-my-nstext.html" rel="nofollow">http://www.cocoabuilder.com/archive/cocoa/3263-your-nscolorwell-got-in-my-nstext.html</a> Guy English suggests making the color well the first responder temporarily (what I tried but had problems due to color well being in a panel which I don't want becoming key)</p></li> <li><p><a href="http://www.cocoabuilder.com/archive/cocoa/180323-detecting-currently-active-nscolorwell.html" rel="nofollow">http://www.cocoabuilder.com/archive/cocoa/180323-detecting-currently-active-nscolorwell.html</a> Martin suggests cutting preventing the <code>changeColor:</code> call in the first place by posing as <code>NSColorPanel</code> and overriding a private method (closest to what I wanted, but more risk of app store rejection than I'm comfortable with)</p></li> </ol> <p><strong>UPDATE</strong>: I tried #1 but it turns out I can't override the first responder (<code>WebView</code> / <code>WebHTMLView</code> grrr). Going with #3, I put the following in a <code>NSColorPanel</code> category, made my color button set <code>panel.avoidsChangingFirstResponder=YES</code>, and it seems to work:</p> <pre><code>static char changeColorPatchAssociatedObjectKey; // address of this is used as a unique runtime value - (BOOL)avoidsChangingFirstResponder { NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject(self, &amp;changeColorPatchAssociatedObjectKey); return changeColorPatchFlag &amp;&amp; changeColorPatchFlag.boolValue; } - (void)setAvoidsChangingFirstResponder:(BOOL)enablePatch { NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject(self, &amp;changeColorPatchAssociatedObjectKey); if ((!changeColorPatchFlag &amp;&amp; enablePatch) || (changeColorPatchFlag &amp;&amp; changeColorPatchFlag.boolValue != enablePatch)) objc_setAssociatedObject(self, &amp;changeColorPatchAssociatedObjectKey, @( enablePatch ), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } + (void)load { if (self == [NSColorPanel class]) { // patch implementation of _forceSendAction:notification:firstResponder: (use swizzle technique from MAKVONotificationCenter.m) // for one that calls original but with the last BOOL parameter conditionally changed to NO SEL methodSel = NSSelectorFromString(@"_forceSendAction:notification:firstResponder:"); Method method = class_getInstanceMethod(self, methodSel); IMP origImpl = method_getImplementation(method); IMP newImpl = imp_implementationWithBlock(^(void *obj, SEL s, BOOL isAct, BOOL isNotif, BOOL isFirstResp) { NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject((__bridge id)(obj), &amp;changeColorPatchAssociatedObjectKey); if (changeColorPatchFlag &amp;&amp; changeColorPatchFlag.boolValue) isFirstResp = NO; ((void (*)(void *, SEL, BOOL, BOOL, BOOL))origImpl)(obj, s, isAct, isNotif, isFirstResp); }); class_replaceMethod(self, methodSel, newImpl, method_getTypeEncoding(method)); } } </code></pre> <p>I hope someone else finds this useful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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