Home | History | Annotate | Download | only in infobars
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_H_
      6 #define CHROME_BROWSER_INFOBARS_INFOBAR_H_
      7 
      8 #include <utility>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "build/build_config.h"
     13 #include "chrome/browser/infobars/infobar_delegate.h"
     14 #include "third_party/skia/include/core/SkColor.h"
     15 #include "ui/base/animation/animation_delegate.h"
     16 #include "ui/base/animation/slide_animation.h"
     17 #include "ui/gfx/size.h"
     18 
     19 // TODO(sail): These functions should be static methods in the InfoBar class
     20 // below once all platforms use that class.
     21 SkColor GetInfoBarTopColor(InfoBarDelegate::Type infobar_type);
     22 SkColor GetInfoBarBottomColor(InfoBarDelegate::Type infobar_type);
     23 
     24 // TODO(pkasting): Same with these notification-related typedefs.
     25 typedef InfoBarDelegate InfoBarAddedDetails;
     26 typedef std::pair<InfoBarDelegate*, bool> InfoBarRemovedDetails;
     27 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> InfoBarReplacedDetails;
     28 
     29 // TODO(pkasting): Port Mac to use this.
     30 #if defined(TOOLKIT_VIEWS) || defined(TOOLKIT_GTK) || defined(OS_ANDROID)
     31 
     32 class InfoBarContainer;
     33 class InfoBarService;
     34 
     35 class InfoBar : public ui::AnimationDelegate {
     36  public:
     37   InfoBar(InfoBarService* owner, InfoBarDelegate* delegate);
     38   virtual ~InfoBar();
     39 
     40   // Platforms must define these.
     41   static const int kDefaultBarTargetHeight;
     42   static const int kSeparatorLineHeight;
     43   static const int kDefaultArrowTargetHeight;
     44   static const int kMaximumArrowTargetHeight;
     45   // The half-width (see comments on |arrow_half_width_| below) scales to its
     46   // default and maximum values proportionally to how the height scales to its.
     47   static const int kDefaultArrowTargetHalfWidth;
     48   static const int kMaximumArrowTargetHalfWidth;
     49 
     50   InfoBarDelegate* delegate() { return delegate_; }
     51   void set_container(InfoBarContainer* container) { container_ = container; }
     52 
     53   // Makes the infobar visible.  If |animate| is true, the infobar is then
     54   // animated to full size.
     55   void Show(bool animate);
     56 
     57   // Makes the infobar hidden.  If |animate| is true, the infobar is first
     58   // animated to zero size.  Once the infobar is hidden, it is removed from its
     59   // container (triggering its deletion), and its delegate is closed.
     60   void Hide(bool animate);
     61 
     62   // Changes the target height of the arrow portion of the infobar.  This has no
     63   // effect once the infobar is animating closed.
     64   void SetArrowTargetHeight(int height);
     65 
     66   // Notifies the infobar that it is no longer owned and should close its
     67   // delegate once it is invisible.
     68   void CloseSoon();
     69 
     70   const ui::SlideAnimation& animation() const { return animation_; }
     71   int arrow_height() const { return arrow_height_; }
     72   int arrow_target_height() const { return arrow_target_height_; }
     73   int arrow_half_width() const { return arrow_half_width_; }
     74   int total_height() const { return arrow_height_ + bar_height_; }
     75 
     76  protected:
     77   // ui::AnimationDelegate:
     78   virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
     79 
     80   // Forwards a close request to our owner.
     81   // NOTE: Subclasses should not call this if we're already unowned.
     82   void RemoveSelf();
     83 
     84   // Changes the target height of the main ("bar") portion of the infobar.
     85   void SetBarTargetHeight(int height);
     86 
     87   // Given a control with size |prefsize|, returns the centered y position
     88   // within us, taking into account animation so the control "slides in" (or
     89   // out) as we animate open and closed.
     90   int OffsetY(const gfx::Size& prefsize) const;
     91 
     92   InfoBarService* owner() const { return owner_; }
     93   const InfoBarContainer* container() const { return container_; }
     94   InfoBarContainer* container() { return container_; }
     95   ui::SlideAnimation* animation() { return &animation_; }
     96   int bar_height() const { return bar_height_; }
     97   int bar_target_height() const { return bar_target_height_; }
     98 
     99   // Platforms may optionally override these if they need to do work during
    100   // processing of the given calls.
    101   virtual void PlatformSpecificShow(bool animate) {}
    102   virtual void PlatformSpecificHide(bool animate) {}
    103   virtual void PlatformSpecificOnCloseSoon() {}
    104   virtual void PlatformSpecificOnHeightsRecalculated() {}
    105 
    106  private:
    107   // ui::AnimationDelegate:
    108   virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE;
    109 
    110   // Finds the new desired arrow and bar heights, and if they differ from the
    111   // current ones, calls PlatformSpecificOnHeightRecalculated().  Informs our
    112   // container our state has changed if either the heights have changed or
    113   // |force_notify| is set.
    114   void RecalculateHeights(bool force_notify);
    115 
    116   // Checks whether we're closed.  If so, notifies the container that it should
    117   // remove us (which will cause the platform-specific code to asynchronously
    118   // delete us) and closes the delegate.
    119   void MaybeDelete();
    120 
    121   InfoBarService* owner_;
    122   InfoBarDelegate* delegate_;
    123   InfoBarContainer* container_;
    124   ui::SlideAnimation animation_;
    125 
    126   // The current and target heights of the arrow and bar portions, and half the
    127   // current arrow width.  (It's easier to work in half-widths as we draw the
    128   // arrow as two halves on either side of a center point.)
    129   int arrow_height_;         // Includes both fill and top stroke.
    130   int arrow_target_height_;
    131   int arrow_half_width_;     // Includes only fill.
    132   int bar_height_;           // Includes both fill and bottom separator.
    133   int bar_target_height_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(InfoBar);
    136 };
    137 
    138 #elif defined(OS_MACOSX)
    139 #include "chrome/browser/ui/cocoa/infobars/infobar.h"
    140 #endif
    141 
    142 #endif  // CHROME_BROWSER_INFOBARS_INFOBAR_H_
    143