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.
5 #include "athena/wm/overview_toolbar.h"
7 #include "athena/resources/grit/athena_resources.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "ui/aura/window.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/compositor/closure_animation_observer.h"
14 #include "ui/compositor/layer.h"
15 #include "ui/compositor/layer_delegate.h"
16 #include "ui/compositor/scoped_layer_animation_settings.h"
17 #include "ui/events/event.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/transform.h"
23 const int kActionButtonImageSize
= 54;
24 const int kActionButtonTextSize
= 20;
25 const int kActionButtonPaddingFromRight
= 32;
30 class ActionButton
: public ui::LayerDelegate
{
32 ActionButton(int resource_id
, const std::string
& label
)
33 : resource_id_(resource_id
), label_(base::UTF8ToUTF16(label
)) {
34 layer_
.reset(new ui::Layer(ui::LAYER_TEXTURED
));
35 layer_
->set_delegate(this);
36 layer_
->SetFillsBoundsOpaquely(false);
37 layer_
->SetVisible(true);
38 layer_
->SetOpacity(0);
41 virtual ~ActionButton() {}
43 static void DestroyAfterFadeout(scoped_ptr
<ActionButton
> button
) {
44 ui::Layer
* layer
= button
->layer();
45 ui::ScopedLayerAnimationSettings
settings(layer
->GetAnimator());
46 settings
.AddObserver(new ui::ClosureAnimationObserver(
47 base::Bind(&ActionButton::DestroyImmediately
, base::Passed(&button
))));
51 void SetPosition(const gfx::Point
& position
) {
54 gfx::Size(kActionButtonImageSize
,
55 kActionButtonImageSize
+ kActionButtonTextSize
)));
58 ui::Layer
* layer() { return layer_
.get(); }
61 static void DestroyImmediately(scoped_ptr
<ActionButton
> button
) {
66 virtual void OnPaintLayer(gfx::Canvas
* canvas
) OVERRIDE
{
67 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
68 canvas
->DrawImageInt(*bundle
.GetImageSkiaNamed(resource_id_
), 0, 0);
69 gfx::ShadowValues shadow
;
70 shadow
.push_back(gfx::ShadowValue(gfx::Point(0, 1), 2, SK_ColorBLACK
));
71 shadow
.push_back(gfx::ShadowValue(gfx::Point(0, -1), 2, SK_ColorBLACK
));
72 canvas
->DrawStringRectWithShadows(label_
,
76 kActionButtonImageSize
,
77 kActionButtonImageSize
,
78 kActionButtonTextSize
),
80 gfx::Canvas::TEXT_ALIGN_CENTER
,
84 virtual void OnDelegatedFrameDamage(
85 const gfx::Rect
& damage_rect_in_dip
) OVERRIDE
{}
87 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
{}
88 virtual base::Closure
PrepareForLayerBoundsChange() OVERRIDE
{
89 return base::Closure();
93 base::string16 label_
;
94 scoped_ptr
<ui::Layer
> layer_
;
96 DISALLOW_COPY_AND_ASSIGN(ActionButton
);
99 OverviewToolbar::OverviewToolbar(aura::Window
* container
)
101 close_(new ActionButton(IDR_ATHENA_OVERVIEW_TRASH
, "Close")),
102 split_(new ActionButton(IDR_ATHENA_OVERVIEW_SPLIT
, "Split")),
103 current_action_(ACTION_TYPE_NONE
),
104 container_bounds_(container
->bounds()) {
105 const int kPaddingFromBottom
= 200;
106 const int kPaddingBetweenButtons
= 200;
108 int x
= container_bounds_
.right() -
109 (kActionButtonPaddingFromRight
+ kActionButtonImageSize
);
110 int y
= container_bounds_
.bottom() -
111 (kPaddingFromBottom
+ kActionButtonImageSize
);
112 split_
->SetPosition(gfx::Point(x
, y
));
113 y
-= kPaddingBetweenButtons
;
114 close_
->SetPosition(gfx::Point(x
, y
));
116 container
->layer()->Add(split_
->layer());
117 container
->layer()->Add(close_
->layer());
120 OverviewToolbar::~OverviewToolbar() {
121 // If the buttons are visible, then fade them out, instead of destroying them
124 ActionButton::DestroyAfterFadeout(split_
.Pass());
125 ActionButton::DestroyAfterFadeout(close_
.Pass());
129 OverviewToolbar::ActionType
OverviewToolbar::GetHighlightAction(
130 const ui::GestureEvent
& event
) const {
131 if (IsEventOverButton(split_
.get(), event
))
132 return ACTION_TYPE_SPLIT
;
133 if (IsEventOverButton(close_
.get(), event
))
134 return ACTION_TYPE_CLOSE
;
135 return ACTION_TYPE_NONE
;
138 void OverviewToolbar::SetHighlightAction(ActionType action
) {
139 if (current_action_
== action
)
141 current_action_
= action
;
145 TransformButton(close_
.get());
146 TransformButton(split_
.get());
150 void OverviewToolbar::ShowActionButtons() {
152 ToggleActionButtonsVisibility();
155 void OverviewToolbar::HideActionButtons() {
157 ToggleActionButtonsVisibility();
160 void OverviewToolbar::ToggleActionButtonsVisibility() {
162 TransformButton(close_
.get());
163 TransformButton(split_
.get());
166 bool OverviewToolbar::IsEventOverButton(ActionButton
* button
,
167 const ui::GestureEvent
& event
) const {
168 const int kBoundsInsetForTarget
= 30;
169 gfx::RectF bounds
= button
->layer()->bounds();
170 bounds
.Inset(-kBoundsInsetForTarget
, -kBoundsInsetForTarget
);
171 return bounds
.Contains(event
.location());
174 gfx::Transform
OverviewToolbar::ComputeTransformFor(
175 ActionButton
* button
) const {
177 return gfx::Transform();
179 const float kHighlightScale
= 1.5;
180 bool button_is_highlighted
=
181 (current_action_
== ACTION_TYPE_CLOSE
&& button
== close_
.get()) ||
182 (current_action_
== ACTION_TYPE_SPLIT
&& button
== split_
.get());
183 gfx::Transform transform
;
184 if (button_is_highlighted
) {
185 transform
.Translate(-kActionButtonImageSize
* (kHighlightScale
- 1) / 2, 0);
186 transform
.Scale(kHighlightScale
, kHighlightScale
);
191 void OverviewToolbar::TransformButton(ActionButton
* button
) {
192 ui::ScopedLayerAnimationSettings
split_settings(
193 button
->layer()->GetAnimator());
194 split_settings
.SetTweenType(gfx::Tween::SMOOTH_IN_OUT
);
195 button
->layer()->SetTransform(ComputeTransformFor(button
));
196 button
->layer()->SetOpacity(shown_
? 1 : 0);
199 } // namespace athena