How to keep a button of a NSStatusItem highlighted?

I just want a stantdard behabiour of my status item. If the button of the status item is clicked, it should be highlighted and a popup appears. As soon the popup disappears, the button is not highlighted any more. Currently the button seems to be a momentary push button, because it is only highlighted between the mouse down and mouse up event.


I tried to change the button styoe without success:


_statusItem = [statusBar statusItemWithLength:NSSquareStatusItemLength];
[_statusItem.button setButtonType:NSOnOffButton];
_statusItem.button.image = [NSImage imageNamed:@"status_item"];
_statusItem.button.image.size = NSMakeSize(22.0, 22.0);
_statusItem.button.image.template = YES;
_statusItem.button.action = @selector(statusItemClicked:);
_statusItem.button.target = self;


Then I tried to highlight the button programmatically:


-(IBAction)statusItemClicked:(id)sender {
    if (!_statusItemWindowController) {
        _statusItemWindowController = [[IQSRStatusItemWindowController alloc] initWithWindowNibName:@"StatusItemPanel"];
    }
  
    if (_statusItemWindowController.window.isVisible) {
        [_statusItemWindowController hideStatusItemWindow];
        [_statusItem.button setState:NSOffState];
        [_statusItem.button highlight:NO];
    } else {
        [_statusItemWindowController showStatusItemWindow];
        [_statusItem.button setState:NSOnState];
        [_statusItem.button highlight:YES];
    }
}


This is not working either. Has someone an idea? I could exchange the image, but this is more a workaround and not a solution.

Replies

Is it IOS or OSX ?


Try using setHighlited instead of highlight.


have a look at this discussion h ttps://stackoverflow.com/questions/24854217/highlight-nsstatusitem-when-triggered-programmatically

Thanks. This solution works for me:


    NSStatusBar *statusBar = [NSStatusBar systemStatusBar];
  
    _statusItem = [statusBar statusItemWithLength:NSSquareStatusItemLength];
    _statusItem.button.image = [NSImage imageNamed:@"status_item"];
    _statusItem.button.image.size = NSMakeSize(22.0, 22.0);
    _statusItem.button.image.template = YES;
    _statusItem.button.target = self;
  
    [NSEvent addLocalMonitorForEventsMatchingMask:(NSLeftMouseDown | NSRightMouseDown)
                                          handler:^NSEvent *(NSEvent *event) {
                                              if (event.window == self.statusItem.button.window) {
                                                  [self statusItemClicked:nil];
                                                  return nil;
                                              }
                                              return event;
                                          }];


with the following action:


-(IBAction)statusItemClicked:(id)sender {
    if (!_statusItemWindowController) {
        _statusItemWindowController = [[IQSRStatusItemWindowController alloc] initWithWindowNibName:@"StatusItemPanel"];
    }
   
    if (_statusItemWindowController.window.isVisible) {
        [_statusItemWindowController hideStatusItemWindow];
        [_statusItem.button highlight:false];
    } else {
        [_statusItemWindowController showStatusItemWindow];
        [_statusItem.button highlight:true];
    }
}

What did you change exactly ? Replace YES by true ?