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/ash_constants.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/root_window_settings.h"
12 #include "ash/shell.h"
13 #include "ash/shell_window_ids.h"
14 #include "ash/wm/caption_buttons/frame_caption_button_container_view.h"
15 #include "ash/wm/window_state.h"
16 #include "ash/wm/window_util.h"
17 #include "base/logging.h" // DCHECK
18 #include "grit/ash_resources.h"
19 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkColor.h"
21 #include "third_party/skia/include/core/SkPaint.h"
22 #include "third_party/skia/include/core/SkPath.h"
23 #include "ui/aura/client/aura_constants.h"
24 #include "ui/aura/env.h"
25 #include "ui/aura/window.h"
26 #include "ui/base/hit_test.h"
27 #include "ui/base/layout.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/base/theme_provider.h"
30 #include "ui/gfx/animation/slide_animation.h"
31 #include "ui/gfx/canvas.h"
32 #include "ui/gfx/font.h"
33 #include "ui/gfx/image/image.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/skia_util.h"
36 #include "ui/views/widget/widget.h"
37 #include "ui/views/widget/widget_delegate.h"
39 using aura::RootWindow;
44 // Space between left edge of window and popup window icon.
45 const int kIconOffsetX = 9;
46 // Height and width of window icon.
47 const int kIconSize = 16;
48 // Space between the title text and the caption buttons.
49 const int kTitleLogoSpacing = 5;
50 // Space between window icon and title text.
51 const int kTitleIconOffsetX = 5;
52 // Space between window edge and title text, when there is no icon.
53 const int kTitleNoIconOffsetX = 8;
54 // Color for the non-maximized window title text.
55 const SkColor kNonMaximizedWindowTitleTextColor = SkColorSetRGB(40, 40, 40);
56 // Color for the maximized window title text.
57 const SkColor kMaximizedWindowTitleTextColor = SK_ColorWHITE;
58 // Size of header/content separator line below the header image.
59 const int kHeaderContentSeparatorSize = 1;
60 // Color of header bottom edge line.
61 const SkColor kHeaderContentSeparatorColor = SkColorSetRGB(128, 128, 128);
62 // In the pre-Ash era the web content area had a frame along the left edge, so
63 // user-generated theme images for the new tab page assume they are shifted
64 // right relative to the header. Now that we have removed the left edge frame
65 // we need to copy the theme image for the window header from a few pixels
66 // inset to preserve alignment with the NTP image, or else we'll break a bunch
67 // of existing themes. We do something similar on OS X for the same reason.
68 const int kThemeFrameImageInsetX = 5;
69 // Duration of crossfade animation for activating and deactivating frame.
70 const int kActivationCrossfadeDurationMs = 200;
71 // Alpha/opacity value for fully-opaque headers.
72 const int kFullyOpaque = 255;
74 // A flag to enable/disable solo window header.
75 bool solo_window_header_enabled = true;
77 // Tiles an image into an area, rounding the top corners. Samples |image|
78 // starting |image_inset_x| pixels from the left of the image.
79 void TileRoundRect(gfx::Canvas* canvas,
80 const gfx::ImageSkia& image,
82 const gfx::Rect& bounds,
83 int top_left_corner_radius,
84 int top_right_corner_radius,
86 SkRect rect = gfx::RectToSkRect(bounds);
87 const SkScalar kTopLeftRadius = SkIntToScalar(top_left_corner_radius);
88 const SkScalar kTopRightRadius = SkIntToScalar(top_right_corner_radius);
90 kTopLeftRadius, kTopLeftRadius, // top-left
91 kTopRightRadius, kTopRightRadius, // top-right
95 path.addRoundRect(rect, radii, SkPath::kCW_Direction);
96 canvas->DrawImageInPath(image, -image_inset_x, 0, path, paint);
99 // Tiles |frame_image| and |frame_overlay_image| into an area, rounding the top
101 void PaintFrameImagesInRoundRect(gfx::Canvas* canvas,
102 const gfx::ImageSkia* frame_image,
103 const gfx::ImageSkia* frame_overlay_image,
104 const SkPaint& paint,
105 const gfx::Rect& bounds,
108 SkXfermode::Mode normal_mode;
109 SkXfermode::AsMode(NULL, &normal_mode);
111 // If |paint| is using an unusual SkXfermode::Mode (this is the case while
112 // crossfading), we must create a new canvas to overlay |frame_image| and
113 // |frame_overlay_image| using |normal_mode| and then paint the result
114 // using the unusual mode. We try to avoid this because creating a new
115 // browser-width canvas is expensive.
116 bool fast_path = (!frame_overlay_image ||
117 SkXfermode::IsMode(paint.getXfermode(), normal_mode));
119 TileRoundRect(canvas, *frame_image, paint, bounds, corner_radius,
120 corner_radius, image_inset_x);
122 if (frame_overlay_image) {
123 // Adjust |bounds| such that |frame_overlay_image| is not tiled.
124 gfx::Rect overlay_bounds = bounds;
125 overlay_bounds.Intersect(
126 gfx::Rect(bounds.origin(), frame_overlay_image->size()));
127 int top_left_corner_radius = corner_radius;
128 int top_right_corner_radius = corner_radius;
129 if (overlay_bounds.width() < bounds.width() - corner_radius)
130 top_right_corner_radius = 0;
131 TileRoundRect(canvas, *frame_overlay_image, paint, overlay_bounds,
132 top_left_corner_radius, top_right_corner_radius, 0);
135 gfx::Canvas temporary_canvas(bounds.size(), canvas->image_scale(), false);
136 temporary_canvas.TileImageInt(*frame_image,
139 bounds.width(), bounds.height());
140 temporary_canvas.DrawImageInt(*frame_overlay_image, 0, 0);
141 TileRoundRect(canvas, gfx::ImageSkia(temporary_canvas.ExtractImageRep()),
142 paint, bounds, corner_radius, corner_radius, 0);
146 // Returns true if |child| and all ancestors are visible. Useful to ensure that
147 // a window is individually visible and is not part of a hidden workspace.
148 bool IsVisibleToRoot(Window* child) {
149 for (Window* window = child; window; window = window->parent()) {
150 // We must use TargetVisibility() because windows animate in and out and
151 // IsVisible() also tracks the layer visibility state.
152 if (!window->TargetVisibility())
158 // Returns true if |window| is a "normal" window for purposes of solo window
159 // computations. Returns false for windows that are:
160 // * Not drawn (for example, DragDropTracker uses one for mouse capture)
161 // * Modal alerts (it looks odd for headers to change when an alert opens)
162 // * Constrained windows (ditto)
163 bool IsSoloWindowHeaderCandidate(aura::Window* window) {
165 window->type() == aura::client::WINDOW_TYPE_NORMAL &&
167 window->layer()->type() != ui::LAYER_NOT_DRAWN &&
168 window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_NONE &&
169 !window->GetProperty(ash::kConstrainedWindowKey);
172 // Returns a list of windows in |root_window|| that potentially could have
173 // a transparent solo-window header.
174 std::vector<Window*> GetWindowsForSoloHeaderUpdate(Window* root_window) {
175 std::vector<Window*> windows;
176 // Avoid memory allocations for typical window counts.
178 // Collect windows from the desktop.
179 Window* desktop = ash::Shell::GetContainer(
180 root_window, ash::internal::kShellWindowId_DefaultContainer);
181 windows.insert(windows.end(),
182 desktop->children().begin(),
183 desktop->children().end());
184 // Collect "always on top" windows.
185 Window* top_container =
186 ash::Shell::GetContainer(
187 root_window, ash::internal::kShellWindowId_AlwaysOnTopContainer);
188 windows.insert(windows.end(),
189 top_container->children().begin(),
190 top_container->children().end());
198 int HeaderPainter::kActiveWindowOpacity = 255; // 1.0
199 int HeaderPainter::kInactiveWindowOpacity = 255; // 1.0
200 int HeaderPainter::kSoloWindowOpacity = 77; // 0.3
202 ///////////////////////////////////////////////////////////////////////////////
203 // HeaderPainter, public:
205 HeaderPainter::HeaderPainter()
209 caption_button_container_(NULL),
212 top_left_corner_(NULL),
214 top_right_corner_(NULL),
215 header_left_edge_(NULL),
216 header_right_edge_(NULL),
217 previous_theme_frame_id_(0),
218 previous_theme_frame_overlay_id_(0),
219 previous_opacity_(0),
220 crossfade_theme_frame_id_(0),
221 crossfade_theme_frame_overlay_id_(0),
222 crossfade_opacity_(0) {}
224 HeaderPainter::~HeaderPainter() {
225 // Sometimes we are destroyed before the window closes, so ensure we clean up.
227 window_->RemoveObserver(this);
228 wm::GetWindowState(window_)->RemoveObserver(this);
232 void HeaderPainter::Init(
233 views::Widget* frame,
234 views::View* header_view,
235 views::View* window_icon,
236 FrameCaptionButtonContainerView* caption_button_container) {
239 // window_icon may be NULL.
240 DCHECK(caption_button_container);
242 header_view_ = header_view;
243 window_icon_ = window_icon;
244 caption_button_container_ = caption_button_container;
246 // Window frame image parts.
247 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
249 rb.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_TOP_LEFT).ToImageSkia();
251 rb.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_TOP).ToImageSkia();
253 rb.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_TOP_RIGHT).ToImageSkia();
255 rb.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_LEFT).ToImageSkia();
257 rb.GetImageNamed(IDR_AURA_WINDOW_HEADER_SHADE_RIGHT).ToImageSkia();
259 window_ = frame->GetNativeWindow();
261 // Observer removes itself in OnWindowDestroying() below, or in the destructor
262 // if we go away before the window.
263 window_->AddObserver(this);
264 wm::GetWindowState(window_)->AddObserver(this);
266 // Solo-window header updates are handled by the WorkspaceLayoutManager when
267 // this window is added to the desktop.
271 void HeaderPainter::SetSoloWindowHeadersEnabled(bool enabled) {
272 solo_window_header_enabled = enabled;
276 void HeaderPainter::UpdateSoloWindowHeader(Window* root_window) {
277 // Use a separate function here so callers outside of HeaderPainter don't need
278 // to know about "ignorable_window".
279 UpdateSoloWindowInRoot(root_window, NULL /* ignorable_window */);
283 gfx::Rect HeaderPainter::GetBoundsForClientView(
285 const gfx::Rect& window_bounds) {
286 gfx::Rect client_bounds(window_bounds);
287 client_bounds.Inset(0, header_height, 0, 0);
288 return client_bounds;
292 gfx::Rect HeaderPainter::GetWindowBoundsForClientBounds(
294 const gfx::Rect& client_bounds) {
295 gfx::Rect window_bounds(client_bounds);
296 window_bounds.Inset(0, -header_height, 0, 0);
297 if (window_bounds.y() < 0)
298 window_bounds.set_y(0);
299 return window_bounds;
302 int HeaderPainter::NonClientHitTest(const gfx::Point& point) const {
303 gfx::Point point_in_header_view(point);
304 views::View::ConvertPointFromWidget(header_view_, &point_in_header_view);
305 if (!GetHeaderLocalBounds().Contains(point_in_header_view))
307 if (caption_button_container_->visible()) {
308 gfx::Point point_in_caption_button_container(point);
309 views::View::ConvertPointFromWidget(caption_button_container_,
310 &point_in_caption_button_container);
311 int component = caption_button_container_->NonClientHitTest(
312 point_in_caption_button_container);
313 if (component != HTNOWHERE)
316 // Caption is a safe default.
320 int HeaderPainter::GetMinimumHeaderWidth() const {
321 // Ensure we have enough space for the window icon and buttons. We allow
322 // the title string to collapse to zero width.
323 return GetTitleOffsetX() +
324 caption_button_container_->GetMinimumSize().width();
327 int HeaderPainter::GetRightInset() const {
328 return caption_button_container_->GetPreferredSize().width();
331 int HeaderPainter::GetThemeBackgroundXInset() const {
332 return kThemeFrameImageInsetX;
335 bool HeaderPainter::ShouldUseMinimalHeaderStyle(Themed header_themed) const {
336 // Use the minimalistic header style whenever |frame_| is maximized or
337 // fullscreen EXCEPT:
338 // - If the user has installed a theme with custom images for the header.
339 // - For windows which are not tracked by the workspace code (which are used
340 // for tab dragging).
341 return (frame_->IsMaximized() || frame_->IsFullscreen()) &&
342 header_themed == THEMED_NO &&
343 wm::GetWindowState(frame_->GetNativeWindow())->tracked_by_workspace();
346 void HeaderPainter::PaintHeader(gfx::Canvas* canvas,
347 HeaderMode header_mode,
349 int theme_frame_overlay_id) {
350 bool initial_paint = (previous_theme_frame_id_ == 0);
351 if (!initial_paint &&
352 (previous_theme_frame_id_ != theme_frame_id ||
353 previous_theme_frame_overlay_id_ != theme_frame_overlay_id)) {
354 aura::Window* parent = frame_->GetNativeWindow()->parent();
355 // Don't animate the header if the parent (a workspace) is already
356 // animating. Doing so results in continually painting during the animation
357 // and gives a slower frame rate.
358 // TODO(sky): expose a better way to determine this rather than assuming
359 // the parent is a workspace.
360 bool parent_animating = parent &&
361 (parent->layer()->GetAnimator()->IsAnimatingProperty(
362 ui::LayerAnimationElement::OPACITY) ||
363 parent->layer()->GetAnimator()->IsAnimatingProperty(
364 ui::LayerAnimationElement::VISIBILITY));
365 if (!parent_animating) {
366 crossfade_animation_.reset(new gfx::SlideAnimation(this));
367 crossfade_theme_frame_id_ = previous_theme_frame_id_;
368 crossfade_theme_frame_overlay_id_ = previous_theme_frame_overlay_id_;
369 crossfade_opacity_ = previous_opacity_;
370 crossfade_animation_->SetSlideDuration(kActivationCrossfadeDurationMs);
371 crossfade_animation_->Show();
373 crossfade_animation_.reset();
378 GetHeaderOpacity(header_mode, theme_frame_id, theme_frame_overlay_id);
379 ui::ThemeProvider* theme_provider = frame_->GetThemeProvider();
380 gfx::ImageSkia* theme_frame = theme_provider->GetImageSkiaNamed(
382 gfx::ImageSkia* theme_frame_overlay = NULL;
383 if (theme_frame_overlay_id != 0) {
384 theme_frame_overlay = theme_provider->GetImageSkiaNamed(
385 theme_frame_overlay_id);
388 int corner_radius = GetHeaderCornerRadius();
391 if (crossfade_animation_.get() && crossfade_animation_->is_animating()) {
392 gfx::ImageSkia* crossfade_theme_frame =
393 theme_provider->GetImageSkiaNamed(crossfade_theme_frame_id_);
394 gfx::ImageSkia* crossfade_theme_frame_overlay = NULL;
395 if (crossfade_theme_frame_overlay_id_ != 0) {
396 crossfade_theme_frame_overlay = theme_provider->GetImageSkiaNamed(
397 crossfade_theme_frame_overlay_id_);
399 if (!crossfade_theme_frame ||
400 (crossfade_theme_frame_overlay_id_ != 0 &&
401 !crossfade_theme_frame_overlay)) {
402 // Reset the animation. This case occurs when the user switches the theme
403 // that they are using.
404 crossfade_animation_.reset();
405 paint.setAlpha(opacity);
407 double current_value = crossfade_animation_->GetCurrentValue();
408 int old_alpha = (1 - current_value) * crossfade_opacity_;
409 int new_alpha = current_value * opacity;
411 // Draw the old header background, clipping the corners to be rounded.
412 paint.setAlpha(old_alpha);
413 paint.setXfermodeMode(SkXfermode::kPlus_Mode);
414 PaintFrameImagesInRoundRect(canvas,
415 crossfade_theme_frame,
416 crossfade_theme_frame_overlay,
418 GetHeaderLocalBounds(),
420 GetThemeBackgroundXInset());
422 paint.setAlpha(new_alpha);
425 paint.setAlpha(opacity);
428 // Draw the header background, clipping the corners to be rounded.
429 PaintFrameImagesInRoundRect(canvas,
433 GetHeaderLocalBounds(),
435 GetThemeBackgroundXInset());
437 previous_theme_frame_id_ = theme_frame_id;
438 previous_theme_frame_overlay_id_ = theme_frame_overlay_id;
439 previous_opacity_ = opacity;
441 // We don't need the extra lightness in the edges when we're at the top edge
442 // of the screen or when the header's corners are not rounded.
444 // TODO(sky): this isn't quite right. What we really want is a method that
445 // returns bounds ignoring transforms on certain windows (such as workspaces)
446 // and is relative to the root.
447 if (frame_->GetNativeWindow()->bounds().y() == 0 || corner_radius == 0)
450 // Draw the top corners and edge.
451 int top_left_width = top_left_corner_->width();
452 int top_left_height = top_left_corner_->height();
453 canvas->DrawImageInt(*top_left_corner_,
454 0, 0, top_left_width, top_left_height,
455 0, 0, top_left_width, top_left_height,
457 canvas->TileImageInt(*top_edge_,
460 header_view_->width() - top_left_width - top_right_corner_->width(),
461 top_edge_->height());
462 int top_right_height = top_right_corner_->height();
463 canvas->DrawImageInt(*top_right_corner_,
465 top_right_corner_->width(), top_right_height,
466 header_view_->width() - top_right_corner_->width(), 0,
467 top_right_corner_->width(), top_right_height,
471 int header_left_height = theme_frame->height() - top_left_height;
472 canvas->TileImageInt(*header_left_edge_,
474 header_left_edge_->width(), header_left_height);
476 // Header right edge.
477 int header_right_height = theme_frame->height() - top_right_height;
478 canvas->TileImageInt(*header_right_edge_,
479 header_view_->width() - header_right_edge_->width(),
481 header_right_edge_->width(),
482 header_right_height);
484 // We don't draw edges around the content area. Web content goes flush
485 // to the edge of the window.
488 void HeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) {
489 canvas->FillRect(gfx::Rect(0,
490 header_height_ - kHeaderContentSeparatorSize,
491 header_view_->width(),
492 kHeaderContentSeparatorSize),
493 kHeaderContentSeparatorColor);
496 int HeaderPainter::HeaderContentSeparatorSize() const {
497 return kHeaderContentSeparatorSize;
500 void HeaderPainter::PaintTitleBar(gfx::Canvas* canvas,
501 const gfx::Font& title_font) {
502 // The window icon is painted by its own views::View.
503 views::WidgetDelegate* delegate = frame_->widget_delegate();
504 if (delegate && delegate->ShouldShowWindowTitle()) {
505 gfx::Rect title_bounds = GetTitleBounds(title_font);
506 SkColor title_color = frame_->IsMaximized() ?
507 kMaximizedWindowTitleTextColor : kNonMaximizedWindowTitleTextColor;
508 canvas->DrawStringInt(delegate->GetWindowTitle(),
511 header_view_->GetMirroredXForRect(title_bounds),
513 title_bounds.width(),
514 title_bounds.height(),
515 gfx::Canvas::NO_SUBPIXEL_RENDERING);
519 void HeaderPainter::LayoutHeader(bool shorter_layout) {
520 caption_button_container_->set_header_style(shorter_layout ?
521 FrameCaptionButtonContainerView::HEADER_STYLE_SHORT :
522 FrameCaptionButtonContainerView::HEADER_STYLE_TALL);
523 caption_button_container_->Layout();
525 gfx::Size caption_button_container_size =
526 caption_button_container_->GetPreferredSize();
527 caption_button_container_->SetBounds(
528 header_view_->width() - caption_button_container_size.width(),
530 caption_button_container_size.width(),
531 caption_button_container_size.height());
534 // Vertically center the window icon with respect to the caption button
537 GetCaptionButtonContainerCenterY() - window_icon_->height() / 2;
538 window_icon_->SetBounds(kIconOffsetX, icon_offset_y, kIconSize, kIconSize);
542 void HeaderPainter::SchedulePaintForTitle(const gfx::Font& title_font) {
543 header_view_->SchedulePaintInRect(GetTitleBounds(title_font));
546 void HeaderPainter::OnThemeChanged() {
547 // We do not cache the images for |previous_theme_frame_id_| and
548 // |previous_theme_frame_overlay_id_|. Changing the theme changes the images
549 // returned from ui::ThemeProvider for |previous_theme_frame_id_|
550 // and |previous_theme_frame_overlay_id_|. Reset the image ids to prevent
551 // starting a crossfade animation with these images.
552 previous_theme_frame_id_ = 0;
553 previous_theme_frame_overlay_id_ = 0;
555 if (crossfade_animation_.get() && crossfade_animation_->is_animating()) {
556 crossfade_animation_.reset();
557 header_view_->SchedulePaintInRect(GetHeaderLocalBounds());
561 ///////////////////////////////////////////////////////////////////////////////
562 // WindowStateObserver overrides:
563 void HeaderPainter::OnTrackedByWorkspaceChanged(wm::WindowState* window_state,
565 // When 'TrackedByWorkspace' changes, we are going to paint the header
566 // differently. Schedule a paint to ensure everything is updated correctly.
567 if (window_state->tracked_by_workspace())
568 header_view_->SchedulePaint();
571 ///////////////////////////////////////////////////////////////////////////////
572 // aura::WindowObserver overrides:
574 void HeaderPainter::OnWindowVisibilityChanged(aura::Window* window,
576 // OnWindowVisibilityChanged can be called for the child windows of |window_|.
577 if (window != window_)
580 // Window visibility change may trigger the change of window solo-ness in a
582 UpdateSoloWindowInRoot(window_->GetRootWindow(), visible ? NULL : window_);
585 void HeaderPainter::OnWindowDestroying(aura::Window* destroying) {
586 DCHECK_EQ(window_, destroying);
588 // Must be removed here and not in the destructor, as the aura::Window is
589 // already destroyed when our destructor runs.
590 window_->RemoveObserver(this);
591 wm::GetWindowState(window_)->RemoveObserver(this);
593 // If we have two or more windows open and we close this one, we might trigger
594 // the solo window appearance for another window.
595 UpdateSoloWindowInRoot(window_->GetRootWindow(), window_);
600 void HeaderPainter::OnWindowBoundsChanged(aura::Window* window,
601 const gfx::Rect& old_bounds,
602 const gfx::Rect& new_bounds) {
603 // TODO(sky): this isn't quite right. What we really want is a method that
604 // returns bounds ignoring transforms on certain windows (such as workspaces).
605 if ((!frame_->IsMaximized() && !frame_->IsFullscreen()) &&
606 ((old_bounds.y() == 0 && new_bounds.y() != 0) ||
607 (old_bounds.y() != 0 && new_bounds.y() == 0))) {
608 SchedulePaintForHeader();
612 void HeaderPainter::OnWindowAddedToRootWindow(aura::Window* window) {
613 // Needs to trigger the window appearance change if the window moves across
614 // root windows and a solo window is already in the new root.
615 UpdateSoloWindowInRoot(window->GetRootWindow(), NULL /* ignore_window */);
618 void HeaderPainter::OnWindowRemovingFromRootWindow(aura::Window* window) {
619 // Needs to trigger the window appearance change if the window moves across
620 // root windows and only one window is left in the previous root. Because
621 // |window| is not yet moved, |window| has to be ignored.
622 UpdateSoloWindowInRoot(window->GetRootWindow(), window);
625 ///////////////////////////////////////////////////////////////////////////////
626 // gfx::AnimationDelegate overrides:
628 void HeaderPainter::AnimationProgressed(const gfx::Animation* animation) {
629 header_view_->SchedulePaintInRect(GetHeaderLocalBounds());
632 ///////////////////////////////////////////////////////////////////////////////
633 // HeaderPainter, private:
635 gfx::Rect HeaderPainter::GetHeaderLocalBounds() const {
636 return gfx::Rect(header_view_->width(), header_height_);
639 int HeaderPainter::GetTitleOffsetX() const {
640 return window_icon_ ?
641 window_icon_->bounds().right() + kTitleIconOffsetX :
645 int HeaderPainter::GetCaptionButtonContainerCenterY() const {
646 return caption_button_container_->y() +
647 caption_button_container_->height() / 2;
650 int HeaderPainter::GetHeaderCornerRadius() const {
651 // Use square corners for maximized and fullscreen windows when they are
652 // tracked by the workspace code. (Windows which are not tracked by the
653 // workspace code are used for tab dragging.)
654 bool square_corners = ((frame_->IsMaximized() || frame_->IsFullscreen())) &&
655 wm::GetWindowState(frame_->GetNativeWindow())->tracked_by_workspace();
656 const int kCornerRadius = 2;
657 return square_corners ? 0 : kCornerRadius;
660 int HeaderPainter::GetHeaderOpacity(
661 HeaderMode header_mode,
663 int theme_frame_overlay_id) const {
664 // User-provided themes are painted fully opaque.
665 ui::ThemeProvider* theme_provider = frame_->GetThemeProvider();
666 if (theme_provider->HasCustomImage(theme_frame_id) ||
667 (theme_frame_overlay_id != 0 &&
668 theme_provider->HasCustomImage(theme_frame_overlay_id))) {
672 // The header is fully opaque when using the minimalistic header style.
673 if (ShouldUseMinimalHeaderStyle(THEMED_NO))
676 // Single browser window is very transparent.
677 if (UseSoloWindowHeader())
678 return kSoloWindowOpacity;
680 // Otherwise, change transparency based on window activation status.
681 if (header_mode == ACTIVE)
682 return kActiveWindowOpacity;
683 return kInactiveWindowOpacity;
686 bool HeaderPainter::UseSoloWindowHeader() const {
687 if (!solo_window_header_enabled)
689 // Don't use transparent headers for panels, pop-ups, etc.
690 if (!IsSoloWindowHeaderCandidate(window_))
692 aura::Window* root = window_->GetRootWindow();
693 // Don't recompute every time, as it would require many window property
695 return internal::GetRootWindowSettings(root)->solo_window_header;
699 bool HeaderPainter::UseSoloWindowHeaderInRoot(Window* root_window,
700 Window* ignore_window) {
701 int visible_window_count = 0;
702 std::vector<Window*> windows = GetWindowsForSoloHeaderUpdate(root_window);
703 for (std::vector<Window*>::const_iterator it = windows.begin();
706 Window* window = *it;
707 // Various sorts of windows "don't count" for this computation.
708 if (ignore_window == window ||
709 !IsSoloWindowHeaderCandidate(window) ||
710 !IsVisibleToRoot(window))
712 if (wm::GetWindowState(window)->IsMaximized())
714 ++visible_window_count;
715 if (visible_window_count > 1)
718 // Count must be tested because all windows might be "don't count" windows
719 // in the loop above.
720 return visible_window_count == 1;
724 void HeaderPainter::UpdateSoloWindowInRoot(Window* root,
725 Window* ignore_window) {
727 // Non-Ash Windows doesn't do solo-window counting for transparency effects,
728 // as the desktop background and window frames are managed by the OS.
729 if (!ash::Shell::HasInstance())
734 internal::RootWindowSettings* root_window_settings =
735 internal::GetRootWindowSettings(root);
736 bool old_solo_header = root_window_settings->solo_window_header;
737 bool new_solo_header = UseSoloWindowHeaderInRoot(root, ignore_window);
738 if (old_solo_header == new_solo_header)
740 root_window_settings->solo_window_header = new_solo_header;
742 // Invalidate all the window frames in the desktop. There should only be
744 std::vector<Window*> windows = GetWindowsForSoloHeaderUpdate(root);
745 for (std::vector<Window*>::const_iterator it = windows.begin();
748 Widget* widget = Widget::GetWidgetForNativeWindow(*it);
749 if (widget && widget->non_client_view())
750 widget->non_client_view()->SchedulePaint();
754 void HeaderPainter::SchedulePaintForHeader() {
755 int top_left_height = top_left_corner_->height();
756 int top_right_height = top_right_corner_->height();
757 header_view_->SchedulePaintInRect(
758 gfx::Rect(0, 0, header_view_->width(),
759 std::max(top_left_height, top_right_height)));
762 gfx::Rect HeaderPainter::GetTitleBounds(const gfx::Font& title_font) {
763 int title_x = GetTitleOffsetX();
764 // Center the text with respect to the caption button container. This way it
765 // adapts to the caption button height and aligns exactly with the window
766 // icon. Don't use |window_icon_| for this computation as it may be NULL.
767 int title_y = GetCaptionButtonContainerCenterY() - title_font.GetHeight() / 2;
770 std::max(0, title_y),
771 std::max(0, caption_button_container_->x() - kTitleLogoSpacing - title_x),
772 title_font.GetHeight());