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/aura/window.h" |
| 10 #include "ui/base/events/event.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 = 50; |
| 19 |
| 20 // The maximum horizontal movement in between switching between two workspaces |
| 21 // before the workspace scrubbing session is cancelled. |
| 22 const int kMaxHorizontalMovementPerWorkspaceStep = 50; |
| 23 |
| 24 // Returns true is scrubbing is enabled. |
| 25 bool IsScrubbingEnabled() { |
| 26 // Scrubbing is disabled if the screen is locked or a modal dialog is open. |
| 27 return !Shell::GetInstance()->IsScreenLocked() && |
| 28 !Shell::GetInstance()->IsSystemModalWindowOpen(); |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 WorkspaceCycler::WorkspaceCycler(WorkspaceManager* workspace_manager) |
| 34 : workspace_manager_(workspace_manager), |
| 35 scrub_state_(NOT_SCRUBBING), |
| 36 initial_workspace_window_(NULL) { |
| 37 ash::Shell::GetInstance()->AddPreTargetHandler(this); |
| 38 } |
| 39 |
| 40 WorkspaceCycler::~WorkspaceCycler() { |
| 41 if (scrub_state_ != NOT_SCRUBBING) |
| 42 StopScrubbing(); |
| 43 ash::Shell::GetInstance()->RemovePreTargetHandler(this); |
| 44 } |
| 45 |
| 46 void WorkspaceCycler::StartScrubbing() { |
| 47 scrub_state_ = SCRUBBING; |
| 48 initial_workspace_window_ = workspace_manager_->GetActiveWorkspaceWindow(); |
| 49 DCHECK(initial_workspace_window_->IsVisible()); |
| 50 initial_workspace_window_->AddObserver(this); |
| 51 } |
| 52 |
| 53 void WorkspaceCycler::StopScrubbing() { |
| 54 if (initial_workspace_window_) |
| 55 initial_workspace_window_->RemoveObserver(this); |
| 56 initial_workspace_window_ = NULL; |
| 57 |
| 58 scrub_state_ = NOT_SCRUBBING; |
| 59 } |
| 60 |
| 61 void WorkspaceCycler::OnWindowDestroyed(aura::Window* window) { |
| 62 DCHECK_EQ(window, initial_workspace_window_); |
| 63 window->RemoveObserver(this); |
| 64 |
| 65 // |initial_workspace_window_| may be destroyed while scrubbing is active |
| 66 // if the user closes or unmaximizes windows via the keyboard in the |
| 67 // workspace corresponding to |initial_workspace_window_|. |
| 68 initial_workspace_window_ = NULL; |
| 69 scrub_state_ = CANCELLED_SCRUBBING; |
| 70 } |
| 71 |
| 72 ui::EventResult WorkspaceCycler::OnMouseEvent(ui::MouseEvent* event) { |
| 73 if (!IsScrubbingEnabled()) |
| 74 return ui::ER_UNHANDLED; |
| 75 |
| 76 if (!(event->type() == ui::ET_MOUSE_PRESSED || |
| 77 event->type() == ui::ET_MOUSE_DRAGGED || |
| 78 event->type() == ui::ET_MOUSE_RELEASED)) { |
| 79 return ui::ER_UNHANDLED; |
| 80 } |
| 81 |
| 82 if (event->type() == ui::ET_MOUSE_RELEASED || |
| 83 !(event->flags() & ui::EF_CONTROL_DOWN) || |
| 84 !(event->flags() & ui::EF_LEFT_MOUSE_BUTTON)) { |
| 85 StopScrubbing(); |
| 86 return ui::ER_UNHANDLED; |
| 87 } |
| 88 |
| 89 if (scrub_state_ == CANCELLED_SCRUBBING) |
| 90 return ui::ER_UNHANDLED; |
| 91 |
| 92 if (scrub_state_ == NOT_SCRUBBING) { |
| 93 StartScrubbing(); |
| 94 last_transition_position_ = event->root_location(); |
| 95 return ui::ER_HANDLED; |
| 96 } |
| 97 |
| 98 gfx::Point event_location = event->root_location(); |
| 99 int delta_x = event_location.x() - last_transition_position_.x(); |
| 100 int delta_y = event_location.y() - last_transition_position_.y(); |
| 101 if (std::abs(delta_x) > kMaxHorizontalMovementPerWorkspaceStep) { |
| 102 scrub_state_ = CANCELLED_SCRUBBING; |
| 103 return ui::ER_UNHANDLED; |
| 104 } |
| 105 |
| 106 if (std::abs(delta_y) > kWorkspaceStepSize) { |
| 107 if (delta_y > 0) { |
| 108 workspace_manager_->CycleToNextVisibleWorkspace(); |
| 109 } else { |
| 110 // If the initial workspace is visible, then cycling to hidden workspaces |
| 111 // should be disabled as not to scrub to workspaces which were minimized |
| 112 // before scrubbing was initiated. |
| 113 if (!initial_workspace_window_->IsVisible()) |
| 114 workspace_manager_->CycleToNextHiddenWorkspace(); |
| 115 } |
| 116 |
| 117 last_transition_position_ = event_location; |
| 118 } |
| 119 |
| 120 return ui::ER_HANDLED; |
| 121 } |
| 122 |
| 123 } // namespace internal |
| 124 } // namespace ash |
OLD | NEW |