Revision: 3250
          http://skim-app.svn.sourceforge.net/skim-app/?rev=3250&view=rev
Author:   amaxwell
Date:     2007-11-24 10:27:52 -0800 (Sat, 24 Nov 2007)

Log Message:
-----------
Incorporate CGLayer gradient drawing optimizations from BibDesk trunk.

Modified Paths:
--------------
    trunk/BDSKGradientView.h
    trunk/BDSKGradientView.m
    trunk/SKStatusBar.h
    trunk/SKStatusBar.m

Modified: trunk/BDSKGradientView.h
===================================================================
--- trunk/BDSKGradientView.h    2007-11-24 18:07:12 UTC (rev 3249)
+++ trunk/BDSKGradientView.h    2007-11-24 18:27:52 UTC (rev 3250)
@@ -44,6 +44,7 @@
     @private
     CIColor *lowerColor;
     CIColor *upperColor;
+    CGLayerRef layer;
 }
 
 - (void)setLowerColor:(NSColor *)color;

Modified: trunk/BDSKGradientView.m
===================================================================
--- trunk/BDSKGradientView.m    2007-11-24 18:07:12 UTC (rev 3249)
+++ trunk/BDSKGradientView.m    2007-11-24 18:27:52 UTC (rev 3250)
@@ -52,21 +52,55 @@
 {
     self = [super initWithFrame:frame];
     [self setDefaultColors];
+    layer = NULL;
     return self;
 }
 
 - (void)dealloc
 {
+    CGLayerRelease(layer);
     [lowerColor release];
     [upperColor release];
     [super dealloc];
 }
 
+- (void)setBounds:(NSRect)aRect
+{
+    // since the gradient is vertical, we only have to reset the layer if the 
height changes; for most of our gradient views, this isn't likely to happen
+    if (ABS(NSHeight(aRect) - NSHeight([self bounds])) > 0.01) {
+        CGLayerRelease(layer);
+        layer = NULL;
+    }
+    [super setBounds:aRect];
+}
+
+
 - (void)drawRect:(NSRect)aRect
-{
+{        
     // fill entire view, not just the (possibly clipped) aRect
-    if ([[self window] styleMask] & NSClosableWindowMask)
-        [[NSBezierPath bezierPathWithRect:[self bounds]] 
fillPathVerticallyWithStartColor:[self lowerColor] endColor:[self upperColor]];
+    if ([[self window] styleMask] & NSClosableWindowMask) {
+        
+        CGContextRef viewContext = [[NSGraphicsContext currentContext] 
graphicsPort];
+        NSRect bounds = [self bounds];
+
+        if (NULL == layer) {
+            NSSize layerSize = bounds.size;
+            layer = CGLayerCreateWithContext(viewContext, *(CGSize 
*)&layerSize, NULL);
+            
+            CGContextRef layerContext = CGLayerGetContext(layer);
+            [NSGraphicsContext saveGraphicsState];
+            [NSGraphicsContext setCurrentContext:[NSGraphicsContext 
graphicsContextWithGraphicsPort:layerContext flipped:NO]];
+            NSRect layerRect = NSZeroRect;
+            layerRect.size = layerSize;
+            
+            [[NSBezierPath bezierPathWithRect:bounds] 
fillPathVerticallyWithStartColor:[self lowerColor] endColor:[self upperColor]];
+            [NSGraphicsContext restoreGraphicsState];
+        }
+        
+        // normal blend mode is copy
+        CGContextSetBlendMode(viewContext, kCGBlendModeNormal);
+        CGContextDrawLayerInRect(viewContext, *(CGRect *)&bounds, layer);
+    }
 }
 
 // -[CIColor initWithColor:] fails (returns nil) with +[NSColor gridColor] 
rdar://problem/4789043

Modified: trunk/SKStatusBar.h
===================================================================
--- trunk/SKStatusBar.h 2007-11-24 18:07:12 UTC (rev 3249)
+++ trunk/SKStatusBar.h 2007-11-24 18:27:52 UTC (rev 3250)
@@ -51,6 +51,7 @@
        id rightCell;
        NSProgressIndicator *progressIndicator;
     int state;
+    CGLayerRef layer;
 }
 
 + (CIColor *)lowerColor;

Modified: trunk/SKStatusBar.m
===================================================================
--- trunk/SKStatusBar.m 2007-11-24 18:07:12 UTC (rev 3249)
+++ trunk/SKStatusBar.m 2007-11-24 18:27:52 UTC (rev 3250)
@@ -72,11 +72,13 @@
         [rightCell setAlignment:NSRightTextAlignment];
         [rightCell setControlView:self];
                progressIndicator = nil;
+        layer = NULL;
     }
     return self;
 }
 
 - (void)dealloc {
+    CGLayerRelease(layer);
        [leftCell release];
        [rightCell release];
        [super dealloc];
@@ -86,12 +88,44 @@
 
 - (BOOL)isFlipped { return NO; }
 
+- (void)setBounds:(NSRect)aRect
+{
+    // since the gradient is vertical, we only have to reset the layer if the 
height changes; for most of our gradient views, this isn't likely to happen
+    if (ABS(NSHeight(aRect) - NSHeight([self bounds])) > 0.01) {
+        CGLayerRelease(layer);
+        layer = NULL;
+    }
+    [super setBounds:aRect];
+}
+
 - (void)drawRect:(NSRect)rect {
-       NSRect textRect, ignored;
-    float rightMargin = RIGHT_MARGIN;
     
-    [[NSBezierPath bezierPathWithRect:[self bounds]] 
fillPathVerticallyWithStartColor:[[self class] lowerColor] endColor:[[self 
class] upperColor]];
+    CGContextRef viewContext = [[NSGraphicsContext currentContext] 
graphicsPort];
+    NSRect bounds = [self bounds];
     
+    if (NULL == layer) {
+        NSSize layerSize = bounds.size;
+        layer = CGLayerCreateWithContext(viewContext, *(CGSize *)&layerSize, 
NULL);
+        
+        CGContextRef layerContext = CGLayerGetContext(layer);
+        [NSGraphicsContext saveGraphicsState];
+        [NSGraphicsContext setCurrentContext:[NSGraphicsContext 
graphicsContextWithGraphicsPort:layerContext flipped:NO]];
+        NSRect layerRect = NSZeroRect;
+        layerRect.size = layerSize;
+        
+        [[NSBezierPath bezierPathWithRect:bounds] 
fillPathVerticallyWithStartColor:[[self class] lowerColor] endColor:[[self 
class] upperColor]];
+        [NSGraphicsContext restoreGraphicsState];
+    }
+    
+    // normal blend mode is copy
+    CGContextSaveGState(viewContext);
+    CGContextSetBlendMode(viewContext, kCGBlendModeNormal);
+    CGContextDrawLayerInRect(viewContext, *(CGRect *)&bounds, layer);
+    CGContextRestoreGState(viewContext);
+
+    NSRect textRect, ignored;
+    float rightMargin = RIGHT_MARGIN;
+
     if (progressIndicator)
         rightMargin += NSWidth([progressIndicator frame]) + SEPARATION;
     NSDivideRect([self bounds], &ignored, &textRect, LEFT_MARGIN, NSMinXEdge);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to