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 UI_WM_CORE_FOCUS_CONTROLLER_H_ 6 #define UI_WM_CORE_FOCUS_CONTROLLER_H_ 7 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_client.h" 16 #include "ui/wm/wm_export.h" 17 18 namespace wm { 19 20 class FocusRules; 21 22 // FocusController handles focus and activation changes for an environment 23 // encompassing one or more RootWindows. Within an environment there can be 24 // only one focused and one active window at a time. When focus or activation 25 // changes notifications are sent using the 26 // aura::client::Focus/ActivationChangeObserver interfaces. 27 // Changes to focus and activation can be from three sources: 28 // . the Aura Client API (implemented here in aura::client::ActivationClient). 29 // (The FocusController must be set as the ActivationClient implementation 30 // for all RootWindows). 31 // . input events (implemented here in ui::EventHandler). 32 // (The FocusController must be registered as a pre-target handler for 33 // the applicable environment owner, either a RootWindow or another type). 34 // . Window disposition changes (implemented here in aura::WindowObserver). 35 // (The FocusController registers itself as an observer of the active and 36 // focused windows). 37 class WM_EXPORT FocusController : public aura::client::ActivationClient, 38 public aura::client::FocusClient, 39 public ui::EventHandler, 40 public aura::WindowObserver { 41 public: 42 // |rules| cannot be NULL. 43 explicit FocusController(FocusRules* rules); 44 virtual ~FocusController(); 45 46 private: 47 // Overridden from aura::client::ActivationClient: 48 virtual void AddObserver( 49 aura::client::ActivationChangeObserver* observer) OVERRIDE; 50 virtual void RemoveObserver( 51 aura::client::ActivationChangeObserver* observer) OVERRIDE; 52 virtual void ActivateWindow(aura::Window* window) OVERRIDE; 53 virtual void DeactivateWindow(aura::Window* window) OVERRIDE; 54 virtual aura::Window* GetActiveWindow() OVERRIDE; 55 virtual aura::Window* GetActivatableWindow(aura::Window* window) OVERRIDE; 56 virtual aura::Window* GetToplevelWindow(aura::Window* window) OVERRIDE; 57 virtual bool OnWillFocusWindow(aura::Window* window, 58 const ui::Event* event) OVERRIDE; 59 virtual bool CanActivateWindow(aura::Window* window) const OVERRIDE; 60 61 // Overridden from aura::client::FocusClient: 62 virtual void AddObserver( 63 aura::client::FocusChangeObserver* observer) OVERRIDE; 64 virtual void RemoveObserver( 65 aura::client::FocusChangeObserver* observer) OVERRIDE; 66 virtual void FocusWindow(aura::Window* window) OVERRIDE; 67 virtual void ResetFocusWithinActiveWindow(aura::Window* window) OVERRIDE; 68 virtual aura::Window* GetFocusedWindow() OVERRIDE; 69 70 // Overridden from ui::EventHandler: 71 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; 72 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; 73 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE; 74 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE; 75 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 76 77 // Overridden from aura::WindowObserver: 78 virtual void OnWindowVisibilityChanged(aura::Window* window, 79 bool visible) OVERRIDE; 80 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE; 81 virtual void OnWindowHierarchyChanging( 82 const HierarchyChangeParams& params) OVERRIDE; 83 virtual void OnWindowHierarchyChanged( 84 const HierarchyChangeParams& params) OVERRIDE; 85 86 // Internal implementation that sets the focused window, fires events etc. 87 // This function must be called with a valid focusable window. 88 void SetFocusedWindow(aura::Window* window); 89 90 // Internal implementation that sets the active window, fires events etc. 91 // This function must be called with a valid |activatable_window|. 92 // |requested window| refers to the window that was passed in to an external 93 // request (e.g. FocusWindow or ActivateWindow). It may be NULL, e.g. if 94 // SetActiveWindow was not called by an external request. |activatable_window| 95 // refers to the actual window to be activated, which may be different. 96 void SetActiveWindow(aura::Window* requested_window, 97 aura::Window* activatable_window); 98 99 // Called when a window's disposition changed such that it and its hierarchy 100 // are no longer focusable/activatable. |next| is a valid window that is used 101 // as a starting point for finding a window to focus next based on rules. 102 void WindowLostFocusFromDispositionChange(aura::Window* window, 103 aura::Window* next); 104 105 // Called when an attempt is made to focus or activate a window via an input 106 // event targeted at that window. Rules determine the best focusable window 107 // for the input window. 108 void WindowFocusedFromInputEvent(aura::Window* window); 109 110 aura::Window* active_window_; 111 aura::Window* focused_window_; 112 113 bool updating_focus_; 114 bool updating_activation_; 115 116 scoped_ptr<FocusRules> rules_; 117 118 ObserverList<aura::client::ActivationChangeObserver> activation_observers_; 119 ObserverList<aura::client::FocusChangeObserver> focus_observers_; 120 121 ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_; 122 123 DISALLOW_COPY_AND_ASSIGN(FocusController); 124 }; 125 126 } // namespace wm 127 128 #endif // UI_WM_CORE_FOCUS_CONTROLLER_H_ 129