问题描述
我遵循了一些使 NSButton
子类作为 NSColorWell
工作的示例(因为我们的 NSButton
子类已经为我们提供了所需的外观行为)),但是我注意到在使用按钮调用面板并更改颜色后,它还在更改文档中所选文本的颜色.如果我改用我们的外观自定义子类 NSColorWell
,它会不会出现此问题?
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?
但是,我仍然希望能找到一种避免该&仍然允许我们使用按钮子类.我已经看到讨论线程建议让按钮本身成为第一响应者,但是由于按钮位于单独的调色板中,因此很难使它起作用.另外,我也不想更改响应者链或使调色板成为关键窗口.在 NSColorPanel
上的类别覆盖setColor:使其发布预期的通知但不触摸第一个响应者会多么邪恶?
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?
(注意,不是简单地打开颜色面板,我目前正在使用DrummerB的 BFColorPickerPopover
https://github.com/DrummerB/BFColorPickerPopover .但是我不认为这很复杂.在集成之前,我遇到了相同的 NSColorPanel
/第一响应者问题它).
(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).
被要求发布源代码,所以这是我的NSButton子类的相关内容(注意,使用上面提到的选择器弹出窗口而不是直接使用 NSColorPanel
):
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
附录:
我尝试使用普通的 NSColorWell
代替我的NSButton子类,并且出现了同样的问题.在面板中选择的颜色除了调用操作方法外,还调用了第一响应者的 changeColor:
.因此,在我的问题中,忘记了有关 NSButton
的所有内容,通常情况下,该如何使用 NSColorWell 来将其颜色也不要推到第一个响应者上?必须一个方法来定制预期的第一响应者以有选择地忽略 changeColor:
,或者使 NSColorWell
成为真正要做的事情还是第一响应者?
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?
推荐答案
是的,一个更好的网络搜索词( NSColorWell第一响应者" ),我看到其他人为 NSColorWell
并在很长一段时间内都遇到了同样的问题.许多旧的邮件列表线程对此进行了介绍,并看到了3种解决方案:
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:
-
http://www.cocoabuilder.com/archive/cocoa/82832-nscolorwell-changecolor-and-first-responder.html 道格拉斯·戴维森(Douglas Davidson)建议对潜在的第一响应者进行子类化,以便他们忽略
changeColor:
(可能我会做什么)
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)
http://www.cocoabuilder.com/archive/cocoa/3263-your-nscolorwell-got-in-my-nstext.html Guy English建议暂时使颜色很好地成为第一响应者(我尝试了一下,但是由于我不想成为关键角色的面板中的色彩很好)
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)
http://www.cocoabuilder.com/archive/cocoa/180323-detecting-currently-active-nscolorwell.html 马丁建议首先假装为 NSColorPanel 来防止
changeColor:
调用代码>并重写私有方法(与我想要的最接近,但是比我所接受的应用商店拒绝的风险更大)
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)
更新:我尝试了#1,但事实证明我无法覆盖第一个响应者( WebView
/ WebHTMLView
grrr).与#3一起,我将以下内容放在 NSColorPanel
类别中,将我的颜色按钮设置为 panel.avoidsChangingFirstResponder = YES
,它似乎可以正常工作:
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.
这篇关于NSButton子类为colorwell&防止NSColorPanel接触第一响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!