Native OSX Lion resize cursor for custom NSWindow

2019-04-24 07:40发布

问题:

If we use NSTrackingArea for some specified region then we can implement such method to change the default cursor:

-(void)cursorUpdate:(NSEvent *)theEvent {
    [[NSCursor resizeLeftCursor] set];
}

I implementing custom resize for NSWindow, which uses NSBorderlessWindowMask. And want to use native Lion's two arrows cursors. But such type of cursors not exist in NSCursor API.

Can I get such native cursors somehow from code?
Or maybe I must redraw them by hand (not a good idea)?

回答1:

WebKit contains images that look exactly the same as the cursors used by the system, in the following directory:

/System/Library/Frameworks/WebKit.framework/Versions/Current/Frameworks/WebCore.framework/Resources/

...for example, the file "northWestSouthEastResizeCursor.png".



回答2:

The ones present in WebKit are not available in retina resolution unfortunatly, looking in

/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/Resources/cursors

you'll find pdf version of resizing cursors.



回答3:

Here's some sample code for loading vector-based (and therefore retina capable) cursors from HIServices.framework:

NSString *cursorName = @"resizenortheastsouthwest";
NSString *cursorPath = [@"/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/Resources/cursors" stringByAppendingPathComponent:cursorName];
NSImage *image = [[NSImage alloc] initByReferencingFile:[cursorPath stringByAppendingPathComponent:@"cursor.pdf"]];
NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[cursorPath stringByAppendingPathComponent:@"info.plist"]];
NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint([[info valueForKey:@"hotx"] doubleValue], [[info valueForKey:@"hoty"] doubleValue])];

Note I don't know whether this works in sandboxed Apps.



回答4:

There are undocumented methods for creating such cursors. Here's an example:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    if ([NSCursor respondsToSelector:@selector(_windowResizeNorthSouthCursor)])
    {
        self.resizeUpDownCursor = [NSCursor performSelector:@selector(_windowResizeNorthSouthCursor)];
    }
    else
    {
        self.resizeUpDownCursor = [NSCursor resizeUpDownCursor];
    }
#pragma clang diagnostic pop

Here's the full list of undocumented cursors in macOS Sierra:

[NSCursor _windowResizeEastCursor]
[NSCursor _windowResizeWestCursor]
[NSCursor _windowResizeEastWestCursor]
[NSCursor _windowResizeNorthCursor]
[NSCursor _windowResizeSouthCursor]
[NSCursor _windowResizeNorthSouthCursor]
[NSCursor _windowResizeNorthEastCursor]
[NSCursor _windowResizeNorthWestCursor]
[NSCursor _windowResizeSouthEastCursor]
[NSCursor _windowResizeSouthWestCursor]
[NSCursor _windowResizeNorthEastSouthWestCursor]
[NSCursor _windowResizeNorthWestSouthEastCursor]
[NSCursor _zoomInCursor]
[NSCursor _zoomOutCursor]
[NSCursor _helpCursor]
[NSCursor _copyDragCursor]
[NSCursor _genericDragCursor]
[NSCursor _handCursor]
[NSCursor _closedHandCursor]
[NSCursor _moveCursor]
[NSCursor _waitCursor]
[NSCursor _crosshairCursor]
[NSCursor _horizontalResizeCursor]
[NSCursor _verticalResizeCursor]
[NSCursor _bottomLeftResizeCursor]
[NSCursor _topLeftResizeCursor]
[NSCursor _bottomRightResizeCursor]
[NSCursor _topRightResizeCursor]
[NSCursor _resizeLeftCursor]
[NSCursor _resizeRightCursor]
[NSCursor _resizeLeftRightCursor]