I'm on Emacs 27.1, Fedora 33, GNOME 3.38.2.
In the latest NEWS it is reported that
The hooks
focus-in-hookandfocus-out-hookare now obsolete. Instead, attach toafter-focus-change-functionusingadd-functionand inspect the focus state of each frame usingframe-focus-state.
The documentation for after-focus-change-function says
Depending on window system, focus events may also be delivered repeatedly and with different focus states before settling to the expected values. Code relying on focus notifications should "debounce" any user-visible updates arising from focus changes, perhaps by deferring work until redisplay.
Indeed, after evaluating this code,
(defun focus-test () (message "ffs: %s" (frame-focus-state))) (add-function :after after-focus-change-function #'focus-test) every time I switch buffer by selecting one with the mouse my message buffer shows
ffs: nil ffs: t ffs: nil ffs: t That is, Emacs sees two back and forth focus changes where I did none, and most importantly it has hallucinations of focus-out events which defeat the usefulness of the after-focus-change-function+frame-focus-state method for triggering functions based on focus events.
My question is, how do I "'debounce' any user-visible updates arising from focus changes"? (I made a naive attempt with
(defun focus-test () (sit-for 0) (message "ffs: %s" (frame-focus-state))) but it didn't work.)
M-x report-emacs-bug. Some have mentioned frame-focus problems introduced with Emacs 27.