Skip to content

Commit

Permalink
fix: update swipe navigation for electron native implementation of sw…
Browse files Browse the repository at this point in the history
…ipe events
  • Loading branch information
lucidNTR committed Sep 30, 2016
1 parent 340fca0 commit 9e46456
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 78 deletions.
17 changes: 0 additions & 17 deletions js/actions/webviewActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

'use strict'

const messages = require('../constants/messages.js')

const getWebview = () =>
document.querySelector('.frameWrapper.isActive webview')

Expand Down Expand Up @@ -53,21 +51,6 @@ const webviewActions = {
}
},

/**
* Check two-finger gesture swipe back/forward ability
* @param {bool} back - true for back, false for forward
*/
checkSwipe: function (back) {
const webview = getWebview()
if (webview) {
if (back) {
webview.send(messages.CHECK_SWIPE_BACK)
} else {
webview.send(messages.CHECK_SWIPE_FORWARD)
}
}
},

/**
* Set/unset webkit fullscreen status
* @param {Boolean} isFullScreen - fullscreen state to go to
Expand Down
74 changes: 13 additions & 61 deletions js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const ImmutableComponent = require('./immutableComponent')
const Immutable = require('immutable')
const electron = global.require('electron')
const ipc = electron.ipcRenderer
const systemPreferences = electron.remote.systemPreferences

// Actions
const windowActions = require('../actions/windowActions')
Expand Down Expand Up @@ -66,6 +65,7 @@ const { isIntermediateAboutPage, getBaseUrl, isNavigatableAboutPage } = require(
const siteSettings = require('../state/siteSettings')
const urlParse = require('url').parse
const debounce = require('../lib/debounce')
const _ = require('underscore')
const currentWindow = require('../../app/renderer/currentWindow')
const emptyMap = new Immutable.Map()
const emptyList = new Immutable.List()
Expand Down Expand Up @@ -184,29 +184,8 @@ class Main extends ImmutableComponent {
}

registerSwipeListener () {
// Navigates back/forward on macOS two-finger swipe
var trackingFingers = false
var swipeGesture = false
var canSwipeBack = false
var canSwipeForward = false
var isSwipeOnEdge = false
var deltaX = 0
var deltaY = 0
var startTime = 0
var time

this.mainWindow.addEventListener('wheel', (e) => {
if (trackingFingers) {
deltaX = deltaX + e.deltaX
deltaY = deltaY + e.deltaY
time = (new Date()).getTime() - startTime
if (deltaX > 0) {
webviewActions.checkSwipe(false)
} else if (deltaX < 0) {
webviewActions.checkSwipe(true)
}
}
})
// Navigates back/forward on macOS two or three-finger swipe

ipc.on(messages.DEBUG_REACT_PROFILE, (e, args) => {
window.perf = require('react-addons-perf')
if (!window.perf.isRunning()) {
Expand All @@ -233,44 +212,17 @@ class Main extends ImmutableComponent {
}, true)
}
})
ipc.on(messages.CAN_SWIPE_BACK, (e) => {
canSwipeBack = true
})
ipc.on(messages.CAN_SWIPE_FORWARD, (e) => {
canSwipeForward = true
})
ipc.on(messages.ENABLE_SWIPE_GESTURE, (e) => {
swipeGesture = true
})
ipc.on(messages.DISABLE_SWIPE_GESTURE, (e) => {
swipeGesture = false
})
ipc.on('scroll-touch-begin', function () {
if (swipeGesture &&
systemPreferences.isSwipeTrackingFromScrollEventsEnabled()) {
trackingFingers = true
isSwipeOnEdge = false
startTime = (new Date()).getTime()
}
})
ipc.on('scroll-touch-end', function () {
if (time > 50 && trackingFingers && Math.abs(deltaY) < 50 && isSwipeOnEdge) {
if (deltaX > 70 && canSwipeForward) {
ipc.emit(messages.SHORTCUT_ACTIVE_FRAME_FORWARD)
} else if (deltaX < -70 && canSwipeBack) {
ipc.emit(messages.SHORTCUT_ACTIVE_FRAME_BACK)
}

function swipe (direction) {
if (direction === 'left') {
ipc.emit(messages.SHORTCUT_ACTIVE_FRAME_BACK)
} else if (direction === 'right') {
ipc.emit(messages.SHORTCUT_ACTIVE_FRAME_FORWARD)
}
trackingFingers = false
canSwipeBack = false
canSwipeForward = false
deltaX = 0
deltaY = 0
startTime = 0
})
ipc.on('scroll-touch-edge', function () {
isSwipeOnEdge = true
})
}
let throttledSwipe = _.throttle(swipe, 500, {leading: true, trailing: false})
currentWindow.on('swipe', (e, direction) => { throttledSwipe(direction) })

ipc.on(messages.LEAVE_FULL_SCREEN, this.exitFullScreen.bind(this))
}

Expand Down

0 comments on commit 9e46456

Please sign in to comment.