Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / focus / widget_focus_manager.h
blob1a25b6d85d4f5d01a3f371b40b6730c7013df477
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 namespace base {
14 template <typename T> struct DefaultSingletonTraits;
17 namespace views {
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 {
25 public:
26 virtual void OnNativeFocusChanged(gfx::NativeView focused_now) = 0;
28 protected:
29 virtual ~WidgetFocusChangeListener() {}
32 class VIEWS_EXPORT WidgetFocusManager {
33 public:
34 // Returns the singleton instance.
35 static WidgetFocusManager* GetInstance();
37 // Adds/removes a WidgetFocusChangeListener |listener| to the set of
38 // active listeners.
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; }
52 private:
53 friend struct base::DefaultSingletonTraits<WidgetFocusManager>;
55 WidgetFocusManager();
56 ~WidgetFocusManager();
58 base::ObserverList<WidgetFocusChangeListener> focus_change_listeners_;
60 bool enabled_;
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 {
68 public:
69 AutoNativeNotificationDisabler();
70 ~AutoNativeNotificationDisabler();
72 private:
73 DISALLOW_COPY_AND_ASSIGN(AutoNativeNotificationDisabler);
76 } // namespace views
78 #endif // UI_VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_