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/frame/header_painter_util.h"
6
7 #include <algorithm>
8
9 #include "ui/aura/window.h"
10 #include "ui/compositor/layer.h"
11 #include "ui/compositor/layer_animator.h"
12 #include "ui/gfx/font_list.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/views/view.h"
15 #include "ui/views/widget/widget.h"
16
17 namespace {
18
19 // Radius of the header's top corners when the window is restored.
20 const int kTopCornerRadiusWhenRestored = 2;
21
22 // Distance between left edge of the window and the header icon.
23 const int kIconXOffset = 9;
24
25 // Default height and width of header icon.
26 const int kDefaultIconSize = 16;
27
28 // Space between the title text and the caption buttons.
29 const int kTitleCaptionButtonSpacing = 5;
30
31 // Space between window icon and title text.
32 const int kTitleIconOffsetX = 5;
33
34 // Space between window edge and title text, when there is no icon.
35 const int kTitleNoIconOffsetX = 8;
36
37 // In the pre-Ash era the web content area had a frame along the left edge, so
38 // user-generated theme images for the new tab page assume they are shifted
39 // right relative to the header. Now that we have removed the left edge frame
40 // we need to copy the theme image for the window header from a few pixels
41 // inset to preserve alignment with the NTP image, or else we'll break a bunch
42 // of existing themes. We do something similar on OS X for the same reason.
43 const int kThemeFrameImageInsetX = 5;
44
45 } // namespace
46
47 namespace ash {
48
49 // static
GetTopCornerRadiusWhenRestored()50 int HeaderPainterUtil::GetTopCornerRadiusWhenRestored() {
51 return kTopCornerRadiusWhenRestored;
52 }
53
54 // static
GetIconXOffset()55 int HeaderPainterUtil::GetIconXOffset() {
56 return kIconXOffset;
57 }
58
59 // static
GetDefaultIconSize()60 int HeaderPainterUtil::GetDefaultIconSize() {
61 return kDefaultIconSize;
62 }
63
64 // static
GetThemeBackgroundXInset()65 int HeaderPainterUtil::GetThemeBackgroundXInset() {
66 return kThemeFrameImageInsetX;
67 }
68
69 // static
GetTitleBounds(const views::View * icon,const views::View * caption_button_container,const gfx::FontList & title_font_list)70 gfx::Rect HeaderPainterUtil::GetTitleBounds(
71 const views::View* icon,
72 const views::View* caption_button_container,
73 const gfx::FontList& title_font_list) {
74 int x = icon ?
75 icon->bounds().right() + kTitleIconOffsetX : kTitleNoIconOffsetX;
76 int height = title_font_list.GetHeight();
77 // Floor when computing the center of |caption_button_container| and when
78 // computing the center of the text.
79 int y = std::max(0, (caption_button_container->height() / 2) - (height / 2));
80 int width = std::max(
81 0, caption_button_container->x() - kTitleCaptionButtonSpacing - x);
82 return gfx::Rect(x, y, width, height);
83 }
84
85 // static
CanAnimateActivation(views::Widget * widget)86 bool HeaderPainterUtil::CanAnimateActivation(views::Widget* widget) {
87 // Do not animate the header if the parent (e.g.
88 // kShellWindowId_DefaultContainer) is already animating. All of the
89 // implementers of HeaderPainter animate activation by continuously painting
90 // during the animation. This gives the parent's animation a slower frame
91 // rate.
92 // TODO(sky): Expose a better way to determine this rather than assuming the
93 // parent is a toplevel container.
94 aura::Window* window = widget->GetNativeWindow();
95 if (!window->parent())
96 return true;
97
98 ui::LayerAnimator* parent_layer_animator =
99 window->parent()->layer()->GetAnimator();
100 return !parent_layer_animator->IsAnimatingProperty(
101 ui::LayerAnimationElement::OPACITY) &&
102 !parent_layer_animator->IsAnimatingProperty(
103 ui::LayerAnimationElement::VISIBILITY);
104 }
105
106 } // namespace ash
107