0

I have NSPopupButton inside NSToolbar that has borderder = false which is having some color burn blend when highlighted. If I use bordered = true the image is drawn with nice dark overlay.

I am trying to achieve to draw highlighted state the same way as it is in bordered=true

PS: NSButtonCell with bordered = false works out of the box.

I can achieve the behaviour by having bordered = true and overriding drawBezel and do nothing there but I want to know

Things I tried:

  • highlightsBy
  • interiorBackgroundStyle
  • setCellAttribute

-

class ToolbarPopUpButtonCell : NSPopUpButtonCell {

  override var isHighlighted: Bool {
      get { return true }
      set { super.isHighlighted = newValue }
  }   

  override func drawImage(withFrame cellFrame: NSRect, in controlView: NSView) {
      super.drawImage(withFrame: cellFrame, in: controlView)
  }


  //used in case bordered = true so we do nothing
  override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {

  }

  //doesn't work
  override var interiorBackgroundStyle: NSView.BackgroundStyle
      {
      return .raised
  }
}

  class ToolbarPopUpButton: NSPopUpButton {

  override func awakeFromNib() {
      cell?.setCellAttribute(.cellLightsByBackground, to: 1)
  }

  override var intrinsicContentSize: NSSize {
      return NSMakeSize(32 + 5, 32)
  }
}

Notice the image on right which works for bordered = false (NSButtonCell)

enter image description here

Marek H
  • 5,173
  • 3
  • 31
  • 42

1 Answers1

0

The only hack I found is to draw a subsitute cell. Still seeking a better solution.

- (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSImage *image = [self image];
    NSButtonCell *swapCell = [[NSButtonCell alloc] initImageCell:image];
    [swapCell setHighlighted:[self isHighlighted]];
    [swapCell drawImage:image withFrame:cellFrame inView:controlView];
}
Marek H
  • 5,173
  • 3
  • 31
  • 42