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"
14 template <typename T
> struct DefaultSingletonTraits
;
19 // This interface should be implemented by classes that want to be notified when
20 // the native focus is about to change. Listeners implementing this interface
21 // will be invoked for all native focus changes across the entire Chrome
22 // application. FocusChangeListeners are only called for changes within the
23 // children of a single top-level native-view.
24 class WidgetFocusChangeListener
{
26 virtual void OnNativeFocusChanged(gfx::NativeView focused_now
) = 0;
29 virtual ~WidgetFocusChangeListener() {}
32 class VIEWS_EXPORT WidgetFocusManager
{
34 // Returns the singleton instance.
35 static WidgetFocusManager
* GetInstance();
37 // Adds/removes a WidgetFocusChangeListener |listener| to the set of
39 void AddFocusChangeListener(WidgetFocusChangeListener
* listener
);
40 void RemoveFocusChangeListener(WidgetFocusChangeListener
* listener
);
42 // Called when native-focus shifts within, or off, a widget. |focused_now| is
43 // null when focus is lost.
44 void OnNativeFocusChanged(gfx::NativeView focused_now
);
46 // Enable/Disable notification of registered listeners during calls
47 // to OnNativeFocusChanged. Used to prevent unwanted focus changes from
48 // propagating notifications.
49 void EnableNotifications() { enabled_
= true; }
50 void DisableNotifications() { enabled_
= false; }
53 friend struct base::DefaultSingletonTraits
<WidgetFocusManager
>;
56 ~WidgetFocusManager();
58 base::ObserverList
<WidgetFocusChangeListener
> focus_change_listeners_
;
62 DISALLOW_COPY_AND_ASSIGN(WidgetFocusManager
);
65 // A basic helper class that is used to disable native focus change
66 // notifications within a scope.
67 class VIEWS_EXPORT AutoNativeNotificationDisabler
{
69 AutoNativeNotificationDisabler();
70 ~AutoNativeNotificationDisabler();
73 DISALLOW_COPY_AND_ASSIGN(AutoNativeNotificationDisabler
);
78 #endif // UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_