Split into horizontal and vertical centering

Also remove forcibly redisplaing. Not sure it's necessary, so let's wait and
see if things break.
This commit is contained in:
larstvei 2024-09-14 23:42:16 +02:00
parent d8551328f0
commit 1db2c7bff6

View File

@ -37,28 +37,30 @@
(line-height (default-line-height))) (line-height (default-line-height)))
(/ (max 0 (- window-height content-height)) (* 2 line-height)))) (/ (max 0 (- window-height content-height)) (* 2 line-height))))
(defun center-content--enable () (defun center-content--horizontal (content-width)
"Enable horizontal and vertical centering of content." "Add overlay for horizontal centering relative to CONTENT-WIDTH."
(let* ((content-size (center-content--content-pixel-size)) (let* ((left-margin (center-content--calculate-left-margin content-width))
(left-margin (center-content--calculate-left-margin (car content-size))) (overlay (make-overlay (point-min) (point-max)))
(top-margin (center-content--calculate-top-margin (cdr content-size))))
;; Apply horizontal centering using overlay
(let ((overlay (make-overlay (point-min) (point-max)))
(space (propertize " " 'display `(space :width ,left-margin)))) (space (propertize " " 'display `(space :width ,left-margin))))
(overlay-put overlay 'line-prefix space) (overlay-put overlay 'line-prefix space)
(setq center-content--horizontal-overlay overlay)) (setq center-content--horizontal-overlay overlay)))
;; Apply vertical centering using header-line-format
(defun center-content--vertical (content-height)
"Add overlay for vertical centering relative to CONTENT-HEIGHT."
(let* ((top-margin (center-content--calculate-top-margin content-height)))
;; Save and remap the header-line face to match the default face ;; Save and remap the header-line face to match the default face
(setq center-content--original-header-line-format header-line-format) (setq center-content--original-header-line-format header-line-format)
(setq center-content--header-line-face-remap-cookie (setq center-content--header-line-face-remap-cookie
(face-remap-add-relative 'header-line 'default)) (face-remap-add-relative 'header-line 'default))
(setq header-line-format (propertize " " 'display `(height ,top-margin))) (setq header-line-format (propertize " " 'display `(height ,top-margin)))))
;; Save and hide the mode line
(defun center-content--enable ()
"Enable horizontal and vertical centering of content."
(let* ((content-size (center-content--content-pixel-size)))
(center-content--horizontal (car content-size))
(center-content--vertical (cdr content-size))
(setq center-content--original-mode-line-format mode-line-format) (setq center-content--original-mode-line-format mode-line-format)
(setq mode-line-format nil) (setq mode-line-format nil)))
;; Force redisplay
(force-mode-line-update)
(redisplay)))
(defun center-content--disable () (defun center-content--disable ()
"Disable centering of content." "Disable centering of content."