mirror of
https://github.com/atom/atom.git
synced 2026-01-25 06:48:28 -05:00
Correctly translate clicks to screen positions w/ var-width fonts
Closes #267
This commit is contained in:
@@ -672,6 +672,18 @@ describe "Editor", ->
|
||||
editor.renderedLines.trigger mousedownEvent(editor: editor, point: [3, 50])
|
||||
expect(editor.getCursorBufferPosition()).toEqual(row: 3, column: 50)
|
||||
|
||||
describe "when the editor is using a variable-width font", ->
|
||||
beforeEach ->
|
||||
editor.setFontFamily('sans-serif')
|
||||
|
||||
afterEach ->
|
||||
editor.clearFontFamily()
|
||||
|
||||
it "positions the cursor to the clicked row and column", ->
|
||||
{top, left} = editor.pixelOffsetForScreenPosition([3, 30])
|
||||
editor.renderedLines.trigger mousedownEvent(pageX: left, pageY: top)
|
||||
expect(editor.getCursorScreenPosition()).toEqual [3, 30]
|
||||
|
||||
describe "double-click", ->
|
||||
it "selects the word under the cursor, and expands the selection wordwise in either direction on a subsequent shift-click", ->
|
||||
expect(editor.getCursorScreenPosition()).toEqual(row: 0, column: 0)
|
||||
|
||||
@@ -1229,14 +1229,30 @@ class Editor extends View
|
||||
offset = @renderedLines.offset()
|
||||
{top: top + offset.top, left: left + offset.left}
|
||||
|
||||
screenPositionFromPixelPosition: ({top, left}) ->
|
||||
screenPosition = new Point(Math.floor(top / @lineHeight), Math.floor(left / @charWidth))
|
||||
|
||||
screenPositionFromMouseEvent: (e) ->
|
||||
{ pageX, pageY } = e
|
||||
@screenPositionFromPixelPosition
|
||||
top: pageY - @scrollView.offset().top + @scrollTop()
|
||||
left: pageX - @scrollView.offset().left + @scrollView.scrollLeft()
|
||||
|
||||
editorRelativeTop = pageY - @scrollView.offset().top + @scrollTop()
|
||||
row = Math.floor(editorRelativeTop / @lineHeight)
|
||||
column = 0
|
||||
|
||||
if lineElement = @lineElementForScreenRow(row)[0]
|
||||
@overlayer.hide()
|
||||
@css '-webkit-user-select', 'auto'
|
||||
if range = document.caretRangeFromPoint(pageX, pageY)
|
||||
clickedTextNode = range.endContainer
|
||||
clickedOffset = range.endOffset
|
||||
range.detach()
|
||||
@css '-webkit-user-select', ''
|
||||
@overlayer.show()
|
||||
|
||||
if clickedTextNode and lineElement
|
||||
iterator = document.createNodeIterator(lineElement, NodeFilter.SHOW_TEXT, acceptNode: -> NodeFilter.FILTER_ACCEPT)
|
||||
while (node = iterator.nextNode()) and node isnt clickedTextNode
|
||||
column += node.textContent.length
|
||||
column += clickedOffset
|
||||
|
||||
new Point(row, column)
|
||||
|
||||
highlightCursorLine: ->
|
||||
return if @mini
|
||||
|
||||
Reference in New Issue
Block a user