Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: ash/wm/default_header_painter.cc

Issue 189463013: [Refactor] Move code for painting the window header for browser windows out of ash (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/wm/header_painter.h" 5 #include "ash/wm/default_header_painter.h"
6
7 #include <vector>
8 6
9 #include "ash/wm/caption_buttons/frame_caption_button_container_view.h" 7 #include "ash/wm/caption_buttons/frame_caption_button_container_view.h"
8 #include "ash/wm/header_metrics.h"
9 #include "base/debug/leak_annotations.h"
10 #include "base/logging.h" // DCHECK 10 #include "base/logging.h" // DCHECK
11 #include "grit/ash_resources.h" 11 #include "grit/ash_resources.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "third_party/skia/include/core/SkColor.h" 12 #include "third_party/skia/include/core/SkColor.h"
14 #include "third_party/skia/include/core/SkPaint.h" 13 #include "third_party/skia/include/core/SkPaint.h"
15 #include "third_party/skia/include/core/SkPath.h" 14 #include "third_party/skia/include/core/SkPath.h"
16 #include "ui/aura/window.h" 15 #include "ui/aura/window.h"
17 #include "ui/base/hit_test.h"
18 #include "ui/base/resource/resource_bundle.h" 16 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/theme_provider.h"
20 #include "ui/gfx/animation/slide_animation.h" 17 #include "ui/gfx/animation/slide_animation.h"
21 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/font_list.h" 19 #include "ui/gfx/font_list.h"
23 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/skia_util.h" 21 #include "ui/gfx/skia_util.h"
22 #include "ui/views/widget/native_widget_aura.h"
25 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
26 #include "ui/views/widget/widget_delegate.h" 24 #include "ui/views/widget/widget_delegate.h"
27 25
28 using aura::Window;
29 using views::Widget; 26 using views::Widget;
30 27
31 namespace { 28 namespace {
32 // Space between left edge of window and popup window icon. 29
33 const int kIconOffsetX = 9; 30 // Color for the window title text.
34 // Height and width of window icon. 31 const SkColor kTitleTextColor = SkColorSetRGB(40, 40, 40);
35 const int kIconSize = 16;
36 // Space between the title text and the caption buttons.
37 const int kTitleLogoSpacing = 5;
38 // Space between window icon and title text.
39 const int kTitleIconOffsetX = 5;
40 // Space between window edge and title text, when there is no icon.
41 const int kTitleNoIconOffsetX = 8;
42 // Color for the non-maximized window title text.
43 const SkColor kNonMaximizedWindowTitleTextColor = SkColorSetRGB(40, 40, 40);
44 // Color for the maximized window title text.
45 const SkColor kMaximizedWindowTitleTextColor = SK_ColorWHITE;
46 // Size of header/content separator line below the header image for non-browser 32 // Size of header/content separator line below the header image for non-browser
47 // windows. 33 // windows.
48 const int kHeaderContentSeparatorSize = 1; 34 const int kHeaderContentSeparatorSize = 1;
49 // Color of the active window header/content separator line for non-browser 35 // Color of the active window header/content separator line for non-browser
50 // windows. 36 // windows.
51 const SkColor kHeaderContentSeparatorColor = SkColorSetRGB(180, 180, 182); 37 const SkColor kHeaderContentSeparatorColor = SkColorSetRGB(180, 180, 182);
52 // Color of the inactive window header/content separator line for non-browser 38 // Color of the inactive window header/content separator line for non-browser
53 // windows. 39 // windows.
54 const SkColor kHeaderContentSeparatorInactiveColor = 40 const SkColor kHeaderContentSeparatorInactiveColor =
55 SkColorSetRGB(150, 150, 152); 41 SkColorSetRGB(150, 150, 152);
56 // In the pre-Ash era the web content area had a frame along the left edge, so
57 // user-generated theme images for the new tab page assume they are shifted
58 // right relative to the header. Now that we have removed the left edge frame
59 // we need to copy the theme image for the window header from a few pixels
60 // inset to preserve alignment with the NTP image, or else we'll break a bunch
61 // of existing themes. We do something similar on OS X for the same reason.
62 const int kThemeFrameImageInsetX = 5;
63 // Duration of crossfade animation for activating and deactivating frame. 42 // Duration of crossfade animation for activating and deactivating frame.
64 const int kActivationCrossfadeDurationMs = 200; 43 const int kActivationCrossfadeDurationMs = 200;
65 44
66 // Tiles an image into an area, rounding the top corners. Samples |image| 45 // Tiles an image into an area, rounding the top corners.
67 // starting |image_inset_x| pixels from the left of the image.
68 void TileRoundRect(gfx::Canvas* canvas, 46 void TileRoundRect(gfx::Canvas* canvas,
69 const gfx::ImageSkia& image, 47 const gfx::ImageSkia& image,
70 const SkPaint& paint, 48 const SkPaint& paint,
71 const gfx::Rect& bounds, 49 const gfx::Rect& bounds,
72 int top_left_corner_radius, 50 int corner_radius) {
73 int top_right_corner_radius,
74 int image_inset_x) {
75 SkRect rect = gfx::RectToSkRect(bounds); 51 SkRect rect = gfx::RectToSkRect(bounds);
76 const SkScalar kTopLeftRadius = SkIntToScalar(top_left_corner_radius); 52 const SkScalar corner_radius_scalar = SkIntToScalar(corner_radius);
77 const SkScalar kTopRightRadius = SkIntToScalar(top_right_corner_radius);
78 SkScalar radii[8] = { 53 SkScalar radii[8] = {
79 kTopLeftRadius, kTopLeftRadius, // top-left 54 corner_radius_scalar, corner_radius_scalar, // top-left
80 kTopRightRadius, kTopRightRadius, // top-right 55 corner_radius_scalar, corner_radius_scalar, // top-right
81 0, 0, // bottom-right 56 0, 0, // bottom-right
82 0, 0}; // bottom-left 57 0, 0}; // bottom-left
83 SkPath path; 58 SkPath path;
84 path.addRoundRect(rect, radii, SkPath::kCW_Direction); 59 path.addRoundRect(rect, radii, SkPath::kCW_Direction);
85 canvas->DrawImageInPath(image, -image_inset_x, 0, path, paint); 60 canvas->DrawImageInPath(image, 0, 0, path, paint);
86 } 61 }
87 62
88 // Tiles |frame_image| and |frame_overlay_image| into an area, rounding the top 63 // Returns the FontList to use for the title.
89 // corners. 64 const gfx::FontList& GetTitleFontList() {
90 void PaintFrameImagesInRoundRect(gfx::Canvas* canvas, 65 static const gfx::FontList* title_font_list =
91 const gfx::ImageSkia* frame_image, 66 new gfx::FontList(views::NativeWidgetAura::GetWindowTitleFontList());
92 const gfx::ImageSkia* frame_overlay_image, 67 ANNOTATE_LEAKING_OBJECT_PTR(title_font_list);
93 const SkPaint& paint, 68 return *title_font_list;
94 const gfx::Rect& bounds,
95 int corner_radius,
96 int image_inset_x) {
97 SkXfermode::Mode normal_mode;
98 SkXfermode::AsMode(NULL, &normal_mode);
99
100 // If |paint| is using an unusual SkXfermode::Mode (this is the case while
101 // crossfading), we must create a new canvas to overlay |frame_image| and
102 // |frame_overlay_image| using |normal_mode| and then paint the result
103 // using the unusual mode. We try to avoid this because creating a new
104 // browser-width canvas is expensive.
105 bool fast_path = (!frame_overlay_image ||
106 SkXfermode::IsMode(paint.getXfermode(), normal_mode));
107 if (fast_path) {
108 TileRoundRect(canvas, *frame_image, paint, bounds, corner_radius,
109 corner_radius, image_inset_x);
110
111 if (frame_overlay_image) {
112 // Adjust |bounds| such that |frame_overlay_image| is not tiled.
113 gfx::Rect overlay_bounds = bounds;
114 overlay_bounds.Intersect(
115 gfx::Rect(bounds.origin(), frame_overlay_image->size()));
116 int top_left_corner_radius = corner_radius;
117 int top_right_corner_radius = corner_radius;
118 if (overlay_bounds.width() < bounds.width() - corner_radius)
119 top_right_corner_radius = 0;
120 TileRoundRect(canvas, *frame_overlay_image, paint, overlay_bounds,
121 top_left_corner_radius, top_right_corner_radius, 0);
122 }
123 } else {
124 gfx::Canvas temporary_canvas(bounds.size(), canvas->image_scale(), false);
125 temporary_canvas.TileImageInt(*frame_image,
126 image_inset_x, 0,
127 0, 0,
128 bounds.width(), bounds.height());
129 temporary_canvas.DrawImageInt(*frame_overlay_image, 0, 0);
130 TileRoundRect(canvas, gfx::ImageSkia(temporary_canvas.ExtractImageRep()),
131 paint, bounds, corner_radius, corner_radius, 0);
132 }
133 } 69 }
134 70
135 } // namespace 71 } // namespace
136 72
137 namespace ash { 73 namespace ash {
138 74
139 /////////////////////////////////////////////////////////////////////////////// 75 ///////////////////////////////////////////////////////////////////////////////
140 // HeaderPainter, public: 76 // HeaderPainter, public:
141 77
142 HeaderPainter::HeaderPainter() 78 DefaultHeaderPainter::DefaultHeaderPainter()
143 : frame_(NULL), 79 : frame_(NULL),
144 header_view_(NULL), 80 view_(NULL),
145 window_icon_(NULL), 81 window_icon_(NULL),
146 caption_button_container_(NULL), 82 caption_button_container_(NULL),
147 header_height_(0), 83 height_(0),
148 previous_theme_frame_id_(0), 84 mode_(MODE_INACTIVE),
149 previous_theme_frame_overlay_id_(0), 85 initial_paint_(true),
150 crossfade_theme_frame_id_(0), 86 activation_animation_(new gfx::SlideAnimation(this)) {
151 crossfade_theme_frame_overlay_id_(0) {}
152
153 HeaderPainter::~HeaderPainter() {
154 } 87 }
155 88
156 void HeaderPainter::Init( 89 DefaultHeaderPainter::~DefaultHeaderPainter() {
157 Style style, 90 }
91
92 void DefaultHeaderPainter::Init(
158 views::Widget* frame, 93 views::Widget* frame,
159 views::View* header_view, 94 views::View* header_view,
160 views::View* window_icon, 95 views::View* window_icon,
161 FrameCaptionButtonContainerView* caption_button_container) { 96 FrameCaptionButtonContainerView* caption_button_container) {
162 DCHECK(frame); 97 DCHECK(frame);
163 DCHECK(header_view); 98 DCHECK(header_view);
164 // window_icon may be NULL. 99 // window_icon may be NULL.
165 DCHECK(caption_button_container); 100 DCHECK(caption_button_container);
166 style_ = style;
167 frame_ = frame; 101 frame_ = frame;
168 header_view_ = header_view; 102 view_ = header_view;
169 window_icon_ = window_icon; 103 window_icon_ = window_icon;
170 caption_button_container_ = caption_button_container; 104 caption_button_container_ = caption_button_container;
105
106 caption_button_container_->SetButtonImages(
107 CAPTION_BUTTON_ICON_MINIMIZE,
108 IDR_AURA_WINDOW_CONTROL_ICON_MINIMIZE,
109 IDR_AURA_WINDOW_CONTROL_ICON_MINIMIZE_I,
110 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
111 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
112 caption_button_container_->SetButtonImages(
113 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE,
114 IDR_AURA_WINDOW_CONTROL_ICON_SIZE,
115 IDR_AURA_WINDOW_CONTROL_ICON_SIZE_I,
116 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
117 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
118 caption_button_container_->SetButtonImages(
119 CAPTION_BUTTON_ICON_CLOSE,
120 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE,
121 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE_I,
122 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
123 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
124
125 // There is no dedicated icon for the snap-left and snap-right buttons
126 // when |frame_| is inactive because they should never be visible while
127 // |frame_| is inactive.
128 caption_button_container_->SetButtonImages(
129 CAPTION_BUTTON_ICON_LEFT_SNAPPED,
130 IDR_AURA_WINDOW_CONTROL_ICON_LEFT_SNAPPED,
131 IDR_AURA_WINDOW_CONTROL_ICON_LEFT_SNAPPED,
132 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
133 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
134 caption_button_container_->SetButtonImages(
135 CAPTION_BUTTON_ICON_RIGHT_SNAPPED,
136 IDR_AURA_WINDOW_CONTROL_ICON_RIGHT_SNAPPED,
137 IDR_AURA_WINDOW_CONTROL_ICON_RIGHT_SNAPPED,
138 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
139 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
171 } 140 }
172 141
173 // static 142 int DefaultHeaderPainter::GetMinimumHeaderWidth() const {
174 gfx::Rect HeaderPainter::GetBoundsForClientView(
175 int header_height,
176 const gfx::Rect& window_bounds) {
177 gfx::Rect client_bounds(window_bounds);
178 client_bounds.Inset(0, header_height, 0, 0);
179 return client_bounds;
180 }
181
182 // static
183 gfx::Rect HeaderPainter::GetWindowBoundsForClientBounds(
184 int header_height,
185 const gfx::Rect& client_bounds) {
186 gfx::Rect window_bounds(client_bounds);
187 window_bounds.Inset(0, -header_height, 0, 0);
188 if (window_bounds.y() < 0)
189 window_bounds.set_y(0);
190 return window_bounds;
191 }
192
193 int HeaderPainter::NonClientHitTest(const gfx::Point& point) const {
194 gfx::Point point_in_header_view(point);
195 views::View::ConvertPointFromWidget(header_view_, &point_in_header_view);
196 if (!GetHeaderLocalBounds().Contains(point_in_header_view))
197 return HTNOWHERE;
198 if (caption_button_container_->visible()) {
199 gfx::Point point_in_caption_button_container(point);
200 views::View::ConvertPointFromWidget(caption_button_container_,
201 &point_in_caption_button_container);
202 int component = caption_button_container_->NonClientHitTest(
203 point_in_caption_button_container);
204 if (component != HTNOWHERE)
205 return component;
206 }
207 // Caption is a safe default.
208 return HTCAPTION;
209 }
210
211 int HeaderPainter::GetMinimumHeaderWidth() const {
212 // Ensure we have enough space for the window icon and buttons. We allow 143 // Ensure we have enough space for the window icon and buttons. We allow
213 // the title string to collapse to zero width. 144 // the title string to collapse to zero width.
214 return GetTitleOffsetX() + 145 return GetTitleBounds().x() +
215 caption_button_container_->GetMinimumSize().width(); 146 caption_button_container_->GetMinimumSize().width();
216 } 147 }
217 148
218 int HeaderPainter::GetRightInset() const { 149 void DefaultHeaderPainter::PaintHeader(gfx::Canvas* canvas, Mode mode) {
219 return caption_button_container_->GetPreferredSize().width(); 150 Mode old_mode = mode_;
220 } 151 mode_ = mode;
221 152
222 int HeaderPainter::GetThemeBackgroundXInset() const { 153 if (mode_ != old_mode) {
223 return kThemeFrameImageInsetX;
224 }
225
226 void HeaderPainter::PaintHeader(gfx::Canvas* canvas,
227 Mode mode,
228 int theme_frame_id,
229 int theme_frame_overlay_id) {
230 bool initial_paint = (previous_theme_frame_id_ == 0);
231 if (!initial_paint &&
232 (previous_theme_frame_id_ != theme_frame_id ||
233 previous_theme_frame_overlay_id_ != theme_frame_overlay_id)) {
234 aura::Window* parent = frame_->GetNativeWindow()->parent(); 154 aura::Window* parent = frame_->GetNativeWindow()->parent();
235 // Don't animate the header if the parent (a workspace) is already 155 // Don't animate the header if the parent
236 // animating. Doing so results in continually painting during the animation 156 // (e.g. kShellWindowId_DefaultContainer) is already animating. Doing so
237 // and gives a slower frame rate. 157 // results in continually painting during the animation and gives a
158 // slower frame rate.
238 // TODO(sky): expose a better way to determine this rather than assuming 159 // TODO(sky): expose a better way to determine this rather than assuming
239 // the parent is a workspace. 160 // the parent is a toplevel container.
240 bool parent_animating = parent && 161 bool parent_animating = parent &&
241 (parent->layer()->GetAnimator()->IsAnimatingProperty( 162 (parent->layer()->GetAnimator()->IsAnimatingProperty(
242 ui::LayerAnimationElement::OPACITY) || 163 ui::LayerAnimationElement::OPACITY) ||
243 parent->layer()->GetAnimator()->IsAnimatingProperty( 164 parent->layer()->GetAnimator()->IsAnimatingProperty(
244 ui::LayerAnimationElement::VISIBILITY)); 165 ui::LayerAnimationElement::VISIBILITY));
245 if (!parent_animating) { 166
246 crossfade_animation_.reset(new gfx::SlideAnimation(this)); 167 if (initial_paint_ || parent_animating) {
James Cook 2014/03/10 17:32:30 I'm a little confused about the usage of initial_p
pkotwicz 2014/03/14 17:56:22 It is possible for session restore to restore > 1
247 crossfade_theme_frame_id_ = previous_theme_frame_id_; 168 initial_paint_ = false;
248 crossfade_theme_frame_overlay_id_ = previous_theme_frame_overlay_id_; 169 if (mode_ == MODE_ACTIVE)
249 crossfade_animation_->SetSlideDuration(kActivationCrossfadeDurationMs); 170 activation_animation_->Reset(1);
250 crossfade_animation_->Show(); 171 else
172 activation_animation_->Reset(0);
251 } else { 173 } else {
252 crossfade_animation_.reset(); 174 activation_animation_->SetSlideDuration(kActivationCrossfadeDurationMs);
175 if (mode_ == MODE_ACTIVE)
176 activation_animation_->Show();
177 else
178 activation_animation_->Hide();
253 } 179 }
254 } 180 }
255 181
256 ui::ThemeProvider* theme_provider = frame_->GetThemeProvider(); 182 int corner_radius = (frame_->IsMaximized() || frame_->IsFullscreen()) ?
257 gfx::ImageSkia* theme_frame = theme_provider->GetImageSkiaNamed( 183 0 : HeaderMetrics::GetTopCornerRadiusWhenRestored();
258 theme_frame_id); 184
259 gfx::ImageSkia* theme_frame_overlay = NULL; 185 int active_alpha = activation_animation_->CurrentValueBetween(0, 255);
260 if (theme_frame_overlay_id != 0) { 186 int inactive_alpha = 255 - active_alpha;
261 theme_frame_overlay = theme_provider->GetImageSkiaNamed( 187
262 theme_frame_overlay_id); 188 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
189 SkPaint paint;
190 if (inactive_alpha > 0) {
191 if (active_alpha > 0)
192 paint.setXfermodeMode(SkXfermode::kPlus_Mode);
193
194 paint.setAlpha(inactive_alpha);
195 gfx::ImageSkia inactive_frame =
196 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_BASE_INACTIVE);
197 TileRoundRect(canvas, inactive_frame, paint, GetLocalBounds(),
198 corner_radius);
263 } 199 }
264 200
265 int corner_radius = GetHeaderCornerRadius(); 201 if (active_alpha > 0) {
266 SkPaint paint; 202 paint.setAlpha(active_alpha);
267 203 gfx::ImageSkia active_frame =
268 if (crossfade_animation_.get() && crossfade_animation_->is_animating()) { 204 *rb.GetImageSkiaNamed(IDR_AURA_WINDOW_HEADER_BASE_ACTIVE);
269 gfx::ImageSkia* crossfade_theme_frame = 205 TileRoundRect(canvas, active_frame, paint, GetLocalBounds(),
270 theme_provider->GetImageSkiaNamed(crossfade_theme_frame_id_); 206 corner_radius);
271 gfx::ImageSkia* crossfade_theme_frame_overlay = NULL;
272 if (crossfade_theme_frame_overlay_id_ != 0) {
273 crossfade_theme_frame_overlay = theme_provider->GetImageSkiaNamed(
274 crossfade_theme_frame_overlay_id_);
275 }
276 if (!crossfade_theme_frame ||
277 (crossfade_theme_frame_overlay_id_ != 0 &&
278 !crossfade_theme_frame_overlay)) {
279 // Reset the animation. This case occurs when the user switches the theme
280 // that they are using.
281 crossfade_animation_.reset();
282 } else {
283 int old_alpha = crossfade_animation_->CurrentValueBetween(255, 0);
284 int new_alpha = 255 - old_alpha;
285
286 // Draw the old header background, clipping the corners to be rounded.
287 paint.setAlpha(old_alpha);
288 paint.setXfermodeMode(SkXfermode::kPlus_Mode);
289 PaintFrameImagesInRoundRect(canvas,
290 crossfade_theme_frame,
291 crossfade_theme_frame_overlay,
292 paint,
293 GetHeaderLocalBounds(),
294 corner_radius,
295 GetThemeBackgroundXInset());
296
297 paint.setAlpha(new_alpha);
298 }
299 } 207 }
300 208
301 // Draw the header background, clipping the corners to be rounded. 209 if (!frame_->IsMaximized() &&
302 PaintFrameImagesInRoundRect(canvas, 210 !frame_->IsFullscreen() &&
303 theme_frame, 211 mode_ == MODE_INACTIVE) {
304 theme_frame_overlay, 212 PaintBorderForInactiveRestoredWindow(canvas);
305 paint, 213 }
306 GetHeaderLocalBounds(), 214 if (frame_->widget_delegate() &&
307 corner_radius, 215 frame_->widget_delegate()->ShouldShowWindowTitle()) {
308 GetThemeBackgroundXInset()); 216 PaintTitleBar(canvas);
309 217 }
310 previous_theme_frame_id_ = theme_frame_id; 218 PaintHeaderContentSeparator(canvas);
311 previous_theme_frame_overlay_id_ = theme_frame_overlay_id;
312
313 PaintBorder(canvas, mode);
314 } 219 }
315 220
316 void HeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas, 221 void DefaultHeaderPainter::LayoutHeader() {
317 Mode mode) {
318 DCHECK_EQ(style_, STYLE_OTHER);
319 SkColor color = (mode == MODE_ACTIVE) ?
320 kHeaderContentSeparatorColor :
321 kHeaderContentSeparatorInactiveColor;
322
323 canvas->FillRect(gfx::Rect(0,
324 header_height_ - kHeaderContentSeparatorSize,
325 header_view_->width(),
326 kHeaderContentSeparatorSize),
327 color);
328 }
329
330 int HeaderPainter::HeaderContentSeparatorSize() const {
331 return kHeaderContentSeparatorSize;
332 }
333
334 void HeaderPainter::PaintTitleBar(gfx::Canvas* canvas,
335 const gfx::FontList& title_font_list) {
336 // The window icon is painted by its own views::View.
337 views::WidgetDelegate* delegate = frame_->widget_delegate();
338 if (delegate && delegate->ShouldShowWindowTitle()) {
339 gfx::Rect title_bounds = GetTitleBounds(title_font_list);
340 title_bounds.set_x(header_view_->GetMirroredXForRect(title_bounds));
341 SkColor title_color = (frame_->IsMaximized() || frame_->IsFullscreen()) ?
342 kMaximizedWindowTitleTextColor : kNonMaximizedWindowTitleTextColor;
343 canvas->DrawStringRectWithFlags(delegate->GetWindowTitle(),
344 title_font_list,
345 title_color,
346 title_bounds,
347 gfx::Canvas::NO_SUBPIXEL_RENDERING);
348 }
349 }
350
351 void HeaderPainter::LayoutHeader() {
352 // Purposefully set |header_height_| to an invalid value. We cannot use
353 // |header_height_| because the computation of |header_height_| may depend
354 // on having laid out the window controls.
355 header_height_ = -1;
356
357 UpdateCaptionButtonImages();
358 caption_button_container_->Layout(); 222 caption_button_container_->Layout();
359 223
360 gfx::Size caption_button_container_size = 224 gfx::Size caption_button_container_size =
361 caption_button_container_->GetPreferredSize(); 225 caption_button_container_->GetPreferredSize();
362 caption_button_container_->SetBounds( 226 caption_button_container_->SetBounds(
363 header_view_->width() - caption_button_container_size.width(), 227 view_->width() - caption_button_container_size.width(),
364 0, 228 0,
365 caption_button_container_size.width(), 229 caption_button_container_size.width(),
366 caption_button_container_size.height()); 230 caption_button_container_size.height());
367 231
368 if (window_icon_) { 232 if (window_icon_) {
369 // Vertically center the window icon with respect to the caption button 233 // Vertically center the window icon with respect to the caption button
370 // container. 234 // container.
371 int icon_offset_y = 235 int icon_size = HeaderMetrics::GetIconSize();
372 GetCaptionButtonContainerCenterY() - window_icon_->height() / 2; 236 int icon_offset_y = caption_button_container_->y() +
373 window_icon_->SetBounds(kIconOffsetX, icon_offset_y, kIconSize, kIconSize); 237 (caption_button_container_->height() - icon_size) / 2;
238 window_icon_->SetBounds(HeaderMetrics::GetIconXOffset(), icon_offset_y,
239 icon_size, icon_size);
374 } 240 }
241
242 SetHeaderHeightForPainting(caption_button_container_->bounds().bottom() +
243 kHeaderContentSeparatorSize);
375 } 244 }
376 245
377 void HeaderPainter::SchedulePaintForTitle( 246 int DefaultHeaderPainter::GetHeaderHeightForPainting() const {
378 const gfx::FontList& title_font_list) { 247 return height_;
379 header_view_->SchedulePaintInRect(GetTitleBounds(title_font_list));
380 } 248 }
381 249
382 void HeaderPainter::OnThemeChanged() { 250 void DefaultHeaderPainter::SetHeaderHeightForPainting(int height) {
383 // We do not cache the images for |previous_theme_frame_id_| and 251 height_ = height;
384 // |previous_theme_frame_overlay_id_|. Changing the theme changes the images 252 }
385 // returned from ui::ThemeProvider for |previous_theme_frame_id_|
386 // and |previous_theme_frame_overlay_id_|. Reset the image ids to prevent
387 // starting a crossfade animation with these images.
388 previous_theme_frame_id_ = 0;
389 previous_theme_frame_overlay_id_ = 0;
390 253
391 if (crossfade_animation_.get() && crossfade_animation_->is_animating()) { 254 void DefaultHeaderPainter::SchedulePaintForTitle() {
392 crossfade_animation_.reset(); 255 view_->SchedulePaintInRect(GetTitleBounds());
393 header_view_->SchedulePaintInRect(GetHeaderLocalBounds());
394 }
395 } 256 }
396 257
397 /////////////////////////////////////////////////////////////////////////////// 258 ///////////////////////////////////////////////////////////////////////////////
398 // gfx::AnimationDelegate overrides: 259 // gfx::AnimationDelegate overrides:
399 260
400 void HeaderPainter::AnimationProgressed(const gfx::Animation* animation) { 261 void DefaultHeaderPainter::AnimationProgressed(
401 header_view_->SchedulePaintInRect(GetHeaderLocalBounds()); 262 const gfx::Animation* animation) {
263 view_->SchedulePaintInRect(GetLocalBounds());
402 } 264 }
403 265
404 /////////////////////////////////////////////////////////////////////////////// 266 ///////////////////////////////////////////////////////////////////////////////
405 // HeaderPainter, private: 267 // DefaultHeaderPainter, private:
406 268
407 void HeaderPainter::PaintBorder(gfx::Canvas* canvas, Mode mode) { 269 void DefaultHeaderPainter::PaintBorderForInactiveRestoredWindow(
408 if (frame_->IsMaximized() || 270 gfx::Canvas* canvas) {
409 frame_->IsFullscreen() || 271 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
410 (style_ == STYLE_OTHER && mode == MODE_ACTIVE)) { 272 gfx::ImageSkia top_edge = *rb.GetImageSkiaNamed(
411 return; 273 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_TOP);
412 } 274 gfx::ImageSkia left_edge = *rb.GetImageSkiaNamed(
275 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_LEFT);
276 gfx::ImageSkia right_edge = *rb.GetImageSkiaNamed(
277 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_RIGHT);
278 gfx::ImageSkia bottom_edge = *rb.GetImageSkiaNamed(
279 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_BOTTOM);
413 280
414 gfx::ImageSkia top_left_corner; 281 int top_height = top_edge.height();
415 gfx::ImageSkia top_right_corner; 282 canvas->TileImageInt(top_edge, 0, 0, view_->width(), top_height);
416 gfx::ImageSkia top_edge;
417 gfx::ImageSkia left_edge;
418 gfx::ImageSkia right_edge;
419 gfx::ImageSkia bottom_edge;
420 283
421 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 284 int bottom = height_ - kHeaderContentSeparatorSize;
422 if (style_ == STYLE_BROWSER) { 285 int bottom_height = bottom_edge.height();
423 top_left_corner = *rb.GetImageSkiaNamed( 286 canvas->TileImageInt(bottom_edge,
424 IDR_AURA_BROWSER_WINDOW_HEADER_SHADE_TOP_LEFT); 287 0, bottom - bottom_height,
425 top_right_corner = *rb.GetImageSkiaNamed( 288 view_->width(), bottom_height);
426 IDR_AURA_BROWSER_WINDOW_HEADER_SHADE_TOP_RIGHT);
427 top_edge = *rb.GetImageSkiaNamed(IDR_AURA_BROWSER_WINDOW_HEADER_SHADE_TOP);
428 left_edge = *rb.GetImageSkiaNamed(
429 IDR_AURA_BROWSER_WINDOW_HEADER_SHADE_LEFT);
430 right_edge = *rb.GetImageSkiaNamed(
431 IDR_AURA_BROWSER_WINDOW_HEADER_SHADE_RIGHT);
432 } else {
433 top_edge = *rb.GetImageSkiaNamed(
434 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_TOP);
435 left_edge = *rb.GetImageSkiaNamed(
436 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_LEFT);
437 right_edge = *rb.GetImageSkiaNamed(
438 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_RIGHT);
439 bottom_edge = *rb.GetImageSkiaNamed(
440 IDR_AURA_WINDOW_HEADER_SHADE_INACTIVE_BOTTOM);
441 }
442 289
443 DCHECK(!top_edge.isNull()); 290 int edge_height = bottom - bottom_height - top_height;
444 DCHECK(!left_edge.isNull());
445 DCHECK(!right_edge.isNull());
446
447 int top_left_width = top_left_corner.width();
448 int top_left_height = top_left_corner.height();
449 if (!top_left_corner.isNull()) {
450 canvas->DrawImageInt(top_left_corner, 0, 0);
451 }
452
453 int top_right_height = top_right_corner.height();
454 if (!top_right_corner.isNull()) {
455 canvas->DrawImageInt(top_right_corner,
456 header_view_->width() - top_right_corner.width(),
457 top_right_height);
458 }
459
460 canvas->TileImageInt(top_edge,
461 top_left_width,
462 0,
463 header_view_->width() - top_left_width - top_right_corner.width(),
464 top_edge.height());
465
466 // TODO(pkotwicz): Compute |bottom| more accurately. The computation is
467 // inaccurate for browser windows.
468 int bottom = header_height_ - kHeaderContentSeparatorSize;
469 int bottom_height = bottom_edge.height();
470 if (!bottom_edge.isNull()) {
471 canvas->TileImageInt(bottom_edge,
472 0, bottom - bottom_height,
473 header_view_->width(), bottom_height);
474 }
475
476 int left_edge_height = bottom - bottom_height - top_left_height;
477 canvas->TileImageInt(left_edge, 291 canvas->TileImageInt(left_edge,
478 0, top_left_height, 292 0, top_height,
479 left_edge.width(), left_edge_height); 293 left_edge.width(), edge_height);
480
481 int right_edge_height = bottom - bottom_height - top_right_height;
482 canvas->TileImageInt(right_edge, 294 canvas->TileImageInt(right_edge,
483 header_view_->width() - right_edge.width(), 295 view_->width() - right_edge.width(),
484 top_right_height, 296 top_height,
485 right_edge.width(), 297 right_edge.width(),
486 right_edge_height); 298 edge_height);
487 } 299 }
488 300
489 void HeaderPainter::UpdateCaptionButtonImages() { 301 void DefaultHeaderPainter::PaintTitleBar(gfx::Canvas* canvas) {
490 if (style_ == STYLE_BROWSER) { 302 // The window icon is painted by its own views::View.
491 if (frame_->IsMaximized() || frame_->IsFullscreen()) { 303 gfx::Rect title_bounds = GetTitleBounds();
492 caption_button_container_->SetButtonImages( 304 title_bounds.set_x(view_->GetMirroredXForRect(title_bounds));
493 CAPTION_BUTTON_ICON_MINIMIZE, 305 canvas->DrawStringRectWithFlags(frame_->widget_delegate()->GetWindowTitle(),
494 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_MINIMIZE, 306 GetTitleFontList(),
495 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_MINIMIZE, 307 kTitleTextColor,
496 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_H, 308 title_bounds,
497 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_P); 309 gfx::Canvas::NO_SUBPIXEL_RENDERING);
498 caption_button_container_->SetButtonImages(
499 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE,
500 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_SIZE,
501 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_SIZE,
502 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_H,
503 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_P);
504 caption_button_container_->SetButtonImages(
505 CAPTION_BUTTON_ICON_CLOSE,
506 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_CLOSE,
507 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_CLOSE,
508 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_H,
509 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_P);
510 caption_button_container_->SetButtonImages(
511 CAPTION_BUTTON_ICON_LEFT_SNAPPED,
512 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_LEFT_SNAPPED,
513 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_LEFT_SNAPPED,
514 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_H,
515 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_P);
516 caption_button_container_->SetButtonImages(
517 CAPTION_BUTTON_ICON_RIGHT_SNAPPED,
518 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_RIGHT_SNAPPED,
519 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_MAXIMIZED_RIGHT_SNAPPED,
520 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_H,
521 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_MAXIMIZED_P);
522 } else {
523 caption_button_container_->SetButtonImages(
524 CAPTION_BUTTON_ICON_MINIMIZE,
525 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_MINIMIZE,
526 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_MINIMIZE,
527 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_H,
528 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_P);
529 caption_button_container_->SetButtonImages(
530 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE,
531 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_SIZE,
532 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_SIZE,
533 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_H,
534 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_P);
535 caption_button_container_->SetButtonImages(
536 CAPTION_BUTTON_ICON_CLOSE,
537 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_CLOSE,
538 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_CLOSE,
539 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_H,
540 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_P);
541 caption_button_container_->SetButtonImages(
542 CAPTION_BUTTON_ICON_LEFT_SNAPPED,
543 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_LEFT_SNAPPED,
544 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_LEFT_SNAPPED,
545 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_H,
546 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_P);
547 caption_button_container_->SetButtonImages(
548 CAPTION_BUTTON_ICON_RIGHT_SNAPPED,
549 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_RIGHT_SNAPPED,
550 IDR_AURA_BROWSER_WINDOW_CONTROL_ICON_RESTORED_RIGHT_SNAPPED,
551 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_H,
552 IDR_AURA_BROWSER_WINDOW_CONTROL_BACKGROUND_RESTORED_P);
553 }
554 } else {
555 caption_button_container_->SetButtonImages(
556 CAPTION_BUTTON_ICON_MINIMIZE,
557 IDR_AURA_WINDOW_CONTROL_ICON_MINIMIZE,
558 IDR_AURA_WINDOW_CONTROL_ICON_MINIMIZE_I,
559 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
560 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
561 caption_button_container_->SetButtonImages(
562 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE,
563 IDR_AURA_WINDOW_CONTROL_ICON_SIZE,
564 IDR_AURA_WINDOW_CONTROL_ICON_SIZE_I,
565 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
566 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
567 caption_button_container_->SetButtonImages(
568 CAPTION_BUTTON_ICON_CLOSE,
569 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE,
570 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE_I,
571 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
572 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
573
574 // There is no dedicated icon for the snap-left and snap-right buttons
575 // when |frame_| is inactive because they should never be visible while
576 // |frame_| is inactive.
577 caption_button_container_->SetButtonImages(
578 CAPTION_BUTTON_ICON_LEFT_SNAPPED,
579 IDR_AURA_WINDOW_CONTROL_ICON_LEFT_SNAPPED,
580 IDR_AURA_WINDOW_CONTROL_ICON_LEFT_SNAPPED,
581 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
582 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
583 caption_button_container_->SetButtonImages(
584 CAPTION_BUTTON_ICON_RIGHT_SNAPPED,
585 IDR_AURA_WINDOW_CONTROL_ICON_RIGHT_SNAPPED,
586 IDR_AURA_WINDOW_CONTROL_ICON_RIGHT_SNAPPED,
587 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
588 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
589 }
590 } 310 }
591 311
592 gfx::Rect HeaderPainter::GetHeaderLocalBounds() const { 312 void DefaultHeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) {
593 return gfx::Rect(header_view_->width(), header_height_); 313 SkColor color = (mode_ == MODE_ACTIVE) ?
314 kHeaderContentSeparatorColor :
315 kHeaderContentSeparatorInactiveColor;
316
317 canvas->FillRect(gfx::Rect(0,
318 height_ - kHeaderContentSeparatorSize,
319 view_->width(),
320 kHeaderContentSeparatorSize),
321 color);
594 } 322 }
595 323
596 int HeaderPainter::GetTitleOffsetX() const { 324 gfx::Rect DefaultHeaderPainter::GetLocalBounds() const {
597 return window_icon_ ? 325 return gfx::Rect(view_->width(), height_);
598 window_icon_->bounds().right() + kTitleIconOffsetX :
599 kTitleNoIconOffsetX;
600 } 326 }
601 327
602 int HeaderPainter::GetCaptionButtonContainerCenterY() const { 328 gfx::Rect DefaultHeaderPainter::GetTitleBounds() const {
603 return caption_button_container_->y() + 329 return HeaderMetrics::GetTitleBounds(
604 caption_button_container_->height() / 2; 330 window_icon_, caption_button_container_, GetTitleFontList());
605 }
606
607 int HeaderPainter::GetHeaderCornerRadius() const {
608 bool square_corners = (frame_->IsMaximized() || frame_->IsFullscreen());
609 const int kCornerRadius = 2;
610 return square_corners ? 0 : kCornerRadius;
611 }
612
613 gfx::Rect HeaderPainter::GetTitleBounds(const gfx::FontList& title_font_list) {
614 int title_x = GetTitleOffsetX();
615 // Center the text with respect to the caption button container. This way it
616 // adapts to the caption button height and aligns exactly with the window
617 // icon. Don't use |window_icon_| for this computation as it may be NULL.
618 int title_y =
619 GetCaptionButtonContainerCenterY() - title_font_list.GetHeight() / 2;
620 return gfx::Rect(
621 title_x,
622 std::max(0, title_y),
623 std::max(0, caption_button_container_->x() - kTitleLogoSpacing - title_x),
624 title_font_list.GetHeight());
625 } 331 }
626 332
627 } // namespace ash 333 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698