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