summaryrefslogtreecommitdiffstats
path: root/ash/wm/overview/window_selector.h
blob: 4fc29d172627c3bc2f158445e6128811571d0e1c (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2013 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.

#ifndef ASH_WM_OVERVIEW_WINDOW_SELECTOR_H_
#define ASH_WM_OVERVIEW_WINDOW_SELECTOR_H_

#include <set>
#include <vector>

#include "ash/ash_export.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/timer/timer.h"
#include "ui/aura/client/activation_change_observer.h"
#include "ui/aura/window_observer.h"

namespace aura {
class RootWindow;
}

namespace ui {
class EventHandler;
}

namespace ash {

namespace internal {
class WindowSelectorTest;
}

class ScopedShowWindow;
class WindowOverview;
class WindowSelectorDelegate;
class WindowSelectorItem;

// The WindowSelector allows selecting a window by alt-tabbing (CYCLE mode) or
// by clicking or tapping on it (OVERVIEW mode). A WindowOverview will be shown
// in OVERVIEW mode or if the user lingers on a window while alt tabbing.
class ASH_EXPORT WindowSelector
    : public aura::WindowObserver,
      public aura::client::ActivationChangeObserver {
 public:
  enum Direction {
    FORWARD,
    BACKWARD
  };
  enum Mode {
    CYCLE,
    OVERVIEW
  };

  typedef std::vector<aura::Window*> WindowList;

  WindowSelector(const WindowList& windows,
                 Mode mode,
                 WindowSelectorDelegate* delegate);
  virtual ~WindowSelector();

  // Step to the next window in |direction|.
  void Step(Direction direction);

  // Choose the currently selected window.
  void SelectWindow();

  // Choose |window| from the available windows to select.
  void SelectWindow(aura::Window* window);

  // Cancels window selection.
  void CancelSelection();

  Mode mode() { return mode_; }

  // aura::WindowObserver:
  virtual void OnWindowAdded(aura::Window* new_window) OVERRIDE;
  virtual void OnWindowBoundsChanged(aura::Window* window,
                                     const gfx::Rect& old_bounds,
                                     const gfx::Rect& new_bounds) OVERRIDE;
  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;

  // Overridden from aura::client::ActivationChangeObserver:
  virtual void OnWindowActivated(aura::Window* gained_active,
                                 aura::Window* lost_active) OVERRIDE;
  virtual void OnAttemptToReactivateWindow(
      aura::Window* request_active,
      aura::Window* actual_active) OVERRIDE;

 private:
  friend class internal::WindowSelectorTest;

  // Begins positioning windows such that all windows are visible on the screen.
  void StartOverview();

  // Stores the currently focused window and removes focus from it.
  void RemoveFocusAndSetRestoreWindow();

  // Resets the stored window from RemoveFocusAndSetRestoreWindow to NULL. If
  // |focus|, restores focus to the stored window.
  void ResetFocusRestoreWindow(bool focus);

  // The collection of items in the overview wrapped by a helper class which
  // restores their state and helps transform them to other root windows.
  ScopedVector<WindowSelectorItem> windows_;

  // Tracks observed windows.
  std::set<aura::Window*> observed_windows_;

  // The window selection mode.
  Mode mode_;

  // An event handler listening for the release of the alt key during alt-tab
  // cycling.
  scoped_ptr<ui::EventHandler> event_handler_;

  // The currently selected window being shown (temporarily brought to the front
  // of the stacking order and made visible).
  scoped_ptr<ScopedShowWindow> showing_window_;

  bool timer_enabled_;
  base::DelayTimer<WindowSelector> start_overview_timer_;
  scoped_ptr<WindowOverview> window_overview_;

  // Weak pointer to the selector delegate which will be called when a
  // selection is made.
  WindowSelectorDelegate* delegate_;

  // Index of the currently selected window if the mode is CYCLE.
  size_t selected_window_;

  // A weak pointer to the window which was focused on entering overview mode.
  // If overview mode is canceled the focus should be restored to this window.
  aura::Window* restore_focus_window_;

  // True when restoring focus to the window. This is used to prevent handling
  // the resulting activation.
  bool restoring_focus_;

  DISALLOW_COPY_AND_ASSIGN(WindowSelector);
};

}  // namespace ash

#endif  // ASH_WM_OVERVIEW_WINDOW_SELECTOR_H_