• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_FOCUS_WIDGET_FOCUS_MANAGER_H_
6 #define UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_
7 
8 #include "base/basictypes.h"
9 #include "base/observer_list.h"
10 #include "ui/gfx/native_widget_types.h"
11 #include "ui/views/views_export.h"
12 
13 template <typename T> struct DefaultSingletonTraits;
14 
15 namespace views {
16 
17 // This interface should be implemented by classes that want to be notified when
18 // the native focus is about to change.  Listeners implementing this interface
19 // will be invoked for all native focus changes across the entire Chrome
20 // application.  FocusChangeListeners are only called for changes within the
21 // children of a single top-level native-view.
22 class WidgetFocusChangeListener {
23  public:
24   virtual void OnNativeFocusChange(gfx::NativeView focused_before,
25                                    gfx::NativeView focused_now) = 0;
26 
27  protected:
~WidgetFocusChangeListener()28   virtual ~WidgetFocusChangeListener() {}
29 };
30 
31 class VIEWS_EXPORT WidgetFocusManager {
32  public:
33   // Returns the singleton instance.
34   static WidgetFocusManager* GetInstance();
35 
36   // Adds/removes a WidgetFocusChangeListener |listener| to the set of
37   // active listeners.
38   void AddFocusChangeListener(WidgetFocusChangeListener* listener);
39   void RemoveFocusChangeListener(WidgetFocusChangeListener* listener);
40 
41   // To be called when native-focus shifts from |focused_before| to
42   // |focused_now|.
43   // TODO(port) : Invocations to this routine are only implemented for
44   // the Win32 platform.  Calls need to be placed appropriately for
45   // non-Windows environments.
46   void OnWidgetFocusEvent(gfx::NativeView focused_before,
47                           gfx::NativeView focused_now);
48 
49   // Enable/Disable notification of registered listeners during calls
50   // to OnWidgetFocusEvent.  Used to prevent unwanted focus changes from
51   // propagating notifications.
EnableNotifications()52   void EnableNotifications() { enabled_ = true; }
DisableNotifications()53   void DisableNotifications() { enabled_ = false; }
54 
55  private:
56   friend struct DefaultSingletonTraits<WidgetFocusManager>;
57 
58   WidgetFocusManager();
59   ~WidgetFocusManager();
60 
61   ObserverList<WidgetFocusChangeListener> focus_change_listeners_;
62 
63   bool enabled_;
64 
65   DISALLOW_COPY_AND_ASSIGN(WidgetFocusManager);
66 };
67 
68 // A basic helper class that is used to disable native focus change
69 // notifications within a scope.
70 class VIEWS_EXPORT AutoNativeNotificationDisabler {
71  public:
72   AutoNativeNotificationDisabler();
73   ~AutoNativeNotificationDisabler();
74 
75  private:
76   DISALLOW_COPY_AND_ASSIGN(AutoNativeNotificationDisabler);
77 };
78 
79 }  // namespace views
80 
81 #endif  // UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_
82