找到你要的答案

Q:Transient NSPopover swallows first click on parent window control

Q:瞬态NSPopover燕子首先点击父窗口控制

I have a transient NSPopover and when it's open and I click a button in the parent window the popover is dismissed instead and the click "swallowed". Only the second click on the button triggers the action properly.

Is there a way to pass the first click through to the control directly and dismiss the popover in one step?

我有一个短暂的NSPopover和当它打开时,我点击一个按钮在父窗口弹出了吧,点击“吞噬”。只有第二个按钮点击触发正确的行动。

有没有办法通过第一点击直接控制和一步把松饼吗?

answer1: 回答1:

NSPopover does seem to swallow the event for the targeted position view. Other views are fine. My solution is to get the delegate to forward the last mouse down event to the target view if hit testing reveals it was the clicked view. Unfortunatety NSApp -currentEvent is nil when the delegate gets messaged - not sure why. So I added an event monitor to the app delegate like so:

- (void)addEventMonitor 
{ 
    if (self.eventMonitor) {
        return;
    }

    self.eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:(NSLeftMouseDownMask) handler:^(NSEvent *incomingEvent) {
    NSEvent *result = incomingEvent;
    self.monitoredEvent = result;
    return result;
    }];

}

When the delegate closes it checks to see it the last monitored event was in the target:

- (void)popoverDidClose:(NSNotification *)notification
{
    // [NSApp currentEvent] is nil here
    NSEvent *event = [(BPApplicationDelegate *)[NSApp delegate] monitoredEvent];
    if (event && (event.type & NSLeftMouseDown)) {
        NSPoint pt = [self.targetView.superview convertPoint:event.locationInWindow fromView:nil];
        if ([self.TargetView hitTest:pt]) {
            [NSApp postEvent:event atStart:NO];
        }
    }
}

NSPopover似乎燕子事件的目标观。其他意见都很好。我的解决方法是让委托将最后一个鼠标向下的事件转发到目标视图,如果点击测试显示它是点击视图。unfortunatety nsapp - currentevent是零的时候代表获取信息-不知道为什么。所以我给事件委托添加了一个事件监视器:

- (void)addEventMonitor 
{ 
    if (self.eventMonitor) {
        return;
    }

    self.eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:(NSLeftMouseDownMask) handler:^(NSEvent *incomingEvent) {
    NSEvent *result = incomingEvent;
    self.monitoredEvent = result;
    return result;
    }];

}

当委托关闭时,它会检查它最后的监视事件是否在目标中:

- (void)popoverDidClose:(NSNotification *)notification
{
    // [NSApp currentEvent] is nil here
    NSEvent *event = [(BPApplicationDelegate *)[NSApp delegate] monitoredEvent];
    if (event && (event.type & NSLeftMouseDown)) {
        NSPoint pt = [self.targetView.superview convertPoint:event.locationInWindow fromView:nil];
        if ([self.TargetView hitTest:pt]) {
            [NSApp postEvent:event atStart:NO];
        }
    }
}
objective-c  swift  cocoa