Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added NSSwitchButton (checkbox) implementation to SNRHUDButtonCell

  • Loading branch information...
commit 9fa64084d09d33393558bc99b4a785c45071437a 1 parent 1e7964d
Indragie Karunaratne authored
Showing with 145 additions and 15 deletions.
  1. +59 −11 Catalog/en.lproj/MainMenu.xib
  2. +86 −4 SNRHUDKit/Classes/SNRHUDButtonCell.m
View
70 Catalog/en.lproj/MainMenu.xib
@@ -1304,7 +1304,7 @@
<object class="NSWindowTemplate" id="657836172">
<int key="NSWindowStyleMask">31</int>
<int key="NSWindowBacking">2</int>
- <string key="NSWindowRect">{{131, 159}, {290, 284}}</string>
+ <string key="NSWindowRect">{{131, 159}, {290, 289}}</string>
<int key="NSWTFlags">611845120</int>
<string key="NSWindowTitle">Catalog</string>
<string key="NSWindowClass">SNRHUDWindow</string>
@@ -1317,7 +1317,7 @@
<object class="NSSegmentedControl" id="725209211">
<reference key="NSNextResponder" ref="616882760"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{49, 253}, {193, 24}}</string>
+ <string key="NSFrame">{{49, 258}, {193, 24}}</string>
<reference key="NSSuperview" ref="616882760"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="244134406"/>
@@ -1359,7 +1359,7 @@
<object class="NSTextField" id="244134406">
<reference key="NSNextResponder" ref="616882760"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{20, 216}, {250, 22}}</string>
+ <string key="NSFrame">{{20, 221}, {250, 22}}</string>
<reference key="NSSuperview" ref="616882760"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="370655449"/>
@@ -1368,7 +1368,7 @@
<object class="NSTextFieldCell" key="NSCell" id="97320464">
<int key="NSCellFlags">-1804468671</int>
<int key="NSCellFlags2">272630784</int>
- <string key="NSContents">text</string>
+ <string key="NSContents"/>
<object class="NSFont" key="NSSupport">
<string key="NSName">LucidaGrande</string>
<double key="NSSize">11</double>
@@ -1403,6 +1403,7 @@
<string key="NSFrame">{{96, 20}, {82, 23}}</string>
<reference key="NSSuperview" ref="616882760"/>
<reference key="NSWindow"/>
+ <reference key="NSNextKeyView" ref="49079046"/>
<string key="NSReuseIdentifierKey">_NS:2510</string>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="372624357">
@@ -1430,7 +1431,6 @@
<string key="NSFrame">{{188, 20}, {82, 23}}</string>
<reference key="NSSuperview" ref="616882760"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="887948449"/>
<string key="NSReuseIdentifierKey">_NS:2510</string>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="547434519">
@@ -1584,7 +1584,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<string key="NSFrame">{{234, 1}, {15, 133}}</string>
<reference key="NSSuperview" ref="370655449"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="49079046"/>
+ <reference key="NSNextKeyView" ref="886907926"/>
<string key="NSReuseIdentifierKey">_NS:1494</string>
<reference key="NSTarget" ref="370655449"/>
<string key="NSAction">_doScroller:</string>
@@ -1606,7 +1606,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<double key="NSPercent">0.94565218687057495</double>
</object>
</array>
- <string key="NSFrame">{{20, 73}, {250, 135}}</string>
+ <string key="NSFrame">{{20, 78}, {250, 135}}</string>
<reference key="NSSuperview" ref="616882760"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="727247371"/>
@@ -1616,8 +1616,39 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<reference key="NSHScroller" ref="931446201"/>
<reference key="NSContentView" ref="727247371"/>
</object>
+ <object class="NSButton" id="886907926">
+ <reference key="NSNextResponder" ref="616882760"/>
+ <int key="NSvFlags">268</int>
+ <string key="NSFrame">{{18, 51}, {142, 18}}</string>
+ <reference key="NSSuperview" ref="616882760"/>
+ <reference key="NSWindow"/>
+ <reference key="NSNextKeyView" ref="887948449"/>
+ <string key="NSReuseIdentifierKey">_NS:771</string>
+ <bool key="NSEnabled">YES</bool>
+ <object class="NSButtonCell" key="NSCell" id="411125317">
+ <int key="NSCellFlags">-2080244224</int>
+ <int key="NSCellFlags2">0</int>
+ <string key="NSContents">Part of compilation</string>
+ <reference key="NSSupport" ref="278356880"/>
+ <string key="NSCellIdentifier">_NS:771</string>
+ <reference key="NSControlView" ref="886907926"/>
+ <int key="NSButtonFlags">1211912703</int>
+ <int key="NSButtonFlags2">2</int>
+ <object class="NSCustomResource" key="NSNormalImage">
+ <string key="NSClassName">NSImage</string>
+ <string key="NSResourceName">NSSwitch</string>
+ </object>
+ <object class="NSButtonImageSource" key="NSAlternateImage">
+ <string key="NSImageName">NSSwitch</string>
+ </object>
+ <string key="NSAlternateContents"/>
+ <string key="NSKeyEquivalent"/>
+ <int key="NSPeriodicDelay">200</int>
+ <int key="NSPeriodicInterval">25</int>
+ </object>
+ </object>
</array>
- <string key="NSFrameSize">{290, 284}</string>
+ <string key="NSFrameSize">{290, 289}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="725209211"/>
@@ -3312,10 +3343,11 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<reference key="object" ref="616882760"/>
<array class="NSMutableArray" key="children">
<reference ref="725209211"/>
- <reference ref="244134406"/>
<reference ref="370655449"/>
<reference ref="49079046"/>
<reference ref="887948449"/>
+ <reference ref="886907926"/>
+ <reference ref="244134406"/>
</array>
<reference key="parent" ref="657836172"/>
</object>
@@ -3396,6 +3428,19 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<reference key="object" ref="608436794"/>
<reference key="parent" ref="370655449"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">564</int>
+ <reference key="object" ref="886907926"/>
+ <array class="NSMutableArray" key="children">
+ <reference ref="411125317"/>
+ </array>
+ <reference key="parent" ref="616882760"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">565</int>
+ <reference key="object" ref="411125317"/>
+ <reference key="parent" ref="886907926"/>
+ </object>
</array>
</object>
<dictionary class="NSMutableDictionary" key="flattenedProperties">
@@ -3538,7 +3583,6 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<string key="536.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<boolean value="YES" key="536.NSWindowTemplate.visibleAtLaunch"/>
<string key="537.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string key="539.CustomClassName">SNRHUDSegmentedControl</string>
<string key="539.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="540.CustomClassName">SNRHUDSegmentedCell</string>
<integer value="2" key="540.IBNSSegmentedControlInspectorSelectedSegmentMetadataKey"/>
@@ -3558,6 +3602,9 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<string key="553.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="554.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="56.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string key="564.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string key="565.CustomClassName">SNRHUDButtonCell</string>
+ <string key="565.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="57.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="58.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="72.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -3576,7 +3623,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
- <int key="maxID">563</int>
+ <int key="maxID">565</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -3689,6 +3736,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<dictionary class="NSMutableDictionary" key="IBDocument.LastKnownImageSizes">
<string key="NSMenuCheckmark">{9, 8}</string>
<string key="NSMenuMixedState">{7, 2}</string>
+ <string key="NSSwitch">{15, 15}</string>
</dictionary>
</data>
</archive>
View
90 SNRHUDKit/Classes/SNRHUDButtonCell.m
@@ -32,6 +32,15 @@
#define SNRButtonBorderColor [NSColor blackColor]
#define SNRButtonHighlightOverlayColor [NSColor colorWithDeviceWhite:0.000 alpha:0.300]
+#define SNRButtonCheckboxTextOffset 5.f
+#define SNRButtonCheckboxCheckmarkColor [NSColor colorWithDeviceWhite:0.780 alpha:1.000]
+#define SNRButtonCheckboxCheckmarkLeftOffset 4.f
+#define SNRButtonCheckboxCheckmarkTopOffset 2.f
+#define SNRButtonCheckboxCheckmarkShadowOffset NSMakeSize(0.f, 0.f)
+#define SNRButtonCheckboxCheckmarkShadowBlurRadius 3.f
+#define SNRButtonCheckboxCheckmarkShadowColor [NSColor colorWithDeviceWhite:0.000 alpha:0.750]
+#define SNRButtonCheckboxCheckmarkLineWidth 2.f
+
static NSString* const SNRButtonReturnKeyEquivalent = @"\r";
@interface SNRHUDButtonCell ()
@@ -40,10 +49,26 @@ - (void)snr_drawButtonBezelWithFrame:(NSRect)frame inView:(NSView*)controlView;
- (void)snr_drawCheckboxBezelWithFrame:(NSRect)frame inView:(NSView*)controlView;
- (NSRect)snr_drawButtonTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView;
- (NSRect)snr_drawCheckboxTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView;
+- (NSBezierPath *)snr_checkmarkPathForRect:(NSRect)rect mixed:(BOOL)mixed;
@end
@implementation SNRHUDButtonCell {
NSBezierPath *__bezelPath;
+ NSButtonType __buttonType;
+}
+
+- (id)initWithCoder:(NSCoder *)aDecoder
+{
+ if ((self = [super initWithCoder:aDecoder])) {
+ __buttonType = (NSButtonType)[[self valueForKey:@"buttonType"] unsignedIntegerValue];
+ }
+ return self;
+}
+
+- (void)setButtonType:(NSButtonType)aType
+{
+ __buttonType = aType;
+ [super setButtonType:aType];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
@@ -65,7 +90,21 @@ - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
{
- return [self snr_drawButtonTitle:title withFrame:frame inView:controlView];
+ switch (__buttonType) {
+ case NSSwitchButton:
+ return [self snr_drawCheckboxTitle:title withFrame:frame inView:controlView];
+ break;
+ default:
+ return [self snr_drawButtonTitle:title withFrame:frame inView:controlView];
+ break;
+ }
+}
+
+- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
+{
+ if (__buttonType == NSSwitchButton) {
+ [self snr_drawCheckboxBezelWithFrame:frame inView:controlView];
+ }
}
- (void)snr_drawButtonBezelWithFrame:(NSRect)frame inView:(NSView*)controlView
@@ -103,7 +142,40 @@ - (void)snr_drawButtonBezelWithFrame:(NSRect)frame inView:(NSView*)controlView
- (void)snr_drawCheckboxBezelWithFrame:(NSRect)frame inView:(NSView*)controlView
{
-
+ // At this time the checkbox uses the same style as the black button so we can use that method to draw the background
+ [self snr_drawButtonBezelWithFrame:frame inView:controlView];
+ // Draw the checkmark itself
+ if ([self state] == NSOffState) { return; }
+ NSBezierPath *path = [self snr_checkmarkPathForRect:frame mixed:[self state] == NSMixedState];
+ [path setLineWidth:SNRButtonCheckboxCheckmarkLineWidth];
+ [SNRButtonCheckboxCheckmarkColor set];
+ NSShadow *shadow = [NSShadow new];
+ [shadow setShadowColor:SNRButtonCheckboxCheckmarkShadowColor];
+ [shadow setShadowBlurRadius:SNRButtonCheckboxCheckmarkShadowBlurRadius];
+ [shadow setShadowOffset:SNRButtonCheckboxCheckmarkShadowOffset];
+ [NSGraphicsContext saveGraphicsState];
+ [shadow set];
+ [path stroke];
+ [NSGraphicsContext restoreGraphicsState];
+}
+
+- (NSBezierPath *)snr_checkmarkPathForRect:(NSRect)rect mixed:(BOOL)mixed
+{
+ NSBezierPath *path = [NSBezierPath bezierPath];
+ if (mixed) {
+ NSPoint left = NSMakePoint(rect.origin.x + SNRButtonCheckboxCheckmarkLeftOffset, round(NSMidY(rect)));
+ NSPoint right = NSMakePoint(NSMaxX(rect) - SNRButtonCheckboxCheckmarkLeftOffset, left.y);
+ [path moveToPoint:left];
+ [path lineToPoint:right];
+ } else {
+ NSPoint top = NSMakePoint(NSMaxX(rect), rect.origin.y);
+ NSPoint bottom = NSMakePoint(round(NSMidX(rect)), round(NSMidY(rect)) + SNRButtonCheckboxCheckmarkTopOffset);
+ NSPoint left = NSMakePoint(rect.origin.x + SNRButtonCheckboxCheckmarkLeftOffset, round(bottom.y / 2.f));
+ [path moveToPoint:top];
+ [path lineToPoint:bottom];
+ [path lineToPoint:left];
+ }
+ return path;
}
- (NSRect)snr_drawButtonTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView
@@ -124,13 +196,23 @@ - (NSRect)snr_drawButtonTitle:(NSAttributedString*)title withFrame:(NSRect)frame
- (NSRect)snr_drawCheckboxTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView
{
-
+ NSString *label = [title string];
+ NSShadow *textShadow = [NSShadow new];
+ [textShadow setShadowOffset:SNRButtonBlackTextShadowOffset];
+ [textShadow setShadowColor:SNRButtonBlackTextShadowColor];
+ [textShadow setShadowBlurRadius:SNRButtonBlackTextShadowBlurRadius];
+ NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:SNRButtonTextFont, NSFontAttributeName, SNRButtonTextColor, NSForegroundColorAttributeName, textShadow, NSShadowAttributeName, nil];
+ NSAttributedString *attrLabel = [[NSAttributedString alloc] initWithString:label attributes:attributes];
+ NSSize labelSize = attrLabel.size;
+ NSRect labelRect = NSMakeRect(frame.origin.x + SNRButtonCheckboxTextOffset, NSMidY(frame) - (labelSize.height / 2.f), labelSize.width, labelSize.height);
+ [attrLabel drawInRect:NSIntegralRect(labelRect)];
+ return labelRect;
}
#pragma mark - Private
- (BOOL)snr_shouldDrawBlueButton
{
- return [[self keyEquivalent] isEqualToString:SNRButtonReturnKeyEquivalent];
+ return [[self keyEquivalent] isEqualToString:SNRButtonReturnKeyEquivalent] && (__buttonType != NSSwitchButton);
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.