Permalink
Browse files

Create scroller colors once and only once (in +initialize)

  • Loading branch information...
1 parent 4869e60 commit ae13bf7af560f27acfa9bb1b04560e544d8e7ee7 @mattball committed Mar 27, 2009
Showing with 89 additions and 28 deletions.
  1. +89 −28 MBCoverFlowScroller.m
View
117 MBCoverFlowScroller.m
@@ -8,6 +8,14 @@
#import "MBCoverFlowScroller.h"
+// Constants
+static NSColor *MBCoverFlowScrollerOutlineColor, *MBCoverFlowScrollerInactiveOutlineColor, *MBCoverFlowScrollerPressedOutlineColor;
+static NSColor *MBCoverFlowScrollerBackgroundTopColor, *MBCoverFlowScrollerInactiveBackgroundTopColor, *MBCoverFlowScrollerPressedBackgroundTopColor;
+static NSColor *MBCoverFlowScrollerBackgroundBottomColor, *MBCoverFlowScrollerInactiveBackgroundBottomColor, *MBCoverFlowScrollerPressedBackgroundBottomColor;
+static NSColor *MBCoverFlowScrollerGlossTopColor, *MBCoverFlowScrollerInactiveGlossTopColor, *MBCoverFlowScrollerPressedGlossTopColor;
+static NSColor *MBCoverFlowScrollerGlossBottomColor, *MBCoverFlowScrollerInactiveGlossBottomColor, *MBCoverFlowScrollerPressedGlossBottomColor;
+static NSColor *MBCoverFlowScrollerSlotBackgroundColor, *MBCoverFlowScrollerInactiveSlotBackgroundColor, *MBCoverFlowScrollerSlotInsetColor;
+
@interface MBCoverFlowScroller ()
- (NSBezierPath *)_leftArrowPath;
- (NSBezierPath *)_rightArrowPath;
@@ -23,6 +31,28 @@ @implementation MBCoverFlowScroller
@synthesize numberOfIncrements=_numberOfIncrements;
++ (void)initialize
+{
+ MBCoverFlowScrollerOutlineColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] retain];
+ MBCoverFlowScrollerInactiveOutlineColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.3] retain];
+ MBCoverFlowScrollerPressedOutlineColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:1.0] retain];
+ MBCoverFlowScrollerBackgroundTopColor = [[NSColor colorWithCalibratedWhite:0.1 alpha:1.0] retain];
+ MBCoverFlowScrollerInactiveBackgroundTopColor = [[NSColor colorWithCalibratedWhite:0.1 alpha:1.0] retain];
+ MBCoverFlowScrollerPressedBackgroundTopColor = [[NSColor colorWithCalibratedWhite:0.8 alpha:1.0] retain];
+ MBCoverFlowScrollerBackgroundBottomColor = [[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] retain];
+ MBCoverFlowScrollerInactiveBackgroundBottomColor = [[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] retain];
+ MBCoverFlowScrollerPressedBackgroundBottomColor = [[NSColor colorWithCalibratedWhite:0.4 alpha:1.0] retain];
+ MBCoverFlowScrollerGlossTopColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.3] retain];
+ MBCoverFlowScrollerInactiveGlossTopColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.3] retain];
+ MBCoverFlowScrollerPressedGlossTopColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.3] retain];
+ MBCoverFlowScrollerGlossBottomColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.1] retain];
+ MBCoverFlowScrollerInactiveGlossBottomColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.0] retain];
+ MBCoverFlowScrollerPressedGlossBottomColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.1] retain];
+ MBCoverFlowScrollerSlotBackgroundColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.3] retain];
+ MBCoverFlowScrollerInactiveSlotBackgroundColor = [[NSColor colorWithCalibratedWhite:1.0 alpha:0.2] retain];
+ MBCoverFlowScrollerSlotInsetColor = [[NSColor colorWithCalibratedWhite:0.0 alpha:0.2] retain];
+}
+
- (void)drawRect:(NSRect)rect
{
[self drawKnobSlotInRect:[self rectForPart:NSScrollerKnobSlot] highlight:NO] ;
@@ -134,15 +164,25 @@ - (void)drawArrow:(NSScrollerArrow)arrow highlight:(BOOL)flag
[[NSGraphicsContext currentContext] saveGraphicsState];
[arrowPath addClip];
- // Draw the background
- NSColor *outlineColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
- NSColor *bgTop = [NSColor colorWithCalibratedWhite:0.1 alpha:1.0];
- NSColor *bgBottom = [NSColor colorWithCalibratedWhite:0.0 alpha:1.0];
+ // Determine the proper colors
+ NSColor *outlineColor = MBCoverFlowScrollerOutlineColor;
+ NSColor *bgTop = MBCoverFlowScrollerBackgroundTopColor;
+ NSColor *bgBottom = MBCoverFlowScrollerBackgroundBottomColor;
+ NSColor *glossTop = MBCoverFlowScrollerGlossTopColor;
+ NSColor *glossBottom = MBCoverFlowScrollerGlossBottomColor;
if (flag) {
- outlineColor = [NSColor colorWithCalibratedWhite:1.0 alpha:1.0];
- bgTop = [NSColor colorWithCalibratedWhite:0.8 alpha:1.0];
- bgBottom = [NSColor colorWithCalibratedWhite:0.4 alpha:1.0];
+ outlineColor = MBCoverFlowScrollerPressedOutlineColor;
+ bgTop = MBCoverFlowScrollerPressedBackgroundTopColor;
+ bgBottom = MBCoverFlowScrollerPressedBackgroundBottomColor;
+ glossTop = MBCoverFlowScrollerPressedGlossTopColor;
+ glossBottom = MBCoverFlowScrollerPressedGlossBottomColor;
+ } else if (![[self window] isKeyWindow]) {
+ outlineColor = MBCoverFlowScrollerInactiveOutlineColor;
+ bgTop = MBCoverFlowScrollerInactiveBackgroundTopColor;
+ bgBottom = MBCoverFlowScrollerInactiveBackgroundBottomColor;
+ glossTop = MBCoverFlowScrollerInactiveGlossTopColor;
+ glossBottom = MBCoverFlowScrollerInactiveGlossBottomColor;
}
// Draw the background
@@ -151,8 +191,6 @@ - (void)drawArrow:(NSScrollerArrow)arrow highlight:(BOOL)flag
[bgGradient release];
// Draw the gloss
- NSColor *glossTop = [NSColor colorWithCalibratedWhite:1.0 alpha:0.3];
- NSColor *glossBottom = [NSColor colorWithCalibratedWhite:1.0 alpha:0.1];
NSGradient *glossGradient = [[NSGradient alloc] initWithStartingColor:glossTop endingColor:glossBottom];
NSRect glossRect = [self rectForPart:NSScrollerDecrementLine];
glossRect.origin.x += 4.0;
@@ -188,24 +226,33 @@ - (void)drawArrow:(NSScrollerArrow)arrow highlight:(BOOL)flag
[[NSGraphicsContext currentContext] saveGraphicsState];
[arrowPath addClip];
- // Draw the background
- NSColor *outlineColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
- NSColor *bgTop = [NSColor colorWithCalibratedWhite:0.1 alpha:1.0];
- NSColor *bgBottom = [NSColor colorWithCalibratedWhite:0.0 alpha:1.0];
+ // Determine the proper colors
+ NSColor *outlineColor = MBCoverFlowScrollerOutlineColor;
+ NSColor *bgTop = MBCoverFlowScrollerBackgroundTopColor;
+ NSColor *bgBottom = MBCoverFlowScrollerBackgroundBottomColor;
+ NSColor *glossTop = MBCoverFlowScrollerGlossTopColor;
+ NSColor *glossBottom = MBCoverFlowScrollerGlossBottomColor;
+
if (flag) {
- outlineColor = [NSColor colorWithCalibratedWhite:1.0 alpha:1.0];
- bgTop = [NSColor colorWithCalibratedWhite:0.8 alpha:1.0];
- bgBottom = [NSColor colorWithCalibratedWhite:0.4 alpha:1.0];
- }
+ outlineColor = MBCoverFlowScrollerPressedOutlineColor;
+ bgTop = MBCoverFlowScrollerPressedBackgroundTopColor;
+ bgBottom = MBCoverFlowScrollerPressedBackgroundBottomColor;
+ glossTop = MBCoverFlowScrollerPressedGlossTopColor;
+ glossBottom = MBCoverFlowScrollerPressedGlossBottomColor;
+ } else if (![[self window] isKeyWindow]) {
+ outlineColor = MBCoverFlowScrollerInactiveOutlineColor;
+ bgTop = MBCoverFlowScrollerInactiveBackgroundTopColor;
+ bgBottom = MBCoverFlowScrollerInactiveBackgroundBottomColor;
+ glossTop = MBCoverFlowScrollerInactiveGlossTopColor;
+ glossBottom = MBCoverFlowScrollerInactiveGlossBottomColor;
+ }
// Draw the background
NSGradient *bgGradient = [[NSGradient alloc] initWithStartingColor:bgTop endingColor:bgBottom];
[bgGradient drawInBezierPath:arrowPath angle:90.0];
[bgGradient release];
// Draw the gloss
- NSColor *glossTop = [NSColor colorWithCalibratedWhite:1.0 alpha:0.3];
- NSColor *glossBottom = [NSColor colorWithCalibratedWhite:1.0 alpha:0.1];
NSGradient *glossGradient = [[NSGradient alloc] initWithStartingColor:glossTop endingColor:glossBottom];
NSRect glossRect = [self rectForPart:NSScrollerIncrementLine];
glossRect.origin.x -= 24.0;
@@ -241,8 +288,14 @@ - (void)drawArrow:(NSScrollerArrow)arrow highlight:(BOOL)flag
- (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag
{
NSBezierPath *slotPath = [NSBezierPath bezierPathWithRect:NSInsetRect(slotRect, 0.5, 0.5)];
- NSColor *bgColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.3];
- NSColor *outlineColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
+
+ // Determine the proper colors
+ NSColor *bgColor = MBCoverFlowScrollerSlotBackgroundColor;
+ NSColor *outlineColor = MBCoverFlowScrollerOutlineColor;
+ if (![[self window] isKeyWindow]) {
+ bgColor = MBCoverFlowScrollerInactiveSlotBackgroundColor;
+ outlineColor = MBCoverFlowScrollerInactiveOutlineColor;
+ }
[bgColor set];
[slotPath fill];
@@ -251,7 +304,7 @@ - (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag
[slotPath stroke];
NSRect insetRect = NSMakeRect(slotRect.origin.x, slotRect.origin.y+1.0, slotRect.size.width, 1.0);
- [[NSColor colorWithCalibratedWhite:0.0 alpha:0.2] set];
+ [MBCoverFlowScrollerSlotInsetColor set];
[NSBezierPath fillRect:insetRect];
}
@@ -275,19 +328,27 @@ - (void)drawKnob
[[NSGraphicsContext currentContext] saveGraphicsState];
[knobPath addClip];
- // Draw the background
- NSColor *outlineColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
- NSColor *bgTop = [NSColor colorWithCalibratedWhite:0.1 alpha:1.0];
- NSColor *bgBottom = [NSColor colorWithCalibratedWhite:0.0 alpha:1.0];
+ // Determine the proper colors
+ NSColor *outlineColor = MBCoverFlowScrollerOutlineColor;
+ NSColor *bgTop = MBCoverFlowScrollerBackgroundTopColor;
+ NSColor *bgBottom = MBCoverFlowScrollerBackgroundBottomColor;
+ NSColor *glossTop = MBCoverFlowScrollerGlossTopColor;
+ NSColor *glossBottom = MBCoverFlowScrollerGlossBottomColor;
+
+ if (![[self window] isKeyWindow]) {
+ outlineColor = MBCoverFlowScrollerInactiveOutlineColor;
+ bgTop = MBCoverFlowScrollerInactiveBackgroundTopColor;
+ bgBottom = MBCoverFlowScrollerInactiveBackgroundBottomColor;
+ glossTop = MBCoverFlowScrollerInactiveGlossTopColor;
+ glossBottom = MBCoverFlowScrollerInactiveGlossBottomColor;
+ }
// Draw the background
NSGradient *bgGradient = [[NSGradient alloc] initWithStartingColor:bgTop endingColor:bgBottom];
[bgGradient drawInBezierPath:knobPath angle:90.0];
[bgGradient release];
// Draw the gloss
- NSColor *glossTop = [NSColor colorWithCalibratedWhite:1.0 alpha:0.3];
- NSColor *glossBottom = [NSColor colorWithCalibratedWhite:1.0 alpha:0.1];
NSGradient *glossGradient = [[NSGradient alloc] initWithStartingColor:glossTop endingColor:glossBottom];
NSRect glossRect = [self rectForPart:NSScrollerKnob];
glossRect.origin.x += 4.0;

0 comments on commit ae13bf7

Please sign in to comment.