aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPo Lu2021-12-03 13:55:39 +0800
committerPo Lu2021-12-03 14:04:04 +0800
commit1afa295aed81357fddf9694bfe68ed0e6d159a2d (patch)
treebf770c1e7919cfdf48b4f91d5b948a6f536a809e
parentc66eb524537f98667507024e5f21e0b24038c964 (diff)
downloademacs-1afa295aed81357fddf9694bfe68ed0e6d159a2d.tar.gz
emacs-1afa295aed81357fddf9694bfe68ed0e6d159a2d.zip
Improve velocity calculation in momentum scrolling
* lisp/pixel-scroll.el (pixel-scroll-precision-momentum-factor): Remove option. (pixel-scroll-precision-initial-velocity-factor) (pixel-scroll-precision-momentum-min-velocity): New user options. (pixel-scroll-accumulate-velocity): Clear velocity ring if sign is different. (pixel-scroll-calculate-velocity): Use current time. (pixel-scroll-start-momentum): Use better algorithm.
-rw-r--r--lisp/pixel-scroll.el74
1 files changed, 49 insertions, 25 deletions
diff --git a/lisp/pixel-scroll.el b/lisp/pixel-scroll.el
index 3c764ff65ab..1c2d95613e5 100644
--- a/lisp/pixel-scroll.el
+++ b/lisp/pixel-scroll.el
@@ -121,8 +121,14 @@ This is only effective if supported by your mouse or touchpad."
121 :type 'float 121 :type 'float
122 :version "29.1") 122 :version "29.1")
123 123
124(defcustom pixel-scroll-precision-momentum-factor 0.95 124(defcustom pixel-scroll-precision-momentum-min-velocity 10.0
125 "Factor by which to reduce scroll velocity on each momentum scroll" 125 "The minimum scrolled pixels per second before momentum scrolling starts."
126 :group 'mouse
127 :type 'float
128 :version "29.1")
129
130(defcustom pixel-scroll-precision-initial-velocity-factor 0.25
131 "Factor applied to the initial velocity before momentum scrolling begins."
126 :group 'mouse 132 :group 'mouse
127 :type 'float 133 :type 'float
128 :version "29.1") 134 :version "29.1")
@@ -524,8 +530,13 @@ It is a vector of the form [ VELOCITY TIME ]."
524(defun pixel-scroll-accumulate-velocity (delta) 530(defun pixel-scroll-accumulate-velocity (delta)
525 "Accumulate DELTA into the current window's kinetic scroll state." 531 "Accumulate DELTA into the current window's kinetic scroll state."
526 (let* ((state (pixel-scroll-kinetic-state)) 532 (let* ((state (pixel-scroll-kinetic-state))
533 (ring (aref state 0))
527 (time (aref state 1))) 534 (time (aref state 1)))
528 (when (and time (> (- (float-time) time) 0.5)) 535 (when (or (and time (> (- (float-time) time) 0.5))
536 (and (not (ring-empty-p ring))
537 (not (eq (< delta 0)
538 (< (cdr (ring-ref ring 0))
539 0)))))
529 (aset state 0 (make-ring 10))) 540 (aset state 0 (make-ring 10)))
530 (ring-insert (aref state 0) 541 (ring-insert (aref state 0)
531 (cons (aset state 1 (float-time)) 542 (cons (aset state 1 (float-time))
@@ -538,8 +549,7 @@ It is a vector of the form [ VELOCITY TIME ]."
538 (total 0)) 549 (total 0))
539 (dolist (tem elts) 550 (dolist (tem elts)
540 (setq total (+ total (cdr tem)))) 551 (setq total (+ total (cdr tem))))
541 (/ total (* (- (caar elts) 552 (/ total (* (- (float-time) (caar elts))
542 (caar (last elts)))
543 100)))) 553 100))))
544 554
545(defun pixel-scroll-start-momentum (event) 555(defun pixel-scroll-start-momentum (event)
@@ -555,26 +565,40 @@ It is a vector of the form [ VELOCITY TIME ]."
555 (while-no-input 565 (while-no-input
556 (unwind-protect (progn 566 (unwind-protect (progn
557 (aset state 0 (pixel-scroll-calculate-velocity state)) 567 (aset state 0 (pixel-scroll-calculate-velocity state))
558 (let ((velocity (/ (aref state 0) 3)) 568 (when (> (abs (aref state 0))
559 (time-spent 0)) 569 pixel-scroll-precision-momentum-min-velocity)
560 (if (> velocity 0) 570 (let* ((velocity (* (aref state 0)
561 (while (and (> velocity 0.2) 571 pixel-scroll-precision-initial-velocity-factor))
562 (<= time-spent pixel-scroll-precision-momentum-seconds)) 572 (original-velocity velocity)
563 (pixel-scroll-precision-scroll-up (ceiling velocity)) 573 (time-spent 0))
564 (setq velocity (* velocity pixel-scroll-precision-momentum-factor)) 574 (if (> velocity 0)
565 (redisplay t) 575 (while (and (> velocity 0)
566 (sit-for pixel-scroll-precision-momentum-tick) 576 (<= time-spent
567 (setq time-spent (+ time-spent 577 pixel-scroll-precision-momentum-seconds))
568 pixel-scroll-precision-momentum-tick)))) 578 (when (> (round velocity) 0)
569 (while (and (< velocity -0.4) 579 (pixel-scroll-precision-scroll-up (round velocity)))
570 (<= time-spent 580 (setq velocity (- velocity
571 pixel-scroll-precision-momentum-seconds)) 581 (/ original-velocity
572 (pixel-scroll-precision-scroll-down (floor (abs velocity))) 582 (/ pixel-scroll-precision-momentum-seconds
573 (setq velocity (* velocity pixel-scroll-precision-momentum-factor)) 583 pixel-scroll-precision-momentum-tick))))
574 (redisplay t) 584 (redisplay t)
575 (sit-for pixel-scroll-precision-momentum-tick) 585 (sit-for pixel-scroll-precision-momentum-tick)
576 (setq time-spent (+ time-spent 586 (setq time-spent (+ time-spent
577 pixel-scroll-precision-momentum-tick))))) 587 pixel-scroll-precision-momentum-tick))))
588 (while (and (< velocity 0)
589 (<= time-spent
590 pixel-scroll-precision-momentum-seconds))
591 (when (> (round (abs velocity)) 0)
592 (pixel-scroll-precision-scroll-down (round
593 (abs velocity))))
594 (setq velocity (+ velocity
595 (/ (abs original-velocity)
596 (/ pixel-scroll-precision-momentum-seconds
597 pixel-scroll-precision-momentum-tick))))
598 (redisplay t)
599 (sit-for pixel-scroll-precision-momentum-tick)
600 (setq time-spent (+ time-spent
601 pixel-scroll-precision-momentum-tick))))))
578 (aset state 0 (make-ring 10)) 602 (aset state 0 (make-ring 10))
579 (aset state 1 nil)))))))) 603 (aset state 1 nil))))))))
580 604