1// Copyright (c) 2012 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_ACTIVATION_CONTROLLER_H_
6#define ASH_WM_ACTIVATION_CONTROLLER_H_
7
8#include "ash/ash_export.h"
9#include "base/basictypes.h"
10#include "base/compiler_specific.h"
11#include "base/observer_list.h"
12#include "base/scoped_observer.h"
13#include "ui/aura/client/activation_client.h"
14#include "ui/aura/client/focus_change_observer.h"
15#include "ui/aura/env_observer.h"
16#include "ui/aura/window_observer.h"
17#include "ui/base/events/event_handler.h"
18
19namespace aura {
20namespace client {
21class ActivationChangeObserver;
22class FocusClient;
23}
24}
25
26namespace ash {
27namespace internal {
28
29class ActivationControllerDelegate;
30
31// Exported for unit tests.
32class ASH_EXPORT ActivationController
33    : public aura::client::ActivationClient,
34      public aura::WindowObserver,
35      public aura::EnvObserver,
36      public aura::client::FocusChangeObserver,
37      public ui::EventHandler {
38 public:
39  // The ActivationController takes ownership of |delegate|.
40  ActivationController(aura::client::FocusClient* focus_client,
41                       ActivationControllerDelegate* delegate);
42  virtual ~ActivationController();
43
44  // Returns true if |window| exists within a container that supports
45  // activation. |event| is the event responsible for initiating the change, or
46  // NULL if there is no event.
47  static aura::Window* GetActivatableWindow(aura::Window* window,
48                                            const ui::Event* event);
49
50  // Overridden from aura::client::ActivationClient:
51  virtual void AddObserver(
52      aura::client::ActivationChangeObserver* observer) OVERRIDE;
53  virtual void RemoveObserver(
54      aura::client::ActivationChangeObserver* observer) OVERRIDE;
55  virtual void ActivateWindow(aura::Window* window) OVERRIDE;
56  virtual void DeactivateWindow(aura::Window* window) OVERRIDE;
57  virtual aura::Window* GetActiveWindow() OVERRIDE;
58  virtual aura::Window* GetActivatableWindow(aura::Window* window) OVERRIDE;
59  virtual aura::Window* GetToplevelWindow(aura::Window* window) OVERRIDE;
60  virtual bool OnWillFocusWindow(aura::Window* window,
61                                 const ui::Event* event) OVERRIDE;
62  virtual bool CanActivateWindow(aura::Window* window) const OVERRIDE;
63
64  // Overridden from aura::WindowObserver:
65  virtual void OnWindowVisibilityChanged(aura::Window* window,
66                                         bool visible) OVERRIDE;
67  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
68
69  // Overridden from aura::EnvObserver:
70  virtual void OnWindowInitialized(aura::Window* window) OVERRIDE;
71
72  // Overridden from aura::client::FocusChangeObserver:
73  virtual void OnWindowFocused(aura::Window* gained_focus,
74                               aura::Window* lost_focus) OVERRIDE;
75
76 private:
77  // Overridden from ui::EventHandler:
78  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
79  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
80  virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
81  virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
82  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
83
84  // Implementation of ActivateWindow() with an Event.
85  void ActivateWindowWithEvent(aura::Window* window,
86                               const ui::Event* event);
87
88  // Shifts activation to the next window, ignoring |window|. Returns the next
89  // window.
90  aura::Window* ActivateNextWindow(aura::Window* window);
91
92  // Returns the next window that should be activated, ignoring |ignore|.
93  aura::Window* GetTopmostWindowToActivate(aura::Window* ignore) const;
94
95  // Returns the next window that should be activated in |container| ignoring
96  // the window |ignore|.
97  aura::Window* GetTopmostWindowToActivateInContainer(
98      aura::Window* container,
99      aura::Window* ignore) const;
100
101  // Called from the ActivationController's event handler implementation to
102  // handle focus to the |event|'s target. Not all targets are focusable or
103  // result in focus changes.
104  void FocusWindowWithEvent(const ui::Event* event);
105
106  aura::client::FocusClient* focus_client_;
107
108  // True inside ActivateWindow(). Used to prevent recursion of focus
109  // change notifications causing activation.
110  bool updating_activation_;
111
112  aura::Window* active_window_;
113
114  ObserverList<aura::client::ActivationChangeObserver> observers_;
115
116  ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_;
117
118  scoped_ptr<ActivationControllerDelegate> delegate_;
119
120  DISALLOW_COPY_AND_ASSIGN(ActivationController);
121};
122
123}  // namespace internal
124}  // namespace ash
125
126#endif  // ASH_WM_ACTIVATION_CONTROLLER_H_
127