From 8d838199c9bd423f666ac87d1a0bdc7282c61a25 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 8 May 2016 09:34:48 +0200 Subject: [PATCH] Fix smooth scrolling speed in newer Firefox versions The following pseudo-code used to work: setFirefoxSpringConstant() startScrollingSmoothly() resetFirefoxSpringConstant() // Some time later: Smooth scrolling finishes. However, in some newer Firefox version that's changed. `resetFirefoxSpringConstant` now has to be called when the smooth scrolling is done, it seems. Unfortunately there appears to be no way of knowing when that's the case. Instead, this commit introduces a timeout before the spring constant pref is reset. The timeout can be customized via the `scroll.reset_timeout` pref if needed. Special care was taken so that, for example, holding `j` to scroll would only reset the pref _once_ (after the last scroll command) and to the correct value. --- documentation/options.md | 8 +++++++- extension/lib/commands.coffee | 24 ++++++++++++++++++++++-- extension/lib/defaults.coffee | 1 + 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/documentation/options.md b/documentation/options.md index 0cacfa7..371efb7 100644 --- a/documentation/options.md +++ b/documentation/options.md @@ -310,10 +310,16 @@ These are VimFx’s variants, and the commands they affect: - `smoothScroll.lines.spring-constant`: `h`, `l`, `j`, `k` - `smoothScroll.pages.spring-constant`: `d`, `u`, ``, `` -- `smoothScroll.other.spring-constant`: `gg`, `G`, `0`, `^`, `$` +- `smoothScroll.other.spring-constant`: `gg`, `G`, `0`, `^`, `$`, `` ` `` Note that the value of these prefs are _strings,_ not numbers! +Unfortunately, Firefox provides no way for code to tell which “spring constant” +it wants when scrolling smoothly. All VimFx can do is to temporarily set +Firefox’s `layout.css.scroll-behavior.spring-constant` pref. It is reset again +after one second (by default). If that doesn’t work out for you, you can +customize that timeout using the `scroll.reset_timeout` pref. + The Firefox pref `general.smoothScroll` lets you turn off smooth scrolling entirely, including all of VimFx’s scrolling commands. diff --git a/extension/lib/commands.coffee b/extension/lib/commands.coffee index 323036a..55e6fd2 100644 --- a/extension/lib/commands.coffee +++ b/extension/lib/commands.coffee @@ -38,6 +38,8 @@ viewportUtils = require('./viewport') {ContentClick} = Cu.import('resource:///modules/ContentClick.jsm', {}) {FORWARD, BACKWARD} = SelectionManager +SPRING_CONSTANT_PREF = 'layout.css.scroll-behavior.spring-constant' + commands = {} @@ -143,6 +145,11 @@ commands.stop_all = ({vim}) -> +springConstant = { + nonce: null + value: null +} + helper_scroll = (vim, uiEvent, args...) -> [ method, type, directions, amounts @@ -155,10 +162,23 @@ helper_scroll = (vim, uiEvent, args...) -> prefs.root.get("general.smoothScroll.#{type}") ) } - reset = prefs.root.tmp( - 'layout.css.scroll-behavior.spring-constant', + + # Temporarily set Firefox’s “spring constant” pref to get the desired smooth + # scrolling speed. Reset it `reset_timeout` milliseconds after the last + # scrolling command was invoked. + springConstant.nonce = nonce = {} + springConstant.value ?= prefs.root.get(SPRING_CONSTANT_PREF) + prefs.root.set( + SPRING_CONSTANT_PREF, vim.options["smoothScroll.#{type}.spring-constant"] ) + reset = -> + vim.window.setTimeout((-> + return unless springConstant.nonce == nonce + prefs.root.set(SPRING_CONSTANT_PREF, springConstant.value) + springConstant.nonce = null + springConstant.value = null + ), vim.options.reset_timeout) helpScroll = help.getHelp(vim.window)?.querySelector('.wrapper') if uiEvent or helpScroll diff --git a/extension/lib/defaults.coffee b/extension/lib/defaults.coffee index 290facd..7eb496b 100644 --- a/extension/lib/defaults.coffee +++ b/extension/lib/defaults.coffee @@ -170,6 +170,7 @@ advanced_options = 'smoothScroll.lines.spring-constant': '1000' 'smoothScroll.pages.spring-constant': '2500' 'smoothScroll.other.spring-constant': '2500' + 'scroll.reset_timeout': 1000 'scroll.full_page_adjustment': 40 'scroll.half_page_adjustment': 20 'scroll.last_position_mark': '`' -- 2.39.3