Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: chrome/browser/cocoa/gradient_button_cell.mm

Issue 2806066: Mac: Add nice fade effect to overflowing bookmark buttons in the bookmark bar (Closed)
Patch Set: remove extra nl Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/cocoa/gradient_button_cell.h" 5 #include "chrome/browser/cocoa/gradient_button_cell.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #import "base/scoped_nsobject.h" 8 #import "base/scoped_nsobject.h"
9 #import "chrome/browser/browser_theme_provider.h" 9 #import "chrome/browser/browser_theme_provider.h"
10 #import "chrome/browser/cocoa/image_utils.h" 10 #import "chrome/browser/cocoa/image_utils.h"
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 NSRect imageRect = NSZeroRect; 450 NSRect imageRect = NSZeroRect;
451 imageRect.size = [overlayImage_ size]; 451 imageRect.size = [overlayImage_ size];
452 [overlayImage_ drawInRect:[self imageRectForBounds:cellFrame] 452 [overlayImage_ drawInRect:[self imageRectForBounds:cellFrame]
453 fromRect:imageRect 453 fromRect:imageRect
454 operation:NSCompositeSourceOver 454 operation:NSCompositeSourceOver
455 fraction:[self isEnabled] ? 1.0 : 0.5 455 fraction:[self isEnabled] ? 1.0 : 0.5
456 neverFlipped:YES]; 456 neverFlipped:YES];
457 } 457 }
458 } 458 }
459 459
460 // Overriden from NSButtonCell so we can display a nice fadeout effect for
461 // button titles that overflow.
462 // TODO(jeremy): Move this to GTM.
463 - (NSRect)drawTitle:(NSAttributedString *)title
464 withFrame:(NSRect)cellFrame
465 inView:(NSView *)controlView {
466 // This method is copied in the most part from GTMFadeTruncatingTextFieldCell.
John Grabowski 2010/07/21 17:19:23 Move comment to top of function. Add brief commen
467 NSSize size = [title size];
468
469 // Don't complicate drawing unless we need to clip
470 if (size.width <= NSWidth(cellFrame)) {
471 return [super drawTitle:title withFrame:cellFrame inView:controlView];
472 }
473
474 // Gradient is about twice our line height long.
475 CGFloat gradientWidth = MIN(size.height * 2, NSWidth(cellFrame) / 4);
476
477 NSRect solidPart, gradientPart;
478 NSDivideRect(cellFrame, &gradientPart, &solidPart, gradientWidth, NSMaxXEdge);
479
480 // Draw non-gradient part without transparency layer, as light text on a dark
481 // background looks bad with a gradient layer.
482 [[NSGraphicsContext currentContext] saveGraphicsState];
483 [NSBezierPath clipRect:solidPart];
484
485 // 11 is the magic number needed to make this match the native NSButtonCell's
486 // label display.
487 CGFloat textLeft = [[self image] size].width + 11;
488
489 // For some reason, the height of cellFrame as passed in is totally bogus.
490 // For vertical centering purposes, we need the bounds of the containing
491 // view.
492 NSRect buttonFrame = [[self controlView] frame];
493
494 // Off-by-one to match native NSButtonCell's version.
495 NSPoint textOffset = NSMakePoint(textLeft,
496 (NSHeight(buttonFrame) - size.height)/2 + 1);
497 [title drawAtPoint:textOffset];
498 [[NSGraphicsContext currentContext] restoreGraphicsState];
499
500 // Draw the gradient part with a transparency layer. This makes the text look
501 // suboptimal, but since it fades out, that's ok.
502 [[NSGraphicsContext currentContext] saveGraphicsState];
503 [NSBezierPath clipRect:gradientPart];
504 CGContextRef context = static_cast<CGContextRef>(
505 [[NSGraphicsContext currentContext] graphicsPort]);
506 CGContextBeginTransparencyLayerWithRect(context,
507 NSRectToCGRect(gradientPart), 0);
508 [title drawAtPoint:textOffset];
509
510 // TODO(alcor): switch this to GTMLinearRGBShading if we ever need on 10.4
511 NSColor *color = [NSColor textColor]; //[self textColor];
512 NSColor *alphaColor = [color colorWithAlphaComponent:0.0];
513 NSGradient *mask = [[NSGradient alloc] initWithStartingColor:color
514 endingColor:alphaColor];
515
516 // Draw the gradient mask
517 CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
518 [mask drawFromPoint:NSMakePoint(NSMaxX(cellFrame) - gradientWidth,
519 NSMinY(cellFrame))
520 toPoint:NSMakePoint(NSMaxX(cellFrame),
521 NSMinY(cellFrame))
522 options:NSGradientDrawsBeforeStartingLocation];
523 [mask release];
524 CGContextEndTransparencyLayer(context);
525 [[NSGraphicsContext currentContext] restoreGraphicsState];
526
527 return cellFrame;
528 }
529
460 - (NSBezierPath*)clipPathForFrame:(NSRect)cellFrame 530 - (NSBezierPath*)clipPathForFrame:(NSRect)cellFrame
461 inView:(NSView*)controlView { 531 inView:(NSView*)controlView {
462 NSBezierPath* boundingPath = nil; 532 NSBezierPath* boundingPath = nil;
463 [self getDrawParamsForFrame:cellFrame 533 [self getDrawParamsForFrame:cellFrame
464 inView:controlView 534 inView:controlView
465 innerFrame:NULL 535 innerFrame:NULL
466 innerPath:NULL 536 innerPath:NULL
467 clipPath:&boundingPath]; 537 clipPath:&boundingPath];
468 return boundingPath; 538 return boundingPath;
469 } 539 }
470 540
471 @end 541 @end
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698