Skip to content

Commit

Permalink
[Refactor] bar and hud.
Browse files Browse the repository at this point in the history
  • Loading branch information
seagle0128 committed May 27, 2021
1 parent f963cbb commit 0e98271
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 78 deletions.
127 changes: 63 additions & 64 deletions doom-modeline-segments.el
Original file line number Diff line number Diff line change
Expand Up @@ -1356,79 +1356,64 @@ of active `multiple-cursors'."

(defvar doom-modeline--bar-active nil)
(defvar doom-modeline--bar-inactive nil)
(doom-modeline-def-segment bar
"The bar regulates the height of the mode-line in GUI."
(unless doom-modeline-hud
(unless (and doom-modeline--bar-active doom-modeline--bar-inactive)
(let ((width doom-modeline-bar-width)
(height (max doom-modeline-height
(doom-modeline--font-height))))
(when (and (numberp width) (numberp height))
(setq doom-modeline--bar-active
(doom-modeline--create-bar-image 'doom-modeline-bar width height)
doom-modeline--bar-inactive
(doom-modeline--create-bar-image
'doom-modeline-bar-inactive width height)))))
(if (doom-modeline--active)
doom-modeline--bar-active
doom-modeline--bar-inactive)))

(defsubst doom-modeline--bar ()
"The default bar regulates the height of the mode-line in GUI."
(unless (and doom-modeline--bar-active doom-modeline--bar-inactive)
(let ((width doom-modeline-bar-width)
(height (max doom-modeline-height
(doom-modeline--font-height))))
(when (and (numberp width) (numberp height))
(setq doom-modeline--bar-active
(doom-modeline--create-bar-image 'doom-modeline-bar width height)
doom-modeline--bar-inactive
(doom-modeline--create-bar-image
'doom-modeline-bar-inactive width height)))))
(if (doom-modeline--active)
doom-modeline--bar-active
doom-modeline--bar-inactive))

(defun doom-modeline-refresh-bars ()
"Refresh mode-line bars on next redraw."
(setq doom-modeline--bar-active nil
doom-modeline--bar-inactive nil))

(doom-modeline-add-variable-watcher
'doom-modeline-height
(lambda (_sym val op _where)
(when (and (eq op 'set) (integerp val))
(doom-modeline-refresh-bars))))

(doom-modeline-add-variable-watcher
'doom-modeline-bar-width
(lambda (_sym val op _where)
(when (and (eq op 'set) (integerp val))
(doom-modeline-refresh-bars))))

(add-hook 'after-setting-font-hook #'doom-modeline-refresh-bars)

(cl-defstruct doom-modeline--hud-cache active inactive top-margin bottom-margin)

(doom-modeline-def-segment hud
(defsubst doom-modeline--hud ()
"Powerline's hud segment reimplemented in the style of Doom's bar segment."
(when doom-modeline-hud
(let* ((ws (window-start))
(we (window-end))
(bs (buffer-size))
(height (max doom-modeline-height
(doom-modeline--font-height)))
(top-margin (if (zerop bs)
0
(/ (* height (1- ws)) bs)))
(bottom-margin (if (zerop bs)
0
(max 0 (/ (* height (- bs we 1)) bs))))
(cache (or (window-parameter nil 'doom-modeline--hud-cache)
(set-window-parameter nil 'doom-modeline--hud-cache
(make-doom-modeline--hud-cache)))))
(unless (and (doom-modeline--hud-cache-active cache)
(doom-modeline--hud-cache-inactive cache)
(= top-margin (doom-modeline--hud-cache-top-margin cache))
(= bottom-margin
(doom-modeline--hud-cache-bottom-margin cache)))
(setf (doom-modeline--hud-cache-active cache)
(doom-modeline--create-hud-image
'doom-modeline-bar 'default doom-modeline-bar-width
height top-margin bottom-margin)
(doom-modeline--hud-cache-inactive cache)
(doom-modeline--create-hud-image
'doom-modeline-bar-inactive 'default doom-modeline-bar-width
height top-margin bottom-margin)
(doom-modeline--hud-cache-top-margin cache) top-margin
(doom-modeline--hud-cache-bottom-margin cache) bottom-margin))
(if (doom-modeline--active)
(doom-modeline--hud-cache-active cache)
(doom-modeline--hud-cache-inactive cache)))))
(let* ((ws (window-start))
(we (window-end))
(bs (buffer-size))
(height (max doom-modeline-height
(doom-modeline--font-height)))
(top-margin (if (zerop bs)
0
(/ (* height (1- ws)) bs)))
(bottom-margin (if (zerop bs)
0
(max 0 (/ (* height (- bs we 1)) bs))))
(cache (or (window-parameter nil 'doom-modeline--hud-cache)
(set-window-parameter nil 'doom-modeline--hud-cache
(make-doom-modeline--hud-cache)))))
(unless (and (doom-modeline--hud-cache-active cache)
(doom-modeline--hud-cache-inactive cache)
(= top-margin (doom-modeline--hud-cache-top-margin cache))
(= bottom-margin
(doom-modeline--hud-cache-bottom-margin cache)))
(setf (doom-modeline--hud-cache-active cache)
(doom-modeline--create-hud-image
'doom-modeline-bar 'default doom-modeline-bar-width
height top-margin bottom-margin)
(doom-modeline--hud-cache-inactive cache)
(doom-modeline--create-hud-image
'doom-modeline-bar-inactive 'default doom-modeline-bar-width
height top-margin bottom-margin)
(doom-modeline--hud-cache-top-margin cache) top-margin
(doom-modeline--hud-cache-bottom-margin cache) bottom-margin))
(if (doom-modeline--active)
(doom-modeline--hud-cache-active cache)
(doom-modeline--hud-cache-inactive cache))))

(defun doom-modeline-invalidate-huds ()
"Invalidate all cached hud images."
Expand All @@ -1440,16 +1425,29 @@ of active `multiple-cursors'."
'doom-modeline-height
(lambda (_sym val op _where)
(when (and (eq op 'set) (integerp val))
(doom-modeline-refresh-bars)
(doom-modeline-invalidate-huds))))

(doom-modeline-add-variable-watcher
'doom-modeline-bar-width
(lambda (_sym val op _where)
(when (and (eq op 'set) (integerp val))
(doom-modeline-refresh-bars)
(doom-modeline-invalidate-huds))))

(add-hook 'after-setting-font-hook #'doom-modeline-refresh-bars)
(add-hook 'after-setting-font-hook #'doom-modeline-invalidate-huds)

(doom-modeline-def-segment bar
"The bar regulates the height of the mode-line in GUI."
(if doom-modeline-hud
(doom-modeline--hud)
(doom-modeline--bar)))

(doom-modeline-def-segment hud
"Powerline's hud segment reimplemented in the style of Doom's bar segment."
(doom-modeline--hud))


;;
;; Window number
Expand Down Expand Up @@ -1484,6 +1482,7 @@ one. The ignored buffers are excluded unless `aw-ignore-on' is nil."
(advice-add #'winum--clear-mode-line :override #'ignore)

(doom-modeline-def-segment window-number
"The current window number."
(let ((num (cond
((bound-and-true-p ace-window-display-mode)
(aw-update)
Expand Down
28 changes: 14 additions & 14 deletions doom-modeline.el
Original file line number Diff line number Diff line change
Expand Up @@ -90,59 +90,59 @@
;;

(doom-modeline-def-modeline 'main
'(bar hud workspace-name window-number modals matches buffer-info remote-host buffer-position word-count parrot selection-info)
'(bar workspace-name window-number modals matches buffer-info remote-host buffer-position word-count parrot selection-info)
'(objed-state misc-info persp-name battery grip irc mu4e gnus github debug repl lsp minor-modes input-method indent-info buffer-encoding major-mode process vcs checker))

(doom-modeline-def-modeline 'minimal
'(bar hud matches buffer-info-simple)
'(bar matches buffer-info-simple)
'(media-info major-mode))

(doom-modeline-def-modeline 'special
'(bar hud window-number modals matches buffer-info buffer-position word-count parrot selection-info)
'(bar window-number modals matches buffer-info buffer-position word-count parrot selection-info)
'(objed-state misc-info battery irc-buffers debug minor-modes input-method indent-info buffer-encoding major-mode process))

(doom-modeline-def-modeline 'project
'(bar hud window-number buffer-default-directory)
'(bar window-number buffer-default-directory)
'(misc-info battery irc mu4e gnus github debug minor-modes input-method major-mode process))

(doom-modeline-def-modeline 'dashboard
'(bar hud window-number buffer-default-directory-simple)
'(bar window-number buffer-default-directory-simple)
'(misc-info battery irc mu4e gnus github debug minor-modes input-method major-mode process))

(doom-modeline-def-modeline 'vcs
'(bar hud window-number modals matches buffer-info buffer-position parrot selection-info)
'(bar window-number modals matches buffer-info buffer-position parrot selection-info)
'(misc-info battery irc mu4e gnus github debug minor-modes buffer-encoding major-mode process))

(doom-modeline-def-modeline 'package
'(bar hud window-number package)
'(bar window-number package)
'(misc-info major-mode process))

(doom-modeline-def-modeline 'info
'(bar hud window-number buffer-info info-nodes buffer-position parrot selection-info)
'(bar window-number buffer-info info-nodes buffer-position parrot selection-info)
'(misc-info buffer-encoding major-mode))

(doom-modeline-def-modeline 'media
'(bar hud window-number buffer-size buffer-info)
'(bar window-number buffer-size buffer-info)
'(misc-info media-info major-mode process vcs))

(doom-modeline-def-modeline 'message
'(bar hud window-number modals matches buffer-info-simple buffer-position word-count parrot selection-info)
'(bar window-number modals matches buffer-info-simple buffer-position word-count parrot selection-info)
'(objed-state misc-info battery debug minor-modes input-method indent-info buffer-encoding major-mode))

(doom-modeline-def-modeline 'pdf
'(bar hud window-number matches buffer-info pdf-pages)
'(bar window-number matches buffer-info pdf-pages)
'(misc-info major-mode process vcs))

(doom-modeline-def-modeline 'org-src
'(bar hud window-number modals matches buffer-info-simple buffer-position word-count parrot selection-info)
'(bar window-number modals matches buffer-info-simple buffer-position word-count parrot selection-info)
'(objed-state misc-info debug lsp minor-modes input-method indent-info buffer-encoding major-mode process checker))

(doom-modeline-def-modeline 'helm
'(bar hud helm-buffer-id helm-number helm-follow helm-prefix-argument)
'(bar helm-buffer-id helm-number helm-follow helm-prefix-argument)
'(helm-help))

(doom-modeline-def-modeline 'timemachine
'(bar hud window-number matches git-timemachine buffer-position word-count parrot selection-info)
'(bar window-number matches git-timemachine buffer-position word-count parrot selection-info)
'(misc-info minor-modes indent-info buffer-encoding major-mode))


Expand Down

0 comments on commit 0e98271

Please sign in to comment.