diff options
Diffstat (limited to 'AppKit/GTMFadeTruncatingTextFieldCell.m')
-rw-r--r-- | AppKit/GTMFadeTruncatingTextFieldCell.m | 62 |
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 |