at commit 2aa41890

1 ;;;; common lisp window manager
2 ;;;;
3 ;;;; maximize clients
4
5 (in-package #:clwm-ext)
6
7 (defclass maximize-mixin (screen-geometry-mixin) ()
8 (:documentation "Maximize clients horizontally, vertically, or both,
9 within the SCREEN-GEOMETRY-MIXIN work area. Geometry is stored to be
10 restored."))
11
12 (defclass maximizable-client ()
13 ((maximized :initform nil :accessor client-maximized
14 :documentation
15 "A plist of :HORIZONTAL and :VERTICAL axis geometry
16 to restore, or NIL if totally unmaximized."))
17 (:documentation "A client that can be maximized."))
18
19 (defmethod client-mixins append ((wm maximize-mixin))
20 '(maximizable-client))
21
22 (defgeneric maximize-state-changed (wm client)
23 (:documentation "Handle a maximization state change.")
24 (:method (wm client)
25 (declare (ignore wm client))
26 nil))
27
28 (defun maximized-axes (client)
29 "The axes a client is maximized along (:HORIZONTAL/:VERTICAL)."
30 (remove-if-not (lambda (axis) (getf (client-maximized client) axis))
31 '(:horizontal :vertical)))
32
33 ;; clear maximizations when resizing maximized windows
34 (defmethod configure-client :before
35 ((wm maximize-mixin) client &key width height &allow-other-keys)
36 (let ((horizontal
37 (and width (remf (client-maximized client) :horizontal)))
38 (vertical
39 (and height (remf (client-maximized client) :vertical))))
40 (when (or horizontal vertical)
41 (maximize-state-changed wm client))))
42
43 (defun axis-span (axis x y width height)
44 "The position and size of a rectangle along AXIS
45 (:HORIZONTAL/:VERTICAL) as a list."
46 (ecase axis
47 (:horizontal (list x width))
48 (:vertical (list y height))))
49
50 (defun span-geometry (axis span)
51 "Helper to build CONFIGURE-CLIENT arguments putting a client at SPAN
52 along AXIS."
53 (destructuring-bind (position size) span
54 (ecase axis
55 (:horizontal (list :x position :width size))
56 (:vertical (list :y position :height size)))))
57
58 (defun remember-axes (client axes x y width height)
59 "Record outer geometry to restore client to along AXES."
60 (dolist (axis axes)
61 (setf (getf (client-maximized client) axis)
62 (axis-span axis x y width height))))
63
64 (defun maximize-axes (wm client axes)
65 "Fill the work area along AXES and remember the restore geometry."
66 (multiple-value-bind (x y width height) (client-geometry wm client)
67 (let ((area (multiple-value-list
68 (work-area wm (area-at wm (+ x (floor width 2))
69 (+ y (floor height 2)))))))
70 (apply #'configure-client wm client
71 (loop for axis in axes
72 append (span-geometry
73 axis
74 (apply #'axis-span axis area))))
75 (remember-axes client axes x y width height)
76 (when axes
77 (maximize-state-changed wm client)))))
78
79 (defun restore-axes (wm client axes)
80 "Restore a client along AXES using the remembered geometry."
81 (let ((geometry
82 (loop for axis in axes
83 for span = (getf (client-maximized client) axis)
84 when span
85 append (span-geometry axis span)
86 do (remf (client-maximized client) axis))))
87 (when geometry
88 (apply #'configure-client wm client geometry)
89 (maximize-state-changed wm client))))
90
91 (defun toggle-maximize (wm client &key (horizontal t) (vertical t))
92 "Toggle maximization, optionally not along both axes."
93 (let ((axes (append (when horizontal '(:horizontal))
94 (when vertical '(:vertical))))
95 (maximized (maximized-axes client)))
96 (if (subsetp axes maximized)
97 (restore-axes wm client axes)
98 (maximize-axes wm client (set-difference axes maximized)))))
99
100 (defun maximize-command (wm client x y)
101 "Maximize or restore the client under the pointer.
102 Expects MAXIMIZE-MIXIN.
103 Respects POINTER-KEYBINDS-MIXIN convention."
104 (declare (ignore x y))
105 (when client (toggle-maximize wm client)))
106
107 (defun vmaximize-command (wm client x y)
108 "Vertically maximize or restore the client under the pointer.
109 Expects MAXIMIZE-MIXIN.
110 Respects POINTER-KEYBINDS-MIXIN convention."
111 (declare (ignore x y))
112 (when client (toggle-maximize wm client :horizontal nil)))
113
114 (defun hmaximize-command (wm client x y)
115 "Horizontally maximize or restore the client under the pointer.
116 Expects MAXIMIZE-MIXIN.
117 Respects POINTER-KEYBINDS-MIXIN convention."
118 (declare (ignore x y))
119 (when client (toggle-maximize wm client :vertical nil)))