Home | History | Annotate | Download | only in core
      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 CanActivateWindow(aura::Window* window) const OVERRIDE;
     58 
     59   // Overridden from aura::client::FocusClient:
     60   virtual void AddObserver(
     61       aura::client::FocusChangeObserver* observer) OVERRIDE;
     62   virtual void RemoveObserver(
     63       aura::client::FocusChangeObserver* observer) OVERRIDE;
     64   virtual void FocusWindow(aura::Window* window) OVERRIDE;
     65   virtual void ResetFocusWithinActiveWindow(aura::Window* window) OVERRIDE;
     66   virtual aura::Window* GetFocusedWindow() OVERRIDE;
     67 
     68   // Overridden from ui::EventHandler:
     69   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
     70   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
     71   virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
     72   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
     73   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
     74 
     75   // Overridden from aura::WindowObserver:
     76   virtual void OnWindowVisibilityChanged(aura::Window* window,
     77                                          bool visible) OVERRIDE;
     78   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
     79   virtual void OnWindowHierarchyChanging(
     80       const HierarchyChangeParams& params) OVERRIDE;
     81   virtual void OnWindowHierarchyChanged(
     82       const HierarchyChangeParams& params) OVERRIDE;
     83 
     84   // Internal implementation that sets the focused window, fires events etc.
     85   // This function must be called with a valid focusable window.
     86   void SetFocusedWindow(aura::Window* window);
     87 
     88   // Internal implementation that sets the active window, fires events etc.
     89   // This function must be called with a valid |activatable_window|.
     90   // |requested window| refers to the window that was passed in to an external
     91   // request (e.g. FocusWindow or ActivateWindow). It may be NULL, e.g. if
     92   // SetActiveWindow was not called by an external request. |activatable_window|
     93   // refers to the actual window to be activated, which may be different.
     94   void SetActiveWindow(aura::Window* requested_window,
     95                        aura::Window* activatable_window);
     96 
     97   // Called when a window's disposition changed such that it and its hierarchy
     98   // are no longer focusable/activatable. |next| is a valid window that is used
     99   // as a starting point for finding a window to focus next based on rules.
    100   void WindowLostFocusFromDispositionChange(aura::Window* window,
    101                                             aura::Window* next);
    102 
    103   // Called when an attempt is made to focus or activate a window via an input
    104   // event targeted at that window. Rules determine the best focusable window
    105   // for the input window.
    106   void WindowFocusedFromInputEvent(aura::Window* window);
    107 
    108   aura::Window* active_window_;
    109   aura::Window* focused_window_;
    110 
    111   bool updating_focus_;
    112   bool updating_activation_;
    113 
    114   scoped_ptr<FocusRules> rules_;
    115 
    116   ObserverList<aura::client::ActivationChangeObserver> activation_observers_;
    117   ObserverList<aura::client::FocusChangeObserver> focus_observers_;
    118 
    119   ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(FocusController);
    122 };
    123 
    124 }  // namespace wm
    125 
    126 #endif  // UI_WM_CORE_FOCUS_CONTROLLER_H_
    127