aboutsummaryrefslogtreecommitdiff
path: root/AppKit/GTMFadeTruncatingTextFieldCell.m
diff options
context:
space:
mode:
authorGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2009-11-23 19:05:18 +0000
committerGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2009-11-23 19:05:18 +0000
commit5cadf77481727a70f7cf7925e641e9a46f546ed6 (patch)
tree0c6648fd40724d78a0e24168427515553a37108f /AppKit/GTMFadeTruncatingTextFieldCell.m
parentd8db7440acd54abce429f06ed0600081eac53b28 (diff)
[Author: thomasvl]
Nicer drawing of text by not using the gradient where it isn't needed, helps where light text is drawn on a dark background. Patch from thakis - http://codereview.appspot.com/159060/show R=thakis,dmaclach DELTA=79 (54 added, 7 deleted, 18 changed)
Diffstat (limited to 'AppKit/GTMFadeTruncatingTextFieldCell.m')
-rw-r--r--AppKit/GTMFadeTruncatingTextFieldCell.m62
1 files changed, 41 insertions, 21 deletions
diff --git a/AppKit/GTMFadeTruncatingTextFieldCell.m b/AppKit/GTMFadeTruncatingTextFieldCell.m
index 98eac8c..0e7344c 100644
--- a/AppKit/GTMFadeTruncatingTextFieldCell.m
+++ b/AppKit/GTMFadeTruncatingTextFieldCell.m
@@ -35,33 +35,53 @@
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
- CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
- CGContextBeginTransparencyLayer(context, 0);
+ NSSize size = [[self attributedStringValue] size];
+
+ // Don't complicate drawing unless we need to clip
+ if (size.width <= NSWidth(cellFrame)) {
+ [super drawInteriorWithFrame:cellFrame inView:controlView];
+ return;
+ }
+
+ // Gradient is about twice our line height long
+ CGFloat gradientWidth = MIN(size.height * 2, NSWidth(cellFrame) / 4);
+ NSRect solidPart, gradientPart;
+ NSDivideRect(cellFrame, &gradientPart, &solidPart, gradientWidth, NSMaxXEdge);
+
+ // Draw non-gradient part without transparency layer, as light text on a dark
+ // background looks bad with a gradient layer.
+ [[NSGraphicsContext currentContext] saveGraphicsState];
+ [NSBezierPath clipRect:solidPart];
[super drawInteriorWithFrame:cellFrame inView:controlView];
+ [[NSGraphicsContext currentContext] restoreGraphicsState];
- // Don't complicate drawing unless we need to clip
- NSSize size = [[self attributedStringValue] size];
- if (size.width > cellFrame.size.width) {
- // Gradient is about twice our line height long
- CGFloat width = MIN(size.height * 2, NSWidth(cellFrame) / 4);
+ // Draw the gradient part with a transparency layer. This makes the text look
+ // suboptimal, but since it fades out, that's ok.
+ [[NSGraphicsContext currentContext] saveGraphicsState];
+ [NSBezierPath clipRect:gradientPart];
+ CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
+ CGContextBeginTransparencyLayerWithRect(context,
+ NSRectToCGRect(gradientPart), 0);
- // TODO(alcor): switch this to GTMLinearRGBShading if we ever need on 10.4
- NSColor *color = [self textColor];
- NSGradient *mask = [[NSGradient alloc]
- initWithStartingColor:color
- endingColor:[color colorWithAlphaComponent:0.0]];
+ [super drawInteriorWithFrame:cellFrame inView:controlView];
- // Draw the gradient mask
- CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
- [mask drawFromPoint:NSMakePoint(NSMaxX(cellFrame) - width,
- NSMinY(cellFrame))
- toPoint:NSMakePoint(NSMaxX(cellFrame),
- NSMinY(cellFrame))
- options:NSGradientDrawsBeforeStartingLocation];
- [mask release];
- }
+ // TODO(alcor): switch this to GTMLinearRGBShading if we ever need on 10.4
+ NSColor *color = [self textColor];
+ NSColor *alphaColor = [color colorWithAlphaComponent:0.0];
+ NSGradient *mask = [[NSGradient alloc] initWithStartingColor:color
+ endingColor:alphaColor];
+
+ // Draw the gradient mask
+ CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
+ [mask drawFromPoint:NSMakePoint(NSMaxX(cellFrame) - gradientWidth,
+ NSMinY(cellFrame))
+ toPoint:NSMakePoint(NSMaxX(cellFrame),
+ NSMinY(cellFrame))
+ options:NSGradientDrawsBeforeStartingLocation];
+ [mask release];
CGContextEndTransparencyLayer(context);
+ [[NSGraphicsContext currentContext] restoreGraphicsState];
}
@end