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
"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."
(let* ((window (selected-window))
(window-width (window-pixel-width window))
(content-width (car (window-text-pixel-size
window
(window-start window)
(window-end window t))))
(let ((window-width (window-pixel-width))
(column-width (string-pixel-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."
(let* ((window (selected-window))
(window-height (window-pixel-height window))
(content-height (cdr (window-text-pixel-size
window
(window-start window)
(window-end window t)))))
(/ (max 0 (- window-height content-height)) (* 2 (frame-char-height)))))
(let ((window-height (window-pixel-height))
(line-height (default-line-height)))
(/ (max 0 (- window-height content-height)) (* 2 line-height))))
(defun center-content--enable ()
"Enable horizontal and vertical centering of content."
(let ((left-margin (center-content--calculate-left-margin))
(top-margin (center-content--calculate-top-margin)))
(let* ((content-size (center-content--content-pixel-size))
(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
(let ((overlay (make-overlay (point-min) (point-max))))
(overlay-put overlay 'line-prefix