1 // Copyright (c) 2012 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/gestures/tray_gesture_handler.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/system/tray/system_tray_bubble.h"
10 #include "ui/aura/window.h"
11 #include "ui/compositor/layer.h"
12 #include "ui/events/event.h"
13 #include "ui/gfx/transform.h"
14 #include "ui/views/widget/widget.h"
16 const int kMinBubbleHeight
= 13;
20 TrayGestureHandler::TrayGestureHandler()
22 gesture_drag_amount_(0) {
23 // TODO(oshima): Support multiple display case.
24 SystemTray
* tray
= Shell::GetInstance()->GetPrimarySystemTray();
25 tray
->ShowDefaultView(BUBBLE_CREATE_NEW
);
26 SystemTrayBubble
* bubble
= tray
->GetSystemBubble();
29 bubble
->bubble_view()->set_gesture_dragging(true);
30 widget_
= bubble
->bubble_view()->GetWidget();
31 widget_
->AddObserver(this);
33 gfx::Rect bounds
= widget_
->GetWindowBoundsInScreen();
34 int height_change
= bounds
.height() - kMinBubbleHeight
;
35 bounds
.set_height(kMinBubbleHeight
);
36 bounds
.set_y(bounds
.y() + height_change
);
37 widget_
->SetBounds(bounds
);
40 TrayGestureHandler::~TrayGestureHandler() {
42 widget_
->RemoveObserver(this);
45 bool TrayGestureHandler::UpdateGestureDrag(const ui::GestureEvent
& event
) {
46 CHECK_EQ(ui::ET_GESTURE_SCROLL_UPDATE
, event
.type());
50 gesture_drag_amount_
+= event
.details().scroll_y();
51 if (gesture_drag_amount_
> 0 && gesture_drag_amount_
< kMinBubbleHeight
) {
56 gfx::Rect bounds
= widget_
->GetWindowBoundsInScreen();
57 int new_height
= std::min(
58 kMinBubbleHeight
+ std::max(0, static_cast<int>(-gesture_drag_amount_
)),
59 widget_
->GetContentsView()->GetPreferredSize().height());
60 int height_change
= bounds
.height() - new_height
;
61 bounds
.set_height(new_height
);
62 bounds
.set_y(bounds
.y() + height_change
);
63 widget_
->SetBounds(bounds
);
67 void TrayGestureHandler::CompleteGestureDrag(const ui::GestureEvent
& event
) {
71 widget_
->RemoveObserver(this);
73 // Close the widget if it hasn't been dragged enough.
74 bool should_close
= false;
75 int height
= widget_
->GetWindowBoundsInScreen().height();
76 int preferred_height
=
77 widget_
->GetContentsView()->GetPreferredSize().height();
78 if (event
.type() == ui::ET_GESTURE_SCROLL_END
) {
79 const float kMinThresholdGestureDrag
= 0.4f
;
80 if (height
< preferred_height
* kMinThresholdGestureDrag
)
82 } else if (event
.type() == ui::ET_SCROLL_FLING_START
) {
83 const float kMinThresholdGestureDragExposeFling
= 0.25f
;
84 const float kMinThresholdGestureFling
= 1000.f
;
85 if (height
< preferred_height
* kMinThresholdGestureDragExposeFling
&&
86 event
.details().velocity_y() > -kMinThresholdGestureFling
)
95 SystemTrayBubble
* bubble
=
96 Shell::GetInstance()->GetPrimarySystemTray()->GetSystemBubble();
98 bubble
->bubble_view()->set_gesture_dragging(false);
102 void TrayGestureHandler::OnWidgetDestroying(views::Widget
* widget
) {
103 CHECK_EQ(widget_
, widget
);