diff options
| author | Po Lu | 2021-12-02 19:35:09 +0800 |
|---|---|---|
| committer | Po Lu | 2021-12-02 19:36:33 +0800 |
| commit | d45e72fe5a9205b2e146d27e8e26233ee8518331 (patch) | |
| tree | 7522ff97f07dbff7d56a5019f46d3cbf87d985a2 | |
| parent | aa6a05c146e94c0afc35b5354d68da55aecb2db7 (diff) | |
| download | emacs-d45e72fe5a9205b2e146d27e8e26233ee8518331.tar.gz emacs-d45e72fe5a9205b2e146d27e8e26233ee8518331.zip | |
Improve momentum scrolling algorithm
* lisp/pixel-scroll.el (pixel-scroll-precision-momentum-tick):
(pixel-scroll-precision-momentum-factor): New user options.
(pixel-scroll-kinetic-state, pixel-scroll-accumulate-velocity):
Set scroll momentum ring size to 10.
(pixel-scroll-start-momentum): Improve algorithm.
| -rw-r--r-- | lisp/pixel-scroll.el | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/lisp/pixel-scroll.el b/lisp/pixel-scroll.el index 092d7215d31..a45a4d41c33 100644 --- a/lisp/pixel-scroll.el +++ b/lisp/pixel-scroll.el | |||
| @@ -109,6 +109,18 @@ This is only effective if supported by your mouse or touchpad." | |||
| 109 | :type 'boolean | 109 | :type 'boolean |
| 110 | :version "29.1") | 110 | :version "29.1") |
| 111 | 111 | ||
| 112 | (defcustom pixel-scroll-precision-momentum-tick 0.16 | ||
| 113 | "Number of seconds between each momentum scroll." | ||
| 114 | :group 'mouse | ||
| 115 | :type 'float | ||
| 116 | :version "29.1") | ||
| 117 | |||
| 118 | (defcustom pixel-scroll-precision-momentum-factor 0.95 | ||
| 119 | "Factor by which to reduce scroll velocity on each momentum scroll" | ||
| 120 | :group 'mouse | ||
| 121 | :type 'float | ||
| 122 | :version "29.1") | ||
| 123 | |||
| 112 | (defun pixel-scroll-in-rush-p () | 124 | (defun pixel-scroll-in-rush-p () |
| 113 | "Return non-nil if next scroll should be non-smooth. | 125 | "Return non-nil if next scroll should be non-smooth. |
| 114 | When scrolling request is delivered soon after the previous one, | 126 | When scrolling request is delivered soon after the previous one, |
| @@ -501,14 +513,14 @@ wheel." | |||
| 501 | It is a vector of the form [ VELOCITY TIME ]." | 513 | It is a vector of the form [ VELOCITY TIME ]." |
| 502 | (or (window-parameter nil 'kinetic-state) | 514 | (or (window-parameter nil 'kinetic-state) |
| 503 | (set-window-parameter nil 'kinetic-state | 515 | (set-window-parameter nil 'kinetic-state |
| 504 | (vector (make-ring 4) nil)))) | 516 | (vector (make-ring 10) nil)))) |
| 505 | 517 | ||
| 506 | (defun pixel-scroll-accumulate-velocity (delta) | 518 | (defun pixel-scroll-accumulate-velocity (delta) |
| 507 | "Accumulate DELTA into the current window's kinetic scroll state." | 519 | "Accumulate DELTA into the current window's kinetic scroll state." |
| 508 | (let* ((state (pixel-scroll-kinetic-state)) | 520 | (let* ((state (pixel-scroll-kinetic-state)) |
| 509 | (time (aref state 1))) | 521 | (time (aref state 1))) |
| 510 | (when (and time (> (- (float-time) time) 0.5)) | 522 | (when (and time (> (- (float-time) time) 0.5)) |
| 511 | (aset state 0 (make-ring 45))) | 523 | (aset state 0 (make-ring 10))) |
| 512 | (ring-insert (aref state 0) | 524 | (ring-insert (aref state 0) |
| 513 | (cons (aset state 1 (float-time)) | 525 | (cons (aset state 1 (float-time)) |
| 514 | delta)))) | 526 | delta)))) |
| @@ -532,23 +544,26 @@ It is a vector of the form [ VELOCITY TIME ]." | |||
| 532 | (state nil)) | 544 | (state nil)) |
| 533 | (with-selected-window window | 545 | (with-selected-window window |
| 534 | (setq state (pixel-scroll-kinetic-state)) | 546 | (setq state (pixel-scroll-kinetic-state)) |
| 535 | (when (aref state 1) | 547 | (when (and (aref state 1) |
| 548 | (listp (aref state 0))) | ||
| 536 | (unwind-protect (progn | 549 | (unwind-protect (progn |
| 537 | (aset state 0 | 550 | (aset state 0 |
| 538 | (pixel-scroll-calculate-velocity state)) | 551 | (/ (pixel-scroll-calculate-velocity state) 2)) |
| 539 | (let ((velocity (aref state 0))) | 552 | (let ((velocity (aref state 0))) |
| 540 | (if (> velocity 0) | 553 | (if (> velocity 0) |
| 541 | (while (> velocity 0) | 554 | (while (> velocity 1) |
| 542 | (pixel-scroll-precision-scroll-up 1) | 555 | (pixel-scroll-precision-scroll-up (round velocity)) |
| 543 | (setq velocity (1- velocity)) | 556 | (setq velocity (* velocity |
| 544 | (sit-for 0.1) | 557 | pixel-scroll-precision-momentum-factor)) |
| 545 | (redisplay t)) | 558 | (redisplay t) |
| 546 | (while (< velocity 0) | 559 | (sit-for pixel-scroll-precision-momentum-tick))) |
| 547 | (pixel-scroll-precision-scroll-down 1) | 560 | (while (< velocity -1) |
| 548 | (setq velocity (1+ velocity)) | 561 | (pixel-scroll-precision-scroll-down (round (abs velocity))) |
| 549 | (sit-for 0.1) | 562 | (setq velocity (* velocity |
| 550 | (redisplay t))))) | 563 | pixel-scroll-precision-momentum-factor)) |
| 551 | (aset state 0 (make-ring 45)) | 564 | (redisplay t) |
| 565 | (sit-for pixel-scroll-precision-momentum-tick)))) | ||
| 566 | (aset state 0 (make-ring 10)) | ||
| 552 | (aset state 1 nil))))))) | 567 | (aset state 1 nil))))))) |
| 553 | 568 | ||
| 554 | ;;;###autoload | 569 | ;;;###autoload |