Note that there are some explanatory texts on larger screens.

plurals
  1. PONSButton subclass as colorwell & preventing NSColorPanel from touching the first responder
    primarykey
    data
    text
    <p>I followed some examples for making an <code>NSButton</code> subclass work as an <code>NSColorWell</code> (since our <code>NSButton</code> 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 <code>NSColorWell</code> with our appearance customizations, would it not have this problem?</p> <p>However, I'm still hoping for a work-around that avoids that &amp; 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 <code>NSColorPanel</code> be that overrode setColor:, making it post the expected notification but not touch the first responder?</p> <p>(Note, rather than simply opening the color panel, I'm currently making use of <code>BFColorPickerPopover</code> by DrummerB <a href="https://github.com/DrummerB/BFColorPickerPopover" rel="nofollow">https://github.com/DrummerB/BFColorPickerPopover</a>. However I don't think that's much of a complication. I had the same <code>NSColorPanel</code> / first responder issue before integrating it).</p> <p>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 <code>NSColorPanel</code> directly):</p> <p>.h:</p> <pre><code>@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 </code></pre> <p>.m:</p> <pre><code>@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 &amp;&amp; !self.active) [self activate]; else if (self.active) [self deactivate]; } - (void)takeColorFrom:(id)sender { if ([sender respondsToSelector:@selector(color)]) self.color = [sender color]; } @end </code></pre> <p><strong>Addendum:</strong></p> <p>I tried using a normal <code>NSColorWell</code> in place of my NSButton subclass, and same issue. Colors chosen in the panel calls the first responder's <code>changeColor:</code> in addition to invoking the action method. So forgetting everything about <code>NSButton</code> in my question, how, in general, does one use a <code>NSColorWell</code> whose color mustn't also be pushed onto the first responder? Must one resort to customizing the expected first responder to selectively ignore <code>changeColor:</code>, or is making the <code>NSColorWell</code> the first responder really the thing to do, or something else?</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.
 

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