Windows screen position to buffer position


#1

What is the method for getting screenPosition from mouse position? I guess such a method must exist since something like this must be called when you click in the buffer.

The hidden method TextEditorElement::pixelPositionForScreenPosition is quite a hassle to work with because it seems that pixelPosition in atom starts form top of the document, even if you have scrolled down.


#2

You can look at the hyperclick package to see how they do it.

This file in particular seems to have everything you need


#3

Thanks for answer. I checked tihs package and i guess you refer to the method
"getMouseEventAsBufferPosition". This seems to be a nice wrapper around the (again hidden) atom component method “screenPositionForMouseEvent”. However, when i use it also offset in some weird way that i can’t figure out.

Here’s my code

atom.commands.add 'atom-text-editor', 'custom:cake', ->
  editor = atom.workspace.getActiveTextEditor()
  a = atom.views.getView(editor)
  b=a.component.screenPositionForMouseEvent({clientX:500, clientY:500})
  editor.insertText(b.toString())

on my screen it misses with atleast 6-8 rows and columns. I use Paint and another utility for getting an approximate pixelposition at cursor to compare with. I have no idea whats going on

Is there an explanation for why many useful tools are not shown at https://atom.io/docs/api/v1.24.0/AtomEnvironment?


#4

Is there a reason you are passing a fixed mouse position? If the editor does not have any rows or columns there, it will go to the closest match on the same row (I believe).

Try the following in your init.js file. This will log the screen and buffer positions of a click, in the file that is open on startup.

editor = atom.workspace.getActiveTextEditor();
editorView = atom.views.getView(editor);

editorView.addEventListener('mousedown', (event) => {
  let screenPos = editorView.component.screenPositionForMouseEvent(event);
  let bufferPos = editor.bufferPositionForScreenPosition(screenPos);
  console.log(screenPos, bufferPos);
});

As for hidden stuff, I take that to mean it’s not officially part of the API and will potentially change or be removed. If you think it would benefit lots of people, you could ask them to mark it as public.

BTW, none of it is truly “hidden”. For example, the methods for getting screen position are completely present in the following file


#5

Hi thanks again for your answer

Is there a reason you are passing a fixed mouse position?

The 500,500 position was meant as a test, but yes it should be an input (current mouse position) from an external source / command and not a mouseclick.

Your snippet works but im looking for a way to achieve the screenposition without clicking, or performing an action with the mouse. Is there a getMouseposition() which returns a mouseEvent with the position of the cursor?

Alternatively, i already have the global screen coordinates of the mouse from an external source, but these do not match with any of the coordinates (offset,screen,client) which you get from the “mousedown” listener in atom. So the problem is solved if there is a consistent way of translating between global screen coordinates and say client coordinates in atom

As for hidden stuff, I take that to mean it’s not officially part of the API and will potentially change or be removed. If you think it would benefit lots of people, you could ask them to mark it as public

I think ill do that. At least some sort of mouseEvent handling could be nice.

BTW, none of it is truly “hidden”. For example, the methods for getting screen position are completely present in the following file

ok yes, I already found it there through another thread, so maybe hidden is not the correct term, i just meant that it is not documented :slight_smile:


#6

Looks like you’re out of luck

You’ll probably need to track the last mouse position (using mousemove event) if you really don’t want to use a click event.