unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Ignore spurious focus events for ‘after-focus-change-function’
@ 2021-01-15 20:26 Andrea Greselin
  2021-01-16  7:02 ` Eli Zaretskii
  2021-01-16  9:56 ` Andrea Greselin
  0 siblings, 2 replies; 4+ messages in thread
From: Andrea Greselin @ 2021-01-15 20:26 UTC (permalink / raw)
  To: help-gnu-emacs

Hello everyone, I'm on Emacs 27.1, Fedora 33, GNOME 3.38.2.

In the latest ‘NEWS’ it is reported that

> The hooks ‘focus-in-hook’ and ‘focus-out-hook’ are now obsolete.
> Instead, attach to ‘after-focus-change-function’ using
> ‘add-function’ and inspect the focus state of each frame using
> ‘frame-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 couple of attempts, with

  (defun focus-test ()
    (sit-for 0)
    (message "ffs: %s" (frame-focus-state)))

and

  (defun payload (_window)
    (message "ffs: %s" (frame-focus-state))
    (remove-hook 'pre-redisplay-functions #'payload))
  (defun focus-test ()
    (add-hook 'pre-redisplay-functions #'payload))
  (add-function :after after-focus-change-function #'focus-test)

but it didn't work.

Andrea


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-01-18 19:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-01-15 20:26 Ignore spurious focus events for ‘after-focus-change-function’ Andrea Greselin
2021-01-16  7:02 ` Eli Zaretskii
2021-01-16  9:56 ` Andrea Greselin
2021-01-18 19:09   ` Andrea Greselin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).