Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSMenu issues #19

Closed
rdmeyers opened this issue Jun 12, 2018 · 3 comments
Closed

NSMenu issues #19

rdmeyers opened this issue Jun 12, 2018 · 3 comments

Comments

@rdmeyers
Copy link

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:

light theme on white

light theme on black

However, the DarkTheme messes them up as follows:

dark theme on white

dark theme on black

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.

@nfgrilo
Copy link
Contributor

nfgrilo commented Jun 15, 2018

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)...

@rdmeyers
Copy link
Author

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:

image

@nfgrilo
Copy link
Contributor

nfgrilo commented Jun 16, 2018

Looks great!

@nfgrilo nfgrilo closed this as completed Jun 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants