0

我正在尝试实现一个弹出菜单(在 Chrome 中当光标位于左箭头上方时按下鼠标右键时可以看到的内容)。

我有一个派生自的类NSToolBarItem,我有另一个派生自NSToolBar. 在我调用的工具栏中setAllowsUserCustomization。所以我右键单击工具栏上的任意位置会弹出工具栏的自定义菜单。

感谢您提供的任何指示。

4

2 回答 2

2

您不需要子类化NSToolbarItem. 只需给一个工具栏项自己的视图(在代码或 IB 中)。在该视图中,您可以使用标准控件(如NSPopUpButton)或具有您喜欢的任何事件处理逻辑的自定义视图。

于 2014-08-04T05:08:11.327 回答
0

如果您希望您的 NSToolbarItem 自定义视图接收 mouseDown 事件,您可以遵循以下模式:使用自定义 NSWindow 子类(或调整-[NSWindow hitTest:]方法)并自己将事件转发到您的视图。

// MyWindow.h

@interface MyWindow : NSWindow

@end

@interface NSView (MyWindow)

- (BOOL)interceptsToolbarRightMouseDownEvents;

@end

// MyWindow.m

@implementation NSView (MyWindow)

- (BOOL)interceptsToolbarRightMouseDownEvents { // overload in your custom toolbar item view return YES
    return NO;
}

@end

@interface NSToolbarView : NSView /* this class is hidden in AppKit */ @end

@implementation MyWindow

- (void)sendEvent:(NSEvent*)event {
    if (event.type == NSRightMouseDown) {
        NSView* frameView = [self.contentView superview];
        NSView* view = [frameView hitTest:[frameView convertPoint:event.locationInWindow fromView:nil]];
        if ([view isKindOfClass:NSToolbarView.class])
            for (NSView* subview in view.subviews) {
                NSView* view = [subview hitTest:[subview convertPoint:event.locationInWindow fromView:nil]];
                if (view.interceptsToolbarRightMouseDownEvents)
                    return [view rightMouseDown:event];
            }
    }

    [super sendEvent:event];
}

@end
于 2015-05-31T14:22:13.743 回答