Skip to content

Instantly share code, notes, and snippets.

@ncreep
Last active August 19, 2019 06:54
Show Gist options
  • Save ncreep/a87f127abd962d3cec56 to your computer and use it in GitHub Desktop.
Save ncreep/a87f127abd962d3cec56 to your computer and use it in GitHub Desktop.
Atom middle mouse scrolling hack
atom.workspace.observeTextEditors (item) ->
# Sets up middle button scroll on the given editor component
middleMouseScrollSetup = (editorComponent) ->
lastClick = {x: 0, y: 0}
lastPosition = {x: 0, y: 0}
{scrollContainer} = editorComponent.refs
onMouseMove = (event) ->
[lastPosition.x, lastPosition.y] = [event.x, event.y]
doScroll = ->
sensitivity = 0.5
dx = sensitivity * (lastClick.x - lastPosition.x)
dy = sensitivity * (lastClick.y - lastPosition.y)
previousScrollLeft = editorComponent.getScrollLeft()
previousScrollTop = editorComponent.getScrollTop()
updatedScrollLeft = previousScrollLeft - Math.round(dx)
updatedScrollTop = previousScrollTop - Math.round(dy)
editorComponent.setScrollLeft(updatedScrollLeft)
editorComponent.setScrollTop(updatedScrollTop)
editorComponent.updateSync()
# We prevent regular scrolling on the scroll-container since it interferes with
# the scrolling on the editor-component
scrollContainer.addEventListener 'scroll', (event) ->
if process.platform is 'win32'
scrollContainer.scrollTop = 0
scrollContainer.scrollLeft = 0
scrollContainer.addEventListener 'mousedown', (event) ->
# click with middle button
if event.button is 1 and process.platform is 'win32'
[lastClick.x, lastClick.y] = [event.x, event.y]
[lastPosition.x, lastPosition.y] = [event.x, event.y]
intervalId = setInterval doScroll, 50
onMouseUp = ->
clearInterval(intervalId)
window.removeEventListener 'mousemove', onMouseMove
window.removeEventListener 'mouseup', onMouseUp
window.addEventListener 'mousemove', onMouseMove
window.addEventListener 'mouseup', onMouseUp
init = () ->
editorView = atom.views.getView(item)
if editorView && editorView.constructor.name == 'atom-text-editor'
editorComponent = editorView.component
middleMouseScrollSetup(editorComponent)
init()
.platform-win32 atom-text-editor.editor:not(.mini) {
div {
div.scroll-view {
overflow: auto !important;
}
}
.scroll-view::-webkit-scrollbar {
display: none;
}
}
@UziTech
Copy link

UziTech commented Nov 3, 2017

I created a package that does this scroll-editor-on-middle-click

scrollonmiddle

@ncreep
Copy link
Author

ncreep commented Nov 6, 2017

I've fixed the Gist to work with the newer Atom versions. Still hacky, so maybe the package linked above will be a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment