OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/system/tray_drag_controller.h" | |
6 | |
7 #include "ash/shell.h" | |
8 #include "ash/system/tray/tray_background_view.h" | |
9 #include "ash/wm/maximize_mode/maximize_mode_controller.h" | |
10 | |
11 namespace ash { | |
12 | |
13 TrayDragController::TrayDragController(Shelf* shelf) : shelf_(shelf) {} | |
14 | |
15 bool TrayDragController::ProcessGestureEvent(ui::GestureEvent* event, | |
16 TrayBackgroundView* target_view) { | |
msw
2017/07/14 02:00:46
Maybe rename |target_view| to |tray_view| or simil
minch1
2017/07/14 19:45:18
Done.
| |
17 if (!Shell::Get() | |
18 ->maximize_mode_controller() | |
19 ->IsMaximizeModeWindowManagerEnabled() || | |
20 !shelf_->IsHorizontalAlignment()) { | |
21 return false; | |
22 } | |
23 | |
24 target_view_ = target_view; | |
25 is_on_bubble_ = event->target() != target_view; | |
26 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | |
27 bool should_start_dragging = StartGestureDrag(*event); | |
28 if (should_start_dragging) | |
29 event->SetHandled(); | |
30 return should_start_dragging; | |
31 } | |
32 | |
33 if (!target_view_->GetBubbleView() || !is_in_drag_) | |
34 return false; | |
35 | |
36 if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | |
37 UpdateGestureDrag(*event); | |
38 event->SetHandled(); | |
39 return true; | |
40 } | |
41 | |
42 if (event->type() == ui::ET_GESTURE_SCROLL_END || | |
43 event->type() == ui::ET_SCROLL_FLING_START) { | |
44 CompleteGestureDrag(*event); | |
45 event->SetHandled(); | |
46 return true; | |
47 } | |
48 | |
49 // Unexpected event. Reset the drag state and close the bubble. | |
50 is_in_drag_ = false; | |
51 target_view_->CloseBubble(); | |
52 return false; | |
53 } | |
54 | |
55 bool TrayDragController::StartGestureDrag(const ui::GestureEvent& gesture) { | |
56 if (!is_on_bubble_) { | |
57 // Dragging happens on the tray view. Close the bubble if there is already | |
58 // one opened. And return false to let shelf handle the event. | |
59 if (target_view_->GetBubbleView()) { | |
60 target_view_->CloseBubble(); | |
61 return false; | |
62 } | |
63 | |
64 // If the scroll sequence begins to scroll downward, return false so that | |
65 // the event will instead by handled by the shelf. | |
66 if (gesture.details().scroll_y_hint() > 0) | |
67 return false; | |
68 | |
69 target_view_->ShowBubble(); | |
70 } | |
71 | |
72 is_in_drag_ = true; | |
73 gesture_drag_amount_ = 0.f; | |
74 tray_bubble_bounds_ = | |
75 target_view_->GetBubbleView()->GetWidget()->GetWindowBoundsInScreen(); | |
76 UpdateBubbleBounds(); | |
77 return true; | |
78 } | |
79 | |
80 void TrayDragController::UpdateGestureDrag(const ui::GestureEvent& gesture) { | |
81 gesture_drag_amount_ += gesture.details().scroll_y(); | |
82 UpdateBubbleBounds(); | |
83 } | |
84 | |
85 void TrayDragController::CompleteGestureDrag(const ui::GestureEvent& gesture) { | |
86 const bool hide_bubble = !ShouldShowBubbleAfterScrollSequence(gesture); | |
87 gfx::Rect target_bounds = tray_bubble_bounds_; | |
88 | |
89 if (hide_bubble) | |
90 target_bounds.set_y(shelf_->GetIdealBounds().y()); | |
91 | |
92 target_view_->AnimateToTargetBounds(target_bounds, hide_bubble); | |
93 is_in_drag_ = false; | |
94 } | |
95 | |
96 void TrayDragController::UpdateBubbleBounds() { | |
97 gfx::Rect current_tray_bubble_bounds = tray_bubble_bounds_; | |
98 int bounds_y = | |
99 (is_on_bubble_ ? tray_bubble_bounds_.y() : shelf_->GetIdealBounds().y()) + | |
100 gesture_drag_amount_; | |
101 current_tray_bubble_bounds.set_y(std::max(bounds_y, tray_bubble_bounds_.y())); | |
102 target_view_->GetBubbleView()->GetWidget()->SetBounds( | |
103 current_tray_bubble_bounds); | |
104 } | |
105 | |
106 bool TrayDragController::ShouldShowBubbleAfterScrollSequence( | |
107 const ui::GestureEvent& sequence_end) { | |
108 // If the scroll sequence terminates with a fling, show the bubble if the | |
109 // fling was fast enough and in the correct direction. | |
110 if (sequence_end.type() == ui::ET_SCROLL_FLING_START && | |
111 fabs(sequence_end.details().velocity_y()) > kFlingVelocity) { | |
112 return sequence_end.details().velocity_y() < 0; | |
113 } | |
114 | |
115 DCHECK(sequence_end.type() == ui::ET_GESTURE_SCROLL_END || | |
116 sequence_end.type() == ui::ET_SCROLL_FLING_START); | |
117 | |
118 // Keep the bubble's original state if the |gesture_drag_amount_| doesn't | |
119 // exceed one-third of the bubble's height. | |
120 if (is_on_bubble_) | |
121 return gesture_drag_amount_ < tray_bubble_bounds_.height() / 3.0; | |
122 return -gesture_drag_amount_ >= tray_bubble_bounds_.height() / 3.0; | |
123 } | |
124 | |
125 } // namespace ash | |
OLD | NEW |