Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting NSPoint from window coordinates

I'm overriding draggingUpdated: in a WebView subclass. This way I'm able to get the NSPoint where the drag destination currently is [(id<NSDraggingInfo>)sender draggingLocation]

I want to get this NSPoint and "convert" it to coordinates within the WebView, and then get the underlying element at that point with WebView's elementAtPoint:

The thing is, I got the NSPoint, and I also have the bounds/frame of the WebView. How is this convertible?

like image 534
Dr.Kameleon Avatar asked Mar 22 '23 16:03

Dr.Kameleon


1 Answers

It is not correct to use -convertPointFromBase: for this. That method is deprecated and the suggested replacement (-convertPointFromBacking:) is not right for this purpose because it assumes the input point is in the backing store coordinate system. (That actually gives a hint as to the original purpose of -convertPointFromBase:, which was part of the now-abandoned, Leopard-era attempts at high-resolution support. The "base coordinate system" it converts from was supposed to be pixel-aligned. Search for 'Pixel Alignment and Transforming View Coordinates To and From "Base" Space' in the Leopard AppKit release notes.)

You want -convertPoint:fromView: and you should pass nil for the fromView parameter. See Cocoa Drawing Guide: Coordinate Systems and Transforms – Converting from Window to View Coordinates.

like image 88
Ken Thomases Avatar answered Mar 31 '23 17:03

Ken Thomases