Custom NSMenu view that does not display selectedMenuItemColor correctly

I wrote my own NSMenu class to show dynamic search results below NSSearchField in NSWindow with no fields. It works well, but did not draw the magic of selectedMenuItemColor correctly if I add an addition to the top of the preview. I put 5 pixels at the top of the container view to simulate NSMenu, and when I do this, the blue selected gradient is turned off. The picture and code should make this clear:

enter image description here

Here is my drawRect code inside my element view (remember this is a regular NSView):

 -(void) drawRect:(NSRect)dirtyRect { CGRect b = self.bounds; if (selected) { [NSGraphicsContext saveGraphicsState]; [[NSColor selectedMenuItemColor] set]; NSRectFill( b ); [NSGraphicsContext restoreGraphicsState]; if (textField) { textField.textColor = [NSColor selectedMenuItemTextColor]; } } else { [[NSColor clearColor] set]; NSRectFillUsingOperation(b, NSCompositeSourceOver); if (textField) { textField.textColor = [NSColor blackColor]; } } } 
+4
source share
1 answer

You should get the start of the template phase according to your perspective.

That is, selectedMenuItemColor is actually a template, not a color, and this template is designed to display "correctly" in the "standard increase the height of the menu item." Since you added the add-on, now it does not appear in the "standard" location.

Try the following:

 -(void) drawRect:(NSRect)dirtyRect { CGRect b = self.bounds; if (selected) { NSPoint origin = [self frame].origin; curContext = [NSGraphicsContext currentContext]; [curContext saveGraphicsState]; [curContext setPatternPhase: origin]; [[NSColor selectedMenuItemColor] set]; NSRectFill( b ); [curContext restoreGraphicsState]; if (textField) { textField.textColor = [NSColor selectedMenuItemTextColor]; } } else { [[NSColor clearColor] set]; NSRectFillUsingOperation(b, NSCompositeSourceOver); if (textField) { textField.textColor = [NSColor blackColor]; } } } 
0
source

Source: https://habr.com/ru/post/1401150/


All Articles