12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef UI_WM_CORE_FOCUS_CONTROLLER_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define UI_WM_CORE_FOCUS_CONTROLLER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/compiler_specific.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/observer_list.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/scoped_observer.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/client/focus_client.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/window_observer.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/events/event_handler.h"
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/wm/public/activation_client.h"
160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/wm/wm_export.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace wm {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FocusRules;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController handles focus and activation changes for an environment
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// encompassing one or more RootWindows. Within an environment there can be
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// only one focused and one active window at a time. When focus or activation
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// changes notifications are sent using the
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// aura::client::Focus/ActivationChangeObserver interfaces.
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Changes to focus and activation can be from three sources:
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// . the Aura Client API (implemented here in aura::client::ActivationClient).
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   (The FocusController must be set as the ActivationClient implementation
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//    for all RootWindows).
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// . input events (implemented here in ui::EventHandler).
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   (The FocusController must be registered as a pre-target handler for
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//    the applicable environment owner, either a RootWindow or another type).
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// . Window disposition changes (implemented here in aura::WindowObserver).
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   (The FocusController registers itself as an observer of the active and
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//    focused windows).
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass WM_EXPORT FocusController : public aura::client::ActivationClient,
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                  public aura::client::FocusClient,
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                  public ui::EventHandler,
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                  public aura::WindowObserver {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |rules| cannot be NULL.
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit FocusController(FocusRules* rules);
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~FocusController();
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Overridden from aura::client::ActivationClient:
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void AddObserver(
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::client::ActivationChangeObserver* observer) OVERRIDE;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void RemoveObserver(
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::client::ActivationChangeObserver* observer) OVERRIDE;
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ActivateWindow(aura::Window* window) OVERRIDE;
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void DeactivateWindow(aura::Window* window) OVERRIDE;
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual aura::Window* GetActiveWindow() OVERRIDE;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual aura::Window* GetActivatableWindow(aura::Window* window) OVERRIDE;
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual aura::Window* GetToplevelWindow(aura::Window* window) OVERRIDE;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool CanActivateWindow(aura::Window* window) const OVERRIDE;
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Overridden from aura::client::FocusClient:
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void AddObserver(
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::client::FocusChangeObserver* observer) OVERRIDE;
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void RemoveObserver(
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::client::FocusChangeObserver* observer) OVERRIDE;
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void FocusWindow(aura::Window* window) OVERRIDE;
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ResetFocusWithinActiveWindow(aura::Window* window) OVERRIDE;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual aura::Window* GetFocusedWindow() OVERRIDE;
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Overridden from ui::EventHandler:
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Overridden from aura::WindowObserver:
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnWindowVisibilityChanged(aura::Window* window,
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                         bool visible) OVERRIDE;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnWindowHierarchyChanging(
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const HierarchyChangeParams& params) OVERRIDE;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnWindowHierarchyChanged(
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const HierarchyChangeParams& params) OVERRIDE;
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Internal implementation that sets the focused window, fires events etc.
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This function must be called with a valid focusable window.
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetFocusedWindow(aura::Window* window);
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Internal implementation that sets the active window, fires events etc.
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This function must be called with a valid |activatable_window|.
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |requested window| refers to the window that was passed in to an external
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // request (e.g. FocusWindow or ActivateWindow). It may be NULL, e.g. if
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // SetActiveWindow was not called by an external request. |activatable_window|
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // refers to the actual window to be activated, which may be different.
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetActiveWindow(aura::Window* requested_window,
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       aura::Window* activatable_window);
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when a window's disposition changed such that it and its hierarchy
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // are no longer focusable/activatable. |next| is a valid window that is used
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // as a starting point for finding a window to focus next based on rules.
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void WindowLostFocusFromDispositionChange(aura::Window* window,
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            aura::Window* next);
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when an attempt is made to focus or activate a window via an input
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // event targeted at that window. Rules determine the best focusable window
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // for the input window.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void WindowFocusedFromInputEvent(aura::Window* window);
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* active_window_;
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* focused_window_;
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool updating_focus_;
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool updating_activation_;
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<FocusRules> rules_;
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ObserverList<aura::client::ActivationChangeObserver> activation_observers_;
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ObserverList<aura::client::FocusChangeObserver> focus_observers_;
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FocusController);
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace wm
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // UI_WM_CORE_FOCUS_CONTROLLER_H_
127