diff options
author | gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3> | 2009-11-23 19:05:18 +0000 |
---|---|---|
committer | gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3> | 2009-11-23 19:05:18 +0000 |
commit | 5cadf77481727a70f7cf7925e641e9a46f546ed6 (patch) | |
tree | 0c6648fd40724d78a0e24168427515553a37108f /AppKit | |
parent | d8db7440acd54abce429f06ed0600081eac53b28 (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')
-rw-r--r-- | AppKit/GTMFadeTruncatingTextFieldCell.m | 62 | ||||
-rw-r--r-- | AppKit/GTMFadeTruncatingTextFieldCellTest.m | 17 | ||||
-rw-r--r-- | AppKit/TestData/GTMFadeTruncatingTextFieldCellTest3.tiff | bin | 0 -> 2596 bytes | |||
-rw-r--r-- | AppKit/TestData/GTMFadeTruncatingTextFieldCellTest4.tiff | bin | 0 -> 2086 bytes |
4 files changed, 58 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 diff --git a/AppKit/GTMFadeTruncatingTextFieldCellTest.m b/AppKit/GTMFadeTruncatingTextFieldCellTest.m index d3f328f..5957269 100644 --- a/AppKit/GTMFadeTruncatingTextFieldCellTest.m +++ b/AppKit/GTMFadeTruncatingTextFieldCellTest.m @@ -31,6 +31,7 @@ NSMakeRect(0, 0, 100, 16)] autorelease]; [field setCell:[[[GTMFadeTruncatingTextFieldCell alloc] initTextCell:@""] autorelease]]; + [field setStringValue:@"A very long string that won't fit"]; GTMAssertObjectImageEqualToImageNamed(field, @"GTMFadeTruncatingTextFieldCellTest1", @@ -39,6 +40,22 @@ GTMAssertObjectImageEqualToImageNamed(field, @"GTMFadeTruncatingTextFieldCellTest2", nil); + + // Dark background, light text (force the background to draw (which is odd + // for a text cell), but this is to make sure the support for light on dark + // is tested. + [field setTextColor:[NSColor whiteColor]]; + [field setDrawsBackground:YES]; + [field setBackgroundColor:[NSColor blackColor]]; + + [field setStringValue:@"A very long string that won't fit"]; + GTMAssertObjectImageEqualToImageNamed(field, + @"GTMFadeTruncatingTextFieldCellTest3", + nil); + [field setStringValue:@"A short string"]; + GTMAssertObjectImageEqualToImageNamed(field, + @"GTMFadeTruncatingTextFieldCellTest4", + nil); } @end diff --git a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest3.tiff b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest3.tiff Binary files differnew file mode 100644 index 0000000..6d7563a --- /dev/null +++ b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest3.tiff diff --git a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest4.tiff b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest4.tiff Binary files differnew file mode 100644 index 0000000..77c03de --- /dev/null +++ b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest4.tiff |