Skip to content

Commit

Permalink
Fix linear layouts to allow alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Aug 8, 2019
1 parent 7cf8c23 commit f56e337
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions layouts/linear.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@

(defclass linear-layout (layout)
((min-size :initarg :min-size :initform (size) :accessor min-size)
(stretch :initarg :stretch :initform T :accessor stretch)))
(stretch :initarg :stretch :initform T :accessor stretch)
(align :initarg :align :initform :start :accessor align)))

(defgeneric update-linear-layout (layout index extent))

(defmethod (setf align) :after (value (layout linear-layout))
(update-linear-layout layout 0 (bounds layout)))

(defmethod (setf stretch) :after (value (layout linear-layout))
(update-linear-layout layout 0 (bounds layout)))

(defmethod (setf min-size) :after (value (layout linear-layout))
(update-linear-layout layout 0 (bounds layout)))

(defmethod notice-bounds :after ((element layout-element) (layout linear-layout))
(let ((updated (update-linear-layout layout (element-index element layout) (bounds layout))))
(unless (extent= (bounds layout) updated)
Expand All @@ -24,32 +34,44 @@
())

(defmethod update-linear-layout ((layout vertical-linear-layout) index extent)
(destructure-extent (:x x :y oy :w w) extent
(destructure-extent (:x x :y oy :w w :h h) extent
(let ((mh (size-h (min-size layout)))
(mw (if (stretch layout) w (size-w (min-size layout))))
(y oy))
(y (ecase (align layout)
(:start oy) (:end (+ oy h)))))
(do-elements (element layout :start index :result (extent x oy w (- y oy)))
(let ((ideal (suggest-bounds (extent x y mw mh) element))
(bounds (bounds element)))
(setf (extent-x bounds) x)
(setf (extent-y bounds) y)
(setf (extent-w bounds) (if (stretch layout) w (extent-w ideal)))
(setf (extent-h bounds) (extent-h ideal))
(incf y (extent-h bounds)))))))
(ecase (align layout)
(:start
(setf (extent-y bounds) y)
(incf y (extent-h bounds)))
(:end
(decf y (extent-h bounds))
(setf (extent-y bounds) y))))))))

(defclass horizontal-linear-layout (linear-layout)
())

(defmethod update-linear-layout ((layout horizontal-linear-layout) index extent)
(destructure-extent (:x ox :y y :h h) extent
(destructure-extent (:x ox :y y :w w :h h) extent
(let ((mh (if (stretch layout) h (size-h (min-size layout))))
(mw (size-w (min-size layout)))
(x ox))
(x (ecase (align layout)
(:start ox) (:end (+ ox w)))))
(do-elements (element layout :start index :result (extent ox y (- x ox) h))
(let ((ideal (suggest-bounds (extent x y mw mh) element))
(bounds (bounds element)))
(setf (extent-x bounds) x)
(setf (extent-y bounds) y)
(setf (extent-w bounds) (extent-w ideal))
(setf (extent-h bounds) (if (stretch layout) h (extent-h ideal)))
(incf x (extent-w bounds)))))))
(ecase (align layout)
(:start
(setf (extent-x bounds) x)
(incf x (extent-w bounds)))
(:end
(decf x (extent-w bounds))
(setf (extent-x bounds) x))))))))

0 comments on commit f56e337

Please sign in to comment.