From 84d8ab6a3ad97697a717a770c42010e9ec9076e5 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 27 May 2026 14:36:57 -0500 Subject: feat(window): remember a side window's size across toggles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The F10 music playlist opened at a fixed fraction every time, so any manual resize was lost the moment it was toggled closed. Now the toggle captures the window's size on close and reopens at that height, for the rest of the session. The mechanism is generic, not music-specific. cj/window-size-fraction (geometry-lib) is the pure kernel: a clamped window/frame ratio. cj/side-window-capture-size and cj/side-window-display (toggle-lib) wrap it for any display-buffer-in-side-window consumer — height for top/bottom, width for left/right — storing the remembered fraction in a caller-supplied state var. It mirrors the direction-split toggle pattern the vterm dispatchers already use, but for atomic side windows that can't be split. music-config wires F10 to it: cj/music-playlist-window-height is the default, cj/--music-playlist-height holds the remembered value (in-memory, resets each session). 12 new tests across the two libs, Normal/Boundary/Error each covered. --- tests/test-cj-window-geometry-lib.el | 29 +++++++++++++++ tests/test-cj-window-toggle-lib.el | 71 ++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) (limited to 'tests') diff --git a/tests/test-cj-window-geometry-lib.el b/tests/test-cj-window-geometry-lib.el index 2b417425..05ed9595 100644 --- a/tests/test-cj-window-geometry-lib.el +++ b/tests/test-cj-window-geometry-lib.el @@ -168,5 +168,34 @@ window forms the full-height right half -> nil." (should (null (cj/window-at-edge 'sideways))) (should (null (cj/window-at-edge nil))))) +;; ----------------------------- window-size-fraction ----------------------------- + +(ert-deftest test-cj-window-geometry-size-fraction-normal () + "Normal: a window half the frame returns 0.5." + (should (= (cj/window-size-fraction 20 40) 0.5))) + +(ert-deftest test-cj-window-geometry-size-fraction-clamps-high () + "Boundary: a near-full window is clamped to the 0.95 ceiling." + (should (= (cj/window-size-fraction 40 40) 0.95))) + +(ert-deftest test-cj-window-geometry-size-fraction-clamps-low () + "Boundary: a vanishingly small window is clamped to the 0.05 floor." + (should (= (cj/window-size-fraction 0 40) 0.05))) + +(ert-deftest test-cj-window-geometry-size-fraction-custom-bounds () + "Boundary: explicit MIN-FRAC/MAX-FRAC override the defaults." + (should (= (cj/window-size-fraction 1 40 0.1 0.9) 0.1)) + (should (= (cj/window-size-fraction 39 40 0.1 0.9) 0.9))) + +(ert-deftest test-cj-window-geometry-size-fraction-zero-frame-nil () + "Error: a non-positive frame size returns nil (no divide-by-zero)." + (should (null (cj/window-size-fraction 20 0))) + (should (null (cj/window-size-fraction 20 -5)))) + +(ert-deftest test-cj-window-geometry-size-fraction-non-number-nil () + "Error: non-numeric arguments return nil." + (should (null (cj/window-size-fraction nil 40))) + (should (null (cj/window-size-fraction 20 nil)))) + (provide 'test-cj-window-geometry-lib) ;;; test-cj-window-geometry-lib.el ends here diff --git a/tests/test-cj-window-toggle-lib.el b/tests/test-cj-window-toggle-lib.el index 2c4ea831..ca4b7fef 100644 --- a/tests/test-cj-window-toggle-lib.el +++ b/tests/test-cj-window-toggle-lib.el @@ -184,5 +184,76 @@ (should (eq (cdr (assq 'direction received-alist)) 'bottom)) (should (= (cdr (assq 'window-height received-alist)) 0.4)))) +;; --------------------------- side-window helpers --------------------------- + +(defvar test-cj-side-window--size nil) + +(ert-deftest test-cj-side-window-capture-records-bottom-height () + "Normal: a bottom side window writes a height fraction into the size var." + (save-window-excursion + (delete-other-windows) + (let* ((buf (get-buffer-create "*cj-side-test*")) + (win (display-buffer-in-side-window + buf '((side . bottom) (window-height . 0.5)))) + (test-cj-side-window--size nil)) + (unwind-protect + (progn + (cj/side-window-capture-size win 'bottom 'test-cj-side-window--size) + (should (floatp test-cj-side-window--size)) + (should (<= 0.05 test-cj-side-window--size 0.95))) + (when (window-live-p win) (delete-window win)) + (kill-buffer buf))))) + +(ert-deftest test-cj-side-window-capture-noop-on-nil-window () + "Error: nil window leaves the size var unchanged." + (let ((test-cj-side-window--size 0.42)) + (cj/side-window-capture-size nil 'bottom 'test-cj-side-window--size) + (should (= test-cj-side-window--size 0.42)))) + +(ert-deftest test-cj-side-window-capture-noop-on-deleted-window () + "Error: a deleted window leaves the size var unchanged." + (let ((test-cj-side-window--size 0.42) + (dead (save-window-excursion + (delete-other-windows) + (let* ((buf (get-buffer-create "*cj-side-dead*")) + (w (display-buffer-in-side-window + buf '((side . bottom) (window-height . 0.5))))) + (delete-window w) + (kill-buffer buf) + w)))) + (cj/side-window-capture-size dead 'bottom 'test-cj-side-window--size) + (should (= test-cj-side-window--size 0.42)))) + +(ert-deftest test-cj-side-window-display-uses-default-when-state-nil () + "Normal: nil state -> default size under window-height for a bottom side." + (let (received-alist + (test-cj-side-window--size nil)) + (cl-letf (((symbol-function 'display-buffer-in-side-window) + (lambda (_b a) (setq received-alist a) 'fake-window))) + (cj/side-window-display 'fake-buf 'bottom 'test-cj-side-window--size 0.3)) + (should (eq (cdr (assq 'side received-alist)) 'bottom)) + (should (= (cdr (assq 'window-height received-alist)) 0.3)) + (should-not (assq 'window-width received-alist)))) + +(ert-deftest test-cj-side-window-display-uses-stored-size () + "Normal: a stored fraction overrides the default." + (let (received-alist + (test-cj-side-window--size 0.55)) + (cl-letf (((symbol-function 'display-buffer-in-side-window) + (lambda (_b a) (setq received-alist a) 'fake-window))) + (cj/side-window-display 'fake-buf 'bottom 'test-cj-side-window--size 0.3)) + (should (= (cdr (assq 'window-height received-alist)) 0.55)))) + +(ert-deftest test-cj-side-window-display-left-uses-window-width () + "Boundary: a left side puts the size under window-width, not window-height." + (let (received-alist + (test-cj-side-window--size nil)) + (cl-letf (((symbol-function 'display-buffer-in-side-window) + (lambda (_b a) (setq received-alist a) 'fake-window))) + (cj/side-window-display 'fake-buf 'left 'test-cj-side-window--size 0.25)) + (should (eq (cdr (assq 'side received-alist)) 'left)) + (should (= (cdr (assq 'window-width received-alist)) 0.25)) + (should-not (assq 'window-height received-alist)))) + (provide 'test-cj-window-toggle-lib) ;;; test-cj-window-toggle-lib.el ends here -- cgit v1.2.3