Home | History | Annotate | Download | only in corewm
      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/scoped_observer.h"
     10 #include "ui/aura/client/activation_client.h"
     11 #include "ui/aura/client/focus_client.h"
     12 #include "ui/aura/window_observer.h"
     13 #include "ui/views/views_export.h"
     14 
     15 namespace views {
     16 namespace corewm {
     17 
     18 class FocusRules;
     19 
     20 // FocusController handles focus and activation changes for an environment
     21 // encompassing one or more RootWindows. Within an environment there can be
     22 // only one focused and one active window at a time. When focus or activation
     23 // changes notifications are sent using the
     24 // aura::client::Focus/ActivationChangeObserver interfaces.
     25 // Changes to focus and activation can be from three sources:
     26 // . the Aura Client API (implemented here in aura::client::ActivationClient).
     27 //   (The FocusController must be set as the ActivationClient implementation
     28 //    for all RootWindows).
     29 // . input events (implemented here in ui::EventHandler).
     30 //   (The FocusController must be registered as a pre-target handler for
     31 //    the applicable environment owner, either a RootWindow or another type).
     32 // . Window disposition changes (implemented here in aura::WindowObserver).
     33 //   (The FocusController registers itself as an observer of the active and
     34 //    focused windows).
     35 class VIEWS_EXPORT FocusController : public aura::client::ActivationClient,
     36                                      public aura::client::FocusClient,
     37                                      public ui::EventHandler,
     38                                      public aura::WindowObserver {
     39  public:
     40   // |rules| cannot be NULL.
     41   explicit FocusController(FocusRules* rules);
     42   virtual ~FocusController();
     43 
     44  private:
     45   // Overridden from aura::client::ActivationClient:
     46   virtual void AddObserver(
     47       aura::client::ActivationChangeObserver* observer) OVERRIDE;
     48   virtual void RemoveObserver(
     49       aura::client::ActivationChangeObserver* observer) OVERRIDE;
     50   virtual void ActivateWindow(aura::Window* window) OVERRIDE;
     51   virtual void DeactivateWindow(aura::Window* window) OVERRIDE;
     52   virtual aura::Window* GetActiveWindow() OVERRIDE;
     53   virtual aura::Window* GetActivatableWindow(aura::Window* window) OVERRIDE;
     54   virtual aura::Window* GetToplevelWindow(aura::Window* window) OVERRIDE;
     55   virtual bool OnWillFocusWindow(aura::Window* window,
     56     const ui::Event* event) 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   virtual void OnWindowHiddenInRootWindow(aura::Window* window,
     68                                           aura::RootWindow* root_window,
     69                                           bool destroyed) 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