1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef ASH_WM_OVERVIEW_WINDOW_SELECTOR_H_
6#define ASH_WM_OVERVIEW_WINDOW_SELECTOR_H_
7
8#include <set>
9#include <vector>
10
11#include "ash/ash_export.h"
12#include "base/compiler_specific.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_vector.h"
15#include "base/time/time.h"
16#include "ui/aura/window_observer.h"
17#include "ui/aura/window_tracker.h"
18#include "ui/events/event_handler.h"
19#include "ui/gfx/display_observer.h"
20#include "ui/views/controls/textfield/textfield_controller.h"
21#include "ui/wm/public/activation_change_observer.h"
22
23namespace aura {
24class RootWindow;
25class Window;
26}
27
28namespace gfx {
29class Rect;
30}
31
32namespace ui {
33class LocatedEvent;
34}
35
36namespace views {
37class Textfield;
38class Widget;
39}
40
41namespace ash {
42class WindowSelectorDelegate;
43class WindowSelectorItem;
44class WindowSelectorTest;
45class WindowGrid;
46
47// The WindowSelector shows a grid of all of your windows, allowing to select
48// one by clicking or tapping on it.
49class ASH_EXPORT WindowSelector
50    : public gfx::DisplayObserver,
51      public aura::WindowObserver,
52      public aura::client::ActivationChangeObserver,
53      public views::TextfieldController {
54 public:
55  // The distance between the top edge of the screen and the bottom edge of
56  // the text filtering textfield.
57  static const int kTextFilterBottomEdge;
58
59  enum Direction {
60    LEFT,
61    UP,
62    RIGHT,
63    DOWN
64  };
65
66  typedef std::vector<aura::Window*> WindowList;
67  typedef ScopedVector<WindowSelectorItem> WindowSelectorItemList;
68
69  WindowSelector(const WindowList& windows,
70                 WindowSelectorDelegate* delegate);
71  virtual ~WindowSelector();
72
73  // Cancels window selection.
74  void CancelSelection();
75
76  // Called when the last window selector item from a grid is deleted.
77  void OnGridEmpty(WindowGrid* grid);
78
79  // gfx::DisplayObserver:
80  virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
81  virtual void OnDisplayRemoved(const gfx::Display& display) OVERRIDE;
82  virtual void OnDisplayMetricsChanged(const gfx::Display& display,
83                                       uint32_t metrics) OVERRIDE;
84
85  // aura::WindowObserver:
86  virtual void OnWindowAdded(aura::Window* new_window) OVERRIDE;
87  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
88
89  // aura::client::ActivationChangeObserver:
90  virtual void OnWindowActivated(aura::Window* gained_active,
91                                 aura::Window* lost_active) OVERRIDE;
92  virtual void OnAttemptToReactivateWindow(
93      aura::Window* request_active,
94      aura::Window* actual_active) OVERRIDE;
95
96  // views::TextfieldController:
97  virtual void ContentsChanged(views::Textfield* sender,
98                               const base::string16& new_contents) OVERRIDE;
99  virtual bool HandleKeyEvent(views::Textfield* sender,
100                              const ui::KeyEvent& key_event) OVERRIDE;
101
102 private:
103  friend class WindowSelectorTest;
104
105  // Begins positioning windows such that all windows are visible on the screen.
106  void StartOverview();
107
108  // Position all of the windows in the overview.
109  void PositionWindows(bool animate);
110
111  // Hide and track all hidden windows not in the overview item list.
112  void HideAndTrackNonOverviewWindows();
113
114  // |focus|, restores focus to the stored window.
115  void ResetFocusRestoreWindow(bool focus);
116
117  // Helper function that moves the selection widget to |direction| on the
118  // corresponding window grid.
119  void Move(Direction direction, bool animate);
120
121  // Tracks observed windows.
122  std::set<aura::Window*> observed_windows_;
123
124  // Weak pointer to the selector delegate which will be called when a
125  // selection is made.
126  WindowSelectorDelegate* delegate_;
127
128  // A weak pointer to the window which was focused on beginning window
129  // selection. If window selection is canceled the focus should be restored to
130  // this window.
131  aura::Window* restore_focus_window_;
132
133  // True when performing operations that may cause window activations. This is
134  // used to prevent handling the resulting expected activation.
135  bool ignore_activations_;
136
137  // List of all the window overview grids, one for each root window.
138  ScopedVector<WindowGrid> grid_list_;
139
140  // Tracks windows which were hidden because they were not part of the
141  // overview.
142  aura::WindowTracker hidden_windows_;
143
144  // Tracks the index of the root window the selection widget is in.
145  size_t selected_grid_index_;
146
147  // The following variables are used for metric collection purposes. All of
148  // them refer to this particular overview session and are not cumulative:
149  // The time when overview was started.
150  base::Time overview_start_time_;
151
152  // The number of arrow key presses.
153  size_t num_key_presses_;
154
155  // The number of items in the overview.
156  size_t num_items_;
157
158  // Indicates if we are showing the selection widget.
159  bool showing_selection_widget_;
160
161  // Window text filter widget. As the user writes on it, we filter the items
162  // in the overview. It is also responsible for handling overview key events,
163  // such as enter key to select.
164  scoped_ptr<views::Widget> text_filter_widget_;
165
166  // The current length of the string entered into the text filtering textfield.
167  size_t text_filter_string_length_;
168
169  // The number of times the text filtering textfield has been cleared of text
170  // during this overview mode session.
171  size_t num_times_textfield_cleared_;
172
173  DISALLOW_COPY_AND_ASSIGN(WindowSelector);
174};
175
176}  // namespace ash
177
178#endif  // ASH_WM_OVERVIEW_WINDOW_SELECTOR_H_
179