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.
5 #include "ash/wm/header_painter.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/wm/caption_buttons/frame_caption_button_container_view.h"
11 #include "base/logging.h" // DCHECK
12 #include "grit/ash_resources.h"
13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkPaint.h"
16 #include "third_party/skia/include/core/SkPath.h"
17 #include "ui/aura/window.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/base/theme_provider.h"
21 #include "ui/gfx/animation/slide_animation.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/font.h"
24 #include "ui/gfx/image/image.h"
25 #include "ui/gfx/screen.h"
26 #include "ui/gfx/skia_util.h"
27 #include "ui/views/widget/widget.h"
28 #include "ui/views/widget/widget_delegate.h"
34 // Space between left edge of window and popup window icon.
35 const int kIconOffsetX
= 9;
36 // Height and width of window icon.
37 const int kIconSize
= 16;
38 // Space between the title text and the caption buttons.
39 const int kTitleLogoSpacing
= 5;
40 // Space between window icon and title text.
41 const int kTitleIconOffsetX
= 5;
42 // Space between window edge and title text, when there is no icon.
43 const int kTitleNoIconOffsetX
= 8;
44 // Color for the non-maximized window title text.
45 const SkColor kNonMaximizedWindowTitleTextColor
= SkColorSetRGB(40, 40, 40);
46 // Color for the maximized window title text.
47 const SkColor kMaximizedWindowTitleTextColor
= SK_ColorWHITE
;
48 // Size of header/content separator line below the header image.
49 const int kHeaderContentSeparatorSize
= 1;
50 // Color of header bottom edge line.
51 const SkColor kHeaderContentSeparatorColor
= SkColorSetRGB(128, 128, 128);
52 // In the pre-Ash era the web content area had a frame along the left edge, so
53 // user-generated theme images for the new tab page assume they are shifted
54 // right relative to the header. Now that we have removed the left edge frame
55 // we need to copy the theme image for the window header from a few pixels
56 // inset to preserve alignment with the NTP image, or else we'll break a bunch
57 // of existing themes. We do something similar on OS X for the same reason.
58 const int kThemeFrameImageInsetX
= 5;
59 // Duration of crossfade animation for activating and deactivating frame.
60 const int kActivationCrossfadeDurationMs
= 200;
62 // Tiles an image into an area, rounding the top corners. Samples |image|
63 // starting |image_inset_x| pixels from the left of the image.
64 void TileRoundRect(gfx::Canvas
* canvas
,
65 const gfx::ImageSkia
& image
,
67 const gfx::Rect
& bounds
,
68 int top_left_corner_radius
,
69 int top_right_corner_radius
,
71 SkRect rect
= gfx::RectToSkRect(bounds
);
72 const SkScalar kTopLeftRadius
= SkIntToScalar(top_left_corner_radius
);
73 const SkScalar kTopRightRadius
= SkIntToScalar(top_right_corner_radius
);
75 kTopLeftRadius
, kTopLeftRadius
, // top-left
76 kTopRightRadius
, kTopRightRadius
, // top-right
80 path
.addRoundRect(rect
, radii
, SkPath::kCW_Direction
);
81 canvas
->DrawImageInPath(image
, -image_inset_x
, 0, path
, paint
);
84 // Tiles |frame_image| and |frame_overlay_image| into an area, rounding the top
86 void PaintFrameImagesInRoundRect(gfx::Canvas
* canvas
,
87 const gfx::ImageSkia
* frame_image
,
88 const gfx::ImageSkia
* frame_overlay_image
,
90 const gfx::Rect
& bounds
,
93 SkXfermode::Mode normal_mode
;
94 SkXfermode::AsMode(NULL
, &normal_mode
);
96 // If |paint| is using an unusual SkXfermode::Mode (this is the case while
97 // crossfading), we must create a new canvas to overlay |frame_image| and
98 // |frame_overlay_image| using |normal_mode| and then paint the result
99 // using the unusual mode. We try to avoid this because creating a new
100 // browser-width canvas is expensive.
101 bool fast_path
= (!frame_overlay_image
||
102 SkXfermode::IsMode(paint
.getXfermode(), normal_mode
));
104 TileRoundRect(canvas
, *frame_image
, paint
, bounds
, corner_radius
,
105 corner_radius
, image_inset_x
);
107 if (frame_overlay_image
) {
108 // Adjust |bounds| such that |frame_overlay_image| is not tiled.
109 gfx::Rect overlay_bounds
= bounds
;
110 overlay_bounds
.Intersect(
111 gfx::Rect(bounds
.origin(), frame_overlay_image
->size()));
112 int top_left_corner_radius
= corner_radius
;
113 int top_right_corner_radius
= corner_radius
;
114 if (overlay_bounds
.width() < bounds
.width() - corner_radius
)
115 top_right_corner_radius
= 0;
116 TileRoundRect(canvas
, *frame_overlay_image
, paint
, overlay_bounds
,
117 top_left_corner_radius
, top_right_corner_radius
, 0);
120 gfx::Canvas
temporary_canvas(bounds
.size(), canvas
->image_scale(), false);
121 temporary_canvas
.TileImageInt(*frame_image
,
124 bounds
.width(), bounds
.height());
125 temporary_canvas
.DrawImageInt(*frame_overlay_image
, 0, 0);
126 TileRoundRect(canvas
, gfx::ImageSkia(temporary_canvas
.ExtractImageRep()),
127 paint
, bounds
, corner_radius
, corner_radius
, 0);
135 ///////////////////////////////////////////////////////////////////////////////
136 // HeaderPainter, public:
138 HeaderPainter::HeaderPainter()
142 caption_button_container_(NULL
),
144 top_left_corner_(NULL
),
146 top_right_corner_(NULL
),
147 header_left_edge_(NULL
),
148 header_right_edge_(NULL
),
149 previous_theme_frame_id_(0),
150 previous_theme_frame_overlay_id_(0),
151 crossfade_theme_frame_id_(0),
152 crossfade_theme_frame_overlay_id_(0) {}
154 HeaderPainter::~HeaderPainter() {
157 void HeaderPainter::Init(
158 views::Widget
* frame
,
159 views::View
* header_view
,
160 views::View
* window_icon
,
161 FrameCaptionButtonContainerView
* caption_button_container
) {
164 // window_icon may be NULL.
165 DCHECK(caption_button_container
);
167 header_view_
= header_view
;
168 window_icon_
= window_icon
;
169 caption_button_container_
= caption_button_container
;
171 // Window frame image parts.
172 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
174 rb
.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_TOP_LEFT
).ToImageSkia();
176 rb
.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_TOP
).ToImageSkia();
178 rb
.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_TOP_RIGHT
).ToImageSkia();
180 rb
.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_LEFT
).ToImageSkia();
182 rb
.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_RIGHT
).ToImageSkia();
186 gfx::Rect
HeaderPainter::GetBoundsForClientView(
188 const gfx::Rect
& window_bounds
) {
189 gfx::Rect
client_bounds(window_bounds
);
190 client_bounds
.Inset(0, header_height
, 0, 0);
191 return client_bounds
;
195 gfx::Rect
HeaderPainter::GetWindowBoundsForClientBounds(
197 const gfx::Rect
& client_bounds
) {
198 gfx::Rect
window_bounds(client_bounds
);
199 window_bounds
.Inset(0, -header_height
, 0, 0);
200 if (window_bounds
.y() < 0)
201 window_bounds
.set_y(0);
202 return window_bounds
;
205 int HeaderPainter::NonClientHitTest(const gfx::Point
& point
) const {
206 gfx::Point
point_in_header_view(point
);
207 views::View::ConvertPointFromWidget(header_view_
, &point_in_header_view
);
208 if (!GetHeaderLocalBounds().Contains(point_in_header_view
))
210 if (caption_button_container_
->visible()) {
211 gfx::Point
point_in_caption_button_container(point
);
212 views::View::ConvertPointFromWidget(caption_button_container_
,
213 &point_in_caption_button_container
);
214 int component
= caption_button_container_
->NonClientHitTest(
215 point_in_caption_button_container
);
216 if (component
!= HTNOWHERE
)
219 // Caption is a safe default.
223 int HeaderPainter::GetMinimumHeaderWidth() const {
224 // Ensure we have enough space for the window icon and buttons. We allow
225 // the title string to collapse to zero width.
226 return GetTitleOffsetX() +
227 caption_button_container_
->GetMinimumSize().width();
230 int HeaderPainter::GetRightInset() const {
231 return caption_button_container_
->GetPreferredSize().width();
234 int HeaderPainter::GetThemeBackgroundXInset() const {
235 return kThemeFrameImageInsetX
;
238 void HeaderPainter::PaintHeader(gfx::Canvas
* canvas
,
240 int theme_frame_overlay_id
) {
241 bool initial_paint
= (previous_theme_frame_id_
== 0);
242 if (!initial_paint
&&
243 (previous_theme_frame_id_
!= theme_frame_id
||
244 previous_theme_frame_overlay_id_
!= theme_frame_overlay_id
)) {
245 aura::Window
* parent
= frame_
->GetNativeWindow()->parent();
246 // Don't animate the header if the parent (a workspace) is already
247 // animating. Doing so results in continually painting during the animation
248 // and gives a slower frame rate.
249 // TODO(sky): expose a better way to determine this rather than assuming
250 // the parent is a workspace.
251 bool parent_animating
= parent
&&
252 (parent
->layer()->GetAnimator()->IsAnimatingProperty(
253 ui::LayerAnimationElement::OPACITY
) ||
254 parent
->layer()->GetAnimator()->IsAnimatingProperty(
255 ui::LayerAnimationElement::VISIBILITY
));
256 if (!parent_animating
) {
257 crossfade_animation_
.reset(new gfx::SlideAnimation(this));
258 crossfade_theme_frame_id_
= previous_theme_frame_id_
;
259 crossfade_theme_frame_overlay_id_
= previous_theme_frame_overlay_id_
;
260 crossfade_animation_
->SetSlideDuration(kActivationCrossfadeDurationMs
);
261 crossfade_animation_
->Show();
263 crossfade_animation_
.reset();
267 ui::ThemeProvider
* theme_provider
= frame_
->GetThemeProvider();
268 gfx::ImageSkia
* theme_frame
= theme_provider
->GetImageSkiaNamed(
270 gfx::ImageSkia
* theme_frame_overlay
= NULL
;
271 if (theme_frame_overlay_id
!= 0) {
272 theme_frame_overlay
= theme_provider
->GetImageSkiaNamed(
273 theme_frame_overlay_id
);
276 int corner_radius
= GetHeaderCornerRadius();
279 if (crossfade_animation_
.get() && crossfade_animation_
->is_animating()) {
280 gfx::ImageSkia
* crossfade_theme_frame
=
281 theme_provider
->GetImageSkiaNamed(crossfade_theme_frame_id_
);
282 gfx::ImageSkia
* crossfade_theme_frame_overlay
= NULL
;
283 if (crossfade_theme_frame_overlay_id_
!= 0) {
284 crossfade_theme_frame_overlay
= theme_provider
->GetImageSkiaNamed(
285 crossfade_theme_frame_overlay_id_
);
287 if (!crossfade_theme_frame
||
288 (crossfade_theme_frame_overlay_id_
!= 0 &&
289 !crossfade_theme_frame_overlay
)) {
290 // Reset the animation. This case occurs when the user switches the theme
291 // that they are using.
292 crossfade_animation_
.reset();
294 int old_alpha
= crossfade_animation_
->CurrentValueBetween(255, 0);
295 int new_alpha
= 255 - old_alpha
;
297 // Draw the old header background, clipping the corners to be rounded.
298 paint
.setAlpha(old_alpha
);
299 paint
.setXfermodeMode(SkXfermode::kPlus_Mode
);
300 PaintFrameImagesInRoundRect(canvas
,
301 crossfade_theme_frame
,
302 crossfade_theme_frame_overlay
,
304 GetHeaderLocalBounds(),
306 GetThemeBackgroundXInset());
308 paint
.setAlpha(new_alpha
);
312 // Draw the header background, clipping the corners to be rounded.
313 PaintFrameImagesInRoundRect(canvas
,
317 GetHeaderLocalBounds(),
319 GetThemeBackgroundXInset());
321 previous_theme_frame_id_
= theme_frame_id
;
322 previous_theme_frame_overlay_id_
= theme_frame_overlay_id
;
324 // We don't need the extra lightness in the edges when the window is maximized
326 if (frame_
->IsMaximized() || frame_
->IsFullscreen())
329 // Draw the top corners and edge.
330 int top_left_width
= top_left_corner_
->width();
331 int top_left_height
= top_left_corner_
->height();
332 canvas
->DrawImageInt(*top_left_corner_
,
333 0, 0, top_left_width
, top_left_height
,
334 0, 0, top_left_width
, top_left_height
,
336 canvas
->TileImageInt(*top_edge_
,
339 header_view_
->width() - top_left_width
- top_right_corner_
->width(),
340 top_edge_
->height());
341 int top_right_height
= top_right_corner_
->height();
342 canvas
->DrawImageInt(*top_right_corner_
,
344 top_right_corner_
->width(), top_right_height
,
345 header_view_
->width() - top_right_corner_
->width(), 0,
346 top_right_corner_
->width(), top_right_height
,
350 int header_left_height
= theme_frame
->height() - top_left_height
;
351 canvas
->TileImageInt(*header_left_edge_
,
353 header_left_edge_
->width(), header_left_height
);
355 // Header right edge.
356 int header_right_height
= theme_frame
->height() - top_right_height
;
357 canvas
->TileImageInt(*header_right_edge_
,
358 header_view_
->width() - header_right_edge_
->width(),
360 header_right_edge_
->width(),
361 header_right_height
);
363 // We don't draw edges around the content area. Web content goes flush
364 // to the edge of the window.
367 void HeaderPainter::PaintHeaderContentSeparator(gfx::Canvas
* canvas
) {
368 canvas
->FillRect(gfx::Rect(0,
369 header_height_
- kHeaderContentSeparatorSize
,
370 header_view_
->width(),
371 kHeaderContentSeparatorSize
),
372 kHeaderContentSeparatorColor
);
375 int HeaderPainter::HeaderContentSeparatorSize() const {
376 return kHeaderContentSeparatorSize
;
379 void HeaderPainter::PaintTitleBar(gfx::Canvas
* canvas
,
380 const gfx::Font
& title_font
) {
381 // The window icon is painted by its own views::View.
382 views::WidgetDelegate
* delegate
= frame_
->widget_delegate();
383 if (delegate
&& delegate
->ShouldShowWindowTitle()) {
384 gfx::Rect title_bounds
= GetTitleBounds(title_font
);
385 SkColor title_color
= (frame_
->IsMaximized() || frame_
->IsFullscreen()) ?
386 kMaximizedWindowTitleTextColor
: kNonMaximizedWindowTitleTextColor
;
387 canvas
->DrawStringInt(delegate
->GetWindowTitle(),
390 header_view_
->GetMirroredXForRect(title_bounds
),
392 title_bounds
.width(),
393 title_bounds
.height(),
394 gfx::Canvas::NO_SUBPIXEL_RENDERING
);
398 void HeaderPainter::LayoutHeader(bool shorter_layout
) {
399 caption_button_container_
->set_header_style(shorter_layout
?
400 FrameCaptionButtonContainerView::HEADER_STYLE_SHORT
:
401 FrameCaptionButtonContainerView::HEADER_STYLE_TALL
);
402 caption_button_container_
->Layout();
404 gfx::Size caption_button_container_size
=
405 caption_button_container_
->GetPreferredSize();
406 caption_button_container_
->SetBounds(
407 header_view_
->width() - caption_button_container_size
.width(),
409 caption_button_container_size
.width(),
410 caption_button_container_size
.height());
413 // Vertically center the window icon with respect to the caption button
416 GetCaptionButtonContainerCenterY() - window_icon_
->height() / 2;
417 window_icon_
->SetBounds(kIconOffsetX
, icon_offset_y
, kIconSize
, kIconSize
);
421 void HeaderPainter::SchedulePaintForTitle(const gfx::Font
& title_font
) {
422 header_view_
->SchedulePaintInRect(GetTitleBounds(title_font
));
425 void HeaderPainter::OnThemeChanged() {
426 // We do not cache the images for |previous_theme_frame_id_| and
427 // |previous_theme_frame_overlay_id_|. Changing the theme changes the images
428 // returned from ui::ThemeProvider for |previous_theme_frame_id_|
429 // and |previous_theme_frame_overlay_id_|. Reset the image ids to prevent
430 // starting a crossfade animation with these images.
431 previous_theme_frame_id_
= 0;
432 previous_theme_frame_overlay_id_
= 0;
434 if (crossfade_animation_
.get() && crossfade_animation_
->is_animating()) {
435 crossfade_animation_
.reset();
436 header_view_
->SchedulePaintInRect(GetHeaderLocalBounds());
440 ///////////////////////////////////////////////////////////////////////////////
441 // gfx::AnimationDelegate overrides:
443 void HeaderPainter::AnimationProgressed(const gfx::Animation
* animation
) {
444 header_view_
->SchedulePaintInRect(GetHeaderLocalBounds());
447 ///////////////////////////////////////////////////////////////////////////////
448 // HeaderPainter, private:
450 gfx::Rect
HeaderPainter::GetHeaderLocalBounds() const {
451 return gfx::Rect(header_view_
->width(), header_height_
);
454 int HeaderPainter::GetTitleOffsetX() const {
455 return window_icon_
?
456 window_icon_
->bounds().right() + kTitleIconOffsetX
:
460 int HeaderPainter::GetCaptionButtonContainerCenterY() const {
461 return caption_button_container_
->y() +
462 caption_button_container_
->height() / 2;
465 int HeaderPainter::GetHeaderCornerRadius() const {
466 bool square_corners
= (frame_
->IsMaximized() || frame_
->IsFullscreen());
467 const int kCornerRadius
= 2;
468 return square_corners
? 0 : kCornerRadius
;
471 gfx::Rect
HeaderPainter::GetTitleBounds(const gfx::Font
& title_font
) {
472 int title_x
= GetTitleOffsetX();
473 // Center the text with respect to the caption button container. This way it
474 // adapts to the caption button height and aligns exactly with the window
475 // icon. Don't use |window_icon_| for this computation as it may be NULL.
476 int title_y
= GetCaptionButtonContainerCenterY() - title_font
.GetHeight() / 2;
479 std::max(0, title_y
),
480 std::max(0, caption_button_container_
->x() - kTitleLogoSpacing
- title_x
),
481 title_font
.GetHeight());