gpt4 book ai didi

macos - NSColorPanel 阻止鼠标按下事件

转载 作者:行者123 更新时间:2023-12-03 16:17:20 25 4
gpt4 key购买 nike

我正在使用 NSColorWell,它被设置为持续更新。我需要知道用户何时完成从颜色面板中的颜色选择器编辑控件(鼠标向上)。

我安装了事件监视器,并成功接收鼠标按下和鼠标移动消息,但是 NSColorPanel 似乎阻止了鼠标向上。

最重要的是,我想将最终选定的颜色添加到我的撤消堆栈中,而无需在用户选择其选择时生成所有中间颜色。

是否有一种方法可以创建自定义 NSColorPanel 并通过覆盖其 mouseUp 并发送消息的想法来替换共享面板?

在我的研究中,这个问题已被提出过几次,但我还没有看到成功的解决方案。

问候,- 乔治·劳伦斯·斯托姆 (George Lawrence Storm),Keencoyote 发明服务

最佳答案

我发现,如果我们观察 NSColorPanelcolor 键路径,我们会在鼠标松开事件上被额外调用一次。这允许我们在按下鼠标左键时忽略来自 NSColorWell 的操作消息,并从键路径观察器获取最终颜色。

在此应用程序委托(delegate)示例代码中 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

关于macos - NSColorPanel 阻止鼠标按下事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14138852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com