Geo*_*orm 3 macos cocoa nscolorpanel
我正在使用 NSColorWell,它被设置为持续更新。我需要知道用户何时完成从颜色面板中的颜色选择器编辑控件(鼠标向上)。
我安装了一个事件监视器,并成功接收鼠标按下和鼠标移动消息,但是 NSColorPanel 似乎阻止了鼠标向上。
最重要的是,我想将最终选择的颜色添加到我的撤消堆栈中,而无需在用户选择其选择时生成所有中间颜色。
有没有一种方法可以创建自定义 NSColorPanel 并通过覆盖其 mouseUp 并发送消息的想法来替换共享面板?
在我的研究中,这个问题曾多次被提出,但我还没有看到成功的解决方案。
此致, - 乔治·劳伦斯·斯托姆 (George Lawrence Storm),Keencoyote 发明服务公司
我发现,如果我们观察colorkeypath ,NSColorPanel我们会在鼠标按下事件时被额外调用一次。这使我们能够忽略NSColorWell鼠标左键按下时的操作消息,并从关键路径观察器获取最终颜色。
在此应用程序委托示例代码中colorChanged:是一个NSColorWell操作方法。
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] & 1) == 1;
}
@end
Run Code Online (Sandbox Code Playgroud)