Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / wm / overview / window_selector_item.cc
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.
4
5 #include "ash/wm/overview/window_selector_item.h"
6
7 #include "ash/screen_util.h"
8 #include "ash/shell.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"
19
20 namespace ash {
21
22 // Foreground label color.
23 static const SkColor kLabelColor = SK_ColorWHITE;
24
25 // Background label color.
26 static const SkColor kLabelBackground = SK_ColorTRANSPARENT;
27
28 // Label shadow color.
29 static const SkColor kLabelShadow = 0xB0000000;
30
31 // Vertical padding for the label, both over and beneath it.
32 static const int kVerticalLabelPadding = 20;
33
34 // Solid shadow length from the label
35 static const int kVerticalShadowOffset = 1;
36
37 // Amount of blur applied to the label shadow
38 static const int kShadowBlur = 10;
39
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;
48   params.parent =
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);
53   widget->Init(params);
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,
64                                                   0,
65                                                   kVerticalLabelPadding,
66                                                   0);
67   label->SetLayoutManager(layout);
68   widget->SetContentsView(label);
69   widget->Show();
70   return widget;
71 }
72
73 const int WindowSelectorItem::kFadeInMilliseconds = 80;
74
75 WindowSelectorItem::WindowSelectorItem()
76     : root_window_(NULL),
77       in_bounds_update_(false) {
78 }
79
80 WindowSelectorItem::~WindowSelectorItem() {
81 }
82
83 void WindowSelectorItem::SetBounds(aura::Window* root_window,
84                                    const gfx::Rect& target_bounds,
85                                    bool animate) {
86   if (in_bounds_update_)
87     return;
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);
94 }
95
96 void WindowSelectorItem::RecomputeWindowTransforms() {
97   if (in_bounds_update_ || target_bounds_.IsEmpty())
98     return;
99   DCHECK(root_window_);
100   base::AutoReset<bool> auto_reset_in_bounds_update(&in_bounds_update_, true);
101   SetItemBounds(root_window_, target_bounds_, false);
102 }
103
104 void WindowSelectorItem::UpdateWindowLabels(const gfx::Rect& window_bounds,
105                                             aura::Window* root_window,
106                                             bool animate) {
107   gfx::Rect converted_bounds = ScreenUtil::ConvertRectFromScreen(root_window,
108                                                                  window_bounds);
109   gfx::Rect label_bounds(converted_bounds.x(),
110                          converted_bounds.bottom(),
111                          converted_bounds.width(),
112                          0);
113
114   // If the root window has changed, force the window label to be recreated
115   // and faded in on the new root window.
116   if (window_label_ &&
117       window_label_->GetNativeWindow()->GetRootWindow() != root_window) {
118     window_label_.reset();
119   }
120
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();
128
129     layer->SetOpacity(0);
130     layer->GetAnimator()->StopAnimating();
131
132     layer->GetAnimator()->SchedulePauseForProperties(
133         base::TimeDelta::FromMilliseconds(
134             ScopedTransformOverviewWindow::kTransitionMilliseconds),
135         ui::LayerAnimationElement::OPACITY);
136
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);
143   } else {
144     label_bounds.set_height(window_label_->
145                             GetContentsView()->GetPreferredSize().height());
146     if (animate) {
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);
154     } else {
155       window_label_->GetNativeWindow()->SetBounds(label_bounds);
156     }
157   }
158
159 }
160
161 }  // namespace ash