all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / 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

* Re: Ignore spurious focus events for ‘after-focus-change-function’
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2021-01-16  7:02 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Andrea Greselin <greselin.andrea@gmail.com>
> Date: Fri, 15 Jan 2021 21:26:32 +0100
> 
> 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.

What do you want to accomplish, and how did it not work?

In any case, I very much doubt that "deferring work until redisplay"
meant that you should _trigger_ redisplay by calling sit-for.  It
probably meant running your code from some hook called by redisplay,
or something similar.



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

* Re: Ignore spurious focus events for ‘after-focus-change-function’
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Andrea Greselin @ 2021-01-16  9:56 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi Eli, I need it for triggering auto-save on focus-out and functions
that dim some colours on focus-out and restore them on focus-in.

Auto-save works when it should, and also when it shouldn't. Changing
the colours on focus out works well but there's some flickering when
they're restored on focus in, which I think may be due to the excess
focus events triggering it repeatedly.

Also, I have an issue with the tab-bar when the Solarized theme is
enabled (the GUI keys -- new tab, close tab, the tab itself -- don't
work when I click on them) and I'm trying to debug it. It must be
something in my configuration because there are no problems with
Solarized in ‘emacs -Q’. I wanted to ignore the spurious focus events
as part of debugging this issue.

> In any case, I very much doubt that "deferring work until redisplay"
> meant that you should _trigger_ redisplay by calling sit-for.

I had tried with ‘(sit-for 0 t)’ first, wich *maybe* was bit cleverer
but yes, it was naive. Now I have something that seems to be working,
from a suggestion on Reddit [1]

  (defvar focus-out-auto-save-timer nil)

  (defun focus-test () (message "ffs: %s" (frame-focus-state)) (setq
    focus-out-auto-save-timer nil))

  (defun focus-test-wrapper () (setq focus-out-auto-save-timer (unless
    (timerp focus-out-auto-save-timer) (run-at-time "0.05 sec" nil
    #'focus-test))))

  (add-function :after after-focus-change-function
  #'focus-test-wrapper)

It's not super-robust (works with delay >= 0.05 s, doesn't work if
it's <= 0.01 s) but it's done its job for the couple of times I tested
it. In particular, it does not see focus-out events unless one
actually happens. I still hope there is a cleaner way, I don't like
this one very much.

1
https://www.reddit.com/r/emacs/comments/kxsgtn/ignore_spurious_focus_events_for/


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

* Re: Ignore spurious focus events for ‘after-focus-change-function’
  2021-01-16  9:56 ` Andrea Greselin
@ 2021-01-18 19:09   ` Andrea Greselin
  0 siblings, 0 replies; 4+ messages in thread
From: Andrea Greselin @ 2021-01-18 19:09 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

I've settled with these home-made remakes of the obsoleted hooks:

  ;; Set up hooks to be run on focus in and focus out

  ;; Mutter sends spurious focus events, including sending focus out
  ;; events when Emacs never really was unfocused.
  ;; The timer filters out these false focus out events, while
  ;; checking the ‘last-focus-state’ allows ignoring repeated events
  ;; of the same kind.
  ;; REVIEW: false focus out events are still received sometimes when
  ;; using the menu bar.
  ;; Adapted from a suggestion by DasEwigeLicht on
  ;;
https://www.reddit.com/r/emacs/comments/kxsgtn/ignore_spurious_focus_events_for/

  (defvar focus-events-timer nil)

  (defvar last-focus-state (frame-focus-state))

  (defvar my-focus-in-hook nil
    "Normal hook run when all frames lose input focus.")

  (defvar my-focus-out-hook nil
    "Normal hook run when a frame gains focus.")

  (defun change-of-focus-functions ()
    (unless (equal last-focus-state (frame-focus-state))
      (if (frame-focus-state)
          (run-hooks 'my-focus-in-hook)
        (run-hooks 'my-focus-out-hook)))
    (setq focus-events-timer nil)
    (setq last-focus-state (frame-focus-state)))

  (defun run-change-of-focus-functions-with-timer ()
    (setq focus-events-timer
          (unless (timerp focus-events-timer)
            (run-at-time "0.05 sec" nil ; Found by trial and error.
                         #'change-of-focus-functions))))

  (add-function :after after-focus-change-function
                #'run-change-of-focus-functions-with-timer)

  ;; Test

  (defun focus-in-test ()
    (message "focus-in"))

  (defun focus-out-test ()
    (message "focus-out"))

  (add-hook 'my-focus-in-hook #'focus-in-test)
  (add-hook 'my-focus-out-hook #'focus-out-test)

  ;; Actual use

  (add-hook 'my-focus-out-hook #'do-auto-save)

Please reply with suggestions if there's something wrong or
less-than-optimal.

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

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.