summaryrefslogtreecommitdiffstats
path: root/views/focus/widget_focus_manager.h
blob: ed430bc8833f2255ee429c224dfdbfb05561690a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_
#define VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_
#pragma once

#include "base/basictypes.h"
#include "base/observer_list.h"
#include "ui/gfx/native_widget_types.h"
#include "views/views_export.h"

template <typename T> struct DefaultSingletonTraits;

namespace views {

// This interface should be implemented by classes that want to be notified when
// the native focus is about to change.  Listeners implementing this interface
// will be invoked for all native focus changes across the entire Chrome
// application.  FocusChangeListeners are only called for changes within the
// children of a single top-level native-view.
class WidgetFocusChangeListener {
 public:
  virtual void NativeFocusWillChange(gfx::NativeView focused_before,
                                     gfx::NativeView focused_now) = 0;

 protected:
  virtual ~WidgetFocusChangeListener() {}
};

class VIEWS_EXPORT WidgetFocusManager {
 public:
  // Returns the singleton instance.
  static WidgetFocusManager* GetInstance();

  // Adds/removes a WidgetFocusChangeListener |listener| to the set of
  // active listeners.
  void AddFocusChangeListener(WidgetFocusChangeListener* listener);
  void RemoveFocusChangeListener(WidgetFocusChangeListener* listener);

  // To be called when native-focus shifts from |focused_before| to
  // |focused_now|.
  // TODO(port) : Invocations to this routine are only implemented for
  // the Win32 platform.  Calls need to be placed appropriately for
  // non-Windows environments.
  void OnWidgetFocusEvent(gfx::NativeView focused_before,
                          gfx::NativeView focused_now);

  // Enable/Disable notification of registered listeners during calls
  // to OnWidgetFocusEvent.  Used to prevent unwanted focus changes from
  // propagating notifications.
  void EnableNotifications() { enabled_ = true; }
  void DisableNotifications() { enabled_ = false; }

 private:
  friend struct DefaultSingletonTraits<WidgetFocusManager>;

  WidgetFocusManager();
  ~WidgetFocusManager();

  ObserverList<WidgetFocusChangeListener> focus_change_listeners_;

  bool enabled_;

  DISALLOW_COPY_AND_ASSIGN(WidgetFocusManager);
};

// A basic helper class that is used to disable native focus change
// notifications within a scope.
class VIEWS_EXPORT AutoNativeNotificationDisabler {
 public:
  AutoNativeNotificationDisabler();
  ~AutoNativeNotificationDisabler();

 private:
  DISALLOW_COPY_AND_ASSIGN(AutoNativeNotificationDisabler);
};

}  // namespace views

#endif  // VIEWS_FOCUS_WIDGET_FOCUS_MANAGER_H_