aboutsummaryrefslogtreecommitdiff
path: root/AppKit
diff options
context:
space:
mode:
authorGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2011-03-28 19:00:23 +0000
committerGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2011-03-28 19:00:23 +0000
commit09c3d1d4c6cc035acec7578eeb0ba0bae228fcb0 (patch)
tree153f54808206929703e87ec57e6f13abe31702cd /AppKit
parent49488762da466f2515d55e0547a8e15aa7560ebe (diff)
[Author: sail]
Tweak fade effect for GTMFadeTruncatingTextFieldCell This change tweaks how we fade the beginning of the text in GTMFadeTruncatingTextFieldCell. Previously we would start drawing the string at the desired position. This meant that the first character was not legible because of the fade effect. With this change we now start drawing a few pixels before the desired position. I also fixed a bug in how we draw the background of the cell. Previously we clipped to the text rect before drawing the background. When the text field had a border this meant that we weren't drawing all of the background because the text rect is slightly smaller than the background rect. With this change we now draw the background rect before setting the text rect clip. R=thomasvl,dmaclach APPROVED=dmaclach DELTA=81 (51 added, 2 deleted, 28 changed)
Diffstat (limited to 'AppKit')
-rw-r--r--AppKit/GTMFadeTruncatingTextFieldCell.m89
-rw-r--r--AppKit/GTMFadeTruncatingTextFieldCellTest.m11
-rw-r--r--AppKit/TestData/GTMFadeTruncatingTextFieldCellTest5.tiffbin2188 -> 2136 bytes
-rw-r--r--AppKit/TestData/GTMFadeTruncatingTextFieldCellTest6.tiffbin2068 -> 2048 bytes
-rw-r--r--AppKit/TestData/GTMFadeTruncatingTextFieldCellTest8.tiffbin0 -> 2896 bytes
5 files changed, 72 insertions, 28 deletions
diff --git a/AppKit/GTMFadeTruncatingTextFieldCell.m b/AppKit/GTMFadeTruncatingTextFieldCell.m
index ec266a2..66e6874 100644
--- a/AppKit/GTMFadeTruncatingTextFieldCell.m
+++ b/AppKit/GTMFadeTruncatingTextFieldCell.m
@@ -43,26 +43,30 @@
// 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:clipRect];
+ [NSBezierPath clipRect:backgroundRect];
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextBeginTransparencyLayerWithRect(context,
- NSRectToCGRect(clipRect), 0);
+ NSRectToCGRect(backgroundRect), 0);
if ([self drawsBackground]) {
[[self backgroundColor] set];
- NSRectFillUsingOperation([self titleRectForBounds:backgroundRect],
+ NSRectFillUsingOperation(backgroundRect,
NSCompositeSourceOver);
}
+
+ [[NSGraphicsContext currentContext] saveGraphicsState];
+ [NSBezierPath clipRect:clipRect];
[attributedString drawInRect:titleRect];
+ [[NSGraphicsContext currentContext] restoreGraphicsState];
NSPoint startPoint;
NSPoint endPoint;
if (fadeToRight) {
- startPoint = clipRect.origin;
- endPoint = NSMakePoint(NSMaxX(clipRect), NSMinY(clipRect));
+ startPoint = backgroundRect.origin;
+ endPoint = NSMakePoint(NSMaxX(backgroundRect), NSMinY(backgroundRect));
} else {
- startPoint = NSMakePoint(NSMaxX(clipRect), NSMinY(clipRect));
- endPoint = clipRect.origin;
+ startPoint = NSMakePoint(NSMaxX(backgroundRect), NSMinY(backgroundRect));
+ endPoint = backgroundRect.origin;
}
// Draw the gradient mask
@@ -83,10 +87,10 @@
titleRect.size.width -= 2;
NSAttributedString *attributedString = [self attributedStringValue];
- NSSize size = [attributedString size];
+ NSSize stringSize = [attributedString size];
// Don't complicate drawing unless we need to clip
- if (size.width <= NSWidth(titleRect)) {
+ if (stringSize.width <= NSWidth(titleRect)) {
[super drawInteriorWithFrame:cellFrame inView:controlView];
return;
}
@@ -97,7 +101,7 @@
case GTMFadeTruncatingTail:
break;
case GTMFadeTruncatingHead:
- offsetX = size.width - titleRect.size.width;
+ offsetX = stringSize.width - titleRect.size.width;
break;
case GTMFadeTruncatingHeadAndTail: {
if (desiredCharactersToTruncateFromHead_ > 0) {
@@ -106,15 +110,24 @@
NSMakeRange(0, desiredCharactersToTruncateFromHead_)];
NSSize clippedHeadSize = [clippedHeadString size];
- // Clip the desired portion from the beginning of the string.
+ // This is the offset at which we start drawing. This causes the
+ // beginning of the string to get clipped.
offsetX = clippedHeadSize.width;
- CGFloat delta = size.width - titleRect.size.width;
+ // Due to the fade effect the first character is hard to see.
+ // We want to make sure the first character starting at
+ // |desiredCharactersToTruncateFromHead_| is readable so we reduce
+ // the offset by a little bit.
+ offsetX = MAX(0, offsetX - stringSize.height);
+
+ // If the offset is so large that there's empty space at the tail
+ // then reduce the offset so we can use up the empty space.
+ CGFloat delta = stringSize.width - titleRect.size.width;
if (offsetX > delta)
offsetX = delta;
} else {
// Center the string and clip equal portions of the head and tail.
- offsetX = round((size.width - titleRect.size.width) / 2.0);
+ offsetX = round((stringSize.width - titleRect.size.width) / 2.0);
}
break;
}
@@ -124,31 +137,48 @@
offsetTitleRect.origin.x -= offsetX;
offsetTitleRect.size.width += offsetX;
BOOL isTruncatingHead = offsetX > 0;
- BOOL isTruncatingTail = (size.width - titleRect.size.width) > offsetX;
+ BOOL isTruncatingTail = (stringSize.width - titleRect.size.width) > offsetX;
// Gradient is about twice our line height long
- CGFloat gradientWidth = MIN(size.height * 2, round(NSWidth(cellFrame) / 4));
- NSRect solidPart = cellFrame;
- NSRect headGradientPart = NSZeroRect;
- NSRect tailGradientPart = NSZeroRect;
+ CGFloat gradientWidth = MIN(stringSize.height * 2, round(NSWidth(cellFrame) / 4));
+
+ // Head, solid, and tail rects for drawing the background.
+ NSRect solidBackgroundPart = [self drawingRectForBounds:cellFrame];
+ NSRect headBackgroundPart = NSZeroRect;
+ NSRect tailBackgroundPart = NSZeroRect;
if (isTruncatingHead)
- NSDivideRect(solidPart, &headGradientPart, &solidPart,
+ NSDivideRect(solidBackgroundPart, &headBackgroundPart, &solidBackgroundPart,
gradientWidth, NSMinXEdge);
if (isTruncatingTail)
- NSDivideRect(solidPart, &tailGradientPart, &solidPart,
+ NSDivideRect(solidBackgroundPart, &tailBackgroundPart, &solidBackgroundPart,
gradientWidth, NSMaxXEdge);
+ // Head, solid and tail rects for clipping the title. This is slightly
+ // smaller than the background rects.
+ NSRect solidTitleClipPart = titleRect;
+ NSRect headTitleClipPart = NSZeroRect;
+ NSRect tailTitleClipPart = NSZeroRect;
+ if (isTruncatingHead) {
+ CGFloat width = NSMinX(solidBackgroundPart) - NSMinX(solidTitleClipPart);
+ NSDivideRect(solidTitleClipPart, &headTitleClipPart, &solidTitleClipPart,
+ width, NSMinXEdge);
+ }
+ if (isTruncatingTail) {
+ CGFloat width = NSMaxX(solidTitleClipPart) - NSMaxX(solidBackgroundPart);
+ NSDivideRect(solidTitleClipPart, &tailTitleClipPart, &solidTitleClipPart,
+ width, NSMaxXEdge);
+ }
+
// Draw non-gradient part without transparency layer, as light text on a dark
// background looks bad with a gradient layer.
- NSRect backgroundRect = [self drawingRectForBounds:cellFrame];
[[NSGraphicsContext currentContext] saveGraphicsState];
- [NSBezierPath clipRect:solidPart];
if ([self drawsBackground]) {
[[self backgroundColor] set];
- NSRectFillUsingOperation(backgroundRect, NSCompositeSourceOver);
+ NSRectFillUsingOperation(solidBackgroundPart, NSCompositeSourceOver);
}
// We draw the text ourselves because [super drawInteriorWithFrame:inView:]
// doesn't draw correctly if the cell draws its own background.
+ [NSBezierPath clipRect:solidTitleClipPart];
[attributedString drawInRect:offsetTitleRect];
[[NSGraphicsContext currentContext] restoreGraphicsState];
@@ -160,15 +190,15 @@
if (isTruncatingHead)
[self drawTextGradientPart:attributedString
titleRect:offsetTitleRect
- backgroundRect:backgroundRect
- clipRect:headGradientPart
+ backgroundRect:headBackgroundPart
+ clipRect:headTitleClipPart
mask:mask
fadeToRight:NO];
if (isTruncatingTail)
[self drawTextGradientPart:attributedString
titleRect:offsetTitleRect
- backgroundRect:backgroundRect
- clipRect:tailGradientPart
+ backgroundRect:tailBackgroundPart
+ clipRect:tailTitleClipPart
mask:mask
fadeToRight:YES];
@@ -197,6 +227,11 @@
return desiredCharactersToTruncateFromHead_;
}
+// The faded ends of the cell are not opaque.
+- (BOOL)isOpaque {
+ return NO;
+}
+
@end
#endif
diff --git a/AppKit/GTMFadeTruncatingTextFieldCellTest.m b/AppKit/GTMFadeTruncatingTextFieldCellTest.m
index a1bf937..c201d27 100644
--- a/AppKit/GTMFadeTruncatingTextFieldCellTest.m
+++ b/AppKit/GTMFadeTruncatingTextFieldCellTest.m
@@ -75,7 +75,7 @@
@"GTMFadeTruncatingTextFieldCellTest5",
nil);
- [field setStringValue:@"Fade on left only AA"];
+ [field setStringValue:@"Fade on left only A"];
GTMAssertObjectImageEqualToImageNamed(field,
@"GTMFadeTruncatingTextFieldCellTest6",
nil);
@@ -92,6 +92,15 @@
GTMAssertObjectImageEqualToImageNamed(field,
@"GTMFadeTruncatingTextFieldCellTest7",
nil);
+
+ // Border with a solid background color.
+ [field setTextColor:[NSColor whiteColor]];
+ [field setDrawsBackground:YES];
+ [field setBackgroundColor:[NSColor blackColor]];
+ [field setBordered:YES];
+ GTMAssertObjectImageEqualToImageNamed(field,
+ @"GTMFadeTruncatingTextFieldCellTest8",
+ nil);
}
@end
diff --git a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest5.tiff b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest5.tiff
index f360479..4d4635f 100644
--- a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest5.tiff
+++ b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest5.tiff
Binary files differ
diff --git a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest6.tiff b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest6.tiff
index d704a81..c8b435d 100644
--- a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest6.tiff
+++ b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest6.tiff
Binary files differ
diff --git a/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest8.tiff b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest8.tiff
new file mode 100644
index 0000000..47d70bd
--- /dev/null
+++ b/AppKit/TestData/GTMFadeTruncatingTextFieldCellTest8.tiff
Binary files differ