This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
/
swipe-handlers.js
134 lines (121 loc) · 4.71 KB
/
swipe-handlers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { ipcRenderer } from 'electron'
import * as pages from './pages'
import * as zoom from './pages/zoom'
const isDarwin = true// TODO window.process.platform === 'darwin'
const SWIPE_TRIGGER_DIST = 400 // how far do you need to travel to trigger the navigation
const ARROW_OFF_DIST = 80 // how far off-screen are the arrows
const RESET_TIMEOUT = 2e3 // how long until we automatically reset the gesture
export function setup () {
var horizontal = 0 // how much x traveled?
var vertical = 0 // how much y traveled?
var hnorm = 0 // normalized to a [-1,1] range
var isTouching = false // is touch event active?
var resetTimeout = null
var leftSwipeArrowEl = document.getElementById('left-swipe-arrow')
var rightSwipeArrowEl = document.getElementById('right-swipe-arrow')
var toolbarSize = document.getElementById('toolbar').clientHeight
const canGoBack = () => {
var page = pages.getActive()
if (page) return page.canGoBack()
}
const shouldGoBack = () => {
return hnorm <= -1
}
const canGoForward = () => {
var page = pages.getActive()
if (page) return page.canGoForward()
}
const shouldGoForward = () => {
return hnorm >= 1
}
const resetArrows = () => {
clearTimeout(resetTimeout)
isTouching = false
horizontal = vertical = hnorm = 0
leftSwipeArrowEl.classList.add('returning')
leftSwipeArrowEl.classList.remove('highlight')
leftSwipeArrowEl.style.left = (-1 * ARROW_OFF_DIST) + 'px'
rightSwipeArrowEl.classList.add('returning')
rightSwipeArrowEl.classList.remove('highlight')
rightSwipeArrowEl.style.right = (-1 * ARROW_OFF_DIST) + 'px'
}
window.addEventListener('mousewheel', e => {
if (!isDarwin && e.ctrlKey === true) {
var page = pages.getActive()
if (e.deltaY > 0) zoom.zoomOut(page)
if (e.deltaY < 0) zoom.zoomIn(page)
}
if (isTouching) {
// track amount of x & y traveled
horizontal += e.deltaX
vertical += e.deltaY
// calculate the normalized horizontal
if (Math.abs(vertical) > Math.abs(horizontal)) {
hnorm = 0 // ignore if there's more vertical motion than horizontal
} else if ((horizontal < 0 && !canGoBack()) || (horizontal > 0 && !canGoForward())) {
hnorm = horizontal = 0 // ignore if the navigation isnt possible in that direction
} else {
hnorm = horizontal / SWIPE_TRIGGER_DIST
}
hnorm = Math.min(1.0, Math.max(-1.0, hnorm)) // clamp to [-1.0, 1.0]
// calculate arrow positions
if (horizontal < 0) {
leftSwipeArrowEl.style.left = ((-1 * ARROW_OFF_DIST) - (hnorm * ARROW_OFF_DIST)) + 'px'
rightSwipeArrowEl.style.right = (-1 * ARROW_OFF_DIST) + 'px'
}
if (horizontal > 0) {
leftSwipeArrowEl.style.left = (-1 * ARROW_OFF_DIST) + 'px'
rightSwipeArrowEl.style.right = ((-1 * ARROW_OFF_DIST) + (hnorm * ARROW_OFF_DIST)) + 'px'
}
// highlight
if (shouldGoBack()) leftSwipeArrowEl.classList.add('highlight')
else leftSwipeArrowEl.classList.remove('highlight')
if (shouldGoForward()) rightSwipeArrowEl.classList.add('highlight')
else rightSwipeArrowEl.classList.remove('highlight')
}
})
// for various humorous reasons, the 'scroll-touch-end' event is emitted in the background process
// so, listen for it over ipc
// https://github.com/electron/electron/pull/4181
ipcRenderer.on('window-event', async function (event, type, data) {
if (type == 'scroll-touch-begin') {
leftSwipeArrowEl.classList.remove('returning')
rightSwipeArrowEl.classList.remove('returning')
// check if the item under the cursor is scrolling
let page = pages.getActive()
if (!page) return
page.webviewEl.executeJavaScript(`
(function() {
var isScrolling = false
// check if the element under the cursor, or any of its parents, are scrolling horizontally right now
var el = document.elementFromPoint(${data.cursorX}, ${(data.cursorY - toolbarSize)})
while (el) {
if (el.scrollWidth > el.clientWidth) {
isScrolling = true
break
}
el = el.parentNode
}
return isScrolling
})()
`, true, (isScrollingEl) => {
if (isScrollingEl) return // dont do anything
isTouching = true
resetTimeout = setTimeout(resetArrows, RESET_TIMEOUT)
})
}
if (type == 'scroll-touch-end' && isTouching) {
// trigger navigation
if (shouldGoBack()) {
let page = pages.getActive()
if (page) page.goBackAsync()
}
if (shouldGoForward()) {
let page = pages.getActive()
if (page) page.goForwardAsync()
}
// reset arrows
resetArrows()
}
})
}