| 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..1ba0bfcdf6ffebe08c9c0a938ed5603d96ea1b21
|
| --- /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)) {
|
| + 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;
|
| + return ui::ER_UNHANDLED;
|
| + }
|
| +
|
| + if (std::abs(delta_y) > kWorkspaceStepSize) {
|
| + if (delta_y > 0) {
|
| + workspace_manager_->CycleToNextVisibleWorkspace();
|
| + } else {
|
| + // If the initial workspace is visible, then cycling to hidden workspaces
|
| + // should be disabled as not to scrub to workspaces which were minimized
|
| + // before scrubbing was initiated.
|
| + if (!initial_workspace_window_->IsVisible())
|
| + workspace_manager_->CycleToNextHiddenWorkspace();
|
| + }
|
| +
|
| + last_transition_position_ = event_location;
|
| + }
|
| +
|
| + return ui::ER_HANDLED;
|
| +}
|
| +
|
| +} // namespace internal
|
| +} // namespace ash
|
|
|