Skip to content

Instantly share code, notes, and snippets.

@ncreep
Last active August 19, 2019 06:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
}
@mirkea
Copy link

mirkea commented Dec 1, 2016

Hi. I get this error in init.coffee:
Object.observe is not a function
Any idea what might cause it?

@ncreep
Copy link
Author

ncreep commented Jan 9, 2017

@mirkea: Sorry, didn't get a notification about the comment here.

Object.observe was deprecated, and I'm not enough of an Atom/Javascript expert to find an alternative for it at the moment.
Removing it from the script and the functionality of the script will be somewhat impaired when moving tabs around.

@zunsakai
Copy link

Uncaught TypeError: Cannot read property 'addEventListener' of undefined ???

C:\Users\phamd.atom\init.coffee:33
Hide Stack Trace
TypeError: Cannot read property 'addEventListener' of undefined
at middleMouseScrollSetup (file:///C:/Users/phamd/.atom/init.coffee:33:19)
at init (file:///C:/Users/phamd/.atom/init.coffee:54:7)
at file:///C:/Users/phamd/.atom/init.coffee:57:3
at Function.module.exports.Emitter.simpleDispatch (C:\Users\phamd\AppData\Local\atom\app-1.19.2\resources\app\node_modules\event-kit\lib\emitter.js:25:20)
at Emitter.module.exports.Emitter.emit (C:\Users\phamd\AppData\Local\atom\app-1.19.2\resources\app\node_modules\event-kit\lib\emitter.js:141:34)
at C:\Users\phamd\AppData\Local\atom\app-1.19.2\resources\app\src\workspace.js:551:30

@nathansobo
Copy link

This is all acessing private APIs and it broke after a rewrite of TextEditorComponent. Other things might break, but you should replace {scrollViewNode} = editorComponent with {scrollContainer} = editorComponent.refs.

@zunsakai
Copy link

zunsakai commented Aug 18, 2017

@nathansobo
image
click with middle button to scroll still not working :((
Atom 1.19.2

@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