Skip to content

CPTrackingArea introduction

Didier Korthoudt edited this page Apr 4, 2016 · 2 revisions

#CPTrackingArea introduction#

Cappuccino 0.9.9 introduces CPTrackingArea. Although this is a great leap forward, you must be aware that this is a disruptive update. Mouse events management changes radically with this update.

If your application relies on tracking mouse movements, it will, without any doubt, be broken with the introduction of CPTrackingArea.

Why ?

Because mouse events handling was really straightforward: every event was sent to all involved views. This resulted in throwing 98% of useless mouse events (on an average application).

With CPTrackingArea, only requesting views will receive requested events.

So, if you do nothing, your application will receive nothing (except, of course, standard Cappuccino controls).

Don't worry, a special behavior has been implemented to ease things (this differs from Cocoa way of doing): when adding a CPView in the view hierarchy (that is, in a window), a special call to -updateTrackingAreas method is done directly on the view. So, to get your CPView subclass back to life, just add the following category:

@implementation MyViewClass (CPTrackingArea)

- (void)updateTrackingAreas
{
    [self removeAllTrackingAreas];

    [self addTrackingArea:[[CPTrackingArea alloc] initWithRect:CGRectMakeZero()
                                                       options:CPTrackingMouseEnteredAndExited | options:CPTrackingMouseMoved | CPTrackingActiveInKeyWindow | CPTrackingInVisibleRect
                                                         owner:self
                                                      userInfo:nil]];
}

@end

Remark: -removeAllTrackinkAreas is a convenience method (that doesn't exist in Cocoa) that, as you can guess, removes all tracking areas of the view.

##Cursor management##

CPTrackingArea also introduces easy cursor management. If you want your view to set a special cursor when mouse is inside it (or a part of it), just add a tracking area with the CPTrackingCursorUpdate option set and implement -cursorUpdate method in the CPView subclass.

##Test application##

You can play with tracking areas using the following application: http://www.segi.be/capptests/TrackingArea/index.html

See also the TrackingArea application in the Tests/Manual folder of Cappuccino 0.9.9.

##Documentation##

Please refer to Apple documentation: