Skip to content

Instantly share code, notes, and snippets.

@fritx
Last active May 25, 2017 11:00
Show Gist options
  • Save fritx/0452ef938210b94d0a76 to your computer and use it in GitHub Desktop.
Save fritx/0452ef938210b94d0a76 to your computer and use it in GitHub Desktop.
nwjs/electron window dragging implementation (replacement of -webkit-app-region: drag/no-drag)
var remote = require('remote')
var $ = require('./lib/jquery')
var win = remote.getCurrentWindow()
var isDragging = false
var winPos
var dX, dY // screen - win
$(document).on('mousedown', '.app-drag', function(e) {
e = e.originalEvent || e
var $el = $(e.target)
var canDrag = $el.closest('.app-no-drag').length <= 0
if (canDrag) {
isDragging = true
winPos = win.getPosition()
dX = e.screenX - winPos[0]
dY = e.screenY - winPos[1]
}
})
$(document).on('mousemove', function(e) {
if (!isDragging) return
e = e.originalEvent || e
var _x = e.screenX - dX
var _y = e.screenY - dY
win.setPosition(_x, _y)
})
$(document).on('mouseup', function(e) {
isDragging = false
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment