Commits

Ryan Wilcox committed c156265

added TransparentMenuItemView class

Comments (0)

Files changed (2)

TransparentMenuItemView.h

+//
+//  TransparentView.h
+//  Keycard
+//
+//  Created by Ryan Wilcox on 5/6/12.
+//  Copyright (c) 2012 Wilcox Development Solutions. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+#import "RWViewWithViewController.h"
+
+/*
+ 
+    TransparentMenuItemView is for those times when you want to customize how a view
+    in an NSMenuItem looks (you want it transparent).
+ 
+    But, are you *sure* you don't want something like MAAttachedWindow, which can
+    center the view under the menu? It ships with an example showing how to use it with status items...
+ 
+    (If you just want this, then great!)
+*/
+@interface TransparentMenuItemView : RWViewWithViewController
+
+- (void)drawRect:(NSRect)dirtyRect;
+
+@end

TransparentMenuItemView.m

+//
+//  TransparentMenuItemView.m
+//  Keycard
+//
+//  Created by Ryan Wilcox on 5/6/12.
+//  Copyright (c) 2012 Wilcox Development Solutions. All rights reserved.
+//
+
+#import "TransparentMenuItemView.h"
+
+@implementation TransparentMenuItemView
+
+- (id)initWithFrame:(NSRect)frame
+{
+    self = [super initWithFrame:frame];
+    if (self) {
+        // Initialization code here.
+    }
+    
+    return self;
+}
+
+- (void)drawRect:(NSRect)dirtyRect
+{
+    /* Thanks, <http://stackoverflow.com/questions/4533816/gap-above-nsmenuitem-custom-view> */
+    
+    if (dirtyRect.size.width != self.bounds.size.width) // size of view. Let super take care of it if it's not the entire size of our view, else we get odd artifacts
+        return [super drawRect: dirtyRect];
+    
+    [NSGraphicsContext saveGraphicsState];
+    
+    NSRect fullBounds = [self bounds];
+    fullBounds.size.height += 8;
+    fullBounds.origin.y -= 5;   // without this it draws extra crap at the bottom of the menu. WD-rpw 05-06-2012
+    [[NSBezierPath bezierPathWithRect:fullBounds] setClip];
+
+    [[NSColor clearColor] set];
+    NSRectFill(fullBounds);
+    
+    [NSGraphicsContext restoreGraphicsState];
+}
+
+@end