Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 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/ui/cocoa/bubble_view.h"
      6 
      7 #include "chrome/browser/themes/theme_service.h"
      8 #import "chrome/browser/ui/cocoa/themed_window.h"
      9 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h"
     10 #import "third_party/GTM/AppKit/GTMNSColor+Luminance.h"
     11 
     12 // The roundedness of the edges of our bubble.
     13 const int kBubbleCornerRadius = 4.0f;
     14 const float kWindowEdge = 0.7f;
     15 
     16 @implementation BubbleView
     17 
     18 // Designated initializer. |provider| is the window from which we get the
     19 // current theme to draw text and backgrounds. If nil, the current window will
     20 // be checked. The caller needs to ensure |provider| can't go away as it will
     21 // not be retained. Defaults to all corners being rounded.
     22 - (id)initWithFrame:(NSRect)frame themeProvider:(NSWindow*)provider {
     23   if ((self = [super initWithFrame:frame])) {
     24     cornerFlags_ = kRoundedAllCorners;
     25     themeProvider_ = provider;
     26   }
     27   return self;
     28 }
     29 
     30 // Sets the string displayed in the bubble. A copy of the string is made.
     31 - (void)setContent:(NSString*)content {
     32   if ([content_ isEqualToString:content])
     33     return;
     34   content_.reset([content copy]);
     35   [self setNeedsDisplay:YES];
     36 }
     37 
     38 // Sets which corners will be rounded.
     39 - (void)setCornerFlags:(unsigned long)flags {
     40   if (cornerFlags_ == flags)
     41     return;
     42   cornerFlags_ = flags;
     43   [self setNeedsDisplay:YES];
     44 }
     45 
     46 - (void)setThemeProvider:(NSWindow*)provider {
     47   if (themeProvider_ == provider)
     48     return;
     49   themeProvider_ = provider;
     50   [self setNeedsDisplay:YES];
     51 }
     52 
     53 - (NSString*)content {
     54   return content_.get();
     55 }
     56 
     57 - (unsigned long)cornerFlags {
     58   return cornerFlags_;
     59 }
     60 
     61 // The font used to display the content string.
     62 - (NSFont*)font {
     63   return [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
     64 }
     65 
     66 // Draws the themed background and the text. Will draw a gray bg if no theme.
     67 - (void)drawRect:(NSRect)rect {
     68   float topLeftRadius =
     69       cornerFlags_ & kRoundedTopLeftCorner ? kBubbleCornerRadius : 0;
     70   float topRightRadius =
     71       cornerFlags_ & kRoundedTopRightCorner ? kBubbleCornerRadius : 0;
     72   float bottomLeftRadius =
     73       cornerFlags_ & kRoundedBottomLeftCorner ? kBubbleCornerRadius : 0;
     74   float bottomRightRadius =
     75       cornerFlags_ & kRoundedBottomRightCorner ? kBubbleCornerRadius : 0;
     76 
     77   ui::ThemeProvider* themeProvider =
     78       themeProvider_ ? [themeProvider_ themeProvider] :
     79                        [[self window] themeProvider];
     80 
     81   // Background / Edge
     82 
     83   NSRect bounds = [self bounds];
     84   bounds = NSInsetRect(bounds, 0.5, 0.5);
     85   NSBezierPath* border =
     86       [NSBezierPath gtm_bezierPathWithRoundRect:bounds
     87                             topLeftCornerRadius:topLeftRadius
     88                            topRightCornerRadius:topRightRadius
     89                          bottomLeftCornerRadius:bottomLeftRadius
     90                         bottomRightCornerRadius:bottomRightRadius];
     91 
     92   if (themeProvider)
     93     [themeProvider->GetNSColor(ThemeService::COLOR_TOOLBAR, true) set];
     94   [border fill];
     95 
     96   [[NSColor colorWithDeviceWhite:kWindowEdge alpha:1.0f] set];
     97   [border stroke];
     98 
     99   // Text
    100   NSColor* textColor = [NSColor blackColor];
    101   if (themeProvider)
    102     textColor = themeProvider->GetNSColor(ThemeService::COLOR_TAB_TEXT,
    103                                           true);
    104   NSFont* textFont = [self font];
    105   scoped_nsobject<NSShadow> textShadow([[NSShadow alloc] init]);
    106   [textShadow setShadowBlurRadius:0.0f];
    107   [textShadow.get() setShadowColor:[textColor gtm_legibleTextColor]];
    108   [textShadow.get() setShadowOffset:NSMakeSize(0.0f, -1.0f)];
    109 
    110   NSDictionary* textDict = [NSDictionary dictionaryWithObjectsAndKeys:
    111       textColor, NSForegroundColorAttributeName,
    112       textFont, NSFontAttributeName,
    113       textShadow.get(), NSShadowAttributeName,
    114       nil];
    115   [content_ drawAtPoint:NSMakePoint(kBubbleViewTextPositionX,
    116                                     kBubbleViewTextPositionY)
    117          withAttributes:textDict];
    118 }
    119 
    120 @end
    121