Home | History | Annotate | Download | only in wm
      1 // Copyright 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 #ifndef ASH_WM_HEADER_PAINTER_H_
      6 #define ASH_WM_HEADER_PAINTER_H_
      7 
      8 #include "ash/ash_export.h"
      9 #include "base/basictypes.h"
     10 #include "base/compiler_specific.h"  // OVERRIDE
     11 #include "base/gtest_prod_util.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "ui/aura/window_observer.h"
     14 #include "ui/gfx/animation/animation_delegate.h"
     15 #include "ui/gfx/rect.h"
     16 
     17 namespace aura {
     18 class Window;
     19 }
     20 namespace gfx {
     21 class Canvas;
     22 class Font;
     23 class ImageSkia;
     24 class Point;
     25 class Size;
     26 class SlideAnimation;
     27 }
     28 namespace views {
     29 class View;
     30 class Widget;
     31 }
     32 
     33 namespace ash {
     34 class FrameCaptionButtonContainerView;
     35 
     36 // Helper class for painting the window header.
     37 class ASH_EXPORT HeaderPainter : public aura::WindowObserver,
     38                                  public gfx::AnimationDelegate {
     39  public:
     40   // Opacity values for the window header in various states, from 0 to 255.
     41   static int kActiveWindowOpacity;
     42   static int kInactiveWindowOpacity;
     43   static int kSoloWindowOpacity;
     44 
     45   enum HeaderMode {
     46     ACTIVE,
     47     INACTIVE
     48   };
     49 
     50   HeaderPainter();
     51   virtual ~HeaderPainter();
     52 
     53   // None of the parameters are owned.
     54   void Init(views::Widget* frame,
     55             views::View* header_view,
     56             views::View* window_icon,
     57             FrameCaptionButtonContainerView* caption_button_container);
     58 
     59   // Returns the bounds of the client view for a window with |header_height|
     60   // and |window_bounds|. The return value and |window_bounds| are in the
     61   // views::NonClientView's coordinates.
     62   static gfx::Rect GetBoundsForClientView(int header_height,
     63                                           const gfx::Rect& window_bounds);
     64 
     65   // Returns the bounds of the window given |header_height| and |client_bounds|.
     66   // The return value and |client_bounds| are in the views::NonClientView's
     67   // coordinates.
     68   static gfx::Rect GetWindowBoundsForClientBounds(
     69       int header_height,
     70       const gfx::Rect& client_bounds);
     71 
     72   // Determines the window HT* code at |point|. Returns HTNOWHERE if |point| is
     73   // not within the top |header_height_| of |header_view_|. |point| is in the
     74   // coordinates of |header_view_|'s widget. The client view must be hittested
     75   // before calling this method because a browser's tabs are in the top
     76   // |header_height_| of |header_view_|.
     77   int NonClientHitTest(const gfx::Point& point) const;
     78 
     79   // Returns the header's minimum width.
     80   int GetMinimumHeaderWidth() const;
     81 
     82   // Returns the inset from the right edge.
     83   int GetRightInset() const;
     84 
     85   // Returns the amount that the theme background should be inset.
     86   int GetThemeBackgroundXInset() const;
     87 
     88   // Paints the header.
     89   // |theme_frame_overlay_id| is 0 if no overlay image should be used.
     90   void PaintHeader(gfx::Canvas* canvas,
     91                    HeaderMode header_mode,
     92                    int theme_frame_id,
     93                    int theme_frame_overlay_id);
     94 
     95   // Paints the header/content separator line. Exists as a separate function
     96   // because some windows with complex headers (e.g. browsers with tab strips)
     97   // need to draw their own line.
     98   void PaintHeaderContentSeparator(gfx::Canvas* canvas);
     99 
    100   // Returns size of the header/content separator line in pixels.
    101   int HeaderContentSeparatorSize() const;
    102 
    103   // Paint the title bar, primarily the title string.
    104   void PaintTitleBar(gfx::Canvas* canvas, const gfx::Font& title_font);
    105 
    106   // Performs layout for the header based on whether we want the shorter
    107   // appearance. |shorter_layout| is typically used for maximized windows, but
    108   // not always.
    109   void LayoutHeader(bool shorter_layout);
    110 
    111   // Sets the height of the header. The height of the header affects painting,
    112   // and non client hit tests. It does not affect layout.
    113   void set_header_height(int header_height) {
    114     header_height_ = header_height;
    115   }
    116 
    117   // Returns the header height.
    118   int header_height() const {
    119     return header_height_;
    120   }
    121 
    122   // Schedule a re-paint of the entire title.
    123   void SchedulePaintForTitle(const gfx::Font& title_font);
    124 
    125   // Called when the browser theme changes.
    126   void OnThemeChanged();
    127 
    128   // aura::WindowObserver overrides:
    129   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
    130   virtual void OnWindowBoundsChanged(aura::Window* window,
    131                                      const gfx::Rect& old_bounds,
    132                                      const gfx::Rect& new_bounds) OVERRIDE;
    133 
    134   // Overridden from gfx::AnimationDelegate
    135   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
    136 
    137  private:
    138   FRIEND_TEST_ALL_PREFIXES(HeaderPainterTest, GetHeaderOpacity);
    139   FRIEND_TEST_ALL_PREFIXES(HeaderPainterTest, TitleIconAlignment);
    140 
    141   // Returns the header bounds in the coordinates of |header_view_|. The header
    142   // is assumed to be positioned at the top left corner of |header_view_| and to
    143   // have the same width as |header_view_|.
    144   gfx::Rect GetHeaderLocalBounds() const;
    145 
    146   // Returns the offset between window left edge and title string.
    147   int GetTitleOffsetX() const;
    148 
    149   // Returns the vertical center of the caption button container in window
    150   // coordinates.
    151   int GetCaptionButtonContainerCenterY() const;
    152 
    153   // Returns the opacity value used to paint the header.
    154   // |theme_frame_overlay_id| is 0 if no overlay image is used.
    155   int GetHeaderOpacity(HeaderMode header_mode,
    156                        int theme_frame_id,
    157                        int theme_frame_overlay_id) const;
    158 
    159   // Returns the radius of the header's top corners.
    160   int GetHeaderCornerRadius() const;
    161 
    162   // Schedules a paint for the header. Used when transitioning from no header to
    163   // a header (or other way around).
    164   void SchedulePaintForHeader();
    165 
    166   // Get the bounds for the title. The provided |title_font| is used to
    167   // determine the correct dimensions.
    168   gfx::Rect GetTitleBounds(const gfx::Font& title_font);
    169 
    170   // Not owned
    171   views::Widget* frame_;
    172   views::View* header_view_;
    173   views::View* window_icon_;  // May be NULL.
    174   FrameCaptionButtonContainerView* caption_button_container_;
    175   aura::Window* window_;
    176 
    177   // The height of the header.
    178   int header_height_;
    179 
    180   // Window frame header/caption parts.
    181   const gfx::ImageSkia* top_left_corner_;
    182   const gfx::ImageSkia* top_edge_;
    183   const gfx::ImageSkia* top_right_corner_;
    184   const gfx::ImageSkia* header_left_edge_;
    185   const gfx::ImageSkia* header_right_edge_;
    186 
    187   // Image ids and opacity last used for painting header.
    188   int previous_theme_frame_id_;
    189   int previous_theme_frame_overlay_id_;
    190   int previous_opacity_;
    191 
    192   // Image ids and opacity we are crossfading from.
    193   int crossfade_theme_frame_id_;
    194   int crossfade_theme_frame_overlay_id_;
    195   int crossfade_opacity_;
    196 
    197   scoped_ptr<gfx::SlideAnimation> crossfade_animation_;
    198 
    199   DISALLOW_COPY_AND_ASSIGN(HeaderPainter);
    200 };
    201 
    202 }  // namespace ash
    203 
    204 #endif  // ASH_WM_HEADER_PAINTER_H_
    205