Change background color on hover on a NSMenuItem with custom NSView
Asked Answered
C

3

6

Here is the thing:

I have created a custom NSMenuItem with a custom NSView in it.

Everything works fine except that I can't get the NSMenuItem to get highlighted (= change the background color on mouseover).

I'm trying to do it inside the drawRect method, as shown in other answers posted here.

What am I doing wrong?


The NSView subclass:

@interface customView : NSView  
@end  
@implementation customView

- (id)initWithFrame:(NSRect)frame
{

    NSRect theRect = NSMakeRect(0, 0, 200, 30);
    self = [super initWithFrame:theRect];
    if (self) {
    NSTrackingArea *   trackingArea = [[NSTrackingArea alloc] initWithRect:theRect
                                                    options: (NSTrackingMouseEnteredAndExited  | NSTrackingActiveInKeyWindow  |NSTrackingActiveAlways)
                                                      owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
    }

    return self;
}

#define menuItem ([self enclosingMenuItem])

- (void) drawRect: (NSRect) rect {


    BOOL isHighlighted = [menuItem isHighlighted];
    if (isHighlighted) {
        //this nslog never happens
        NSLog(@"it's highlighted");
}



- (void)mouseUp:(NSEvent*) event {
    NSMenuItem* mitem = [self enclosingMenuItem];
    NSMenu* m = [mitem menu];
    [m cancelTracking];


    NSLog(@"you clicked the %ld item",[m indexOfItem: mitem]);
}
@end

The NSMenuItem subclass:
(I add subviews on the custom view here so i can have access to the controls through the NSMenuItem instance)

@interface customItem : NSMenuItem{

}

-(void)setTheText:(NSString*)theString;

@property NSTextField *theLabel;
@end
#import "customItem.h"
#import "customView.h"
@implementation customItem
@synthesize theLabel;
-(id)init{

    if (self){

        customView *cv = [[customView alloc] init];
        theLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 8, 130, 17)];
        [theLabel setEditable:NO];
        [theLabel setBordered:NO];
        NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(170, 7, 20, 20)];
        NSButton *myButton1 = [[NSButton alloc] initWithFrame:NSMakeRect(150, 7, 20, 20)];

        [myButton setBezelStyle:NSCircularBezelStyle]; 
        [myButton1 setBezelStyle:NSCircularBezelStyle]; 

        [myButton setTitle:@""];
        [myButton1 setTitle:@""];
        [cv addSubview:myButton];
        [cv addSubview:myButton1];
        [cv addSubview:theLabel];
        [self setView:cv];
        [theLabel setStringValue:@"A Value "];

    }

    return self;
}

-(void)setTheText:(NSString *)theString{

    [theLabel setStringValue:theString];
}


@end   

And this is the App Delegate :

@interface AppDelegate : NSObject <NSApplicationDelegate>{

    NSStatusItem *statusItem;
    IBOutlet NSMenu *theMenu;
}

@property (assign) IBOutlet NSWindow *window;

@end  
#import "customItem.h"
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}

- (void)awakeFromNib{

    statusItem = [[NSStatusBar systemStatusBar]
                  statusItemWithLength:NSSquareStatusItemLength];
    NSBundle *bundle = [NSBundle mainBundle];

    NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon" ofType:@"png"]];
   NSImage  *highlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon_H" ofType:@"png"]];

    [statusItem setImage:statusImage];
    [statusItem setAlternateImage:highlightImage];


    [statusItem setMenu:theMenu];

    [theMenu removeAllItems];
     customItem *mi = [[customItem alloc] init];

   [theMenu addItem:mi];
   customItem *mi2 = [[customItem alloc] init];
   [theMenu addItem:mi2];  
}
@end

This is what i get:

NSMenuItem Issue Screenschot

Copywriter answered 12/7, 2013 at 0:38 Comment(0)
C
0

OK i think i got it.
I added a public bool variable in the NSView subclass.
Then i used

-(void)mouseEntered:(NSEvent *)theEvent

and

-(void)mouseExited:(NSEvent *)theEvent

to set the variable to YES or to NO. After setting the variable i used

[self setNeedsDisplay:YES] 

to call

-(void) drawRect: (NSRect) rect 

That's how i got it working :)

Copywriter answered 15/7, 2013 at 23:44 Comment(0)
W
4

No need to add Booleans or anything else, you can do it from within your custom NSView which is attached to your NSMenuItem

- (void)drawRect:(NSRect)rect {

[super drawRect:rect];

//Handle the hightlight
if ([[self enclosingMenuItem] isHighlighted])
{
    [self.lbl_title setTextColor:[NSColor whiteColor]];
    [self.lbl_amount setTextColor:[NSColor colorWithDeviceRed:151.0f/255.0f green:164.0f/255.0f blue:179.0f/255.0f alpha:1.0f]];
    [[NSColor selectedMenuItemColor] setFill];
}
else
{
    [self.lbl_title setTextColor:[NSColor blackColor]];
    [self.lbl_amount setTextColor:[NSColor whiteColor]];
    [[self backgroundColor] setFill];
}
NSRectFill(rect);}
Windowshop answered 26/8, 2014 at 9:35 Comment(0)
C
0

OK i think i got it.
I added a public bool variable in the NSView subclass.
Then i used

-(void)mouseEntered:(NSEvent *)theEvent

and

-(void)mouseExited:(NSEvent *)theEvent

to set the variable to YES or to NO. After setting the variable i used

[self setNeedsDisplay:YES] 

to call

-(void) drawRect: (NSRect) rect 

That's how i got it working :)

Copywriter answered 15/7, 2013 at 23:44 Comment(0)
B
0

Here is the right way of changing the background colour (highlighting the item) without maintaining a local variable and force the view to draw again,

-(void)mouseEntered:(NSEvent *)event {
    self.layer.backgroundColor = [[NSColor blueColor] colorWithAlphaComponent:0.3].CGColor;
    [theLabel setTextColor:[NSColor whiteColor]];
}

-(void)mouseExited:(NSEvent *)event {
    self.layer.backgroundColor = [NSColor clearColor].CGColor;
    [theLabel setTextColor:[NSColor blackColor]];
}

Make sure you also set [self setWantsLayer:YES] before changing the layer background colour.

Bodega answered 22/3, 2019 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.