Improve detection of focus changes

* exwm-input.el: (exwm-input--on-buffer-list-update): Keep track
	of last selected window and buffer, update focus only when any
	of those changes.
	(exwm-input--update-focus-defer): Add commentary.
	(exwm-input--update-focus-window-buffer): Add
	variable.

Copyright-paperwork-exempt: yes
Co-Author: Adrián Medraño Calvo <adrian@medranocalvo.com>
This commit is contained in:
Qiang Fang 2020-01-31 17:50:38 +08:00 committed by Adrián Medraño Calvo
parent 95d6aa96cf
commit 763e423d39

View file

@ -138,8 +138,16 @@ defined in `exwm-mode-map' here."
"Timer for deferring the update of input focus.") "Timer for deferring the update of input focus.")
(defvar exwm-input--update-focus-window nil "The (Emacs) window to be focused. (defvar exwm-input--update-focus-window nil "The (Emacs) window to be focused.
It also helps us discern whether a `buffer-list-update-hook' was caused by a
different window having been selected.
This value should always be overwritten.") This value should always be overwritten.")
(defvar exwm-input--update-focus-window-buffer nil
"Buffer displayed in `exwm-input--update-focus-window'.
Helps us discern whether a `buffer-list-update-hook' was caused by the selected
window switching to a different buffer.")
(defvar exwm-input--echo-area-timer nil "Timer for detecting echo area dirty.") (defvar exwm-input--echo-area-timer nil "Timer for detecting echo area dirty.")
(defvar exwm-input--event-hook nil (defvar exwm-input--event-hook nil
@ -292,13 +300,25 @@ ARGS are additional arguments to CALLBACK."
(defun exwm-input--on-buffer-list-update () (defun exwm-input--on-buffer-list-update ()
"Run in `buffer-list-update-hook' to track input focus." "Run in `buffer-list-update-hook' to track input focus."
;; `buffer-list-update-hook' is invoked by several functions
;; (`get-buffer-create', `select-window', `with-temp-buffer', etc.), but we
;; just want to notice when a different window has been selected, or when the
;; selected window displays a different buffer, so that we can set the focus
;; to the associated X window (in case of an `exwm-mode' buffer). In order to
;; differentiate, we keep track of the last selected window and buffer in the
;; `exwm-input--update-focus-window' and
;; `exwm-input--update-focus-window-buffer' variables.
(let* ((win (selected-window))
(buf (window-buffer win)))
(when (and (not (exwm-workspace--client-p)) (when (and (not (exwm-workspace--client-p))
(not exwm-input--skip-buffer-list-update)) (not exwm-input--skip-buffer-list-update)
(exwm--log "current-buffer=%S selected-window=%S" (not (and (eq exwm-input--update-focus-window win)
(current-buffer) (selected-window)) (eq exwm-input--update-focus-window-buffer buf))))
(exwm--log "selected-window=%S current-buffer=%S" win buf)
(setq exwm-input--update-focus-window win)
(setq exwm-input--update-focus-window-buffer buf)
(redirect-frame-focus (selected-frame) nil) (redirect-frame-focus (selected-frame) nil)
(setq exwm-input--update-focus-window (selected-window)) (exwm-input--update-focus-defer))))
(exwm-input--update-focus-defer)))
(defun exwm-input--update-focus-defer () (defun exwm-input--update-focus-defer ()
"Defer updating input focus." "Defer updating input focus."