Skip to content

Commit

Permalink
Merge pull request #15546 from atom/as-never-autoscroll-when-clicking…
Browse files Browse the repository at this point in the history
…-on-content

Don't autoscroll when using the mouse to add, delete or move selections
  • Loading branch information
Antonio Scandurra committed Sep 6, 2017
1 parent 13a3eec commit acc2d42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
33 changes: 23 additions & 10 deletions spec/text-editor-component-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,8 @@ describe('TextEditorComponent', () => {
clientY: clientTopForLine(component, 3) + lineHeight / 2
})
expect(editor.getCursorScreenPosition()).toEqual([3, 16])

expect(editor.testAutoscrollRequests).toEqual([])
})

it('selects words on double-click', () => {
Expand All @@ -2748,6 +2750,7 @@ describe('TextEditorComponent', () => {
component.didMouseDownOnContent({detail: 1, button: 0, clientX, clientY})
component.didMouseDownOnContent({detail: 2, button: 0, clientX, clientY})
expect(editor.getSelectedScreenRange()).toEqual([[1, 13], [1, 21]])
expect(editor.testAutoscrollRequests).toEqual([])
})

it('selects lines on triple-click', () => {
Expand All @@ -2757,6 +2760,7 @@ describe('TextEditorComponent', () => {
component.didMouseDownOnContent({detail: 2, button: 0, clientX, clientY})
component.didMouseDownOnContent({detail: 3, button: 0, clientX, clientY})
expect(editor.getSelectedScreenRange()).toEqual([[1, 0], [2, 0]])
expect(editor.testAutoscrollRequests).toEqual([])
})

it('adds or removes cursors when holding cmd or ctrl when single-clicking', () => {
Expand Down Expand Up @@ -2794,7 +2798,7 @@ describe('TextEditorComponent', () => {
expect(editor.getCursorScreenPositions()).toEqual([[1, 16]])

// cmd-clicking within a selection destroys it
editor.addSelectionForScreenRange([[2, 10], [2, 15]])
editor.addSelectionForScreenRange([[2, 10], [2, 15]], {autoscroll: false})
expect(editor.getSelectedScreenRanges()).toEqual([
[[1, 16], [1, 16]],
[[2, 10], [2, 15]]
Expand Down Expand Up @@ -2824,7 +2828,7 @@ describe('TextEditorComponent', () => {

// ctrl-click adds cursors on platforms *other* than macOS
component.props.platform = 'win32'
editor.setCursorScreenPosition([1, 4])
editor.setCursorScreenPosition([1, 4], {autoscroll: false})
component.didMouseDownOnContent(
Object.assign(clientPositionForCharacter(component, 1, 16), {
detail: 1,
Expand All @@ -2833,11 +2837,13 @@ describe('TextEditorComponent', () => {
})
)
expect(editor.getCursorScreenPositions()).toEqual([[1, 4], [1, 16]])

expect(editor.testAutoscrollRequests).toEqual([])
})

it('adds word selections when holding cmd or ctrl when double-clicking', () => {
const {component, editor} = buildComponent()
editor.addCursorAtScreenPosition([1, 16])
editor.addCursorAtScreenPosition([1, 16], {autoscroll: false})
expect(editor.getCursorScreenPositions()).toEqual([[0, 0], [1, 16]])

component.didMouseDownOnContent(
Expand All @@ -2858,11 +2864,12 @@ describe('TextEditorComponent', () => {
[[0, 0], [0, 0]],
[[1, 13], [1, 21]]
])
expect(editor.testAutoscrollRequests).toEqual([])
})

it('adds line selections when holding cmd or ctrl when triple-clicking', () => {
const {component, editor} = buildComponent()
editor.addCursorAtScreenPosition([1, 16])
editor.addCursorAtScreenPosition([1, 16], {autoscroll: false})
expect(editor.getCursorScreenPositions()).toEqual([[0, 0], [1, 16]])

const {clientX, clientY} = clientPositionForCharacter(component, 1, 16)
Expand All @@ -2874,12 +2881,13 @@ describe('TextEditorComponent', () => {
[[0, 0], [0, 0]],
[[1, 0], [2, 0]]
])
expect(editor.testAutoscrollRequests).toEqual([])
})

it('expands the last selection on shift-click', () => {
const {component, element, editor} = buildComponent()

editor.setCursorScreenPosition([2, 18])
editor.setCursorScreenPosition([2, 18], {autoscroll: false})
component.didMouseDownOnContent(Object.assign({
detail: 1,
button: 0,
Expand All @@ -2896,8 +2904,8 @@ describe('TextEditorComponent', () => {

// reorients word-wise selections to keep the word selected regardless of
// where the subsequent shift-click occurs
editor.setCursorScreenPosition([2, 18])
editor.getLastSelection().selectWord()
editor.setCursorScreenPosition([2, 18], {autoscroll: false})
editor.getLastSelection().selectWord({autoscroll: false})
component.didMouseDownOnContent(Object.assign({
detail: 1,
button: 0,
Expand All @@ -2914,8 +2922,8 @@ describe('TextEditorComponent', () => {

// reorients line-wise selections to keep the word selected regardless of
// where the subsequent shift-click occurs
editor.setCursorScreenPosition([2, 18])
editor.getLastSelection().selectLine()
editor.setCursorScreenPosition([2, 18], {autoscroll: false})
editor.getLastSelection().selectLine(null, {autoscroll: false})
component.didMouseDownOnContent(Object.assign({
detail: 1,
button: 0,
Expand All @@ -2929,6 +2937,8 @@ describe('TextEditorComponent', () => {
shiftKey: true
}, clientPositionForCharacter(component, 3, 11)))
expect(editor.getSelectedScreenRange()).toEqual([[2, 0], [4, 0]])

expect(editor.testAutoscrollRequests).toEqual([])
})

it('expands the last selection on drag', () => {
Expand Down Expand Up @@ -4284,7 +4294,10 @@ function buildEditor (params = {}) {
for (const paramName of ['mini', 'autoHeight', 'autoWidth', 'lineNumberGutterVisible', 'showLineNumbers', 'placeholderText', 'softWrapped', 'scrollSensitivity']) {
if (params[paramName] != null) editorParams[paramName] = params[paramName]
}
return new TextEditor(editorParams)
const editor = new TextEditor(editorParams)
editor.testAutoscrollRequests = []
editor.onDidRequestAutoscroll((request) => { editor.testAutoscrollRequests.push(request) })
return editor
}

function buildComponent (params = {}) {
Expand Down
10 changes: 5 additions & 5 deletions src/text-editor-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1786,22 +1786,22 @@ class TextEditorComponent {
if (existingSelection) {
if (model.hasMultipleCursors()) existingSelection.destroy()
} else {
model.addCursorAtScreenPosition(screenPosition)
model.addCursorAtScreenPosition(screenPosition, {autoscroll: false})
}
} else {
if (shiftKey) {
model.selectToScreenPosition(screenPosition)
model.selectToScreenPosition(screenPosition, {autoscroll: false})
} else {
model.setCursorScreenPosition(screenPosition)
model.setCursorScreenPosition(screenPosition, {autoscroll: false})
}
}
break
case 2:
if (addOrRemoveSelection) model.addCursorAtScreenPosition(screenPosition)
if (addOrRemoveSelection) model.addCursorAtScreenPosition(screenPosition, {autoscroll: false})
model.getLastSelection().selectWord({autoscroll: false})
break
case 3:
if (addOrRemoveSelection) model.addCursorAtScreenPosition(screenPosition)
if (addOrRemoveSelection) model.addCursorAtScreenPosition(screenPosition, {autoscroll: false})
model.getLastSelection().selectLine(null, {autoscroll: false})
break
}
Expand Down

0 comments on commit acc2d42

Please sign in to comment.