summaryrefslogtreecommitdiff
path: root/chromium/ui/base/cocoa/bubble_closer.h
blob: 608d117b848b77a3d35f9881197a7ec95013d758 (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
// Copyright 2017 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 UI_BASE_COCOA_BUBBLE_CLOSER_H_
#define UI_BASE_COCOA_BUBBLE_CLOSER_H_

#include <objc/objc.h>

#include "base/callback.h"
#include "base/macros.h"
#include "ui/base/cocoa/weak_ptr_nsobject.h"
#include "ui/base/ui_base_export.h"
#include "ui/gfx/native_widget_types.h"

namespace ui {

// Monitors mouse events to allow a regular window to have menu-like popup
// behavior when clicking outside the window. This is needed because macOS
// suppresses window activation events when clicking rapidly.
class UI_BASE_EXPORT BubbleCloser {
 public:
  // Installs an event monitor watching for mouse clicks outside of |window| or
  // any of its child windows. Invokes |on_click_outside| on each event.
  BubbleCloser(gfx::NativeWindow window,
               base::RepeatingClosure on_click_outside);
  ~BubbleCloser();

 private:
  void OnClickOutside();

  id event_tap_;  // Weak. Owned by AppKit.
  base::RepeatingClosure on_click_outside_;
  WeakPtrNSObjectFactory<BubbleCloser> factory_;

  DISALLOW_COPY_AND_ASSIGN(BubbleCloser);
};

}  // namespace ui

#endif  // UI_BASE_COCOA_BUBBLE_CLOSER_H_