Chromium Code Reviews| 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() { | |
|
sadrul
2012/11/26 16:55:50
RemovePreTargetHandler here
| |
| 41 } | |
| 42 | |
| 43 void WorkspaceCycler::StartScrubbing() { | |
| 44 scrub_state_ = SCRUBBING; | |
| 45 initial_workspace_window_ = workspace_manager_->GetActiveWorkspaceWindow(); | |
| 46 DCHECK(initial_workspace_window_->IsVisible()); | |
| 47 initial_workspace_window_->AddObserver(this); | |
| 48 } | |
| 49 | |
| 50 void WorkspaceCycler::StopScrubbing() { | |
| 51 if (initial_workspace_window_) | |
| 52 initial_workspace_window_->RemoveObserver(this); | |
| 53 initial_workspace_window_ = NULL; | |
| 54 | |
| 55 scrub_state_ = NOT_SCRUBBING; | |
| 56 } | |
| 57 | |
| 58 void WorkspaceCycler::OnWindowDestroyed(aura::Window* window) { | |
| 59 DCHECK_EQ(window, initial_workspace_window_); | |
| 60 window->RemoveObserver(this); | |
| 61 | |
| 62 // |initial_workspace_window_| may be destroyed while scrubbing is active | |
| 63 // if the user closes or unmaximizes windows via the keyboard in the | |
| 64 // workspace corresponding to |initial_workspace_window_|. | |
| 65 initial_workspace_window_ = NULL; | |
| 66 scrub_state_ = CANCELLED_SCRUBBING; | |
| 67 } | |
| 68 | |
| 69 ui::EventResult WorkspaceCycler::OnKeyEvent(ui::KeyEvent* event) { | |
| 70 return ui::ER_UNHANDLED; | |
| 71 } | |
| 72 | |
| 73 ui::EventResult WorkspaceCycler::OnMouseEvent(ui::MouseEvent* event) { | |
| 74 if (!IsScrubbingEnabled()) | |
| 75 return ui::ER_UNHANDLED; | |
| 76 | |
| 77 if (!(event->type() == ui::ET_MOUSE_PRESSED || | |
| 78 event->type() == ui::ET_MOUSE_DRAGGED || | |
| 79 event->type() == ui::ET_MOUSE_RELEASED)) { | |
| 80 return ui::ER_UNHANDLED; | |
| 81 } | |
| 82 | |
| 83 if (event->type() == ui::ET_MOUSE_RELEASED || | |
| 84 !(event->flags() & ui::EF_CONTROL_DOWN) || | |
| 85 !(event->flags() & ui::EF_LEFT_MOUSE_BUTTON)) { | |
| 86 StopScrubbing(); | |
| 87 return ui::ER_UNHANDLED; | |
| 88 } | |
| 89 | |
| 90 if (scrub_state_ == CANCELLED_SCRUBBING) | |
| 91 return ui::ER_UNHANDLED; | |
| 92 | |
| 93 if (scrub_state_ == NOT_SCRUBBING) { | |
| 94 StartScrubbing(); | |
| 95 last_transition_position_ = event->root_location(); | |
| 96 return ui::ER_HANDLED; | |
| 97 } | |
| 98 | |
| 99 gfx::Point event_location = event->root_location(); | |
| 100 int delta_x = event_location.x() - last_transition_position_.x(); | |
| 101 int delta_y = event_location.y() - last_transition_position_.y(); | |
| 102 if (std::abs(delta_x) > kMaxHorizontalMovementPerWorkspaceStep) { | |
| 103 scrub_state_ = CANCELLED_SCRUBBING; | |
| 104 return ui::ER_UNHANDLED; | |
| 105 } | |
| 106 | |
| 107 if (std::abs(delta_y) > kWorkspaceStepSize) { | |
| 108 if (delta_y > 0) { | |
| 109 workspace_manager_->CycleToNextUnhiddenWorkspace(); | |
| 110 } else { | |
| 111 // If the initial workspace is visible, then cycling to hidden workspaces | |
| 112 // should be disabled as not to scrub to workspaces which were minimized | |
| 113 // before scrubbing was initiated. | |
| 114 if (!initial_workspace_window_->IsVisible()) | |
| 115 workspace_manager_->CycleToNextHiddenWorkspace(); | |
| 116 } | |
| 117 | |
| 118 last_transition_position_ = event_location; | |
| 119 } | |
| 120 | |
| 121 return ui::ER_HANDLED; | |
| 122 } | |
| 123 | |
| 124 ui::EventResult WorkspaceCycler::OnScrollEvent(ui::ScrollEvent* event) { | |
|
sadrul
2012/11/26 16:55:50
You don't need to override these since you aren't
| |
| 125 return ui::ER_UNHANDLED; | |
| 126 } | |
| 127 | |
| 128 ui::EventResult WorkspaceCycler::OnTouchEvent(ui::TouchEvent* event) { | |
| 129 return ui::ER_UNHANDLED; | |
| 130 } | |
| 131 | |
| 132 ui::EventResult WorkspaceCycler::OnGestureEvent(ui::GestureEvent* event) { | |
| 133 return ui::ER_UNHANDLED; | |
| 134 } | |
| 135 | |
| 136 } // namespace internal | |
| 137 } // namespace ash | |
| OLD | NEW |