all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Andrea Greselin <greselin.andrea@gmail.com>
To: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: Re: Ignore spurious focus events for ‘after-focus-change-function’
Date: Mon, 18 Jan 2021 20:09:44 +0100	[thread overview]
Message-ID: <CAJ_oJbYt9jGxfm3CO+5XD=vqzyXh+2odxDjQqnLPMKPvQRrfNA@mail.gmail.com> (raw)
In-Reply-To: <CAJ_oJbaWN5QGoRrqaU+gbqCG4aaCAOHX6F_=6=O3MsTr6VjLAQ@mail.gmail.com>

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


      reply	other threads:[~2021-01-18 19:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJ_oJbYt9jGxfm3CO+5XD=vqzyXh+2odxDjQqnLPMKPvQRrfNA@mail.gmail.com' \
    --to=greselin.andrea@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.