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/frame/caption_buttons/frame_caption_button_container_view.h"
10 #include "ash/ash_switches.h"
11 #include "ash/frame/caption_buttons/frame_caption_button.h"
12 #include "ash/frame/caption_buttons/frame_size_button.h"
13 #include "ash/metrics/user_metrics_recorder.h"
14 #include "ash/shell.h"
15 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
16 #include "ui/base/hit_test.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
19 #include "ui/gfx/animation/slide_animation.h"
20 #include "ui/gfx/animation/tween.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/geometry/insets.h"
23 #include "ui/gfx/geometry/point.h"
24 #include "ui/strings/grit/ui_strings.h" // Accessibility names
25 #include "ui/views/widget/widget.h"
26 #include "ui/views/widget/widget_delegate.h"
32 // Duration of the animation of the position of |minimize_button_|.
33 const int kPositionAnimationDurationMs
= 500;
35 // Duration of the animation of the alpha of |size_button_|.
36 const int kAlphaAnimationDurationMs
= 250;
38 // Delay during |maximize_mode_animation_| hide to wait before beginning to
39 // animate the position of |minimize_button_|.
40 const int kHidePositionDelayMs
= 100;
42 // Duration of |maximize_mode_animation_| hiding.
43 // Hiding size button 250
44 // |------------------------|
45 // Delay 100 Slide minimize button 500
46 // |---------|-------------------------------------------------|
47 const int kHideAnimationDurationMs
=
48 kHidePositionDelayMs
+ kPositionAnimationDurationMs
;
50 // Delay during |maximize_mode_animation_| show to wait before beginning to
51 // animate the alpha of |size_button_|.
52 const int kShowAnimationAlphaDelayMs
= 100;
54 // Duration of |maximize_mode_animation_| showing.
55 // Slide minimize button 500
56 // |-------------------------------------------------|
57 // Delay 100 Show size button 250
58 // |---------|-----------------------|
59 const int kShowAnimationDurationMs
= kPositionAnimationDurationMs
;
61 // Value of |maximize_mode_animation_| showing to begin animating alpha of
63 float SizeButtonShowStartValue() {
64 return static_cast<float>(kShowAnimationAlphaDelayMs
)
65 / kShowAnimationDurationMs
;
68 // Amount of |maximize_mode_animation_| showing to animate the alpha of
70 float SizeButtonShowDuration() {
71 return static_cast<float>(kAlphaAnimationDurationMs
)
72 / kShowAnimationDurationMs
;
75 // Amount of |maximize_mode_animation_| hiding to animate the alpha of
77 float SizeButtonHideDuration() {
78 return static_cast<float>(kAlphaAnimationDurationMs
)
79 / kHideAnimationDurationMs
;
82 // Value of |maximize_mode_animation_| hiding to begin animating the position of
83 // |minimize_button_|.
84 float HidePositionStartValue() {
85 return 1.0f
- static_cast<float>(kHidePositionDelayMs
)
86 / kHideAnimationDurationMs
;
89 // Converts |point| from |src| to |dst| and hittests against |dst|.
90 bool ConvertPointToViewAndHitTest(const views::View
* src
,
91 const views::View
* dst
,
92 const gfx::Point
& point
) {
93 gfx::Point
converted(point
);
94 views::View::ConvertPointToTarget(src
, dst
, &converted
);
95 return dst
->HitTestPoint(converted
);
98 // Bounds animation values to the range 0.0 - 1.0. Allows for mapping of offset
99 // animations to the expected range so that gfx::Tween::CalculateValue() can be
101 double CapAnimationValue(double value
) {
102 return std::min(1.0, std::max(0.0, value
));
108 const char FrameCaptionButtonContainerView::kViewClassName
[] =
109 "FrameCaptionButtonContainerView";
111 FrameCaptionButtonContainerView::FrameCaptionButtonContainerView(
112 views::Widget
* frame
)
114 minimize_button_(NULL
),
116 close_button_(NULL
) {
117 bool size_button_visibility
= ShouldSizeButtonBeVisible();
118 maximize_mode_animation_
.reset(new gfx::SlideAnimation(this));
119 maximize_mode_animation_
->SetTweenType(gfx::Tween::LINEAR
);
121 // Ensure animation tracks visibility of size button.
122 if (size_button_visibility
)
123 maximize_mode_animation_
->Reset(1.0f
);
125 // Insert the buttons left to right.
126 minimize_button_
= new FrameCaptionButton(this, CAPTION_BUTTON_ICON_MINIMIZE
);
127 minimize_button_
->SetAccessibleName(
128 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_MINIMIZE
));
129 minimize_button_
->SetVisible(frame_
->widget_delegate()->CanMinimize());
130 AddChildView(minimize_button_
);
132 size_button_
= new FrameSizeButton(this, frame
, this);
133 size_button_
->SetAccessibleName(
134 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_MAXIMIZE
));
135 size_button_
->SetVisible(size_button_visibility
);
136 AddChildView(size_button_
);
138 close_button_
= new FrameCaptionButton(this, CAPTION_BUTTON_ICON_CLOSE
);
139 close_button_
->SetAccessibleName(
140 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE
));
141 AddChildView(close_button_
);
144 FrameCaptionButtonContainerView::~FrameCaptionButtonContainerView() {
147 void FrameCaptionButtonContainerView::TestApi::EndAnimations() {
148 container_view_
->maximize_mode_animation_
->End();
151 void FrameCaptionButtonContainerView::SetButtonImages(
152 CaptionButtonIcon icon
,
154 int hovered_background_image_id
,
155 int pressed_background_image_id
) {
156 button_icon_id_map_
[icon
] = ButtonIconIds(icon_image_id
,
157 hovered_background_image_id
,
158 pressed_background_image_id
);
159 FrameCaptionButton
* buttons
[] = {
160 minimize_button_
, size_button_
, close_button_
162 for (size_t i
= 0; i
< arraysize(buttons
); ++i
) {
163 if (buttons
[i
]->icon() == icon
) {
164 buttons
[i
]->SetImages(icon
,
165 FrameCaptionButton::ANIMATE_NO
,
167 hovered_background_image_id
,
168 pressed_background_image_id
);
173 void FrameCaptionButtonContainerView::SetPaintAsActive(bool paint_as_active
) {
174 minimize_button_
->set_paint_as_active(paint_as_active
);
175 size_button_
->set_paint_as_active(paint_as_active
);
176 close_button_
->set_paint_as_active(paint_as_active
);
179 void FrameCaptionButtonContainerView::ResetWindowControls() {
180 SetButtonsToNormal(ANIMATE_NO
);
183 int FrameCaptionButtonContainerView::NonClientHitTest(
184 const gfx::Point
& point
) const {
185 if (close_button_
->visible() &&
186 ConvertPointToViewAndHitTest(this, close_button_
, point
)) {
188 } else if (size_button_
->visible() &&
189 ConvertPointToViewAndHitTest(this, size_button_
, point
)) {
191 } else if (minimize_button_
->visible() &&
192 ConvertPointToViewAndHitTest(this, minimize_button_
, point
)) {
198 void FrameCaptionButtonContainerView::UpdateSizeButtonVisibility() {
199 bool visible
= ShouldSizeButtonBeVisible();
201 size_button_
->SetVisible(true);
202 maximize_mode_animation_
->SetSlideDuration(kShowAnimationDurationMs
);
203 maximize_mode_animation_
->Show();
205 maximize_mode_animation_
->SetSlideDuration(kHideAnimationDurationMs
);
206 maximize_mode_animation_
->Hide();
210 gfx::Size
FrameCaptionButtonContainerView::GetPreferredSize() const {
212 for (int i
= 0; i
< child_count(); ++i
) {
213 const views::View
* child
= child_at(i
);
214 if (child
->visible())
215 width
+= child_at(i
)->GetPreferredSize().width();
217 return gfx::Size(width
, close_button_
->GetPreferredSize().height());
220 void FrameCaptionButtonContainerView::Layout() {
222 for (int i
= 0; i
< child_count(); ++i
) {
223 views::View
* child
= child_at(i
);
224 if (!child
->visible())
227 gfx::Size size
= child
->GetPreferredSize();
228 child
->SetBounds(x
, 0, size
.width(), size
.height());
231 if (maximize_mode_animation_
->is_animating()) {
232 AnimationProgressed(maximize_mode_animation_
.get());
236 const char* FrameCaptionButtonContainerView::GetClassName() const {
237 return kViewClassName
;
240 void FrameCaptionButtonContainerView::AnimationEnded(
241 const gfx::Animation
* animation
) {
242 // Ensure that position is calculated at least once.
243 AnimationProgressed(animation
);
245 double current_value
= maximize_mode_animation_
->GetCurrentValue();
246 if (current_value
== 0.0) {
247 size_button_
->SetVisible(false);
248 PreferredSizeChanged();
252 void FrameCaptionButtonContainerView::AnimationProgressed(
253 const gfx::Animation
* animation
) {
254 double current_value
= animation
->GetCurrentValue();
257 if (maximize_mode_animation_
->IsShowing()) {
258 double scaled_value
= CapAnimationValue(
259 (current_value
- SizeButtonShowStartValue())
260 / SizeButtonShowDuration());
261 double tweened_value_alpha
=
262 gfx::Tween::CalculateValue(gfx::Tween::EASE_OUT
,scaled_value
);
263 size_alpha
= gfx::Tween::LinearIntValueBetween(tweened_value_alpha
, 0, 255);
265 double tweened_value_slide
=
266 gfx::Tween::CalculateValue(gfx::Tween::EASE_OUT
, current_value
);
267 minimize_x
= gfx::Tween::LinearIntValueBetween(tweened_value_slide
,
268 size_button_
->x(), 0);
270 double scaled_value_alpha
= CapAnimationValue(
271 (1.0f
- current_value
) / SizeButtonHideDuration());
272 double tweened_value_alpha
=
273 gfx::Tween::CalculateValue(gfx::Tween::EASE_IN
, scaled_value_alpha
);
274 size_alpha
= gfx::Tween::LinearIntValueBetween(tweened_value_alpha
, 255, 0);
276 double scaled_value_position
= CapAnimationValue(
277 (HidePositionStartValue() - current_value
)
278 / HidePositionStartValue());
279 double tweened_value_position
=
280 gfx::Tween::CalculateValue(gfx::Tween::EASE_OUT
, scaled_value_position
);
281 minimize_x
= gfx::Tween::LinearIntValueBetween(tweened_value_position
, 0,
284 size_button_
->SetAlpha(size_alpha
);
285 minimize_button_
->SetX(minimize_x
);
288 void FrameCaptionButtonContainerView::SetButtonIcon(FrameCaptionButton
* button
,
289 CaptionButtonIcon icon
,
291 // The early return is dependant on |animate| because callers use
292 // SetButtonIcon() with ANIMATE_NO to progress |button|'s crossfade animation
294 if (button
->icon() == icon
&&
295 (animate
== ANIMATE_YES
|| !button
->IsAnimatingImageSwap())) {
299 FrameCaptionButton::Animate fcb_animate
= (animate
== ANIMATE_YES
) ?
300 FrameCaptionButton::ANIMATE_YES
: FrameCaptionButton::ANIMATE_NO
;
301 std::map
<CaptionButtonIcon
, ButtonIconIds
>::const_iterator it
=
302 button_icon_id_map_
.find(icon
);
303 if (it
!= button_icon_id_map_
.end()) {
304 button
->SetImages(icon
,
306 it
->second
.icon_image_id
,
307 it
->second
.hovered_background_image_id
,
308 it
->second
.pressed_background_image_id
);
312 bool FrameCaptionButtonContainerView::ShouldSizeButtonBeVisible() const {
313 return !Shell::GetInstance()->maximize_mode_controller()->
314 IsMaximizeModeWindowManagerEnabled() &&
315 frame_
->widget_delegate()->CanMaximize();
318 void FrameCaptionButtonContainerView::ButtonPressed(views::Button
* sender
,
319 const ui::Event
& event
) {
320 // Abort any animations of the button icons.
321 SetButtonsToNormal(ANIMATE_NO
);
323 ash::UserMetricsAction action
=
324 ash::UMA_WINDOW_MAXIMIZE_BUTTON_CLICK_MINIMIZE
;
325 if (sender
== minimize_button_
) {
327 } else if (sender
== size_button_
) {
328 if (frame_
->IsFullscreen()) { // Can be clicked in immersive fullscreen.
330 action
= ash::UMA_WINDOW_MAXIMIZE_BUTTON_CLICK_EXIT_FULLSCREEN
;
331 } else if (frame_
->IsMaximized()) {
333 action
= ash::UMA_WINDOW_MAXIMIZE_BUTTON_CLICK_RESTORE
;
336 action
= ash::UMA_WINDOW_MAXIMIZE_BUTTON_CLICK_MAXIMIZE
;
338 } else if (sender
== close_button_
) {
340 action
= ash::UMA_WINDOW_CLOSE_BUTTON_CLICK
;
344 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction(action
);
347 bool FrameCaptionButtonContainerView::IsMinimizeButtonVisible() const {
348 return minimize_button_
->visible();
351 void FrameCaptionButtonContainerView::SetButtonsToNormal(Animate animate
) {
352 SetButtonIcons(CAPTION_BUTTON_ICON_MINIMIZE
, CAPTION_BUTTON_ICON_CLOSE
,
354 minimize_button_
->SetState(views::Button::STATE_NORMAL
);
355 size_button_
->SetState(views::Button::STATE_NORMAL
);
356 close_button_
->SetState(views::Button::STATE_NORMAL
);
359 void FrameCaptionButtonContainerView::SetButtonIcons(
360 CaptionButtonIcon minimize_button_icon
,
361 CaptionButtonIcon close_button_icon
,
363 SetButtonIcon(minimize_button_
, minimize_button_icon
, animate
);
364 SetButtonIcon(close_button_
, close_button_icon
, animate
);
367 const FrameCaptionButton
* FrameCaptionButtonContainerView::GetButtonClosestTo(
368 const gfx::Point
& position_in_screen
) const {
369 // Since the buttons all have the same size, the closest button is the button
370 // with the center point closest to |position_in_screen|.
371 // TODO(pkotwicz): Make the caption buttons not overlap.
372 gfx::Point
position(position_in_screen
);
373 views::View::ConvertPointFromScreen(this, &position
);
375 FrameCaptionButton
* buttons
[] = {
376 minimize_button_
, size_button_
, close_button_
378 int min_squared_distance
= INT_MAX
;
379 FrameCaptionButton
* closest_button
= NULL
;
380 for (size_t i
= 0; i
< arraysize(buttons
); ++i
) {
381 FrameCaptionButton
* button
= buttons
[i
];
382 if (!button
->visible())
385 gfx::Point center_point
= button
->GetLocalBounds().CenterPoint();
386 views::View::ConvertPointToTarget(button
, this, ¢er_point
);
387 int squared_distance
= static_cast<int>(
388 pow(static_cast<double>(position
.x() - center_point
.x()), 2) +
389 pow(static_cast<double>(position
.y() - center_point
.y()), 2));
390 if (squared_distance
< min_squared_distance
) {
391 min_squared_distance
= squared_distance
;
392 closest_button
= button
;
395 return closest_button
;
398 void FrameCaptionButtonContainerView::SetHoveredAndPressedButtons(
399 const FrameCaptionButton
* to_hover
,
400 const FrameCaptionButton
* to_press
) {
401 FrameCaptionButton
* buttons
[] = {
402 minimize_button_
, size_button_
, close_button_
404 for (size_t i
= 0; i
< arraysize(buttons
); ++i
) {
405 FrameCaptionButton
* button
= buttons
[i
];
406 views::Button::ButtonState new_state
= views::Button::STATE_NORMAL
;
407 if (button
== to_hover
)
408 new_state
= views::Button::STATE_HOVERED
;
409 else if (button
== to_press
)
410 new_state
= views::Button::STATE_PRESSED
;
411 button
->SetState(new_state
);
415 FrameCaptionButtonContainerView::ButtonIconIds::ButtonIconIds()
417 hovered_background_image_id(-1),
418 pressed_background_image_id(-1) {
421 FrameCaptionButtonContainerView::ButtonIconIds::ButtonIconIds(
423 int hovered_background_id
,
424 int pressed_background_id
)
425 : icon_image_id(icon_id
),
426 hovered_background_image_id(hovered_background_id
),
427 pressed_background_image_id(pressed_background_id
) {
430 FrameCaptionButtonContainerView::ButtonIconIds::~ButtonIconIds() {