OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ash/wm/workspace/workspace_cycler.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/wm/workspace/workspace_manager.h" |
| 9 #include "ui/base/events/event.h" |
| 10 #include "ui/base/events/event_utils.h" |
| 11 |
| 12 namespace ash { |
| 13 namespace internal { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // The required vertical distance to scrub to the next workspace. |
| 18 const int kWorkspaceStepSize = 10; |
| 19 |
| 20 // Returns true is scrubbing is enabled. |
| 21 bool IsScrubbingEnabled() { |
| 22 // Scrubbing is disabled if the screen is locked or a modal dialog is open. |
| 23 return !Shell::GetInstance()->IsScreenLocked() && |
| 24 !Shell::GetInstance()->IsSystemModalWindowOpen(); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 WorkspaceCycler::WorkspaceCycler(WorkspaceManager* workspace_manager) |
| 30 : workspace_manager_(workspace_manager), |
| 31 scrubbing_(false), |
| 32 scroll_x_(0), |
| 33 scroll_y_(0) { |
| 34 ash::Shell::GetInstance()->AddPreTargetHandler(this); |
| 35 } |
| 36 |
| 37 WorkspaceCycler::~WorkspaceCycler() { |
| 38 scrubbing_ = false; |
| 39 ash::Shell::GetInstance()->RemovePreTargetHandler(this); |
| 40 } |
| 41 |
| 42 ui::EventResult WorkspaceCycler::OnScrollEvent(ui::ScrollEvent* event) { |
| 43 if (event->finger_count() != 3 || |
| 44 event->type() != ui::ET_SCROLL) { |
| 45 scrubbing_ = false; |
| 46 return ui::ER_UNHANDLED; |
| 47 } |
| 48 |
| 49 if (!IsScrubbingEnabled()) { |
| 50 scrubbing_ = false; |
| 51 return ui::ER_UNHANDLED; |
| 52 } |
| 53 |
| 54 if (!scrubbing_) { |
| 55 scrubbing_ = true; |
| 56 scroll_x_ = 0; |
| 57 scroll_y_ = 0; |
| 58 } |
| 59 |
| 60 if (ui::IsNaturalScrollEnabled()) { |
| 61 scroll_x_ += event->x_offset(); |
| 62 scroll_y_ += event->y_offset(); |
| 63 } else { |
| 64 scroll_x_ -= event->x_offset(); |
| 65 scroll_y_ -= event->y_offset(); |
| 66 } |
| 67 |
| 68 // TODO(pkotwicz): Implement scrubbing through several workspaces as the |
| 69 // result of a single scroll event. |
| 70 if (std::abs(scroll_y_) > kWorkspaceStepSize) { |
| 71 workspace_manager_->CycleToWorkspace(scroll_y_ > 0 ? |
| 72 WorkspaceManager::CYCLE_NEXT : WorkspaceManager::CYCLE_PREVIOUS); |
| 73 |
| 74 scroll_x_ = 0; |
| 75 scroll_y_ = 0; |
| 76 return ui::ER_HANDLED; |
| 77 } |
| 78 |
| 79 if (std::abs(scroll_x_) > kWorkspaceStepSize) { |
| 80 // Update |scroll_x_| and |scroll_y_| such that workspaces are only cycled |
| 81 // through when there recently was a significant amount of vertical movement |
| 82 // as opposed to vertical movement accumulated over a long horizontal three |
| 83 // finger scroll. |
| 84 scroll_x_ = 0; |
| 85 scroll_y_ = 0; |
| 86 } |
| 87 |
| 88 // The active workspace was not changed, do not consume the event. |
| 89 return ui::ER_UNHANDLED; |
| 90 } |
| 91 |
| 92 } // namespace internal |
| 93 } // namespace ash |
OLD | NEW |