NSStatusBarButton keep highlighted

后端 未结 9 2416
一个人的身影
一个人的身影 2020-12-16 16:05

As of OS X 10.10 most of NSStatusItem has been deprecated in favour of the button property, which consists of an NSStatusBarButton. It should work like a norma

9条回答
  •  鱼传尺愫
    2020-12-16 16:51

    Here is one more option. Don't set NSStatusItem's action property. Instead add a local event monitor:

    [NSEvent addLocalMonitorForEventsMatchingMask:(NSLeftMouseDown | NSRightMouseDown)
                                          handler:^NSEvent *(NSEvent *event) {
                                              if (event.window == self.statusItem.button.window) {
                                                  [self itemClicked];
                                                  return nil;
                                              }
                                              return event;
                                          }];
    

    Then in -itemClicked highlight the button using highlight: method:

    - (void)itemClicked {
        [self.statusItem.button highlight:YES];
        // Do other stuff 
    }
    

    To unhighlight just call button's highlight:NO where you need.

提交回复
热议问题