Skip to content

Commit 663c8b7

Browse files
authored
Merge pull request #182 from televator-apps/172-smooth-scrolling
Smooth Scrolling
2 parents a449bd8 + 835a56e commit 663c8b7

6 files changed

Lines changed: 68 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changelog
99
* You can enter insert mode by pressing <kbd>i</kbd> and exit the mode by pressing <kbd>esc</kbd>. Activating either mode will display the HUD.
1010
* 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.
1111
* Add `goToFirstInput` action on <kbd>g i</kbd> by default (by [isundaylee](https://github.com/isundaylee)).
12+
* Add smooth scrolling (based on sVim implementation).
1213

1314
### 2.0.3 (2019-09-26)
1415

Vimari Extension/Info.plist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
<string>$(PRODUCT_MODULE_NAME).SafariExtensionHandler</string>
3131
<key>SFSafariContentScript</key>
3232
<array>
33+
<dict>
34+
<key>Script</key>
35+
<string>svim-scripts.js</string>
36+
</dict>
3337
<dict>
3438
<key>Script</key>
3539
<string>SafariExtensionCommunicator.js</string>

Vimari Extension/js/injected.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ var actionMap = {
4444
function() { extensionCommunicator.requestTabBackward() },
4545

4646
'scrollDown':
47-
function() { window.scrollBy(0, settings.scrollSize); },
47+
function() { customScrollBy(0, settings.scrollSize); },
4848

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

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

5555
'scrollRight':
56-
function() { window.scrollBy(settings.scrollSize, 0); },
56+
function() { customScrollBy(settings.scrollSize, 0); },
5757

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

7373
'scrollDownHalfPage':
74-
function() { window.scrollBy(0, window.innerHeight / 2); },
74+
function() { customScrollBy(0, window.innerHeight / 2); },
7575

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

7979
'goToPageBottom':
80-
function() { window.scrollBy(0, document.body.scrollHeight); },
80+
function() { customScrollBy(0, document.body.scrollHeight); },
8181

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

8585
'goToFirstInput':
86-
function() { goToFirstInput(); },
86+
function() { goToFirstInput(); }
8787
};
8888

8989
// Inspiration and general algorithm taken from sVim.
@@ -134,7 +134,7 @@ function goToFirstInput() {
134134
if (inputToFocus !== null) {
135135
inputToFocus.focus();
136136
}
137-
};
137+
}
138138

139139
// Meant to be overridden, but still has to be copy/pasted from the original...
140140
Mousetrap.prototype.stopCallback = function(e, element, combo) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Code in this file is taken from sVim, it has been adjusted to
3+
* work with Vimari.
4+
* Assumes global variable: settings.
5+
*/
6+
7+
let animationFrame = null;
8+
9+
function customScrollBy(x, y) {
10+
// If smooth scroll is off then use regular scroll
11+
if (settings == undefined || settings.smoothScroll === undefined || !settings.smoothScroll) {
12+
window.scrollBy(x, y);
13+
return;
14+
}
15+
window.cancelAnimationFrame(animationFrame);
16+
17+
// Smooth scroll
18+
let i = 0;
19+
let delta = 0;
20+
21+
// Ease function
22+
function easeOutExpo(t, b, c, d) {
23+
return c * (-Math.pow(2, -10 * t / d) + 1) + b;
24+
}
25+
26+
// Animate the scroll
27+
function animLoop() {
28+
const toScroll = Math.round(easeOutExpo(i, 0, y, settings.scrollDuration) - delta);
29+
if (toScroll !== 0) {
30+
if (y) {
31+
window.scrollBy(0, toScroll);
32+
} else {
33+
window.scrollBy(toScroll, 0);
34+
}
35+
}
36+
37+
if (i < this.settings.scrollDuration) {
38+
animationFrame = window.requestAnimationFrame(animLoop);
39+
}
40+
41+
delta = easeOutExpo(i, 0, (x || y), settings.scrollDuration);
42+
i += 1;
43+
}
44+
45+
// Start scroll
46+
animLoop();
47+
}

Vimari Extension/json/defaultSettings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"scrollSize": 50,
66
"openTabUrl": "https://duckduckgo.com/",
77
"modifier": "",
8+
"smoothScroll": true,
9+
"scrollDuration": 25,
810
"bindings": {
911
"hintToggle": "f",
1012
"newTabHintToggle": "shift+f",

Vimari.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

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

5960
/* Begin PBXFileReference section */
61+
659033F024E2B77400432D0E /* svim-scripts.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "svim-scripts.js"; sourceTree = "<group>"; };
6062
65E444F224CC3A1B008EA1DC /* SafariExtensionCommunicator.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = SafariExtensionCommunicator.js; sourceTree = "<group>"; };
6163
B1E3C16F23A65ED400A56807 /* ConfigurationModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationModel.swift; sourceTree = "<group>"; };
6264
B1FD3B9823A588DE00677A52 /* defaultSettings.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = defaultSettings.json; sourceTree = "<group>"; };
@@ -188,6 +190,7 @@
188190
E380F27D233183EE00640547 /* lib */ = {
189191
isa = PBXGroup;
190192
children = (
193+
659033F024E2B77400432D0E /* svim-scripts.js */,
191194
E380F27E233183EE00640547 /* mousetrap.js */,
192195
E380F27F233183EE00640547 /* vimium-scripts.js */,
193196
);
@@ -297,6 +300,7 @@
297300
files = (
298301
E380F26C2331806500640547 /* ToolbarItemIcon.pdf in Resources */,
299302
B1FD3B9923A588DE00677A52 /* defaultSettings.json in Resources */,
303+
659033F124E2B77400432D0E /* svim-scripts.js in Resources */,
300304
65E444F324CC3A1B008EA1DC /* SafariExtensionCommunicator.js in Resources */,
301305
E380F28B233183EF00640547 /* injected.css in Resources */,
302306
E380F285233183EF00640547 /* injected.js in Resources */,

0 commit comments

Comments
 (0)