Chromium Code Reviews| Index: ash/wm/workspace/workspace_cycler.cc |
| diff --git a/ash/wm/workspace/workspace_cycler.cc b/ash/wm/workspace/workspace_cycler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a062903046bbc72597bc92818c87b374deb4ffbc |
| --- /dev/null |
| +++ b/ash/wm/workspace/workspace_cycler.cc |
| @@ -0,0 +1,124 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/wm/workspace/workspace_cycler.h" |
| + |
| +#include "ash/shell.h" |
| +#include "ash/wm/workspace/workspace_manager.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/base/events/event.h" |
| + |
| +namespace ash { |
| +namespace internal { |
| + |
| +namespace { |
| + |
| +// The required vertical distance to scrub to the next workspace. |
| +const int kWorkspaceStepSize = 50; |
| + |
| +// The maximum horizontal movement in between switching between two workspaces |
| +// before the workspace scrubbing session is cancelled. |
| +const int kMaxHorizontalMovementPerWorkspaceStep = 50; |
| + |
| +// Returns true is scrubbing is enabled. |
| +bool IsScrubbingEnabled() { |
| + // Scrubbing is disabled if the screen is locked or a modal dialog is open. |
| + return !Shell::GetInstance()->IsScreenLocked() && |
| + !Shell::GetInstance()->IsSystemModalWindowOpen(); |
| +} |
| + |
| +} // namespace |
| + |
| +WorkspaceCycler::WorkspaceCycler(WorkspaceManager* workspace_manager) |
| + : workspace_manager_(workspace_manager), |
| + scrub_state_(NOT_SCRUBBING), |
| + initial_workspace_window_(NULL) { |
| + ash::Shell::GetInstance()->AddPreTargetHandler(this); |
| +} |
| + |
| +WorkspaceCycler::~WorkspaceCycler() { |
| + if (scrub_state_ != NOT_SCRUBBING) |
| + StopScrubbing(); |
| + ash::Shell::GetInstance()->RemovePreTargetHandler(this); |
| +} |
| + |
| +void WorkspaceCycler::StartScrubbing() { |
| + scrub_state_ = SCRUBBING; |
| + initial_workspace_window_ = workspace_manager_->GetActiveWorkspaceWindow(); |
| + DCHECK(initial_workspace_window_->IsVisible()); |
| + initial_workspace_window_->AddObserver(this); |
| +} |
| + |
| +void WorkspaceCycler::StopScrubbing() { |
| + if (initial_workspace_window_) |
| + initial_workspace_window_->RemoveObserver(this); |
| + initial_workspace_window_ = NULL; |
| + |
| + scrub_state_ = NOT_SCRUBBING; |
| +} |
| + |
| +void WorkspaceCycler::OnWindowDestroyed(aura::Window* window) { |
| + DCHECK_EQ(window, initial_workspace_window_); |
| + window->RemoveObserver(this); |
| + |
| + // |initial_workspace_window_| may be destroyed while scrubbing is active |
| + // if the user closes or unmaximizes windows via the keyboard in the |
| + // workspace corresponding to |initial_workspace_window_|. |
| + initial_workspace_window_ = NULL; |
| + scrub_state_ = CANCELLED_SCRUBBING; |
| +} |
| + |
| +ui::EventResult WorkspaceCycler::OnMouseEvent(ui::MouseEvent* event) { |
| + if (!IsScrubbingEnabled()) |
| + return ui::ER_UNHANDLED; |
| + |
| + if (!(event->type() == ui::ET_MOUSE_PRESSED || |
| + event->type() == ui::ET_MOUSE_DRAGGED || |
| + event->type() == ui::ET_MOUSE_RELEASED)) { |
|
sky
2012/11/28 15:15:34
nit: indent to match previous line.
Also, this cou
pkotwicz
2012/11/29 01:01:46
Fixed nit. Added comment to explain behavior.
We d
|
| + return ui::ER_UNHANDLED; |
| + } |
| + |
| + if (event->type() == ui::ET_MOUSE_RELEASED || |
| + !(event->flags() & ui::EF_CONTROL_DOWN) || |
| + !(event->flags() & ui::EF_LEFT_MOUSE_BUTTON)) { |
| + StopScrubbing(); |
| + return ui::ER_UNHANDLED; |
| + } |
| + |
| + if (scrub_state_ == CANCELLED_SCRUBBING) |
| + return ui::ER_UNHANDLED; |
| + |
| + if (scrub_state_ == NOT_SCRUBBING) { |
| + StartScrubbing(); |
| + last_transition_position_ = event->root_location(); |
| + return ui::ER_HANDLED; |
| + } |
| + |
| + gfx::Point event_location = event->root_location(); |
| + int delta_x = event_location.x() - last_transition_position_.x(); |
| + int delta_y = event_location.y() - last_transition_position_.y(); |
| + if (std::abs(delta_x) > kMaxHorizontalMovementPerWorkspaceStep) { |
| + scrub_state_ = CANCELLED_SCRUBBING; |
|
sky
2012/11/28 15:15:34
What is the rationale for cancelling when you move
pkotwicz
2012/11/29 01:01:46
The intent of this old code was to allow horizonta
|
| + return ui::ER_UNHANDLED; |
| + } |
| + |
| + if (std::abs(delta_y) > kWorkspaceStepSize) { |
| + if (delta_y > 0) { |
| + workspace_manager_->CycleToWorkspace(WorkspaceManager::WORKSPACE_BELOW); |
|
sky
2012/11/28 15:15:34
Wow, we really don't let you cycle to the previous
pkotwicz
2012/11/29 01:01:46
I believe that there are two cases where this code
|
| + } else { |
| + // If the initial workspace is visible, then cycling to workspaces above |
| + // should be disabled as not to scrub to workspaces above the initial |
| + // workspace. |
| + if (!initial_workspace_window_->IsVisible()) |
| + workspace_manager_->CycleToWorkspace(WorkspaceManager::WORKSPACE_ABOVE); |
| + } |
| + |
| + last_transition_position_ = event_location; |
| + } |
| + |
| + return ui::ER_HANDLED; |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |