-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
45 lines (32 loc) · 1012 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var tryRequire = require('try-require')
var $ = require('dombo')
var electron = tryRequire('electron')
var remote = electron ? electron.remote : tryRequire('remote')
var mouseConstructor = tryRequire('osx-mouse') || tryRequire('win-mouse')
var supported = !!mouseConstructor
var noop = function () { return noop }
var drag = function (element) {
element = $(element)
var offset = null
var mouse = mouseConstructor()
var onmousedown = function (e) {
offset = [e.clientX, e.clientY]
}
element.on('mousedown', onmousedown)
mouse.on('left-drag', function (x, y) {
if (!offset) return
x = Math.round(x - offset[0])
y = Math.round(y - offset[1])
// setPosition throws error if called with -0
remote.getCurrentWindow().setPosition(x + 0, y + 0)
})
mouse.on('left-up', function () {
offset = null
})
return function () {
element.off('mousedown', onmousedown)
mouse.destroy()
}
}
drag.supported = supported
module.exports = supported ? drag : noop