summaryrefslogtreecommitdiffstats
path: root/ash/wm/workspace/workspace_cycler.cc
blob: 1080bb0830964fc2df907c9d2a08edd5590372b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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/base/events/event.h"
#include "ui/base/events/event_utils.h"

namespace ash {
namespace internal {

namespace {

// The required vertical distance to scrub to the next workspace.
const int kWorkspaceStepSize = 10;

// 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),
      scrubbing_(false),
      scroll_x_(0),
      scroll_y_(0) {
  ash::Shell::GetInstance()->AddPreTargetHandler(this);
}

WorkspaceCycler::~WorkspaceCycler() {
  scrubbing_ = false;
  ash::Shell::GetInstance()->RemovePreTargetHandler(this);
}

void WorkspaceCycler::OnScrollEvent(ui::ScrollEvent* event) {
  if (event->finger_count() != 3 ||
      event->type() != ui::ET_SCROLL) {
    scrubbing_ = false;
    return;
  }

  if (!IsScrubbingEnabled()) {
    scrubbing_ = false;
    return;
  }

  if (!scrubbing_) {
    scrubbing_ = true;
    scroll_x_ = 0;
    scroll_y_ = 0;
  }

  if (ui::IsNaturalScrollEnabled()) {
    scroll_x_ += event->x_offset();
    scroll_y_ += event->y_offset();
  } else {
    scroll_x_ -= event->x_offset();
    scroll_y_ -= event->y_offset();
  }

  // TODO(pkotwicz): Implement scrubbing through several workspaces as the
  // result of a single scroll event.
  if (std::abs(scroll_y_) > kWorkspaceStepSize) {
    workspace_manager_->CycleToWorkspace(scroll_y_ > 0 ?
        WorkspaceManager::CYCLE_NEXT : WorkspaceManager::CYCLE_PREVIOUS);

    scroll_x_ = 0;
    scroll_y_ = 0;
    event->SetHandled();
    return;
  }

  if (std::abs(scroll_x_) > kWorkspaceStepSize) {
    // Update |scroll_x_| and |scroll_y_| such that workspaces are only cycled
    // through when there recently was a significant amount of vertical movement
    // as opposed to vertical movement accumulated over a long horizontal three
    // finger scroll.
    scroll_x_ = 0;
    scroll_y_ = 0;
  }

  // The active workspace was not changed, do not consume the event.
}

}  // namespace internal
}  // namespace ash