[Optimize] Calculate font height.

Not use font-info which is slow and may hang.
Only create xpm images if available on GUI.

Close #176.
This commit is contained in:
Vincent Zhang 2019-05-23 16:35:15 +08:00
parent 839f94e889
commit b2b7e60798
2 changed files with 35 additions and 41 deletions

View file

@ -485,14 +485,6 @@ If the actual char height is larger, it respects the actual char height.")
"Whether is an active window." "Whether is an active window."
(eq (selected-window) doom-modeline-current-window)) (eq (selected-window) doom-modeline-current-window))
(defun doom-modeline--char-height ()
"Calculate the actual char height of the mode-line."
(or
(let ((font (face-font 'mode-line)))
(when (and font (fboundp 'font-info))
(* 2 (aref (font-info font) 2))))
(round (* 1.3 (frame-char-height)))))
(defsubst doom-modeline-vspc () (defsubst doom-modeline-vspc ()
"Text style with icons in mode-line." "Text style with icons in mode-line."
(propertize " " 'face (if (doom-modeline--active) (propertize " " 'face (if (doom-modeline--active)
@ -505,6 +497,10 @@ If the actual char height is larger, it respects the actual char height.")
'mode-line 'mode-line
'mode-line-inactive))) 'mode-line-inactive)))
(defsubst doom-modeline--char-height ()
"Calculate the actual char height of the mode-line."
(ceiling (* 1.68 (frame-char-height))))
(defun doom-modeline-icon-octicon (&rest args) (defun doom-modeline-icon-octicon (&rest args)
"Display octicon via ARGS." "Display octicon via ARGS."
(when doom-modeline-icon (when doom-modeline-icon
@ -558,30 +554,31 @@ If the actual char height is larger, it respects the actual char height.")
(defun doom-modeline--make-xpm (face width height) (defun doom-modeline--make-xpm (face width height)
"Create an XPM bitmap via FACE, WIDTH and HEIGHT. Inspired by `powerline''s `pl/make-xpm'." "Create an XPM bitmap via FACE, WIDTH and HEIGHT. Inspired by `powerline''s `pl/make-xpm'."
(propertize (when (and (display-graphic-p)
" " 'display (image-type-available-p 'xpm))
(let ((data (make-list height (make-list width 1))) (propertize
(color (or (face-background face nil t) "None"))) " " 'display
(ignore-errors (let ((data (make-list height (make-list width 1)))
(create-image (color (or (face-background face nil t) "None")))
(concat (ignore-errors
(format "/* XPM */\nstatic char * percent[] = {\n\"%i %i 2 1\",\n\". c %s\",\n\" c %s\"," (create-image
(length (car data)) (concat
(length data) (format
color "/* XPM */\nstatic char * percent[] = {\n\"%i %i 2 1\",\n\". c %s\",\n\" c %s\","
color) (length (car data)) (length data) color color)
(apply #'concat (apply #'concat
(cl-loop with idx = 0 (cl-loop with idx = 0
with len = (length data) with len = (length data)
for dl in data for dl in data
do (cl-incf idx) do (cl-incf idx)
collect collect
(concat "\"" (concat
(cl-loop for d in dl "\""
if (= d 0) collect (string-to-char " ") (cl-loop for d in dl
else collect (string-to-char ".")) if (= d 0) collect (string-to-char " ")
(if (eq idx len) "\"};" "\",\n"))))) else collect (string-to-char "."))
'xpm t :ascent 'center))))) (if (eq idx len) "\"};" "\",\n")))))
'xpm t :ascent 'center))))))
;; Fix: invalid-regexp "Trailing backslash" while handling $HOME on Windows ;; Fix: invalid-regexp "Trailing backslash" while handling $HOME on Windows
(defun doom-modeline-shrink-path--dirs-internal (full-path &optional truncate-all) (defun doom-modeline-shrink-path--dirs-internal (full-path &optional truncate-all)

View file

@ -1212,13 +1212,10 @@ of active `multiple-cursors'."
(defvar doom-modeline--bar-active nil) (defvar doom-modeline--bar-active nil)
(defvar doom-modeline--bar-inactive nil) (defvar doom-modeline--bar-inactive nil)
(doom-modeline-def-segment bar (doom-modeline-def-segment bar
"The bar regulates the height of the mode-line in GUI Emacs. "The bar regulates the height of the mode-line in GUI."
Returns \"\" to not break --no-window-system." (if (doom-modeline--active)
(if (display-graphic-p) doom-modeline--bar-active
(if (doom-modeline--active) doom-modeline--bar-inactive))
doom-modeline--bar-active
doom-modeline--bar-inactive)
""))
(defun doom-modeline-refresh-bars (&optional width height) (defun doom-modeline-refresh-bars (&optional width height)
"Refresh mode-line bars with `WIDTH' and `HEIGHT'." "Refresh mode-line bars with `WIDTH' and `HEIGHT'."
@ -1226,12 +1223,12 @@ Returns \"\" to not break --no-window-system."
(doom-modeline--make-xpm 'doom-modeline-bar (doom-modeline--make-xpm 'doom-modeline-bar
(or width doom-modeline-bar-width) (or width doom-modeline-bar-width)
(max (or height doom-modeline-height) (max (or height doom-modeline-height)
(doom-modeline--char-height))) (doom-modeline--font-height)))
doom-modeline--bar-inactive doom-modeline--bar-inactive
(doom-modeline--make-xpm 'doom-modeline-inactive-bar (doom-modeline--make-xpm 'doom-modeline-inactive-bar
(or width doom-modeline-bar-width) (or width doom-modeline-bar-width)
(max (or height doom-modeline-height) (max (or height doom-modeline-height)
(doom-modeline--char-height))))) (doom-modeline--font-height)))))
(when (>= emacs-major-version 26) (when (>= emacs-major-version 26)
(add-variable-watcher (add-variable-watcher