12

我想创建一个NSButton在单击它时发送一个动作,但是当它被按下 1 或 2 秒时它会显示一个 NSMenu。和这里的这个问题一模一样,但是由于那个答案不能解决我的问题,我决定再问一次。

例如,转到 Finder,打开一个新窗口,浏览一些文件夹,然后单击后退按钮:您转到上一个文件夹。现在单击并按住返回按钮:显示一个菜单。我不知道如何使用NSPopUpButton.

4

4 回答 4

14

使用NSSegmentedControl.

通过发送setMenu:forSegment:到控件来添加菜单(将任何东西连接到menuIB 中的插座不会起到作用)。有一个连接到控件的操作(这很重要)。

应该完全按照您的描述工作。

于 2012-03-20T04:43:06.267 回答
6

创建一个子类NSPopUpButton并覆盖mouseDown/mouseUp事件。

在调用 的实现之前让mouseDown事件延迟片刻,super并且仅当鼠标仍被按住时。

在触发按钮的/之前将mouseUp事件设置selectedMenuItemnil(因此selectedMenuItemIndex将是) 。-1targetaction

唯一的另一个问题是处理快速单击,其中单击的计时器可能会在鼠标按下以进行将来的单击时触发。我没有使用 anNSTimer并使其无效,而是选择了一个简单的mouseDown事件计数器,并在计数器发生变化时退出。

这是我在子类中使用的代码:

// MyClickAndHoldPopUpButton.h
@interface MyClickAndHoldPopUpButton : NSPopUpButton

@end

// MyClickAndHoldPopUpButton.m
@interface MyClickAndHoldPopUpButton ()

@property BOOL mouseIsDown;
@property BOOL menuWasShownForLastMouseDown;
@property int mouseDownUniquenessCounter;

@end

@implementation MyClickAndHoldPopUpButton

// highlight the button immediately but wait a moment before calling the super method (which will show our popup menu) if the mouse comes up
// in that moment, don't tell the super method about the mousedown at all.
- (void)mouseDown:(NSEvent *)theEvent
{
  self.mouseIsDown = YES;
  self.menuWasShownForLastMouseDown = NO;
  self.mouseDownUniquenessCounter++;
  int mouseDownUniquenessCounterCopy = self.mouseDownUniquenessCounter;

  [self highlight:YES];

  float delayInSeconds = [NSEvent doubleClickInterval];
  dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
  dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    if (self.mouseIsDown && mouseDownUniquenessCounterCopy == self.mouseDownUniquenessCounter) {
      self.menuWasShownForLastMouseDown = YES;
      [super mouseDown:theEvent];
    }
  });
}

// if the mouse was down for a short enough period to avoid showing a popup menu, fire our target/action with no selected menu item, then
// remove the button highlight.
- (void)mouseUp:(NSEvent *)theEvent
{
  self.mouseIsDown = NO;

  if (!self.menuWasShownForLastMouseDown) {
    [self selectItem:nil];

    [self sendAction:self.action to:self.target];
  }

  [self highlight:NO];
}

@end
于 2012-12-30T00:18:46.640 回答
6

如果有人仍然需要这个,这是我基于普通 NSButton 的解决方案,而不是分段控件。

继承 NSButton 并实现mouseDown在当前运行循环中启动计时器的自定义。在mouseUp中,检查计时器是否未触发。在这种情况下,取消它并执行默认操作。

这是一种非常简单的方法,它适用于您可以在 IB 中使用的任何 NSButton。

下面的代码:

- (void)mouseDown:(NSEvent *)theEvent {
    [self setHighlighted:YES];
    [self setNeedsDisplay:YES];

    _menuShown = NO;
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(showContextMenu:) userInfo:nil repeats:NO];

    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void)mouseUp:(NSEvent *)theEvent {
    [self setHighlighted:NO];
    [self setNeedsDisplay:YES];

    [_timer invalidate];
    _timer = nil;

    if(!_menuShown) {
        [NSApp sendAction:[self action] to:[self target] from:self];
    }

    _menuShown = NO;
}

- (void)showContextMenu:(NSTimer*)timer {
    if(!_timer) {
        return;
    }

    _timer = nil;
    _menuShown = YES;

    NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];

    [[theMenu addItemWithTitle:@"Beep" action:@selector(beep:) keyEquivalent:@""] setTarget:self];
    [[theMenu addItemWithTitle:@"Honk" action:@selector(honk:) keyEquivalent:@""] setTarget:self];

    [theMenu popUpMenuPositioningItem:nil atLocation:NSMakePoint(self.bounds.size.width-8, self.bounds.size.height-1) inView:self];

    NSWindow* window = [self window];

    NSEvent* fakeMouseUp = [NSEvent mouseEventWithType:NSLeftMouseUp
                                              location:self.bounds.origin
                                         modifierFlags:0
                                             timestamp:[NSDate timeIntervalSinceReferenceDate]
                                          windowNumber:[window windowNumber]
                                               context:[NSGraphicsContext currentContext]
                                           eventNumber:0
                                            clickCount:1
                                              pressure:0.0];

    [window postEvent:fakeMouseUp atStart:YES];

    [self setState:NSOnState];
}

我已经在我的 GitHub 上发布了一个工作示例。

于 2016-10-10T05:25:07.287 回答
2

派对迟到了,但这里有一点不同的方法,也是子类化NSButton

///
/// @copyright © 2018 Vadim Shpakovski. All rights reserved.
///

import AppKit

/// Button with a delayed menu like Safari Go Back & Forward buttons.
public class DelayedMenuButton: NSButton {

  /// Click & Hold menu, appears after `NSEvent.doubleClickInterval` seconds.
  public var delayedMenu: NSMenu?
}

// MARK: -

extension DelayedMenuButton {

  public override func mouseDown(with event: NSEvent) {

    // Run default implementation if delayed menu is not assigned
    guard delayedMenu != nil, isEnabled else {
      super.mouseDown(with: event)
      return
    }

    /// Run the popup menu if the mouse is down during `doubleClickInterval` seconds
    let delayedItem = DispatchWorkItem { [weak self] in
      self?.showDelayedMenu()
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(Int(NSEvent.doubleClickInterval * 1000)), execute: delayedItem)

    /// Action will be set to nil if the popup menu runs during `super.mouseDown`
    let defaultAction = self.action

    // Run standard tracking
    super.mouseDown(with: event)

    // Restore default action if popup menu assigned it to nil
    self.action = defaultAction

    // Cancel popup menu once tracking is over
    delayedItem.cancel()
  }
}

// MARK: - Private API

private extension DelayedMenuButton {

  /// Cancels current tracking and runs the popup menu
  func showDelayedMenu() {

    // Simulate mouse up to stop native tracking
    guard
      let delayedMenu = delayedMenu, delayedMenu.numberOfItems > 0, let window = window, let location = NSApp.currentEvent?.locationInWindow,
      let mouseUp = NSEvent.mouseEvent(
        with: .leftMouseUp, location: location, modifierFlags: [], timestamp: Date.timeIntervalSinceReferenceDate,
        windowNumber: window.windowNumber, context: NSGraphicsContext.current, eventNumber: 0, clickCount: 1, pressure: 0
      )
    else {
        return
    }

    // Cancel default action
    action = nil

    // Show the default menu
    delayedMenu.popUp(positioning: nil, at: .init(x: -4, y: bounds.height + 2), in: self)

    // Send mouse up when the menu is on screen
    window.postEvent(mouseUp, atStart: false)
  }
}
于 2018-08-03T11:59:28.570 回答