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/overview/window_selector_item.h"
7 #include "ash/screen_util.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/overview/scoped_transform_overview_window.h"
11 #include "base/auto_reset.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/aura/window.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/compositor/scoped_layer_animation_settings.h"
16 #include "ui/views/controls/label.h"
17 #include "ui/views/layout/box_layout.h"
18 #include "ui/views/widget/widget.h"
22 // Foreground label color.
23 static const SkColor kLabelColor = SK_ColorWHITE;
25 // Background label color.
26 static const SkColor kLabelBackground = SK_ColorTRANSPARENT;
28 // Label shadow color.
29 static const SkColor kLabelShadow = 0xB0000000;
31 // Vertical padding for the label, both over and beneath it.
32 static const int kVerticalLabelPadding = 20;
34 // Solid shadow length from the label
35 static const int kVerticalShadowOffset = 1;
37 // Amount of blur applied to the label shadow
38 static const int kShadowBlur = 10;
40 views::Widget* CreateWindowLabel(aura::Window* root_window,
41 const base::string16 title) {
42 views::Widget* widget = new views::Widget;
43 views::Widget::InitParams params;
44 params.type = views::Widget::InitParams::TYPE_POPUP;
45 params.can_activate = false;
46 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
47 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
49 Shell::GetContainer(root_window, ash::kShellWindowId_OverlayContainer);
50 params.accept_events = false;
51 params.visible_on_all_workspaces = true;
52 widget->set_focus_on_creation(false);
54 views::Label* label = new views::Label;
55 label->SetEnabledColor(kLabelColor);
56 label->SetBackgroundColor(kLabelBackground);
57 label->SetShadowColors(kLabelShadow, kLabelShadow);
58 label->SetShadowOffset(0, kVerticalShadowOffset);
59 label->set_shadow_blur(kShadowBlur);
60 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
61 label->SetFontList(bundle.GetFontList(ui::ResourceBundle::BoldFont));
62 label->SetText(title);
63 views::BoxLayout* layout = new views::BoxLayout(views::BoxLayout::kVertical,
65 kVerticalLabelPadding,
67 label->SetLayoutManager(layout);
68 widget->SetContentsView(label);
73 const int WindowSelectorItem::kFadeInMilliseconds = 80;
75 WindowSelectorItem::WindowSelectorItem()
77 in_bounds_update_(false) {
80 WindowSelectorItem::~WindowSelectorItem() {
83 void WindowSelectorItem::SetBounds(aura::Window* root_window,
84 const gfx::Rect& target_bounds,
86 if (in_bounds_update_)
88 base::AutoReset<bool> auto_reset_in_bounds_update(&in_bounds_update_, true);
89 root_window_ = root_window;
90 target_bounds_ = target_bounds;
91 SetItemBounds(root_window, target_bounds, animate);
92 // TODO(nsatragno): Handle window title updates.
93 UpdateWindowLabels(target_bounds, root_window, animate);
96 void WindowSelectorItem::RecomputeWindowTransforms() {
97 if (in_bounds_update_ || target_bounds_.IsEmpty())
100 base::AutoReset<bool> auto_reset_in_bounds_update(&in_bounds_update_, true);
101 SetItemBounds(root_window_, target_bounds_, false);
104 void WindowSelectorItem::UpdateWindowLabels(const gfx::Rect& window_bounds,
105 aura::Window* root_window,
107 gfx::Rect converted_bounds = ScreenUtil::ConvertRectFromScreen(root_window,
109 gfx::Rect label_bounds(converted_bounds.x(),
110 converted_bounds.bottom(),
111 converted_bounds.width(),
114 // If the root window has changed, force the window label to be recreated
115 // and faded in on the new root window.
117 window_label_->GetNativeWindow()->GetRootWindow() != root_window) {
118 window_label_.reset();
121 if (!window_label_) {
122 window_label_.reset(CreateWindowLabel(root_window,
123 SelectionWindow()->title()));
124 label_bounds.set_height(window_label_->
125 GetContentsView()->GetPreferredSize().height());
126 window_label_->GetNativeWindow()->SetBounds(label_bounds);
127 ui::Layer* layer = window_label_->GetNativeWindow()->layer();
129 layer->SetOpacity(0);
130 layer->GetAnimator()->StopAnimating();
132 layer->GetAnimator()->SchedulePauseForProperties(
133 base::TimeDelta::FromMilliseconds(
134 ScopedTransformOverviewWindow::kTransitionMilliseconds),
135 ui::LayerAnimationElement::OPACITY);
137 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
138 settings.SetPreemptionStrategy(
139 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
140 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
141 kFadeInMilliseconds));
142 layer->SetOpacity(1);
144 label_bounds.set_height(window_label_->
145 GetContentsView()->GetPreferredSize().height());
147 ui::ScopedLayerAnimationSettings settings(
148 window_label_->GetNativeWindow()->layer()->GetAnimator());
149 settings.SetPreemptionStrategy(
150 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
151 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
152 ScopedTransformOverviewWindow::kTransitionMilliseconds));
153 window_label_->GetNativeWindow()->SetBounds(label_bounds);
155 window_label_->GetNativeWindow()->SetBounds(label_bounds);