[Optimize] Cache window font width.

Fix #215.
This commit is contained in:
Vincent Zhang 2019-08-31 00:01:03 +08:00
parent 47a9538197
commit b433d87f42
2 changed files with 12 additions and 5 deletions

View file

@ -344,4 +344,3 @@ Run `M-x customize-group RET doom-modeline RET` or set the variables.
```
Please refer to [#189](https://github.com/seagle0128/doom-modeline/issues/189).
This is no recommended due to the performance issue.

View file

@ -446,9 +446,8 @@ If the actual char height is larger, it respects the actual char height."
;; Modeline library
;;
(eval-and-compile
(defvar doom-modeline-fn-alist ())
(defvar doom-modeline-var-alist ()))
(defvar doom-modeline-var-alist ())
(defmacro doom-modeline-def-segment (name &rest body)
"Defines a modeline segment NAME with BODY and byte compiles it."
@ -485,6 +484,15 @@ If the actual char height is larger, it respects the actual char height."
((error "%s is not a valid segment" seg))))
(nreverse forms)))
(defvar doom-modeline--width-cache nil)
(defun doom-modeline--window-font-width ()
"Cache the font width."
(let ((attributes (face-all-attributes 'default)))
(or (cdr (assoc attributes doom-modeline--width-cache))
(let ((width (window-font-width nil 'mode-line)))
(push (cons attributes width) doom-modeline--width-cache)
width))))
(defun doom-modeline-def-modeline (name lhs &optional rhs)
"Defines a modeline format and byte-compiles it.
NAME is a symbol to identify it (used by `doom-modeline' for retrieval).
@ -507,7 +515,7 @@ If the actual char height is larger, it respects the actual char height."
'face (if (doom-modeline--active) 'mode-line 'mode-line-inactive)
'display `((space :align-to (- (+ right right-fringe right-margin)
,(* (if (number-or-marker-p (face-attribute 'mode-line :height))
(/ (window-font-width nil 'mode-line)
(/ (doom-modeline--window-font-width)
(frame-char-width) 1.0)
1)
(string-width