changeset 159:2a93c3ca0244

Variants of vbox and hbox that fill available space.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 18 Nov 2014 00:41:05 +0300
parents e0063c1d0f7f
children d149f03d1feb
files src/indyvon/views.clj
diffstat 1 files changed, 31 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/indyvon/views.clj	Mon Nov 17 10:42:09 2014 +0300
+++ b/src/indyvon/views.clj	Tue Nov 18 00:41:05 2014 +0300
@@ -119,38 +119,52 @@
          (.fill *graphics* (Rectangle2D$Double. 0.0 0.0 *width* *height*)))
        (render! content))))
 
+(defn hbox-proportional [& contents]
+  (apply hbox* true contents))
+
 (defn hbox
   "Creates a view that draws the specified content views placing them
-   horizontally."
+  horizontally."
   [& contents]
+  (apply hbox* false contents))
+
+(defn- hbox*
+  [proportional? & contents]
   (reify
-   View
-   (render! [_]
-     (let [widths (map #(width (geometry %)) contents)
-           xs (cons 0 (reductions + widths))
-           widths-sum (last xs)
-           scale (/ *width* widths-sum)]
-       (doseq [[c w x] (map vector contents widths xs)]
-         (draw! c x 0 w *height*))))
-   (geometry [_]
-     (reduce #(->Size (+ (width %1) (width %2))
-                      (max (height %1) (height %2)))
-             (->Size 0 0)
-             (map geometry contents)))))
+    View
+    (render! [_]
+      (let [widths (map #(width (geometry %)) contents)
+            xs (cons 0 (reductions + widths))
+            widths-sum (last xs)
+            scale (if proportional? (/ *width* widths-sum) 1)]
+        (doseq [[c w x] (map vector contents widths xs)]
+          (draw! c (* scale x) 0 (* scale w) *height*))))
+    (geometry [_]
+      (reduce #(->Size (+ (width %1) (width %2))
+                       (max (height %1) (height %2)))
+              (->Size 0 0)
+              (map geometry contents)))))
+
+(defn vbox-proportional [& contents]
+  (apply vbox* true contents))
 
 (defn vbox
   "Creates a view that draws the specified content views placing them
-   vertically."
+  horizontally."
   [& contents]
+  (apply vbox* false contents))
+
+(defn vbox*
+  [proportional? & contents]
   (reify
    View
    (render! [_]
      (let [heights (map #(height (geometry %)) contents)
            ys (cons 0 (reductions + heights))
            heights-sum (last ys)
-           scale (/ *height* heights-sum)]
+           scale (if proportional? (/ *height* heights-sum) 1)]
        (doseq [[c h y] (map vector contents heights ys)]
-         (draw! c 0 y *width* h))))
+         (draw! c 0 (* scale y) *width* (* scale h)))))
    (geometry [_]
      (reduce #(->Size (max (width %1) (width %2))
                       (+ (height %1) (height %2)))