Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:25:34+00:00 2026-06-17T10:25:34+00:00

I followed some examples for making an NSButton subclass work as an NSColorWell (since

  • 0

I followed some examples for making an NSButton subclass work as an NSColorWell (since our NSButton subclass already gives us the appearance behavior we need), however I’ve noticed that after using the button to invoke the panel and changing the color, it’s also changing the color of selected text in our document. If I instead subclassed NSColorWell with our appearance customizations, would it not have this problem?

However, I’m still hoping for a work-around that avoids that & still lets us use our button subclass. I’ve seen discussion threads suggest letting the button itself become the first responder, however with the button being in a separate palette I’m having trouble getting this to work. Also I’d prefer not to alter the responder chain or have the palette become the key window. How evil would a category on NSColorPanel be that overrode setColor:, making it post the expected notification but not touch the first responder?

(Note, rather than simply opening the color panel, I’m currently making use of BFColorPickerPopover by DrummerB https://github.com/DrummerB/BFColorPickerPopover. However I don’t think that’s much of a complication. I had the same NSColorPanel / first responder issue before integrating it).

Was asked to post source code, so here’s the relevant bits from my NSButton subclass (note, uses the picker popover mentioned above rather than NSColorPanel directly):

.h:

@interface ...
@property (nonatomic, strong) NSColor *color;
@property (nonatomic, assign) BOOL active;
@property (nonatomic, strong) NSColor *buttonColor;
@property (nonatomic, weak) BFColorPickerPopover *popover;
- (void)activate:(BOOL)exclusive; // param ignored, always exclusive
- (void)activate;
- (void)deactivate;
- (void)takeColorFrom:(id)sender;
@end

.m:

@implementation ...
@dynamic color;
- (NSColor *)color
{
    return self.buttonColor;
}
- (void)setColor:(NSColor *)newColor
{
    self.buttonColor = newColor;
    [self generateSwatch];
    self.needsDisplay = YES;
    self.popover.color = newColor;
}
- (void)activate:(BOOL)exclusive
{
    [self activate]; // always exclusive
}
- (void)activate
{
    self.popover = [BFColorPickerPopover sharedPopover];
    self.popover.color = self.buttonColor;
    [self.popover showRelativeToRect:self.frame ofView:self.superview
                             preferredEdge:self.preferredEdgeForPopover];
    [[NSNotificationCenter defaultCenter] addObserver:self
                        selector:@selector(popoverDidClose:)
                            name:NSPopoverDidCloseNotification
                          object:self.popover];
    [[NSNotificationCenter defaultCenter] addObserver:self
                        selector:@selector(colorDidChange:)
                            name:NSColorPanelColorDidChangeNotification
                          object:self.popover.colorPanel];
    activeButton = self;
    self.active = YES;
}
- (void)deactivate
{
    if (self.popover)
    {
        [self.popover close];
        self.popover = nil;
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self
           name:NSPopoverDidCloseNotification object:self.popover];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                      name:NSColorPanelColorDidChangeNotification
                   object:self.popover.colorPanel];
    if (activeButton == self) activeButton = nil;
    self.active = NO;
}
- (void)popoverDidClose:(NSNotification *)notification
{
    self.popover = nil; // don't let deactivate ask it to close again
    [self deactivate];
}
- (void)colorDidChange:(NSNotification *)notification
{
    self.buttonColor = self.popover.colorPanel.color;
    [self generateSwatch];
    self.needsDisplay = YES;
    [self sendAction:self.action to:self.target];
}
- (void)mouseDown:(NSEvent *)theEvent
{
    if (self.isEnabled && !self.active)
        [self activate];
    else if (self.active)
        [self deactivate];
}
- (void)takeColorFrom:(id)sender
{
    if ([sender respondsToSelector:@selector(color)])
        self.color = [sender color];
}
@end

Addendum:

I tried using a normal NSColorWell in place of my NSButton subclass, and same issue. Colors chosen in the panel calls the first responder’s changeColor: in addition to invoking the action method. So forgetting everything about NSButton in my question, how, in general, does one use a NSColorWell whose color mustn’t also be pushed onto the first responder? Must one resort to customizing the expected first responder to selectively ignore changeColor:, or is making the NSColorWell the first responder really the thing to do, or something else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:25:35+00:00Added an answer on June 17, 2026 at 10:25 am

    Yes, a better web search term (NSColorWell “first responder”) and I see others have struggled with NSColorWell and this same problem for a long time. Many old mailing list threads have covered this and saw 3 solutions:

    1. http://www.cocoabuilder.com/archive/cocoa/82832-nscolorwell-changecolor-and-first-responder.html Douglas Davidson suggests subclassing the potential first responder(s) so they ignore changeColor: (probably what I’ll do)

    2. http://www.cocoabuilder.com/archive/cocoa/3263-your-nscolorwell-got-in-my-nstext.html 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)

    3. http://www.cocoabuilder.com/archive/cocoa/180323-detecting-currently-active-nscolorwell.html Martin suggests cutting preventing the changeColor: call in the first place by posing as NSColorPanel and overriding a private method (closest to what I wanted, but more risk of app store rejection than I’m comfortable with)

    UPDATE: I tried #1 but it turns out I can’t override the first responder (WebView / WebHTMLView grrr). Going with #3, I put the following in a NSColorPanel category, made my color button set panel.avoidsChangingFirstResponder=YES, and it seems to work:

    static char changeColorPatchAssociatedObjectKey; // address of this is used as a unique runtime value
    
    - (BOOL)avoidsChangingFirstResponder
    {
        NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject(self, &changeColorPatchAssociatedObjectKey);
        return changeColorPatchFlag && changeColorPatchFlag.boolValue;
    }
    
    - (void)setAvoidsChangingFirstResponder:(BOOL)enablePatch
    {
        NSNumber *changeColorPatchFlag = (NSNumber *)objc_getAssociatedObject(self, &changeColorPatchAssociatedObjectKey);
        if ((!changeColorPatchFlag && enablePatch) || (changeColorPatchFlag && changeColorPatchFlag.boolValue != enablePatch))
            objc_setAssociatedObject(self, &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), &changeColorPatchAssociatedObjectKey);
                if (changeColorPatchFlag && changeColorPatchFlag.boolValue)
                    isFirstResp = NO;
                ((void (*)(void *, SEL, BOOL, BOOL, BOOL))origImpl)(obj, s, isAct, isNotif, isFirstResp);
            });
            class_replaceMethod(self, methodSel, newImpl, method_getTypeEncoding(method));
        }
    }
    

    I hope someone else finds this useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Selenium for making some work: script should click at link followed by
I'm writing my very first steps in Objective-C. I followed some examples and the
I am obviously new to Rails, so I followed some examples on how to
I been really enjoying playing with OpenGl and have followed some examples etc My
I've been digging into EmberJS for one day :). I've followed several examples. Some
I recently followed some instructions with RVM to install ruby-debug and I think it
I'm trying to use ASP.net profiles I've followed some instructions which means I have
I am working on Windows Azure. I followed some tutorial about how to store
I really don't know much about Apache, I followed some tutorials and installed the
I am trying to make my application device administrator , i followed some notes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.