From: Juri Linkov Date: Sat, 30 Nov 2019 21:16:03 +0000 (+0200) Subject: Use run-with-idle-timer instead of debounce for responsive image scaling. X-Git-Tag: emacs-27.0.90~474 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d64ea182fb6e2bf3af8ac8a289e8029ded36407e;p=emacs.git Use run-with-idle-timer instead of debounce for responsive image scaling. * lisp/emacs-lisp/timer.el (debounce, debounce-reduce): Revert macro addition. https://lists.gnu.org/archive/html/emacs-devel/2019-11/msg01133.html * lisp/image.el (image-increase-size, image-decrease-size): Use run-with-idle-timer. (image--change-size): Rename back from image--change-size-function. * lisp/image-mode.el (image-mode--setup-mode): Remove hooks window-size-change-functions and window-selection-change-functions (bug#32672) (image-fit-to-window): Rename from image--window-change-function. (image--window-state-change): Rename from image--window-change. Use run-with-idle-timer. --- diff --git a/etc/NEWS b/etc/NEWS index 2cbc373eb03..8a72c31bf9b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2849,11 +2849,6 @@ doing computations on a decoded time structure), 'make-decoded-time' filled out), and 'encoded-time-set-defaults' (which fills in nil elements as if it's midnight January 1st, 1970) have been added. -** New macros 'debounce' and 'debounce-reduce' postpone function call -until after specified time have elapsed since the last time it was invoked. -This improves performance of processing events occurring rapidly -in quick succession. - ** 'define-minor-mode' automatically documents the meaning of ARG. +++ diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el index 5fdf9a426a7..561cc70078f 100644 --- a/lisp/emacs-lisp/timer.el +++ b/lisp/emacs-lisp/timer.el @@ -488,50 +488,6 @@ The argument should be a value previously returned by `with-timeout-suspend'." If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." (with-timeout (seconds default-value) (y-or-n-p prompt))) - -(defmacro debounce (secs function) - "Call FUNCTION after SECS seconds have elapsed. -Postpone FUNCTION call until after SECS seconds have elapsed since the -last time it was invoked. On consecutive calls within the interval of -SECS seconds, cancel all previous calls that occur rapidly in quick succession, -and execute only the last call. This improves performance of event processing." - (declare (indent 1) (debug t)) - (let ((timer-sym (make-symbol "timer"))) - `(let (,timer-sym) - (lambda (&rest args) - (when (timerp ,timer-sym) - (cancel-timer ,timer-sym)) - (setq ,timer-sym - (run-with-timer - ,secs nil (lambda () - (apply ,function args)))))))) - -(defmacro debounce-reduce (secs initial-state state-function function) - "Call FUNCTION after SECS seconds have elapsed. -Postpone FUNCTION call until after SECS seconds have elapsed since the -last time it was invoked. On consecutive calls within the interval of -SECS seconds, cancel all previous calls that occur rapidly in quick succession, -and execute only the last call. This improves performance of event processing. - -STATE-FUNCTION can be used to accumulate the state on consecutive calls -starting with the value of INITIAL-STATE, and then execute the last call -with the collected state value." - (declare (indent 1) (debug t)) - (let ((timer-sym (make-symbol "timer")) - (state-sym (make-symbol "state"))) - `(let (,timer-sym (,state-sym ,initial-state)) - (lambda (&rest args) - (setq ,state-sym (apply ,state-function ,state-sym args)) - (when (timerp ,timer-sym) - (cancel-timer ,timer-sym)) - (setq ,timer-sym - (run-with-timer - ,secs nil (lambda () - (apply ,function (if (listp ,state-sym) - ,state-sym - (list ,state-sym))) - (setq ,state-sym ,initial-state)))))))) - (defconst timer-duration-words (list (cons "microsec" 0.000001) diff --git a/lisp/image-mode.el b/lisp/image-mode.el index 09d7828047e..b9ba376cafc 100644 --- a/lisp/image-mode.el +++ b/lisp/image-mode.el @@ -599,9 +599,7 @@ Key bindings: (add-hook 'change-major-mode-hook #'image-toggle-display-text nil t) (add-hook 'after-revert-hook #'image-after-revert-hook nil t) - (add-hook 'window-size-change-functions #'image--window-change nil t) - (add-hook 'window-state-change-functions #'image--window-change nil t) - (add-hook 'window-selection-change-functions #'image--window-change nil t) + (add-hook 'window-state-change-functions #'image--window-state-change nil t) (run-mode-hooks 'image-mode-hook) (let ((image (image-get-display-property)) @@ -860,26 +858,31 @@ Otherwise, display the image by calling `image-mode'." (get-buffer-window-list (current-buffer) 'nomini 'visible)) (image-toggle-display-image))) -(defvar image--window-change-function - (debounce 1.0 - (lambda (window) - (when (window-live-p window) - (with-current-buffer (window-buffer) - (when (derived-mode-p 'image-mode) - (let ((spec (image-get-display-property))) - (when (eq (car-safe spec) 'image) - (let* ((image-width (plist-get (cdr spec) :max-width)) - (image-height (plist-get (cdr spec) :max-height)) - (edges (window-inside-pixel-edges window)) - (window-width (- (nth 2 edges) (nth 0 edges))) - (window-height (- (nth 3 edges) (nth 1 edges)))) - (when (and image-width image-height - (or (not (= image-width window-width)) - (not (= image-height window-height)))) - (image-toggle-display-image))))))))))) - -(defun image--window-change (window) - (funcall image--window-change-function window)) +(defun image--window-state-change (window) + ;; Wait for a bit of idle-time before actually performing the change, + ;; so as to batch together sequences of closely consecutive size changes. + ;; `image-fit-to-window' just changes one value in a plist. The actual + ;; image resizing happens later during redisplay. So if those + ;; consecutive calls happen without any redisplay between them, + ;; the costly operation of image resizing should happen only once. + (run-with-idle-timer 1 nil #'image-fit-to-window window)) + +(defun image-fit-to-window (window) + "Adjust size of image to display it exactly in WINDOW boundaries." + (when (window-live-p window) + (with-current-buffer (window-buffer) + (when (derived-mode-p 'image-mode) + (let ((spec (image-get-display-property))) + (when (eq (car-safe spec) 'image) + (let* ((image-width (plist-get (cdr spec) :max-width)) + (image-height (plist-get (cdr spec) :max-height)) + (edges (window-inside-pixel-edges window)) + (window-width (- (nth 2 edges) (nth 0 edges))) + (window-height (- (nth 3 edges) (nth 1 edges)))) + (when (and image-width image-height + (or (not (= image-width window-width)) + (not (= image-height window-height)))) + (image-toggle-display-image))))))))) ;;; Animated images diff --git a/lisp/image.el b/lisp/image.el index c4304782327..f4ed4e79fc0 100644 --- a/lisp/image.el +++ b/lisp/image.el @@ -1017,20 +1017,34 @@ has no effect." If N is 3, then the image size will be increased by 30%. The default is 20%." (interactive "P") - (funcall image--change-size-function - (if n - (1+ (/ (prefix-numeric-value n) 10.0)) - 1.2))) + ;; Wait for a bit of idle-time before actually performing the change, + ;; so as to batch together sequences of closely consecutive size changes. + ;; `image--change-size' just changes one value in a plist. The actual + ;; image resizing happens later during redisplay. So if those + ;; consecutive calls happen without any redisplay between them, + ;; the costly operation of image resizing should happen only once. + (run-with-idle-timer 0.3 nil + #'image--change-size + (if n + (1+ (/ (prefix-numeric-value n) 10.0)) + 1.2))) (defun image-decrease-size (&optional n) "Decrease the image size by a factor of N. If N is 3, then the image size will be decreased by 30%. The default is 20%." (interactive "P") - (funcall image--change-size-function - (if n - (- 1 (/ (prefix-numeric-value n) 10.0)) - 0.8))) + ;; Wait for a bit of idle-time before actually performing the change, + ;; so as to batch together sequences of closely consecutive size changes. + ;; `image--change-size' just changes one value in a plist. The actual + ;; image resizing happens later during redisplay. So if those + ;; consecutive calls happen without any redisplay between them, + ;; the costly operation of image resizing should happen only once. + (run-with-idle-timer 0.3 nil + #'image--change-size + (if n + (- 1 (/ (prefix-numeric-value n) 10.0)) + 0.8))) (defun image-mouse-increase-size (&optional event) "Increase the image size using the mouse." @@ -1065,16 +1079,12 @@ default is 20%." (plist-put (cdr image) :type 'imagemagick)) image)) -(defvar image--change-size-function - (debounce-reduce 0.3 1 - (lambda (state factor) - (* state factor)) - (lambda (factor) - (let* ((image (image--get-imagemagick-and-warn)) - (new-image (image--image-without-parameters image)) - (scale (image--current-scaling image new-image))) - (setcdr image (cdr new-image)) - (plist-put (cdr image) :scale (* scale factor)))))) +(defun image--change-size (factor) + (let* ((image (image--get-imagemagick-and-warn)) + (new-image (image--image-without-parameters image)) + (scale (image--current-scaling image new-image))) + (setcdr image (cdr new-image)) + (plist-put (cdr image) :scale (* scale factor)))) (defun image--image-without-parameters (image) (cons (pop image)