Find cursor position for pixel position


#1

I have a touch event which provides an x and y coord.

I’m trying to map that to a cursor position, but can’t seem to get it working.

I’ve tried text-editor screenPositionForPixelPosition, but it doesn’t appear correctly.
any advice greatly appreciated.


#2

In a package I did I had to step through all the htmlElements in the text line, check the offset screen positions for each one, and then look for one containing the cursor position. Not fun. I guess it could be turned into a module for everyone to use.


#3

Can you be more specific? How is it appearing vs what you expected? I’m actually sort of wondering if it might be a bug.


#4

It’s essentially the issue below (click event used as it is easier for others to replicate):

I put this in my init.coffee to replicate it. I am using the coords from the event, converting them to a screen position and then setting the cursor at that screen position.

document.addEventListener 'click', (args) =>
  console.log args.screenX
  console.log args.screenY

  editor = atom.workspace.getActiveTextEditor()

  screenPosition = editor.screenPositionForPixelPosition({top: args.screenY, left: args.screenX })
  editor.setCursorScreenPosition(screenPosition)


#5

I notice screenPositionForPixelPosition isn’t in the API. Is it used in core?


#6

@mark_hahn yeah, it’s used to handle other events. [quote=“mark_hahn, post:5, topic:16821”]
used
[/quote]

Good point, yeah I just traced through function calls, doesn’t look like it’s used. :expressionless:

So my next point is - how do I convert an event in pixels to a cursor position? I would prefer not to have to recalculate this manually every time we use it. :smile:


#7

When I needed this I couldn’t think of any way other than the tedious process I described. Don’t forget that atom just emits dom elements and has no real way to tell where those elements end up being rendered. Chrome might even change it’s mind in a future version. Some people even use proportional fonts. They work better in Atom than you would expect.

Edit: By tedious process I just meant complex code. It is plenty fast enough. And once the bugs are removed you can quit thinking about it. (grin). My code seems to work. I don’t even remember what package it is in.


#8

If it is a mouse click just look at the target element. No need to look at pixels.


#9

That was my next question: where can I access your code :smiley:

Ok, I’ll have a play, see what I can find.


#10

This is embarrassing. I can’t find it. I found instances of comparing the cursor position in a click to the current selection but nothing that scans the elements in a line. I either dreamed this or I used it in code that was thrown away.

I could swear I remember writing it. I also just now remember having problems because whole words would be in an element and I’d have to interpolate the position to guess which character was at that position. So it wasn’t 100% accurate.

Oh well, I still think the technique would work.


#11

All good :smile:

I might leave this bit for a while given text document change could break it if I do it now.

I was trying to provide the “tap” event, but without cursor position it will not be very useful.

I’ll focus on swipe and better zoom functionality.

I’m also providing the events as a service for other packages.


#12

When I get un-buried I’ll give you a hand. Bug me in a week or so.