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