NSMenu issues
rdmeyers opened this issue · 3 comments
I create context menus on the fly based upon different criteria. One of these menus is a Highlight menu which has various colors to choose from. The LightTheme displays them properly regardless of the color scheme beneath the menu as follows:
However, the DarkTheme messes them up as follows:
Since menus do not have views by default, the allowsVibrancy work-around is not an option. Is this easily fixed with another work-around?
Following is a code snippet to give you an idea of how I am adding these highlight colors to the menu:
// orange highlighter
item = [[NSMenuItem alloc] initWithTitle:@" "
action:@selector(highlight:)
keyEquivalent:@""];
attributedTitle = [[NSMutableAttributedString alloc] initWithString:item.title
attributes:attribute];
[attributedTitle addAttribute:NSBackgroundColorAttributeName
value:[NSColor colorWithRed:1.0 green:0.8 blue:0.6 alpha:1.0]
range:NSMakeRange(0, attributedTitle.length)];
item.attributedTitle = attributedTitle;
item.tag = 0;
[submenu addItem:item];
Interestingly enough, during menu tracking (when you hover the mouse over the menu items) they switch to the appropriate color.
I have not tested this, but one thing I remembered that you can try is to use a custom NSMenuItem view instead, and then use an approach analogous to #18 (subclassing NSView
and overriding allowsVibrancy
)...
Abandoning the default NSMenuItem for a custom view is too ugly a work-around. Absolutely everything needs to be drawn (i.e. menu tracking) and the layout won't match the rest of the menu items. I opted to use images instead which works nicely! The code is smaller too :-)
// orange highlighter
item = [[NSMenuItem alloc] initWithTitle:@""
action:@selector(highlight:)
keyEquivalent:@""];
[item setImage:[NSImage imageNamed:@"orange"]];
item.tag = 0;
[submenu addItem:item];
That code now results in this:
Looks great!