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 | |
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)
-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 | |||
-rw-r--r-- | GTM.xcodeproj/project.pbxproj | 16 |
5 files changed, 70 insertions, 25 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 diff --git a/GTM.xcodeproj/project.pbxproj b/GTM.xcodeproj/project.pbxproj index e360a09..947bda3 100644 --- a/GTM.xcodeproj/project.pbxproj +++ b/GTM.xcodeproj/project.pbxproj @@ -330,6 +330,8 @@ F4C0B9DA108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-0.tiff in Resources */ = {isa = PBXBuildFile; fileRef = F4C0B9D7108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-0.tiff */; }; F4C0B9DB108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-1.tiff in Resources */ = {isa = PBXBuildFile; fileRef = F4C0B9D8108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-1.tiff */; }; F4C0B9DC108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-2.tiff in Resources */ = {isa = PBXBuildFile; fileRef = F4C0B9D9108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-2.tiff */; }; + F4C58CC310BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest3.tiff in Resources */ = {isa = PBXBuildFile; fileRef = F4C58CC110BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest3.tiff */; }; + F4C58CC410BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest4.tiff in Resources */ = {isa = PBXBuildFile; fileRef = F4C58CC210BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest4.tiff */; }; F4C62489109753960069CADD /* GTMIBArrayTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C62486109753960069CADD /* GTMIBArrayTest.m */; }; F4C6248A109753960069CADD /* GTMIBArrayTest.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4C62487109753960069CADD /* GTMIBArrayTest.xib */; }; F4C6248B109753960069CADD /* GTMIBArray.h in Headers */ = {isa = PBXBuildFile; fileRef = F4C62483109753960069CADD /* GTMIBArray.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -415,28 +417,28 @@ isa = PBXContainerItemProxy; containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; proxyType = 1; - remoteGlobalIDString = 8BFE13BC0FB0F2D8001BE894 /* UnitTest - AddressBook */; + remoteGlobalIDString = 8BFE13BC0FB0F2D8001BE894; remoteInfo = "UnitTest - AddressBook"; }; F4E4279310B7484B00F28A35 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; proxyType = 1; - remoteGlobalIDString = F48FE2630D198C1E009257D2 /* UnitTest - AppKit */; + remoteGlobalIDString = F48FE2630D198C1E009257D2; remoteInfo = "UnitTest - AppKit"; }; F4E4279510B7484B00F28A35 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; proxyType = 1; - remoteGlobalIDString = F42E08110D19987200D5DDE0 /* UnitTest - Foundation */; + remoteGlobalIDString = F42E08110D19987200D5DDE0; remoteInfo = "UnitTest - Foundation"; }; F4E4279710B7484B00F28A35 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; proxyType = 1; - remoteGlobalIDString = 8B45A0270DA4696C001148C5 /* UnitTest - UnitTesting */; + remoteGlobalIDString = 8B45A0270DA4696C001148C5; remoteInfo = "UnitTest - UnitTesting"; }; /* End PBXContainerItemProxy section */ @@ -756,6 +758,8 @@ F4C0B9D7108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-0.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = "GTMUILocalizerAndLayoutTweakerTest5-0.tiff"; sourceTree = "<group>"; }; F4C0B9D8108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-1.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = "GTMUILocalizerAndLayoutTweakerTest5-1.tiff"; sourceTree = "<group>"; }; F4C0B9D9108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-2.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = "GTMUILocalizerAndLayoutTweakerTest5-2.tiff"; sourceTree = "<group>"; }; + F4C58CC110BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest3.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = GTMFadeTruncatingTextFieldCellTest3.tiff; sourceTree = "<group>"; }; + F4C58CC210BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest4.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = GTMFadeTruncatingTextFieldCellTest4.tiff; sourceTree = "<group>"; }; F4C62483109753960069CADD /* GTMIBArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTMIBArray.h; sourceTree = "<group>"; }; F4C62484109753960069CADD /* GTMIBArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTMIBArray.m; sourceTree = "<group>"; }; F4C62485109753960069CADD /* GTMIBArrayTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTMIBArrayTest.h; sourceTree = "<group>"; }; @@ -1006,6 +1010,8 @@ 8B409E960F952CCA00DF540E /* Resources */, 7F97DBA2104ED861004DDDEE /* GTMFadeTruncatingTextFieldCellTest1.tiff */, 7F97DBA3104ED861004DDDEE /* GTMFadeTruncatingTextFieldCellTest2.tiff */, + F4C58CC110BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest3.tiff */, + F4C58CC210BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest4.tiff */, 8B1802410E25592200280961 /* GTMLargeTypeWindowMediumTextTest.gtmUTState */, 8B1801A80E25341B00280961 /* GTMLargeTypeWindowImageTest.gtmUTState */, 8B1801AC0E25341B00280961 /* GTMLargeTypeWindowLongTextTest.gtmUTState */, @@ -1756,6 +1762,8 @@ F4C0B9DC108E3142002FC8E4 /* GTMUILocalizerAndLayoutTweakerTest5-2.tiff in Resources */, F4C6248A109753960069CADD /* GTMIBArrayTest.xib in Resources */, F4AA2CB2109B37650025C956 /* GTMUILocalizerAndLayoutTweakerTest3-4.tiff in Resources */, + F4C58CC310BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest3.tiff in Resources */, + F4C58CC410BAD75200651068 /* GTMFadeTruncatingTextFieldCellTest4.tiff in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; |