🐎 Avoid to measure invisible lines when dragging

This commit is contained in:
Antonio Scandurra
2015-10-05 16:05:17 +02:00
parent 56488748db
commit 581ffb1360
3 changed files with 38 additions and 15 deletions

View File

@@ -44,8 +44,7 @@ describe "LinesYardstick", ->
mockLineNodesProvider =
updateSync: (state) -> availableScreenRows = state
lineNodeForLineIdAndScreenRow: (lineId, screenRow) ->
if availableScreenRows[lineId] isnt screenRow
throw new Error("No line node found!")
return if availableScreenRows[lineId] isnt screenRow
buildLineNode(screenRow)
@@ -120,6 +119,18 @@ describe "LinesYardstick", ->
expect(linesYardstick.pixelPositionForScreenPosition([0, 9]).left).toBe 67
expect(linesYardstick.pixelPositionForScreenPosition([0, 11]).left).toBe 84
it "doesn't measure invisible lines if it is explicitly told so", ->
atom.styles.addStyleSheet """
* {
font-size: 12px;
font-family: monospace;
}
"""
expect(linesYardstick.pixelPositionForScreenPosition([0, 0], true, true)).toEqual({left: 0, top: 0})
expect(linesYardstick.pixelPositionForScreenPosition([0, 1], true, true)).toEqual({left: 0, top: 0})
expect(linesYardstick.pixelPositionForScreenPosition([0, 5], true, true)).toEqual({left: 0, top: 0})
describe "::screenPositionForPixelPosition(pixelPosition)", ->
it "converts pixel positions to screen positions", ->
atom.styles.addStyleSheet """
@@ -152,3 +163,15 @@ describe "LinesYardstick", ->
expect(linesYardstick.screenPositionForPixelPosition(top: Infinity, left: Infinity)).toEqual [12, 2]
expect(linesYardstick.screenPositionForPixelPosition(top: (editor.getLastScreenRow() + 1) * 14, left: 0)).toEqual [12, 2]
expect(linesYardstick.screenPositionForPixelPosition(top: editor.getLastScreenRow() * 14, left: 0)).toEqual [12, 0]
it "doesn't measure invisible lines if it is explicitly told so", ->
atom.styles.addStyleSheet """
* {
font-size: 12px;
font-family: monospace;
}
"""
expect(linesYardstick.screenPositionForPixelPosition({top: 0, left: 13}, true)).toEqual([0, 0])
expect(linesYardstick.screenPositionForPixelPosition({top: 14, left: 20}, true)).toEqual([1, 0])
expect(linesYardstick.screenPositionForPixelPosition({top: 28, left: 100}, true)).toEqual([2, 0])

View File

@@ -20,7 +20,7 @@ class LinesYardstick
clearScreenRowsForMeasurement: ->
@presenter.clearScreenRowsToMeasure()
screenPositionForPixelPosition: (pixelPosition) ->
screenPositionForPixelPosition: (pixelPosition, measureVisibleLinesOnly) ->
targetTop = pixelPosition.top
targetLeft = pixelPosition.left
defaultCharWidth = @model.getDefaultCharWidth()
@@ -30,7 +30,7 @@ class LinesYardstick
row = Math.min(row, @model.getLastScreenRow())
row = Math.max(0, row)
@prepareScreenRowsForMeasurement([row])
@prepareScreenRowsForMeasurement([row]) unless measureVisibleLinesOnly
line = @model.tokenizedLineForScreenRow(row)
lineNode = @lineNodesProvider.lineNodeForLineIdAndScreenRow(line?.id, row)
@@ -78,26 +78,26 @@ class LinesYardstick
previousColumn = column
column += charLength
@clearScreenRowsForMeasurement()
@clearScreenRowsForMeasurement() unless measureVisibleLinesOnly
if targetLeft <= previousLeft + (charWidth / 2)
new Point(row, previousColumn)
else
new Point(row, column)
pixelPositionForScreenPosition: (screenPosition, clip=true) ->
pixelPositionForScreenPosition: (screenPosition, clip=true, measureVisibleLinesOnly) ->
screenPosition = Point.fromObject(screenPosition)
screenPosition = @model.clipScreenPosition(screenPosition) if clip
targetRow = screenPosition.row
targetColumn = screenPosition.column
@prepareScreenRowsForMeasurement([targetRow])
@prepareScreenRowsForMeasurement([targetRow]) unless measureVisibleLinesOnly
top = targetRow * @model.getLineHeightInPixels()
left = @leftPixelPositionForScreenPosition(targetRow, targetColumn)
@clearScreenRowsForMeasurement()
@clearScreenRowsForMeasurement() unless measureVisibleLinesOnly
{top, left}

View File

@@ -423,14 +423,14 @@ class TextEditorComponent
getVisibleRowRange: ->
@presenter.getVisibleRowRange()
pixelPositionForScreenPosition: (screenPosition) ->
@linesYardstick.pixelPositionForScreenPosition(screenPosition)
pixelPositionForScreenPosition: ->
@linesYardstick.pixelPositionForScreenPosition(arguments...)
screenPositionForPixelPosition: (pixelPosition) ->
@linesYardstick.screenPositionForPixelPosition(pixelPosition)
screenPositionForPixelPosition: ->
@linesYardstick.screenPositionForPixelPosition(arguments...)
pixelRectForScreenRange: (screenRange) ->
@linesYardstick.pixelRectForScreenRange(screenRange)
pixelRectForScreenRange: ->
@linesYardstick.pixelRectForScreenRange(arguments...)
pixelRangeForScreenRange: (screenRange, clip=true) ->
{start, end} = Range.fromObject(screenRange)
@@ -850,7 +850,7 @@ class TextEditorComponent
screenPositionForMouseEvent: (event, linesClientRect) ->
pixelPosition = @pixelPositionForMouseEvent(event, linesClientRect)
@screenPositionForPixelPosition(pixelPosition)
@screenPositionForPixelPosition(pixelPosition, true)
pixelPositionForMouseEvent: (event, linesClientRect) ->
{clientX, clientY} = event