From cd3dd7829731f6e36eb70ac1ac4397598e615153 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 18 Jul 2016 18:16:01 +0800 Subject: [PATCH] Prevent two-finger swipe forard/back when scrolling on viewport fix #2577 --- .../brave/content/scripts/inputHandler.js | 14 ++++++++++ js/actions/webviewActions.js | 17 +++++++++++ js/components/frame.js | 6 ++++ js/components/main.js | 28 +++++++++++++------ js/constants/messages.js | 4 +++ 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/app/extensions/brave/content/scripts/inputHandler.js b/app/extensions/brave/content/scripts/inputHandler.js index e67c325e88c..55c1ca2cee8 100644 --- a/app/extensions/brave/content/scripts/inputHandler.js +++ b/app/extensions/brave/content/scripts/inputHandler.js @@ -156,3 +156,17 @@ document.addEventListener('keydown', (e /*: Event*/) => { break } }) + +chrome.ipc.on('check-swipe-back', (e) => { + if (document.scrollingElement.scrollLeft === 0) { + chrome.ipc.sendToHost('can-swipe-back') + } +}) + +chrome.ipc.on('check-swipe-forward', (e) => { + const scrollEle = document.scrollingElement + if (scrollEle.scrollLeft === 0 || + scrollEle.scrollLeft === (scrollEle.scrollWidth - scrollEle.clientWidth)) { + chrome.ipc.sendToHost('can-swipe-forward') + } +}) diff --git a/js/actions/webviewActions.js b/js/actions/webviewActions.js index 801447cd25a..9c12d1e0857 100644 --- a/js/actions/webviewActions.js +++ b/js/actions/webviewActions.js @@ -4,6 +4,8 @@ 'use strict' +const messages = require('../constants/messages.js') + const getWebview = () => document.querySelector('.frameWrapper.isActive webview') @@ -49,6 +51,21 @@ const webviewActions = { if (webview) { webview.showDefinitionForSelection() } + }, + + /** + * 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) + } + } } } diff --git a/js/components/frame.js b/js/components/frame.js index 9808b5a9ed1..9b4eef90b3f 100644 --- a/js/components/frame.js +++ b/js/components/frame.js @@ -505,6 +505,12 @@ class Frame extends ImmutableComponent { case messages.GO_FORWARD: method = () => this.webview.goForward() break + case messages.CAN_SWIPE_BACK: + remote.getCurrentWindow().webContents.send(messages.CAN_SWIPE_BACK) + break + case messages.CAN_SWIPE_FORWARD: + remote.getCurrentWindow().webContents.send(messages.CAN_SWIPE_FORWARD) + break case messages.NEW_FRAME: method = (frameOpts, openInForeground) => { windowActions.newFrame(frameOpts, openInForeground) diff --git a/js/components/main.js b/js/components/main.js index a39f5bdd251..4a133ad4427 100644 --- a/js/components/main.js +++ b/js/components/main.js @@ -110,34 +110,46 @@ class Main extends ImmutableComponent { registerSwipeListener () { // Navigates back/forward on macOS two-finger swipe var trackingFingers = false + var canSwipeBack = false + var canSwipeForward = false var deltaX = 0 var deltaY = 0 var startTime = 0 - var xVelocity = 0 - var yVelocity = 0 + var time this.mainWindow.addEventListener('wheel', (e) => { if (trackingFingers) { deltaX = deltaX + e.deltaX deltaY = deltaY + e.deltaY - var time = (new Date()).getTime() - startTime - xVelocity = deltaX / time - yVelocity = deltaY / time + time = (new Date()).getTime() - startTime + if (deltaX > 0) { + webviewActions.checkSwipe(false) + } else if (deltaX < 0) { + webviewActions.checkSwipe(true) + } } }) + ipc.on(messages.CAN_SWIPE_BACK, (e) => { + canSwipeBack = true + }) + ipc.on(messages.CAN_SWIPE_FORWARD, (e) => { + canSwipeForward = true + }) ipc.on('scroll-touch-begin', function () { trackingFingers = true startTime = (new Date()).getTime() }) ipc.on('scroll-touch-end', function () { - if (trackingFingers && Math.abs(yVelocity) < 1) { - if (xVelocity > 1.5) { + if (time > 50 && trackingFingers && Math.abs(deltaY) < 50) { + if (deltaX > 100 && canSwipeForward) { ipc.emit(messages.SHORTCUT_ACTIVE_FRAME_FORWARD) - } else if (xVelocity < -1.5) { + } else if (deltaX < -100 && canSwipeBack) { ipc.emit(messages.SHORTCUT_ACTIVE_FRAME_BACK) } } trackingFingers = false + canSwipeBack = false + canSwipeForward = false deltaX = 0 deltaY = 0 startTime = 0 diff --git a/js/constants/messages.js b/js/constants/messages.js index c4df60172d6..941c2e535d9 100644 --- a/js/constants/messages.js +++ b/js/constants/messages.js @@ -74,6 +74,10 @@ const messages = { SET_RESOURCE_ENABLED: _, GO_BACK: _, GO_FORWARD: _, + CAN_SWIPE_BACK: _, + CAN_SWIPE_FORWARD: _, + CHECK_SWIPE_BACK: _, + CHECK_SWIPE_FORWARD: _, // Password manager GET_PASSWORDS: _, /** @arg {string} formOrigin, @arg {string} action */ GOT_PASSWORD: _, /** @arg {string} username, @arg {string} password, @arg {string} origin, @arg {string} action, @arg {boolean} isUnique */