OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/cocoa/bubble_view.h" |
| 6 |
| 7 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h" |
| 8 #import "third_party/GTM/AppKit/GTMNSColor+Luminance.h" |
| 9 #import "third_party/GTM/AppKit/GTMTheme.h" |
| 10 |
| 11 // The roundedness of the edges of our bubble. |
| 12 const int kBubbleCornerRadius = 4.0f; |
| 13 const float kWindowEdge = 0.7f; |
| 14 |
| 15 @implementation BubbleView |
| 16 |
| 17 // Designated initializer. |provider| is the window from which we get the |
| 18 // current theme to draw text and backgrounds. If nil, the current window will |
| 19 // be checked. Defaults to all corners being rounded. The caller needs to |
| 20 // ensure |provider| can't go away as it will not be retained. |
| 21 - (id)initWithFrame:(NSRect)frame themeProvider:(NSWindow*)provider { |
| 22 if ((self = [super initWithFrame:frame])) { |
| 23 cornerFlags_ = kRoundedAllCorners; |
| 24 themeProvider_ = provider; |
| 25 } |
| 26 return self; |
| 27 } |
| 28 |
| 29 // Sets the string displayed in the bubble. A copy of the string is made. |
| 30 - (void)setContent:(NSString*)content { |
| 31 content_.reset([content copy]); |
| 32 [self setNeedsDisplay:YES]; |
| 33 } |
| 34 |
| 35 // Sets which corners will be rounded. |
| 36 - (void)setCornerFlags:(unsigned long)flags { |
| 37 cornerFlags_ = flags; |
| 38 [self setNeedsDisplay:YES]; |
| 39 } |
| 40 |
| 41 - (NSString*)content { |
| 42 return content_.get(); |
| 43 } |
| 44 |
| 45 - (unsigned long)cornerFlags { |
| 46 return cornerFlags_; |
| 47 } |
| 48 |
| 49 // The font used to display the content string. |
| 50 - (NSFont*)font { |
| 51 return [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; |
| 52 } |
| 53 |
| 54 // Asks the given theme provider for its theme. If there isn't one specified, |
| 55 // check the window we are in. May still return nil if the window doesn't |
| 56 // support themeing. |
| 57 - (GTMTheme*)gtm_theme { |
| 58 GTMTheme* theme = [themeProvider_ gtm_theme]; |
| 59 if (!theme) |
| 60 theme = [[self window] gtm_theme]; |
| 61 return theme; |
| 62 } |
| 63 |
| 64 // Draws the themed background and the text. Will draw a gray bg if no theme. |
| 65 - (void)drawRect:(NSRect)rect { |
| 66 float topLeftRadius = |
| 67 cornerFlags_ & kRoundedTopLeftCorner ? kBubbleCornerRadius : 0; |
| 68 float topRightRadius = |
| 69 cornerFlags_ & kRoundedTopRightCorner ? kBubbleCornerRadius : 0; |
| 70 float bottomLeftRadius = |
| 71 cornerFlags_ & kRoundedBottomLeftCorner ? kBubbleCornerRadius : 0; |
| 72 float bottomRightRadius = |
| 73 cornerFlags_ & kRoundedBottomRightCorner ? kBubbleCornerRadius : 0; |
| 74 |
| 75 GTMTheme* theme = [self gtm_theme]; |
| 76 |
| 77 // Background / Edge |
| 78 |
| 79 NSRect bounds = [self bounds]; |
| 80 bounds = NSInsetRect(bounds, 0.5, 0.5); |
| 81 NSBezierPath *border = |
| 82 [NSBezierPath gtm_bezierPathWithRoundRect:bounds |
| 83 topLeftCornerRadius:topLeftRadius |
| 84 topRightCornerRadius:topRightRadius |
| 85 bottomLeftCornerRadius:bottomLeftRadius |
| 86 bottomRightCornerRadius:bottomRightRadius]; |
| 87 |
| 88 NSColor* color = |
| 89 [theme backgroundColorForStyle:GTMThemeStyleToolBar |
| 90 state:GTMThemeStateActiveWindow]; |
| 91 |
| 92 // workaround for default theme |
| 93 // TODO(alcor) next GTM update return nil for background color if not set; |
| 94 if ([color isEqual:[NSColor colorWithCalibratedWhite:0.5 alpha:1.0]]) |
| 95 color = nil; |
| 96 if (!color) |
| 97 color = [NSColor colorWithCalibratedWhite:0.9 alpha:1.0]; |
| 98 [color set]; |
| 99 [border fill]; |
| 100 |
| 101 border = [NSBezierPath gtm_bezierPathWithRoundRect:bounds |
| 102 topLeftCornerRadius:topLeftRadius |
| 103 topRightCornerRadius:topRightRadius |
| 104 bottomLeftCornerRadius:bottomLeftRadius |
| 105 bottomRightCornerRadius:bottomRightRadius]; |
| 106 |
| 107 [[NSColor colorWithDeviceWhite:kWindowEdge alpha:1.0f] set]; |
| 108 [border stroke]; |
| 109 |
| 110 // Text |
| 111 NSColor* textColor = [theme textColorForStyle:GTMThemeStyleToolBar |
| 112 state:GTMThemeStateActiveWindow]; |
| 113 NSFont* textFont = [self font]; |
| 114 scoped_nsobject<NSShadow> textShadow([[NSShadow alloc] init]); |
| 115 [textShadow setShadowBlurRadius:0.0f]; |
| 116 [textShadow setShadowColor:[textColor gtm_legibleTextColor]]; |
| 117 [textShadow setShadowOffset:NSMakeSize(0.0f, -1.0f)]; |
| 118 |
| 119 NSDictionary* textDict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 120 textColor, NSForegroundColorAttributeName, |
| 121 textFont, NSFontAttributeName, |
| 122 textShadow.get(), NSShadowAttributeName, |
| 123 nil]; |
| 124 [content_ drawAtPoint:NSMakePoint(kBubbleViewTextPositionX, |
| 125 kBubbleViewTextPositionY) |
| 126 withAttributes:textDict]; |
| 127 } |
| 128 |
| 129 @end |
OLD | NEW |