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 CHROMEOS_UI_FRAME_FRAME_HEADER_H_
6 #define CHROMEOS_UI_FRAME_FRAME_HEADER_H_
7 
8 #include "base/callback.h"
9 #include "base/component_export.h"
10 #include "base/optional.h"
11 #include "base/strings/string16.h"
12 #include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/base/ui_base_types.h"
15 #include "ui/compositor/layer_animation_observer.h"
16 #include "ui/views/window/frame_caption_button.h"
17 
18 namespace ash {
19 FORWARD_DECLARE_TEST(DefaultFrameHeaderTest, BackButtonAlignment);
20 FORWARD_DECLARE_TEST(DefaultFrameHeaderTest, TitleIconAlignment);
21 FORWARD_DECLARE_TEST(DefaultFrameHeaderTest, FrameColors);
22 class FramePaintWaiter;
23 }  // namespace ash
24 
25 namespace gfx {
26 class Canvas;
27 class Rect;
28 }  // namespace gfx
29 
30 namespace views {
31 enum class CaptionButtonLayoutSize;
32 class View;
33 class Widget;
34 }  // namespace views
35 
36 namespace chromeos {
37 
38 class CaptionButtonModel;
39 
40 // Helper class for managing the window header.
COMPONENT_EXPORT(CHROMEOS_UI_FRAME)41 class COMPONENT_EXPORT(CHROMEOS_UI_FRAME) FrameHeader {
42  public:
43   enum Mode { MODE_ACTIVE, MODE_INACTIVE };
44 
45   static FrameHeader* Get(views::Widget* widget);
46 
47   virtual ~FrameHeader();
48 
49   const base::string16& frame_text_override() const {
50     return frame_text_override_;
51   }
52 
53   // Returns the header's minimum width.
54   int GetMinimumHeaderWidth() const;
55 
56   // Paints the header.
57   void PaintHeader(gfx::Canvas* canvas);
58 
59   // Performs layout for the header.
60   void LayoutHeader();
61 
62   // Get the height of the header.
63   int GetHeaderHeight() const;
64 
65   // Gets / sets how much of the header is painted. This allows the header to
66   // paint under things (like the tabstrip) which have transparent /
67   // non-painting sections. This height does not affect LayoutHeader().
68   int GetHeaderHeightForPainting() const;
69   void SetHeaderHeightForPainting(int height_for_painting);
70 
71   // Schedule a re-paint of the entire title.
72   void SchedulePaintForTitle();
73 
74   // True to instruct the frame header to paint the header as an active
75   // state.
76   void SetPaintAsActive(bool paint_as_active);
77 
78   // Called when frame show state is changed.
79   void OnShowStateChanged(ui::WindowShowState show_state);
80 
81   void SetLeftHeaderView(views::View* view);
82   void SetBackButton(views::FrameCaptionButton* view);
83   views::FrameCaptionButton* GetBackButton() const;
84   const chromeos::CaptionButtonModel* GetCaptionButtonModel() const;
85 
86   // Updates the frame header painting to reflect a change in frame colors.
87   virtual void UpdateFrameColors() = 0;
88 
89   // Sets text to display in place of the window's title. This will be shown
90   // regardless of what ShouldShowWindowTitle() returns.
91   void SetFrameTextOverride(const base::string16& frame_text_override);
92 
93   void UpdateFrameHeaderKey();
94 
95   views::View* view() { return view_; }
96 
97   chromeos::FrameCaptionButtonContainerView* caption_button_container() {
98     return caption_button_container_;
99   }
100 
101  protected:
102   FrameHeader(views::Widget* target_widget, views::View* view);
103 
104   views::Widget* target_widget() { return target_widget_; }
105   const views::Widget* target_widget() const { return target_widget_; }
106 
107   // Returns bounds of the region in |view_| which is painted with the header
108   // images. The region is assumed to start at the top left corner of |view_|
109   // and to have the same width as |view_|.
110   gfx::Rect GetPaintedBounds() const;
111 
112   void UpdateCaptionButtonColors();
113 
114   void PaintTitleBar(gfx::Canvas* canvas);
115 
116   void SetCaptionButtonContainer(
117       chromeos::FrameCaptionButtonContainerView* caption_button_container);
118 
119   Mode mode() const { return mode_; }
120 
121   virtual void DoPaintHeader(gfx::Canvas* canvas) = 0;
122   virtual views::CaptionButtonLayoutSize GetButtonLayoutSize() const = 0;
123   virtual SkColor GetTitleColor() const = 0;
124   virtual SkColor GetCurrentFrameColor() const = 0;
125 
126   // Starts fade transition animation with given duration.
127   void StartTransitionAnimation(base::TimeDelta duration);
128 
129  private:
130   class FrameAnimatorView;
131   FRIEND_TEST_ALL_PREFIXES(ash::DefaultFrameHeaderTest, BackButtonAlignment);
132   FRIEND_TEST_ALL_PREFIXES(ash::DefaultFrameHeaderTest, TitleIconAlignment);
133   FRIEND_TEST_ALL_PREFIXES(ash::DefaultFrameHeaderTest, FrameColors);
134   friend class ash::FramePaintWaiter;
135 
136   void LayoutHeaderInternal();
137 
138   gfx::Rect GetTitleBounds() const;
139 
140   // The widget that the caption buttons act on. This can be different from
141   // |view_|'s widget.
142   views::Widget* target_widget_;
143 
144   // The view into which |this| paints.
145   views::View* view_;
146   views::FrameCaptionButton* back_button_ = nullptr;  // May remain nullptr.
147   views::View* left_header_view_ = nullptr;           // May remain nullptr.
148   chromeos::FrameCaptionButtonContainerView* caption_button_container_ =
149       nullptr;
150   FrameAnimatorView* frame_animator_ = nullptr;  // owned by view tree.
151 
152   // The height of the header to paint.
153   int painted_height_ = 0;
154 
155   // Used to skip animation when the frame hasn't painted yet.
156   bool painted_ = false;
157 
158   // Whether the header should be painted as active.
159   Mode mode_ = MODE_INACTIVE;
160 
161   base::string16 frame_text_override_;
162 
163   DISALLOW_COPY_AND_ASSIGN(FrameHeader);
164 };
165 
166 }  // namespace chromeos
167 
168 #endif  // CHROMEOS_UI_FRAME_FRAME_HEADER_H_
169