mouse_capture.h 1.9 KB

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