OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 #include "ash/common/frame/default_header_painter.h" | |
6 | |
7 #include "ash/common/ash_layout_constants.h" | |
8 #include "ash/common/frame/caption_buttons/frame_caption_button_container_view.h
" | |
9 #include "ash/common/frame/header_painter_util.h" | |
10 #include "ash/resources/grit/ash_resources.h" | |
11 #include "ash/resources/vector_icons/vector_icons.h" | |
12 #include "base/debug/leak_annotations.h" | |
13 #include "base/logging.h" // DCHECK | |
14 #include "third_party/skia/include/core/SkPath.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "ui/gfx/animation/slide_animation.h" | |
17 #include "ui/gfx/canvas.h" | |
18 #include "ui/gfx/color_utils.h" | |
19 #include "ui/gfx/font_list.h" | |
20 #include "ui/gfx/geometry/rect.h" | |
21 #include "ui/gfx/image/image.h" | |
22 #include "ui/gfx/scoped_canvas.h" | |
23 #include "ui/gfx/skia_util.h" | |
24 #include "ui/views/view.h" | |
25 #include "ui/views/widget/native_widget_aura.h" | |
26 #include "ui/views/widget/widget.h" | |
27 #include "ui/views/widget/widget_delegate.h" | |
28 | |
29 using views::Widget; | |
30 | |
31 namespace { | |
32 | |
33 // Color for the window title text. | |
34 const SkColor kTitleTextColor = SkColorSetRGB(40, 40, 40); | |
35 // Color of the active window header/content separator line. | |
36 const SkColor kHeaderContentSeparatorColor = SkColorSetRGB(150, 150, 152); | |
37 // Color of the inactive window header/content separator line. | |
38 const SkColor kHeaderContentSeparatorInactiveColor = | |
39 SkColorSetRGB(180, 180, 182); | |
40 // The default color of the frame. | |
41 const SkColor kDefaultFrameColor = SkColorSetRGB(242, 242, 242); | |
42 // Duration of crossfade animation for activating and deactivating frame. | |
43 const int kActivationCrossfadeDurationMs = 200; | |
44 | |
45 // Tiles an image into an area, rounding the top corners. | |
46 void TileRoundRect(gfx::Canvas* canvas, | |
47 const cc::PaintFlags& flags, | |
48 const gfx::Rect& bounds, | |
49 int corner_radius) { | |
50 SkRect rect = gfx::RectToSkRect(bounds); | |
51 const SkScalar corner_radius_scalar = SkIntToScalar(corner_radius); | |
52 SkScalar radii[8] = {corner_radius_scalar, | |
53 corner_radius_scalar, // top-left | |
54 corner_radius_scalar, | |
55 corner_radius_scalar, // top-right | |
56 0, | |
57 0, // bottom-right | |
58 0, | |
59 0}; // bottom-left | |
60 SkPath path; | |
61 path.addRoundRect(rect, radii, SkPath::kCW_Direction); | |
62 canvas->DrawPath(path, flags); | |
63 } | |
64 | |
65 // Returns the FontList to use for the title. | |
66 const gfx::FontList& GetTitleFontList() { | |
67 static const gfx::FontList* title_font_list = | |
68 new gfx::FontList(views::NativeWidgetAura::GetWindowTitleFontList()); | |
69 ANNOTATE_LEAKING_OBJECT_PTR(title_font_list); | |
70 return *title_font_list; | |
71 } | |
72 | |
73 } // namespace | |
74 | |
75 namespace ash { | |
76 | |
77 /////////////////////////////////////////////////////////////////////////////// | |
78 // DefaultHeaderPainter, public: | |
79 | |
80 DefaultHeaderPainter::DefaultHeaderPainter() | |
81 : frame_(NULL), | |
82 view_(NULL), | |
83 left_header_view_(NULL), | |
84 active_frame_color_(kDefaultFrameColor), | |
85 inactive_frame_color_(kDefaultFrameColor), | |
86 caption_button_container_(NULL), | |
87 painted_height_(0), | |
88 mode_(MODE_INACTIVE), | |
89 initial_paint_(true), | |
90 activation_animation_(new gfx::SlideAnimation(this)) {} | |
91 | |
92 DefaultHeaderPainter::~DefaultHeaderPainter() {} | |
93 | |
94 void DefaultHeaderPainter::Init( | |
95 views::Widget* frame, | |
96 views::View* header_view, | |
97 FrameCaptionButtonContainerView* caption_button_container) { | |
98 DCHECK(frame); | |
99 DCHECK(header_view); | |
100 DCHECK(caption_button_container); | |
101 frame_ = frame; | |
102 view_ = header_view; | |
103 caption_button_container_ = caption_button_container; | |
104 caption_button_container_->SetButtonSize( | |
105 GetAshLayoutSize(AshLayoutSize::NON_BROWSER_CAPTION_BUTTON)); | |
106 UpdateAllButtonImages(); | |
107 } | |
108 | |
109 int DefaultHeaderPainter::GetMinimumHeaderWidth() const { | |
110 // Ensure we have enough space for the window icon and buttons. We allow | |
111 // the title string to collapse to zero width. | |
112 return GetTitleBounds().x() + | |
113 caption_button_container_->GetMinimumSize().width(); | |
114 } | |
115 | |
116 void DefaultHeaderPainter::PaintHeader(gfx::Canvas* canvas, Mode mode) { | |
117 Mode old_mode = mode_; | |
118 mode_ = mode; | |
119 | |
120 if (mode_ != old_mode) { | |
121 UpdateAllButtonImages(); | |
122 if (!initial_paint_ && HeaderPainterUtil::CanAnimateActivation(frame_)) { | |
123 activation_animation_->SetSlideDuration(kActivationCrossfadeDurationMs); | |
124 if (mode_ == MODE_ACTIVE) | |
125 activation_animation_->Show(); | |
126 else | |
127 activation_animation_->Hide(); | |
128 } else { | |
129 if (mode_ == MODE_ACTIVE) | |
130 activation_animation_->Reset(1); | |
131 else | |
132 activation_animation_->Reset(0); | |
133 } | |
134 initial_paint_ = false; | |
135 } | |
136 | |
137 int corner_radius = (frame_->IsMaximized() || frame_->IsFullscreen()) | |
138 ? 0 | |
139 : HeaderPainterUtil::GetTopCornerRadiusWhenRestored(); | |
140 | |
141 cc::PaintFlags flags; | |
142 int active_alpha = activation_animation_->CurrentValueBetween(0, 255); | |
143 flags.setColor(color_utils::AlphaBlend(active_frame_color_, | |
144 inactive_frame_color_, active_alpha)); | |
145 flags.setAntiAlias(true); | |
146 TileRoundRect(canvas, flags, GetLocalBounds(), corner_radius); | |
147 | |
148 if (!frame_->IsMaximized() && !frame_->IsFullscreen() && | |
149 mode_ == MODE_INACTIVE && !UsesCustomFrameColors()) { | |
150 PaintHighlightForInactiveRestoredWindow(canvas); | |
151 } | |
152 if (frame_->widget_delegate()->ShouldShowWindowTitle()) | |
153 PaintTitleBar(canvas); | |
154 if (!UsesCustomFrameColors()) | |
155 PaintHeaderContentSeparator(canvas); | |
156 } | |
157 | |
158 void DefaultHeaderPainter::LayoutHeader() { | |
159 caption_button_container_->SetUseLightImages(ShouldUseLightImages()); | |
160 UpdateSizeButtonImages(); | |
161 caption_button_container_->Layout(); | |
162 | |
163 gfx::Size caption_button_container_size = | |
164 caption_button_container_->GetPreferredSize(); | |
165 caption_button_container_->SetBounds( | |
166 view_->width() - caption_button_container_size.width(), 0, | |
167 caption_button_container_size.width(), | |
168 caption_button_container_size.height()); | |
169 | |
170 if (left_header_view_) { | |
171 // Vertically center the left header view with respect to the caption button | |
172 // container. | |
173 // Floor when computing the center of |caption_button_container_|. | |
174 gfx::Size size = left_header_view_->GetPreferredSize(); | |
175 int icon_offset_y = | |
176 caption_button_container_->height() / 2 - size.height() / 2; | |
177 left_header_view_->SetBounds(HeaderPainterUtil::GetLeftViewXInset(), | |
178 icon_offset_y, size.width(), size.height()); | |
179 } | |
180 | |
181 // The header/content separator line overlays the caption buttons. | |
182 SetHeaderHeightForPainting(caption_button_container_->height()); | |
183 } | |
184 | |
185 int DefaultHeaderPainter::GetHeaderHeight() const { | |
186 return caption_button_container_->height(); | |
187 } | |
188 | |
189 int DefaultHeaderPainter::GetHeaderHeightForPainting() const { | |
190 return painted_height_; | |
191 } | |
192 | |
193 void DefaultHeaderPainter::SetHeaderHeightForPainting(int height) { | |
194 painted_height_ = height; | |
195 } | |
196 | |
197 void DefaultHeaderPainter::SchedulePaintForTitle() { | |
198 view_->SchedulePaintInRect(GetTitleBounds()); | |
199 } | |
200 | |
201 void DefaultHeaderPainter::SetFrameColors(SkColor active_frame_color, | |
202 SkColor inactive_frame_color) { | |
203 active_frame_color_ = active_frame_color; | |
204 inactive_frame_color_ = inactive_frame_color; | |
205 UpdateAllButtonImages(); | |
206 } | |
207 | |
208 SkColor DefaultHeaderPainter::GetActiveFrameColor() const { | |
209 return active_frame_color_; | |
210 } | |
211 | |
212 SkColor DefaultHeaderPainter::GetInactiveFrameColor() const { | |
213 return inactive_frame_color_; | |
214 } | |
215 | |
216 void DefaultHeaderPainter::UpdateLeftHeaderView(views::View* left_header_view) { | |
217 left_header_view_ = left_header_view; | |
218 } | |
219 | |
220 /////////////////////////////////////////////////////////////////////////////// | |
221 // gfx::AnimationDelegate overrides: | |
222 | |
223 void DefaultHeaderPainter::AnimationProgressed( | |
224 const gfx::Animation* animation) { | |
225 view_->SchedulePaintInRect(GetLocalBounds()); | |
226 } | |
227 | |
228 /////////////////////////////////////////////////////////////////////////////// | |
229 // DefaultHeaderPainter, private: | |
230 | |
231 void DefaultHeaderPainter::PaintHighlightForInactiveRestoredWindow( | |
232 gfx::Canvas* canvas) { | |
233 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
234 gfx::ImageSkia top_edge = | |
235 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_TOP); | |
236 gfx::ImageSkia left_edge = | |
237 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_LEFT); | |
238 gfx::ImageSkia right_edge = | |
239 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_RIGHT); | |
240 gfx::ImageSkia bottom_edge = | |
241 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_BOTTOM); | |
242 | |
243 int left_edge_width = left_edge.width(); | |
244 int right_edge_width = right_edge.width(); | |
245 canvas->DrawImageInt(left_edge, 0, 0); | |
246 canvas->DrawImageInt(right_edge, view_->width() - right_edge_width, 0); | |
247 canvas->TileImageInt(top_edge, left_edge_width, 0, | |
248 view_->width() - left_edge_width - right_edge_width, | |
249 top_edge.height()); | |
250 | |
251 DCHECK_EQ(left_edge.height(), right_edge.height()); | |
252 int bottom = left_edge.height(); | |
253 int bottom_height = bottom_edge.height(); | |
254 canvas->TileImageInt(bottom_edge, left_edge_width, bottom - bottom_height, | |
255 view_->width() - left_edge_width - right_edge_width, | |
256 bottom_height); | |
257 } | |
258 | |
259 void DefaultHeaderPainter::PaintTitleBar(gfx::Canvas* canvas) { | |
260 // The window icon is painted by its own views::View. | |
261 gfx::Rect title_bounds = GetTitleBounds(); | |
262 title_bounds.set_x(view_->GetMirroredXForRect(title_bounds)); | |
263 canvas->DrawStringRectWithFlags( | |
264 frame_->widget_delegate()->GetWindowTitle(), GetTitleFontList(), | |
265 kTitleTextColor, title_bounds, gfx::Canvas::NO_SUBPIXEL_RENDERING); | |
266 } | |
267 | |
268 void DefaultHeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) { | |
269 gfx::ScopedCanvas scoped_canvas(canvas); | |
270 const float scale = canvas->UndoDeviceScaleFactor(); | |
271 gfx::RectF rect(0, painted_height_ * scale - 1, view_->width() * scale, 1); | |
272 cc::PaintFlags flags; | |
273 flags.setColor((mode_ == MODE_ACTIVE) ? kHeaderContentSeparatorColor | |
274 : kHeaderContentSeparatorInactiveColor); | |
275 canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), flags); | |
276 } | |
277 | |
278 bool DefaultHeaderPainter::ShouldUseLightImages() { | |
279 return color_utils::IsDark(mode_ == MODE_INACTIVE ? inactive_frame_color_ | |
280 : active_frame_color_); | |
281 } | |
282 | |
283 void DefaultHeaderPainter::UpdateAllButtonImages() { | |
284 caption_button_container_->SetUseLightImages(ShouldUseLightImages()); | |
285 caption_button_container_->SetButtonImage(CAPTION_BUTTON_ICON_MINIMIZE, | |
286 kWindowControlMinimizeIcon); | |
287 | |
288 UpdateSizeButtonImages(); | |
289 | |
290 caption_button_container_->SetButtonImage(CAPTION_BUTTON_ICON_CLOSE, | |
291 kWindowControlCloseIcon); | |
292 | |
293 caption_button_container_->SetButtonImage(CAPTION_BUTTON_ICON_LEFT_SNAPPED, | |
294 kWindowControlLeftSnappedIcon); | |
295 | |
296 caption_button_container_->SetButtonImage(CAPTION_BUTTON_ICON_RIGHT_SNAPPED, | |
297 kWindowControlRightSnappedIcon); | |
298 } | |
299 | |
300 void DefaultHeaderPainter::UpdateSizeButtonImages() { | |
301 const gfx::VectorIcon& icon = frame_->IsMaximized() || frame_->IsFullscreen() | |
302 ? kWindowControlRestoreIcon | |
303 : kWindowControlMaximizeIcon; | |
304 caption_button_container_->SetButtonImage( | |
305 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE, icon); | |
306 } | |
307 | |
308 gfx::Rect DefaultHeaderPainter::GetLocalBounds() const { | |
309 return gfx::Rect(view_->width(), painted_height_); | |
310 } | |
311 | |
312 gfx::Rect DefaultHeaderPainter::GetTitleBounds() const { | |
313 return HeaderPainterUtil::GetTitleBounds( | |
314 left_header_view_, caption_button_container_, GetTitleFontList()); | |
315 } | |
316 | |
317 bool DefaultHeaderPainter::UsesCustomFrameColors() const { | |
318 return active_frame_color_ != kDefaultFrameColor || | |
319 inactive_frame_color_ != kDefaultFrameColor; | |
320 } | |
321 | |
322 } // namespace ash | |
OLD | NEW |