From 6b0e23412d367a3b90db01de9456431158525938 Mon Sep 17 00:00:00 2001 From: Po Lu <luangruo@yahoo.com> Date: Tue, 22 Feb 2022 11:35:27 +0800 Subject: [PATCH] Improve momentum pixel scrolling on a non-selected window * lisp/pixel-scroll.el (pixel-scroll-kinetic-state): New argument `window'. (pixel-scroll-start-momentum): Don't select the window under the event when calculating velocity or redisplaying. --- lisp/pixel-scroll.el | 102 ++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/lisp/pixel-scroll.el b/lisp/pixel-scroll.el index 54724ca3285..463e106c7ac 100644 --- a/lisp/pixel-scroll.el +++ b/lisp/pixel-scroll.el @@ -699,11 +699,12 @@ wheel." (message (error-message-string '(end-of-buffer)))))))))) (mwheel-scroll event nil)))) -(defun pixel-scroll-kinetic-state () - "Return the kinetic scroll state of the current window. +(defun pixel-scroll-kinetic-state (&optional window) + "Return the kinetic scroll state of WINDOW. +If WINDOW is nil, return the state of the current window. It is a vector of the form [ VELOCITY TIME SIGN ]." - (or (window-parameter nil 'kinetic-state) - (set-window-parameter nil 'kinetic-state + (or (window-parameter window 'kinetic-state) + (set-window-parameter window 'kinetic-state (vector (make-ring 30) nil nil)))) (defun pixel-scroll-accumulate-velocity (delta) @@ -737,53 +738,54 @@ It is a vector of the form [ VELOCITY TIME SIGN ]." (when pixel-scroll-precision-use-momentum (let ((window (mwheel-event-window event)) (state nil)) - (with-selected-window window - (setq state (pixel-scroll-kinetic-state)) - (when (and (aref state 1) - (listp (aref state 0))) - (condition-case nil - (while-no-input - (unwind-protect (progn - (aset state 0 (pixel-scroll-calculate-velocity state)) - (when (> (abs (aref state 0)) - pixel-scroll-precision-momentum-min-velocity) - (let* ((velocity (aref state 0)) - (original-velocity velocity) - (time-spent 0)) - (if (> velocity 0) - (while (and (> velocity 0) - (<= time-spent - pixel-scroll-precision-momentum-seconds)) - (when (> (round velocity) 0) - (pixel-scroll-precision-scroll-up (round velocity))) - (setq velocity (- velocity - (/ original-velocity - (/ pixel-scroll-precision-momentum-seconds - pixel-scroll-precision-momentum-tick)))) - (redisplay t) - (sit-for pixel-scroll-precision-momentum-tick) - (setq time-spent (+ time-spent - pixel-scroll-precision-momentum-tick)))) - (while (and (< velocity 0) - (<= time-spent - pixel-scroll-precision-momentum-seconds)) - (when (> (round (abs velocity)) 0) + (setq state (pixel-scroll-kinetic-state window)) + (when (and (aref state 1) + (listp (aref state 0))) + (condition-case nil + (while-no-input + (unwind-protect (progn + (aset state 0 (pixel-scroll-calculate-velocity state)) + (when (> (abs (aref state 0)) + pixel-scroll-precision-momentum-min-velocity) + (let* ((velocity (aref state 0)) + (original-velocity velocity) + (time-spent 0)) + (if (> velocity 0) + (while (and (> velocity 0) + (<= time-spent + pixel-scroll-precision-momentum-seconds)) + (when (> (round velocity) 0) + (with-selected-window window + (pixel-scroll-precision-scroll-up (round velocity)))) + (setq velocity (- velocity + (/ original-velocity + (/ pixel-scroll-precision-momentum-seconds + pixel-scroll-precision-momentum-tick)))) + (redisplay t) + (sit-for pixel-scroll-precision-momentum-tick) + (setq time-spent (+ time-spent + pixel-scroll-precision-momentum-tick)))) + (while (and (< velocity 0) + (<= time-spent + pixel-scroll-precision-momentum-seconds)) + (when (> (round (abs velocity)) 0) + (with-selected-window window (pixel-scroll-precision-scroll-down (round - (abs velocity)))) - (setq velocity (+ velocity - (/ (abs original-velocity) - (/ pixel-scroll-precision-momentum-seconds - pixel-scroll-precision-momentum-tick)))) - (redisplay t) - (sit-for pixel-scroll-precision-momentum-tick) - (setq time-spent (+ time-spent - pixel-scroll-precision-momentum-tick)))))) - (aset state 0 (make-ring 30)) - (aset state 1 nil))) - (beginning-of-buffer - (message (error-message-string '(beginning-of-buffer)))) - (end-of-buffer - (message (error-message-string '(end-of-buffer)))))))))) + (abs velocity))))) + (setq velocity (+ velocity + (/ (abs original-velocity) + (/ pixel-scroll-precision-momentum-seconds + pixel-scroll-precision-momentum-tick)))) + (redisplay t) + (sit-for pixel-scroll-precision-momentum-tick) + (setq time-spent (+ time-spent + pixel-scroll-precision-momentum-tick)))))) + (aset state 0 (make-ring 30)) + (aset state 1 nil))) + (beginning-of-buffer + (message (error-message-string '(beginning-of-buffer)))) + (end-of-buffer + (message (error-message-string '(end-of-buffer))))))))) (defun pixel-scroll-interpolate-down () "Interpolate a scroll downwards by one page." -- 2.39.5