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.
5 #ifndef UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_
6 #define UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_
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"
13 template <typename T
> struct DefaultSingletonTraits
;
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
{
24 virtual void OnNativeFocusChanged(gfx::NativeView focused_now
) = 0;
27 virtual ~WidgetFocusChangeListener() {}
30 class VIEWS_EXPORT WidgetFocusManager
{
32 // Returns the singleton instance.
33 static WidgetFocusManager
* GetInstance();
35 // Adds/removes a WidgetFocusChangeListener |listener| to the set of
37 void AddFocusChangeListener(WidgetFocusChangeListener
* listener
);
38 void RemoveFocusChangeListener(WidgetFocusChangeListener
* listener
);
40 // Called when native-focus shifts within, or off, a widget. |focused_now| is
41 // null when focus is lost.
42 void OnNativeFocusChanged(gfx::NativeView focused_now
);
44 // Enable/Disable notification of registered listeners during calls
45 // to OnNativeFocusChanged. Used to prevent unwanted focus changes from
46 // propagating notifications.
47 void EnableNotifications() { enabled_
= true; }
48 void DisableNotifications() { enabled_
= false; }
51 friend struct DefaultSingletonTraits
<WidgetFocusManager
>;
54 ~WidgetFocusManager();
56 ObserverList
<WidgetFocusChangeListener
> focus_change_listeners_
;
60 DISALLOW_COPY_AND_ASSIGN(WidgetFocusManager
);
63 // A basic helper class that is used to disable native focus change
64 // notifications within a scope.
65 class VIEWS_EXPORT AutoNativeNotificationDisabler
{
67 AutoNativeNotificationDisabler();
68 ~AutoNativeNotificationDisabler();
71 DISALLOW_COPY_AND_ASSIGN(AutoNativeNotificationDisabler
);
76 #endif // UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_