Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth Scrolling #182

Merged
merged 3 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changelog
* You can enter insert mode by pressing <kbd>i</kbd> and exit the mode by pressing <kbd>esc</kbd>.
* In insert mode Vimari keybindings are disabled (except for <kbd>esc</kbd> which brings you back to normal mode) allowing you to interact with the underlying website.
* Add `goToFirstInput` action on <kbd>g i</kbd> by default (by [isundaylee](https://github.com/isundaylee)).
* Add smooth scrolling (based on sVim implementation).

### 2.0.3 (2019-09-26)

Expand Down
4 changes: 4 additions & 0 deletions Vimari Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<string>$(PRODUCT_MODULE_NAME).SafariExtensionHandler</string>
<key>SFSafariContentScript</key>
<array>
<dict>
<key>Script</key>
<string>svim-scripts.js</string>
</dict>
<dict>
<key>Script</key>
<string>SafariExtensionCommunicator.js</string>
Expand Down
20 changes: 10 additions & 10 deletions Vimari Extension/js/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ var actionMap = {
function() { extensionCommunicator.requestTabBackward() },

'scrollDown':
function() { window.scrollBy(0, settings.scrollSize); },
function() { customScrollBy(0, settings.scrollSize); },

'scrollUp':
function() { window.scrollBy(0, -settings.scrollSize); },
function() { customScrollBy(0, -settings.scrollSize); },

'scrollLeft':
function() { window.scrollBy(-settings.scrollSize, 0); },
function() { customScrollBy(-settings.scrollSize, 0); },

'scrollRight':
function() { window.scrollBy(settings.scrollSize, 0); },
function() { customScrollBy(settings.scrollSize, 0); },

'goBack':
function() { window.history.back(); },
Expand All @@ -71,19 +71,19 @@ var actionMap = {
function() { extensionCommunicator.requestCloseTab(); },

'scrollDownHalfPage':
function() { window.scrollBy(0, window.innerHeight / 2); },
function() { customScrollBy(0, window.innerHeight / 2); },

'scrollUpHalfPage':
function() { window.scrollBy(0, window.innerHeight / -2); },
function() { customScrollBy(0, window.innerHeight / -2); },

'goToPageBottom':
function() { window.scrollBy(0, document.body.scrollHeight); },
function() { customScrollBy(0, document.body.scrollHeight); },

'goToPageTop':
function() { window.scrollBy(0, -document.body.scrollHeight); },
function() { customScrollBy(0, -document.body.scrollHeight); },

'goToFirstInput':
function() { goToFirstInput(); },
function() { goToFirstInput(); }
};

// Inspiration and general algorithm taken from sVim.
Expand Down Expand Up @@ -134,7 +134,7 @@ function goToFirstInput() {
if (inputToFocus !== null) {
inputToFocus.focus();
}
};
}

// Meant to be overridden, but still has to be copy/pasted from the original...
Mousetrap.prototype.stopCallback = function(e, element, combo) {
Expand Down
47 changes: 47 additions & 0 deletions Vimari Extension/js/lib/svim-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Code in this file is taken from sVim, it has been adjusted to
* work with Vimari.
* Assumes global variable: settings.
*/

let animationFrame = null;

function customScrollBy(x, y) {
// If smooth scroll is off then use regular scroll
if (settings == undefined || settings.smoothScroll === undefined || !settings.smoothScroll) {
window.scrollBy(x, y);
return;
}
window.cancelAnimationFrame(animationFrame);

// Smooth scroll
let i = 0;
let delta = 0;

// Ease function
function easeOutExpo(t, b, c, d) {
return c * (-Math.pow(2, -10 * t / d) + 1) + b;
}

// Animate the scroll
function animLoop() {
const toScroll = Math.round(easeOutExpo(i, 0, y, settings.scrollDuration) - delta);
if (toScroll !== 0) {
if (y) {
window.scrollBy(0, toScroll);
} else {
window.scrollBy(toScroll, 0);
}
}

if (i < this.settings.scrollDuration) {
animationFrame = window.requestAnimationFrame(animLoop);
}

delta = easeOutExpo(i, 0, (x || y), settings.scrollDuration);
i += 1;
}

// Start scroll
animLoop();
}
2 changes: 2 additions & 0 deletions Vimari Extension/json/defaultSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scrollSize": 50,
"openTabUrl": "https://duckduckgo.com/",
"modifier": "",
"smoothScroll": true,
"scrollDuration": 25,
"bindings": {
"hintToggle": "f",
"newTabHintToggle": "shift+f",
Expand Down
4 changes: 4 additions & 0 deletions Vimari.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
659033F124E2B77400432D0E /* svim-scripts.js in Resources */ = {isa = PBXBuildFile; fileRef = 659033F024E2B77400432D0E /* svim-scripts.js */; };
65E444F324CC3A1B008EA1DC /* SafariExtensionCommunicator.js in Resources */ = {isa = PBXBuildFile; fileRef = 65E444F224CC3A1B008EA1DC /* SafariExtensionCommunicator.js */; };
B1E3C17023A65ED400A56807 /* ConfigurationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1E3C16F23A65ED400A56807 /* ConfigurationModel.swift */; };
B1FD3B9923A588DE00677A52 /* defaultSettings.json in Resources */ = {isa = PBXBuildFile; fileRef = B1FD3B9823A588DE00677A52 /* defaultSettings.json */; };
Expand Down Expand Up @@ -57,6 +58,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
659033F024E2B77400432D0E /* svim-scripts.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "svim-scripts.js"; sourceTree = "<group>"; };
65E444F224CC3A1B008EA1DC /* SafariExtensionCommunicator.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = SafariExtensionCommunicator.js; sourceTree = "<group>"; };
B1E3C16F23A65ED400A56807 /* ConfigurationModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationModel.swift; sourceTree = "<group>"; };
B1FD3B9823A588DE00677A52 /* defaultSettings.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = defaultSettings.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -188,6 +190,7 @@
E380F27D233183EE00640547 /* lib */ = {
isa = PBXGroup;
children = (
659033F024E2B77400432D0E /* svim-scripts.js */,
E380F27E233183EE00640547 /* mousetrap.js */,
E380F27F233183EE00640547 /* vimium-scripts.js */,
);
Expand Down Expand Up @@ -297,6 +300,7 @@
files = (
E380F26C2331806500640547 /* ToolbarItemIcon.pdf in Resources */,
B1FD3B9923A588DE00677A52 /* defaultSettings.json in Resources */,
659033F124E2B77400432D0E /* svim-scripts.js in Resources */,
65E444F324CC3A1B008EA1DC /* SafariExtensionCommunicator.js in Resources */,
E380F28B233183EF00640547 /* injected.css in Resources */,
E380F285233183EF00640547 /* injected.js in Resources */,
Expand Down