title: Keeping an NSStatusBarButton highlighted while popover is displayed
body: <p>Practically all of <code>NSStatusItem</code> has been deprecated for 10.10 and the behavior of the underlying <code>NSStatusBarButton</code> seems to be confusing.</p>
  
  <p>Currently I am working on a menu bar application. When the user clicks the menu bar icon for the app, a method in my application delegate is called via target-action which displays an NSPopover (or closes it if it's already visible) with some information.</p>
  
  <p>Normally, if you've associated, say, an NSMenu with an NSStatusItem when the user clicks on the menu bar icon that icon remains highlighted blue until the menu is closed. Similarly clicking the system volume icon pops down a slider and highlights its icon blue until the view containing the slider disappears.</p>
  
  <p>However, since I'm the one opening the NSPopover, the system instead highlights the icon blue on mouse down, then returns it to normal on mouse up after my method has been called. Meaning there's nothing I can seem to do on that loop to maintain the highlight. I want the icon to continue being highlighted on mouse up and only return to normal when I tell it to (ie. when I close my popover.)</p>
  
  <p>I have no idea how to do this. I've tried using</p>
  
  <pre><code>[self.statusItem.button setHighlighted: YES];
  //or [self.statusItem.button highlight: YES];
  </code></pre>
  
  <p>when I receive the mouse up event in my app delegate and open the popover. The problem is the system still has it, apparently, highlighted this frame/loop from the earlier mouse down and immediately after I set it to highlighted, it sets it to unhighlighted due to the mouse up. I can get around this by encapsulating this in a method and running the method using a timer or delayed selector a split second later. This allows me to keep the icon highlighted but introduces a flicker; the icon is highlighted automatically as the mouse goes down, as the mouse goes up it unhighlights it for a frame, then my method re-highlights it.</p>
  
  <p>I also figured perhaps I could use the deprecated <code>setHighlightMode:</code> and set it to <code>NO</code> to prevent the icon from being highlighted automatically on click, then using <code>setHighlighted: / highlighted:</code> to set it manually but that doesn't work either. Similarly, I thought maybe this would work as well:</p>
  
  <pre><code> NSButtonCell* cell = (NSButtonCell*)self.statusItem.button.cell;
  cell.highlightsBy = NSNoCellMask;
  </code></pre>
  
  <p>But regardless clicking it automatically highlights the icon and dehighlights it on mouse up right after my method is called.</p>
  
  <p>Basically:</p>
  
  <blockquote>
    <ol>
    <li>The undesirable automatic highlighting behavior of <code>NSStatusBarButton</code> interferes with manually setting the highlight state, unless I delay manually setting it which introduces a short flicker.</li>
    <li>The only thing that seems to successfully disable this automatic behavior is the deprecated <code>setHighlightMode:</code>, but this seems to prevent all highlighting, manual or not.</li>
    <li>The only work around seems to be to add a subview to the NSButtonCell, add an event listener for mouse up and then set the highlight state of the superview as per here:
    <a href="https://stackoverflow.com/questions/26004684/nsstatusbarbutton-keep-highlighted">NSStatusBarButton keep highlighted</a>
    but I would think there'd be a simpler way to just... disable the automatic highlighting altogether.</li>
    </ol>
  </blockquote>
  
  <hr>
  
  <p>tl;dr: Is there a way for me to easily obtain full control over when and when not my menu bar icon is highlighted, so that I can have it highlight naturally while my NSPopover is displayed?</p>