From 5cadf77481727a70f7cf7925e641e9a46f546ed6 Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Mon, 23 Nov 2009 19:05:18 +0000 Subject: [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) --- AppKit/GTMFadeTruncatingTextFieldCell.m | 62 ++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 21 deletions(-) (limited to 'AppKit/GTMFadeTruncatingTextFieldCell.m') 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 -- cgit v1.2.3