unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] xwidget.el: limit buffer-name flicker in mode-line.
@ 2021-11-08  6:23 tumashu
  2021-11-08  6:27 ` Po Lu
  0 siblings, 1 reply; 8+ messages in thread
From: tumashu @ 2021-11-08  6:23 UTC (permalink / raw)
  To: emacs-devel@gnu.org


[-- Attachment #1.1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #1.2: Type: text/html, Size: 111 bytes --]

[-- Attachment #2: 0001-xwidget.el-limit-buffer-name-flicker-in-mode-line.patch --]
[-- Type: application/octet-stream, Size: 6545 bytes --]

From e2ccea178782fdafe70c812eabf6ad44704ded1c Mon Sep 17 00:00:00 2001
From: Feng Shu <tumashu@163.com>
Date: Mon, 8 Nov 2021 14:14:15 +0800
Subject: [PATCH] xwidget.el: limit buffer-name flicker in mode-line.

* lisp/xwidget.el (xwidget-webkit-buffer-name-prefix): New variable.
(xwidget-webkit-callback): with-current-buffer only after get title,
which can limit buffer-name change in mode-line.
(xwidget-webkit-new-session, xwidget-webkit-import-widget): Use
current buffer to generate a temp xwidget-webkit buffer.
---
 lisp/xwidget.el | 85 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 53 insertions(+), 32 deletions(-)

diff --git a/lisp/xwidget.el b/lisp/xwidget.el
index 1d91e863e4..5efda3db45 100644
--- a/lisp/xwidget.el
+++ b/lisp/xwidget.el
@@ -89,6 +89,9 @@ xwidget-at
 (require 'seq)
 (require 'url-handlers)
 
+(defvar xwidget-webkit-buffer-name-prefix "* xwidget-webkit "
+  "Buffer name prefix used by xwidget-webkit buffer.")
+
 (defvar-local xwidget-webkit--title ""
   "The title of the WebKit widget, used for the header line.")
 
@@ -345,32 +348,36 @@ xwidget-webkit-callback
   (if (not (buffer-live-p (xwidget-buffer xwidget)))
       (xwidget-log
        "error: callback called for xwidget with dead buffer")
-    (with-current-buffer (xwidget-buffer xwidget)
-      (cond ((eq xwidget-event-type 'load-changed)
-             (let ((title (xwidget-webkit-title xwidget)))
-               (setq xwidget-webkit--title title)
-               (force-mode-line-update)
-               (xwidget-log "webkit finished loading: %s" title)
-               ;; Do not adjust webkit size to window here, the selected window
-               ;; can be the mini-buffer window unwantedly.
-               (rename-buffer (format "*xwidget webkit: %s *" title) t)))
-            ((eq xwidget-event-type 'decide-policy)
-             (let ((strarg  (nth 3 last-input-event)))
-               (if (string-match ".*#\\(.*\\)" strarg)
-                   (xwidget-webkit-show-id-or-named-element
-                    xwidget
-                    (match-string 1 strarg)))))
-            ;; TODO: Response handling other than download.
-            ((eq xwidget-event-type 'download-callback)
-             (let ((url  (nth 3 last-input-event))
-                   (mime-type (nth 4 last-input-event))
-                   (file-name (nth 5 last-input-event)))
-               (xwidget-webkit-save-as-file url mime-type file-name)))
-            ((eq xwidget-event-type 'javascript-callback)
-             (let ((proc (nth 3 last-input-event))
-                   (arg  (nth 4 last-input-event)))
-               (funcall proc arg)))
-            (t (xwidget-log "unhandled event:%s" xwidget-event-type))))))
+    (cond ((eq xwidget-event-type 'load-changed)
+           (let ((title (xwidget-webkit-title xwidget)))
+             ;; This funciton will be called multi times, so only
+             ;; change buffer name when get a valid title.  this can
+             ;; limit buffer-name flicker in mode-line.
+             (when (> (length title) 0)
+               (with-current-buffer (xwidget-buffer xwidget)
+                 (setq xwidget-webkit--title title)
+                 (force-mode-line-update)
+                 (xwidget-log "webkit finished loading: %s" title)
+                 ;; Do not adjust webkit size to window here, the selected window
+                 ;; can be the mini-buffer window unwantedly.
+                 (rename-buffer (concat xwidget-webkit-buffer-name-prefix title) t)))))
+          ((eq xwidget-event-type 'decide-policy)
+           (let ((strarg  (nth 3 last-input-event)))
+             (if (string-match ".*#\\(.*\\)" strarg)
+                 (xwidget-webkit-show-id-or-named-element
+                  xwidget
+                  (match-string 1 strarg)))))
+          ;; TODO: Response handling other than download.
+          ((eq xwidget-event-type 'download-callback)
+           (let ((url  (nth 3 last-input-event))
+                 (mime-type (nth 4 last-input-event))
+                 (file-name (nth 5 last-input-event)))
+             (xwidget-webkit-save-as-file url mime-type file-name)))
+          ((eq xwidget-event-type 'javascript-callback)
+           (let ((proc (nth 3 last-input-event))
+                 (arg  (nth 4 last-input-event)))
+             (funcall proc arg)))
+          (t (xwidget-log "unhandled event:%s" xwidget-event-type)))))
 
 (defvar bookmark-make-record-function)
 (when (memq window-system '(mac ns))
@@ -682,11 +689,16 @@ xwidget-webkit-adjust-size-in-frame
 
 (defun xwidget-webkit-new-session (url &optional callback)
   "Create a new webkit session buffer with URL."
-  (let*
-      ((bufname (generate-new-buffer-name "*xwidget-webkit*"))
-       (callback (or callback #'xwidget-webkit-callback))
-       (current-session (xwidget-webkit-current-session))
-       xw)
+  (let* ((bufname
+          ;; Generate a temp-name based on current buffer name. it
+          ;; will be renamed by `xwidget-webkit-callback' in the
+          ;; future.  This approach can limit flicker of buffer-name
+          ;; in mode-line.  .
+          (concat xwidget-webkit-buffer-name-prefix
+                  (generate-new-buffer-name (buffer-name))))
+         (callback (or callback #'xwidget-webkit-callback))
+         (current-session (xwidget-webkit-current-session))
+         xw)
     (setq xwidget-webkit-last-session-buffer (switch-to-buffer
                                               (get-buffer-create bufname)))
     ;; The xwidget id is stored in a text property, so we need to have
@@ -707,7 +719,16 @@ xwidget-webkit-new-session
 (defun xwidget-webkit-import-widget (xwidget)
   "Create a new webkit session buffer from XWIDGET, an existing xwidget.
 Return the buffer."
-  (let* ((bufname (generate-new-buffer-name "*xwidget-webkit*"))
+  (let* ((bufname
+          ;; Generate a temp-name based on current buffer name.  it
+          ;; will be renamed by `xwidget-webkit-callback' in the
+          ;; future.  This approach can limit flicker of buffer-name
+          ;; in mode-line.
+          (if (string-prefix-p xwidget-webkit-buffer-name-prefix
+                               (buffer-name))
+              (generate-new-buffer-name (buffer-name))
+            (concat xwidget-webkit-buffer-name-prefix
+                    (generate-new-buffer-name (buffer-name)))))
          (callback #'xwidget-webkit-callback)
          (buffer (get-buffer-create bufname)))
     (with-current-buffer buffer
-- 
2.30.2


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

end of thread, other threads:[~2021-11-09  3:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-08  6:23 [PATCH] xwidget.el: limit buffer-name flicker in mode-line tumashu
2021-11-08  6:27 ` Po Lu
2021-11-08  6:32   ` tumashu
2021-11-08  6:40     ` Po Lu
2021-11-08  6:54       ` tumashu
2021-11-08  6:59         ` Po Lu
2021-11-08  7:06           ` tumashu
2021-11-09  3:53             ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

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

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).