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* tray_view) { | |
17 if (!Shell::Get() | |
18 ->maximize_mode_controller() | |
19 ->IsMaximizeModeWindowManagerEnabled() || | |
20 !shelf_->IsHorizontalAlignment()) { | |
21 return false; | |
22 } | |
23 | |
24 tray_view_ = tray_view; | |
25 is_on_bubble_ = event->target() != tray_view; | |
26 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | |
27 bool should_start_dragging = StartGestureDrag(*event); | |
tdanderson
2017/07/17 22:44:37
nit: const
minch1
2017/07/18 03:58:59
Done.
| |
28 if (should_start_dragging) | |
29 event->SetHandled(); | |
30 return should_start_dragging; | |
31 } | |
32 | |
33 if (!tray_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 tray_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 (tray_view_->GetBubbleView()) { | |
60 tray_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 tray_view_->ShowBubble(); | |
70 } | |
71 | |
72 if (!tray_view_->GetBubbleView()) | |
73 return false; | |
74 | |
75 is_in_drag_ = true; | |
76 gesture_drag_amount_ = 0.f; | |
77 tray_bubble_bounds_ = | |
78 tray_view_->GetBubbleView()->GetWidget()->GetWindowBoundsInScreen(); | |
79 UpdateBubbleBounds(); | |
80 return true; | |
81 } | |
82 | |
83 void TrayDragController::UpdateGestureDrag(const ui::GestureEvent& gesture) { | |
84 gesture_drag_amount_ += gesture.details().scroll_y(); | |
85 UpdateBubbleBounds(); | |
86 } | |
87 | |
88 void TrayDragController::CompleteGestureDrag(const ui::GestureEvent& gesture) { | |
89 const bool hide_bubble = !ShouldShowBubbleAfterScrollSequence(gesture); | |
90 gfx::Rect target_bounds = tray_bubble_bounds_; | |
91 | |
92 if (hide_bubble) | |
93 target_bounds.set_y(shelf_->GetIdealBounds().y()); | |
94 | |
95 tray_view_->AnimateToTargetBounds(target_bounds, hide_bubble); | |
96 is_in_drag_ = false; | |
97 } | |
98 | |
99 void TrayDragController::UpdateBubbleBounds() { | |
100 DCHECK(tray_view_->GetBubbleView()); | |
101 | |
102 gfx::Rect current_tray_bubble_bounds = tray_bubble_bounds_; | |
103 int bounds_y = | |
tdanderson
2017/07/17 22:44:37
nit: const
minch1
2017/07/18 03:58:59
Done.
| |
104 (is_on_bubble_ ? tray_bubble_bounds_.y() : shelf_->GetIdealBounds().y()) + | |
105 gesture_drag_amount_; | |
106 current_tray_bubble_bounds.set_y(std::max(bounds_y, tray_bubble_bounds_.y())); | |
107 tray_view_->GetBubbleView()->GetWidget()->SetBounds( | |
108 current_tray_bubble_bounds); | |
109 } | |
110 | |
111 bool TrayDragController::ShouldShowBubbleAfterScrollSequence( | |
112 const ui::GestureEvent& sequence_end) { | |
113 // If the scroll sequence terminates with a fling, show the bubble if the | |
114 // fling was fast enough and in the correct direction. | |
tdanderson
2017/07/17 22:44:37
If |is_on_bubble_| is true and I'm flinging downwa
minch1
2017/07/18 03:58:59
Yes, that should close the bubble. And the logic h
tdanderson
2017/07/18 16:38:05
My mistake, sorry, thank you for clarifying. I had
| |
115 if (sequence_end.type() == ui::ET_SCROLL_FLING_START && | |
116 fabs(sequence_end.details().velocity_y()) > kFlingVelocity) { | |
117 return sequence_end.details().velocity_y() < 0; | |
118 } | |
119 | |
120 DCHECK(sequence_end.type() == ui::ET_GESTURE_SCROLL_END || | |
121 sequence_end.type() == ui::ET_SCROLL_FLING_START); | |
122 | |
123 // Keep the bubble's original state if the |gesture_drag_amount_| doesn't | |
124 // exceed one-third of the bubble's height. | |
125 if (is_on_bubble_) | |
126 return gesture_drag_amount_ < tray_bubble_bounds_.height() / 3.0; | |
127 return -gesture_drag_amount_ >= tray_bubble_bounds_.height() / 3.0; | |
128 } | |
129 | |
130 } // namespace ash | |
OLD | NEW |