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. 4 5 #ifndef UI_BASE_COCOA_TRACKING_AREA_H_ 6 #define UI_BASE_COCOA_TRACKING_AREA_H_ 7 8 #import <AppKit/AppKit.h> 9 10 #include "base/mac/scoped_nsobject.h" 11 #include "base/macros.h" 12 #include "ui/base/ui_base_export.h" 13 14 @class CrTrackingAreaOwnerProxy; 15 16 // The CrTrackingArea can be used in place of an NSTrackingArea to shut off 17 // messaging to the |owner| at a specific point in time. 18 UI_BASE_EXPORT 19 @interface CrTrackingArea : NSTrackingArea { 20 @private 21 base::scoped_nsobject<CrTrackingAreaOwnerProxy> _ownerProxy; 22 } 23 24 // Designated initializer. Forwards all arguments to the superclass, but wraps 25 // |owner| in a proxy object. 26 - (instancetype)initWithRect:(NSRect)rect 27 options:(NSTrackingAreaOptions)options 28 owner:(id)owner 29 userInfo:(NSDictionary*)userInfo; 30 31 // Prevents any future messages from being delivered to the |owner|. 32 - (void)clearOwner; 33 34 // Watches |window| for its NSWindowWillCloseNotification and calls 35 // |-clearOwner| when the notification is observed. 36 - (void)clearOwnerWhenWindowWillClose:(NSWindow*)window; 37 38 // Returns YES if the mouse is inside the tracking area's rect. |view| is the 39 // NSView the tracking area is attached to. 40 - (BOOL)mouseInsideTrackingAreaForView:(NSView*)view; 41 42 @end 43 44 // Scoper ////////////////////////////////////////////////////////////////////// 45 46 namespace ui { 47 48 // Use an instance of this class to call |-clearOwner| on the |tracking_area_| 49 // when this goes out of scope. 50 class UI_BASE_EXPORT ScopedCrTrackingArea { 51 public: 52 // Takes ownership of |tracking_area| without retaining it. 53 explicit ScopedCrTrackingArea(CrTrackingArea* tracking_area = nil); 54 ~ScopedCrTrackingArea(); 55 56 // This will call |scoped_nsobject<>::reset()| to take ownership of the new 57 // tracking area. Note that -clearOwner is NOT called on the existing 58 // tracking area. 59 void reset(CrTrackingArea* tracking_area = nil); 60 61 CrTrackingArea* get() const; 62 63 private: 64 base::scoped_nsobject<CrTrackingArea> tracking_area_; 65 DISALLOW_COPY_AND_ASSIGN(ScopedCrTrackingArea); 66 }; 67 68 } // namespace ui 69 70 #endif // UI_BASE_COCOA_TRACKING_AREA_H_ 71