Home | History | Annotate | Download | only in toolbar
      1 // Copyright (c) 2013 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/toolbar/wrench_toolbar_button_cell.h"
      6 
      7 #import "chrome/browser/ui/cocoa/themed_window.h"
      8 #include "ui/gfx/canvas_skia_paint.h"
      9 #include "ui/gfx/rect.h"
     10 
     11 class WrenchIconPainterDelegateMac : public WrenchIconPainter::Delegate {
     12  public:
     13   explicit WrenchIconPainterDelegateMac(NSCell* cell) : cell_(cell) {}
     14   virtual ~WrenchIconPainterDelegateMac() {}
     15 
     16   virtual void ScheduleWrenchIconPaint() OVERRIDE {
     17     [[cell_ controlView] setNeedsDisplay:YES];
     18   }
     19 
     20  private:
     21   NSCell* cell_;
     22 
     23   DISALLOW_COPY_AND_ASSIGN(WrenchIconPainterDelegateMac);
     24 };
     25 
     26 @interface WrenchToolbarButtonCell ()
     27 - (void)commonInit;
     28 - (WrenchIconPainter::BezelType)currentBezelType;
     29 @end
     30 
     31 @implementation WrenchToolbarButtonCell
     32 
     33 - (id)initTextCell:(NSString*)text {
     34   if ((self = [super initTextCell:text])) {
     35     [self commonInit];
     36   }
     37   return self;
     38 }
     39 
     40 - (id)initWithCoder:(NSCoder*)decoder {
     41   if ((self = [super initWithCoder:decoder])) {
     42     [self commonInit];
     43   }
     44   return self;
     45 }
     46 
     47 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
     48   gfx::CanvasSkiaPaint canvas(cellFrame, false);
     49   canvas.set_composite_alpha(true);
     50   canvas.SaveLayerAlpha(255 *
     51                         [self imageAlphaForWindowState:[controlView window]]);
     52   ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
     53   if (themeProvider) {
     54     wrenchIconPainter_->Paint(&canvas,
     55                               [[controlView window] themeProvider],
     56                               gfx::Rect(NSRectToCGRect(cellFrame)),
     57                               [self currentBezelType]);
     58   }
     59   canvas.Restore();
     60 
     61   [self drawFocusRingWithFrame:cellFrame inView:controlView];
     62 }
     63 
     64 - (void)setSeverity:(WrenchIconPainter::Severity)severity
     65       shouldAnimate:(BOOL)shouldAnimate {
     66   wrenchIconPainter_->SetSeverity(severity, shouldAnimate);
     67 }
     68 
     69 - (void)commonInit {
     70   delegate_.reset(new WrenchIconPainterDelegateMac(self));
     71   wrenchIconPainter_.reset(new WrenchIconPainter(delegate_.get()));
     72 }
     73 
     74 - (WrenchIconPainter::BezelType)currentBezelType {
     75   if ([self isHighlighted])
     76     return WrenchIconPainter::BEZEL_PRESSED;
     77   if ([self isMouseInside])
     78     return WrenchIconPainter::BEZEL_HOVER;
     79   return WrenchIconPainter::BEZEL_NONE;
     80 }
     81 
     82 @end
     83