Fix in content-size calculation

This commit is contained in:
larstvei 2024-09-14 23:22:11 +02:00
parent 26785e7ac4
commit 4925bca107

View File

@ -19,31 +19,26 @@
(defvar-local center-content--header-line-face-remap-cookie nil (defvar-local center-content--header-line-face-remap-cookie nil
"Cookie for the face remapping of `header-line`.") "Cookie for the face remapping of `header-line`.")
(defun center-content--calculate-left-margin () (defun center-content--content-pixel-size ()
(window-text-pixel-size (selected-window) (window-start) (window-end)))
(defun center-content--calculate-left-margin (content-width)
"Calculate the left margin needed to center the content horizontally." "Calculate the left margin needed to center the content horizontally."
(let* ((window (selected-window)) (let ((window-width (window-pixel-width))
(window-width (window-pixel-width window)) (column-width (string-pixel-width " ")))
(content-width (car (window-text-pixel-size
window
(window-start window)
(window-end window t))))
(column-width (string-pixel-width " ")))
(/ (max 0 (- window-width content-width)) (* 2 column-width)))) (/ (max 0 (- window-width content-width)) (* 2 column-width))))
(defun center-content--calculate-top-margin () (defun center-content--calculate-top-margin (content-height)
"Calculate the top margin needed to center the content vertically." "Calculate the top margin needed to center the content vertically."
(let* ((window (selected-window)) (let ((window-height (window-pixel-height))
(window-height (window-pixel-height window)) (line-height (default-line-height)))
(content-height (cdr (window-text-pixel-size (/ (max 0 (- window-height content-height)) (* 2 line-height))))
window
(window-start window)
(window-end window t)))))
(/ (max 0 (- window-height content-height)) (* 2 (frame-char-height)))))
(defun center-content--enable () (defun center-content--enable ()
"Enable horizontal and vertical centering of content." "Enable horizontal and vertical centering of content."
(let ((left-margin (center-content--calculate-left-margin)) (let* ((content-size (center-content--content-pixel-size))
(top-margin (center-content--calculate-top-margin))) (left-margin (center-content--calculate-left-margin (car content-size)))
(top-margin (center-content--calculate-top-margin (cdr content-size))))
;; Apply horizontal centering using overlay ;; Apply horizontal centering using overlay
(let ((overlay (make-overlay (point-min) (point-max)))) (let ((overlay (make-overlay (point-min) (point-max))))
(overlay-put overlay 'line-prefix (overlay-put overlay 'line-prefix