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.
5 #ifndef UI_WM_CORE_FOCUS_CONTROLLER_H_
6 #define UI_WM_CORE_FOCUS_CONTROLLER_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/observer_list.h"
11 #include "base/scoped_observer.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/window_observer.h"
14 #include "ui/events/event_handler.h"
15 #include "ui/wm/public/activation_change_observer.h"
16 #include "ui/wm/public/activation_client.h"
17 #include "ui/wm/wm_export.h"
23 // FocusController handles focus and activation changes for an environment
24 // encompassing one or more RootWindows. Within an environment there can be
25 // only one focused and one active window at a time. When focus or activation
26 // changes notifications are sent using the
27 // aura::client::Focus/ActivationChangeObserver interfaces.
28 // Changes to focus and activation can be from three sources. The source can be
29 // determined by the ActivationReason parameter in
30 // ActivationChangeObserver::OnWindowActivated(...).
31 // . ActivationReason::ACTIVATION_CLIENT: The Aura Client API (implemented here
32 // in aura::client::ActivationClient). (The FocusController must be set as the
33 // ActivationClient implementation for all RootWindows).
34 // . ActivationReason::INPUT_EVENT: Input events (implemented here in
35 // ui::EventHandler). (The FocusController must be registered as a pre-target
36 // handler for the applicable environment owner, either a RootWindow or
38 // . ActivationReason::WINDOW_DISPOSITION_CHANGED: Window disposition changes
39 // (implemented here in aura::WindowObserver). (The FocusController registers
40 // itself as an observer of the active and focused windows).
41 class WM_EXPORT FocusController
: public aura::client::ActivationClient
,
42 public aura::client::FocusClient
,
43 public ui::EventHandler
,
44 public aura::WindowObserver
{
46 // |rules| cannot be NULL.
47 explicit FocusController(FocusRules
* rules
);
48 ~FocusController() override
;
51 // Overridden from aura::client::ActivationClient:
52 void AddObserver(aura::client::ActivationChangeObserver
* observer
) override
;
54 aura::client::ActivationChangeObserver
* observer
) override
;
55 void ActivateWindow(aura::Window
* window
) override
;
56 void DeactivateWindow(aura::Window
* window
) override
;
57 aura::Window
* GetActiveWindow() override
;
58 aura::Window
* GetActivatableWindow(aura::Window
* window
) override
;
59 aura::Window
* GetToplevelWindow(aura::Window
* window
) override
;
60 bool CanActivateWindow(aura::Window
* window
) const override
;
62 // Overridden from aura::client::FocusClient:
63 void AddObserver(aura::client::FocusChangeObserver
* observer
) override
;
64 void RemoveObserver(aura::client::FocusChangeObserver
* observer
) override
;
65 void FocusWindow(aura::Window
* window
) override
;
66 void ResetFocusWithinActiveWindow(aura::Window
* window
) override
;
67 aura::Window
* GetFocusedWindow() override
;
69 // Overridden from ui::EventHandler:
70 void OnKeyEvent(ui::KeyEvent
* event
) override
;
71 void OnMouseEvent(ui::MouseEvent
* event
) override
;
72 void OnScrollEvent(ui::ScrollEvent
* event
) override
;
73 void OnTouchEvent(ui::TouchEvent
* event
) override
;
74 void OnGestureEvent(ui::GestureEvent
* event
) override
;
76 // Overridden from aura::WindowObserver:
77 void OnWindowVisibilityChanged(aura::Window
* window
, bool visible
) override
;
78 void OnWindowDestroying(aura::Window
* window
) override
;
79 void OnWindowHierarchyChanging(const HierarchyChangeParams
& params
) override
;
80 void OnWindowHierarchyChanged(const HierarchyChangeParams
& params
) override
;
82 // Internal implementation that coordinates window focus and activation
84 void FocusAndActivateWindow(
85 aura::client::ActivationChangeObserver::ActivationReason reason
,
86 aura::Window
* window
);
88 // Internal implementation that sets the focused window, fires events etc.
89 // This function must be called with a valid focusable window.
90 void SetFocusedWindow(aura::Window
* window
);
92 // Internal implementation that sets the active window, fires events etc.
93 // This function must be called with a valid |activatable_window|.
94 // |requested_window| refers to the window that was passed in to an external
95 // request (e.g. FocusWindow or ActivateWindow). It may be NULL, e.g. if
96 // SetActiveWindow was not called by an external request. |activatable_window|
97 // refers to the actual window to be activated, which may be different.
99 aura::client::ActivationChangeObserver::ActivationReason reason
,
100 aura::Window
* requested_window
,
101 aura::Window
* activatable_window
);
103 // Stack the |active_window_| on top of the window stack. This function is
104 // called when activating a window or re-activating the current active window.
105 void StackActiveWindow();
107 // Called when a window's disposition changed such that it and its hierarchy
108 // are no longer focusable/activatable. |next| is a valid window that is used
109 // as a starting point for finding a window to focus next based on rules.
110 void WindowLostFocusFromDispositionChange(aura::Window
* window
,
113 // Called when an attempt is made to focus or activate a window via an input
114 // event targeted at that window. Rules determine the best focusable window
115 // for the input window.
116 void WindowFocusedFromInputEvent(aura::Window
* window
);
118 aura::Window
* active_window_
;
119 aura::Window
* focused_window_
;
121 bool updating_focus_
;
122 bool updating_activation_
;
124 scoped_ptr
<FocusRules
> rules_
;
126 base::ObserverList
<aura::client::ActivationChangeObserver
>
127 activation_observers_
;
128 base::ObserverList
<aura::client::FocusChangeObserver
> focus_observers_
;
130 ScopedObserver
<aura::Window
, aura::WindowObserver
> observer_manager_
;
132 DISALLOW_COPY_AND_ASSIGN(FocusController
);
137 #endif // UI_WM_CORE_FOCUS_CONTROLLER_H_