Chromium Code Reviews| 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/wm/overview/overview_window_drag_controller.h" | |
| 6 | |
| 7 #include "ash/screen_util.h" | |
| 8 #include "ash/shell.h" | |
| 9 #include "ash/wm/overview/scoped_transform_overview_window.h" | |
| 10 #include "ash/wm/overview/window_selector.h" | |
| 11 #include "ash/wm/overview/window_selector_item.h" | |
| 12 #include "ash/wm/splitview/split_view_controller.h" | |
| 13 #include "ash/wm/window_positioning_utils.h" | |
| 14 #include "ash/wm/window_state.h" | |
| 15 #include "ash/wm/wm_event.h" | |
| 16 #include "ash/wm/workspace/phantom_window_controller.h" | |
| 17 #include "ui/aura/window.h" | |
| 18 #include "ui/wm/core/coordinate_conversion.h" | |
| 19 | |
| 20 namespace ash { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // The minimum offset that will be considered as a drag event. | |
| 25 const int kMinimiumDragOffset = 5; | |
| 26 | |
| 27 // Snapping distance between the dragged window with the screen edge. It's | |
| 28 // useful especially for touch events. | |
| 29 const int kScreenEdgeInsetForDrag = 200; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 OverviewWindowDragController::OverviewWindowDragController( | |
| 34 WindowSelector* window_selector) | |
| 35 : window_selector_(window_selector), | |
| 36 split_view_controller_(Shell::Get()->split_view_controller()) {} | |
| 37 | |
| 38 OverviewWindowDragController::~OverviewWindowDragController() {} | |
| 39 | |
| 40 void OverviewWindowDragController::InitiateDrag( | |
| 41 WindowSelectorItem* item, | |
| 42 const gfx::Point& location_in_screen) { | |
| 43 initial_event_location_ = location_in_screen; | |
| 44 previous_event_location_ = location_in_screen; | |
| 45 item_ = item; | |
| 46 } | |
| 47 | |
| 48 void OverviewWindowDragController::Drag(WindowSelectorItem* item, | |
| 49 const gfx::Point& location_in_screen) { | |
| 50 DCHECK_EQ(item_, item); | |
| 51 | |
| 52 if (!did_move_ && | |
| 53 (std::abs(location_in_screen.x() - initial_event_location_.x()) < | |
| 54 kMinimiumDragOffset || | |
| 55 std::abs(location_in_screen.y() - initial_event_location_.y()) < | |
| 56 kMinimiumDragOffset)) { | |
| 57 return; | |
| 58 } | |
| 59 did_move_ = true; | |
| 60 | |
| 61 // Updates the dragged |item_|'s board accordingly. | |
| 62 gfx::Rect bounds(item_->target_bounds()); | |
| 63 bounds.Offset(location_in_screen.x() - previous_event_location_.x(), | |
| 64 location_in_screen.y() - previous_event_location_.y()); | |
| 65 item_->SetBounds(bounds, OverviewAnimationType::OVERVIEW_ANIMATION_NONE); | |
| 66 previous_event_location_ = location_in_screen; | |
| 67 | |
| 68 // Then updates the phantom window if applicable. | |
| 69 UpdatePhantomWindow(location_in_screen); | |
| 70 } | |
| 71 | |
| 72 void OverviewWindowDragController::CompleteDrag(WindowSelectorItem* item) { | |
| 73 DCHECK_EQ(item_, item); | |
| 74 | |
| 75 phantom_window_controller_.reset(); | |
| 76 | |
| 77 if (!did_move_) { | |
| 78 // If no drag was initiated (e.g., a click/tap on the overview window), | |
| 79 // activate the window. If the split view is active and has a left window, | |
| 80 // snap the current window to right. If the split view is active and has a | |
| 81 // right window, snap the current window to left. | |
| 82 SplitViewController::State split_state = split_view_controller_->state(); | |
| 83 if (split_state == SplitViewController::NOSNAP) | |
| 84 window_selector_->SelectWindow(item); | |
| 85 else if (split_state == SplitViewController::LEFT_SNAPPED) | |
| 86 SnapToRight(); | |
| 87 else if (split_state == SplitViewController::RIGHT_SNAPPED) | |
| 88 SnapToLeft(); | |
| 89 } else { | |
| 90 // If the window was dragged around but should not be snapped, move it back | |
| 91 // to overview window grid. | |
| 92 if (snap_type_ == SNAP_NONE) | |
| 93 window_selector_->PositionWindows(true /* animate */); | |
| 94 else if (snap_type_ == SNAP_LEFT) | |
| 95 SnapToLeft(); | |
| 96 else | |
| 97 SnapToRight(); | |
| 98 did_move_ = false; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void OverviewWindowDragController::UpdatePhantomWindow( | |
| 103 const gfx::Point& location_in_screen) { | |
| 104 SnapType last_snap_type = snap_type_; | |
| 105 snap_type_ = GetSnapType(location_in_screen); | |
| 106 if (snap_type_ == SNAP_NONE || snap_type_ != last_snap_type) { | |
| 107 phantom_window_controller_.reset(); | |
| 108 if (snap_type_ == SNAP_NONE) | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 const bool can_snap = snap_type_ != SNAP_NONE && | |
| 113 wm::GetWindowState(item_->GetWindow())->CanSnap(); | |
| 114 if (!can_snap) { | |
| 115 snap_type_ = SNAP_NONE; | |
| 116 phantom_window_controller_.reset(); | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 aura::Window* target_window = item_->GetWindow(); | |
| 121 gfx::Rect phantom_bounds_in_screen = | |
| 122 (snap_type_ == SNAP_LEFT) | |
| 123 ? Shell::Get() | |
| 124 ->split_view_controller() | |
| 125 ->GetSnappedWindowBoundsInScreen( | |
| 126 target_window, SplitViewController::LEFT_SNAPPED) | |
| 127 : Shell::Get() | |
| 128 ->split_view_controller() | |
| 129 ->GetSnappedWindowBoundsInScreen( | |
| 130 target_window, SplitViewController::RIGHT_SNAPPED); | |
|
oshima
2017/06/16 01:20:57
snap_type == LEFT ? SplitViewController::LEFT_SNAP
xdai1
2017/06/16 18:49:52
Done. I think use two enum might be better here si
| |
| 131 | |
| 132 if (!phantom_window_controller_) { | |
| 133 phantom_window_controller_ = | |
| 134 base::MakeUnique<PhantomWindowController>(target_window); | |
| 135 } | |
| 136 phantom_window_controller_->Show(phantom_bounds_in_screen); | |
| 137 } | |
| 138 | |
| 139 OverviewWindowDragController::SnapType | |
| 140 OverviewWindowDragController::GetSnapType( | |
| 141 const gfx::Point& location_in_screen) { | |
| 142 gfx::Rect area( | |
| 143 ScreenUtil::GetDisplayWorkAreaBoundsInParent(item_->GetWindow())); | |
| 144 ::wm::ConvertRectToScreen(item_->GetWindow()->GetRootWindow(), &area); | |
| 145 area.Inset(kScreenEdgeInsetForDrag, 0, kScreenEdgeInsetForDrag, 0); | |
| 146 | |
| 147 if (location_in_screen.x() <= area.x()) | |
| 148 return SNAP_LEFT; | |
| 149 if (location_in_screen.x() >= area.right() - 1) | |
| 150 return SNAP_RIGHT; | |
| 151 return SNAP_NONE; | |
| 152 } | |
| 153 | |
| 154 void OverviewWindowDragController::SnapToLeft() { | |
| 155 item_->EnsureVisible(); | |
| 156 item_->RestoreWindow(); | |
| 157 | |
| 158 // |item_| will be deleted after RemoveWindowSelectorItem(). | |
| 159 aura::Window* left_window = item_->GetWindow(); | |
| 160 window_selector_->RemoveWindowSelectorItem(item_); | |
| 161 split_view_controller_->SetLeftWindow(left_window); | |
| 162 item_ = nullptr; | |
| 163 } | |
| 164 | |
| 165 void OverviewWindowDragController::SnapToRight() { | |
| 166 item_->EnsureVisible(); | |
| 167 item_->RestoreWindow(); | |
| 168 | |
| 169 // |item_| will be deleted after RemoveWindowSelectorItem(). | |
| 170 aura::Window* right_window = item_->GetWindow(); | |
| 171 window_selector_->RemoveWindowSelectorItem(item_); | |
| 172 split_view_controller_->SetRightWindow(right_window); | |
| 173 item_ = nullptr; | |
| 174 } | |
| 175 | |
| 176 } // namespace ash | |
| OLD | NEW |