1 // Copyright 2014 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.
4 
5 #ifndef COMPONENTS_REMOTE_COCOA_APP_SHIM_MOUSE_CAPTURE_H_
6 #define COMPONENTS_REMOTE_COCOA_APP_SHIM_MOUSE_CAPTURE_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "components/remote_cocoa/app_shim/remote_cocoa_app_shim_export.h"
12 
13 @class NSWindow;
14 
15 namespace remote_cocoa {
16 
17 class CocoaMouseCaptureDelegate;
18 
19 // Basic mouse capture to simulate ::SetCapture() from Windows. This is used to
20 // support menu widgets (e.g. on Combo boxes). Clicking anywhere other than the
21 // menu should dismiss the menu and "swallow" the mouse event. All events are
22 // forwarded, but only events to the same application are "swallowed", which is
23 // consistent with how native NSMenus behave.
24 class REMOTE_COCOA_APP_SHIM_EXPORT CocoaMouseCapture {
25  public:
26   explicit CocoaMouseCapture(CocoaMouseCaptureDelegate* delegate);
27   ~CocoaMouseCapture();
28 
29   // Returns the NSWindow with capture or nil if no window has capture
30   // currently.
31   static NSWindow* GetGlobalCaptureWindow();
32 
33   // True if the event tap is active (i.e. not stolen by a later instance).
IsActive()34   bool IsActive() const { return !!active_handle_; }
35 
36  private:
37   class ActiveEventTap;
38 
39   // Deactivates the event tap if still active.
40   void OnOtherClientGotCapture();
41 
42   CocoaMouseCaptureDelegate* delegate_;  // Weak. Owns this.
43 
44   // The active event tap for this capture. Owned by this, but can be cleared
45   // out early if another instance of CocoaMouseCapture is created.
46   std::unique_ptr<ActiveEventTap> active_handle_;
47 
48   DISALLOW_COPY_AND_ASSIGN(CocoaMouseCapture);
49 };
50 
51 }  // namespace remote_cocoa
52 
53 #endif  // COMPONENTS_REMOTE_COCOA_APP_SHIM_MOUSE_CAPTURE_H_
54