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..0e945b1acde31316eb397e7f9865f16932490ac0 |
| --- /dev/null |
| +++ b/ash/wm/workspace/workspace_cycler.cc |
| @@ -0,0 +1,155 @@ |
| +// 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() { |
| +} |
| + |
| +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::OnKeyEvent(ui::KeyEvent* event) { |
| + return ui::ER_UNHANDLED; |
| +} |
| + |
| +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; |
| + } |
| + |
| + int num_scrub_steps = std::abs(delta_y / kWorkspaceStepSize); |
| + for (int i = 0; i < num_scrub_steps; ++i) { |
|
sadrul
2012/11/23 20:02:07
Why the loop here?
|
| + bool switching_succeeded = false; |
| + if (delta_y > 0) { |
| + switching_succeeded = |
| + workspace_manager_->CycleAwayFromCurrentlyActiveWorkspace(); |
| + } 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. |
|
sadrul
2012/11/23 20:02:07
Do we not want to go to previously minimized windo
pkotwicz
2012/11/23 22:08:32
I am unsure. My personal preference is not to scru
sadrul
2012/11/26 16:55:50
We should confirm this with UX
|
| + if (initial_workspace_window_->IsVisible()) |
| + break; |
| + |
| + switching_succeeded = workspace_manager_->CycleToNextHiddenWorkspace(); |
| + } |
| + if (!switching_succeeded) |
| + break; |
| + } |
| + |
| + if (delta_y != 0) { |
| + // Set |last_transition_y| such that it is the minimum y position which |
| + // would have produced |num_scrub_steps|. |
| + int last_transition_y = event_location.y(); |
| + if (delta_y > 0) |
| + last_transition_y -= delta_y % kWorkspaceStepSize; |
| + else if (delta_y < 0) |
| + last_transition_y += std::abs(delta_y) % kWorkspaceStepSize; |
| + |
| + last_transition_position_ = |
| + gfx::Point(event_location.x(), last_transition_y); |
| + } |
| + |
| + return ui::ER_HANDLED; |
|
sadrul
2012/11/23 20:02:07
The logic in this function seems rather complex. I
|
| +} |
| + |
| +ui::EventResult WorkspaceCycler::OnScrollEvent(ui::ScrollEvent* event) { |
| + return ui::ER_UNHANDLED; |
| +} |
| + |
| +ui::EventResult WorkspaceCycler::OnTouchEvent(ui::TouchEvent* event) { |
| + return ui::ER_UNHANDLED; |
| +} |
| + |
| +ui::EventResult WorkspaceCycler::OnGestureEvent(ui::GestureEvent* event) { |
| + return ui::ER_UNHANDLED; |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |