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 diff
     1.1 --- a/src/indyvon/views.clj	Mon Nov 17 10:42:09 2014 +0300
     1.2 +++ b/src/indyvon/views.clj	Tue Nov 18 00:41:05 2014 +0300
     1.3 @@ -119,38 +119,52 @@
     1.4           (.fill *graphics* (Rectangle2D$Double. 0.0 0.0 *width* *height*)))
     1.5         (render! content))))
     1.6  
     1.7 +(defn hbox-proportional [& contents]
     1.8 +  (apply hbox* true contents))
     1.9 +
    1.10  (defn hbox
    1.11    "Creates a view that draws the specified content views placing them
    1.12 -   horizontally."
    1.13 +  horizontally."
    1.14    [& contents]
    1.15 +  (apply hbox* false contents))
    1.16 +
    1.17 +(defn- hbox*
    1.18 +  [proportional? & contents]
    1.19    (reify
    1.20 -   View
    1.21 -   (render! [_]
    1.22 -     (let [widths (map #(width (geometry %)) contents)
    1.23 -           xs (cons 0 (reductions + widths))
    1.24 -           widths-sum (last xs)
    1.25 -           scale (/ *width* widths-sum)]
    1.26 -       (doseq [[c w x] (map vector contents widths xs)]
    1.27 -         (draw! c x 0 w *height*))))
    1.28 -   (geometry [_]
    1.29 -     (reduce #(->Size (+ (width %1) (width %2))
    1.30 -                      (max (height %1) (height %2)))
    1.31 -             (->Size 0 0)
    1.32 -             (map geometry contents)))))
    1.33 +    View
    1.34 +    (render! [_]
    1.35 +      (let [widths (map #(width (geometry %)) contents)
    1.36 +            xs (cons 0 (reductions + widths))
    1.37 +            widths-sum (last xs)
    1.38 +            scale (if proportional? (/ *width* widths-sum) 1)]
    1.39 +        (doseq [[c w x] (map vector contents widths xs)]
    1.40 +          (draw! c (* scale x) 0 (* scale w) *height*))))
    1.41 +    (geometry [_]
    1.42 +      (reduce #(->Size (+ (width %1) (width %2))
    1.43 +                       (max (height %1) (height %2)))
    1.44 +              (->Size 0 0)
    1.45 +              (map geometry contents)))))
    1.46 +
    1.47 +(defn vbox-proportional [& contents]
    1.48 +  (apply vbox* true contents))
    1.49  
    1.50  (defn vbox
    1.51    "Creates a view that draws the specified content views placing them
    1.52 -   vertically."
    1.53 +  horizontally."
    1.54    [& contents]
    1.55 +  (apply vbox* false contents))
    1.56 +
    1.57 +(defn vbox*
    1.58 +  [proportional? & contents]
    1.59    (reify
    1.60     View
    1.61     (render! [_]
    1.62       (let [heights (map #(height (geometry %)) contents)
    1.63             ys (cons 0 (reductions + heights))
    1.64             heights-sum (last ys)
    1.65 -           scale (/ *height* heights-sum)]
    1.66 +           scale (if proportional? (/ *height* heights-sum) 1)]
    1.67         (doseq [[c h y] (map vector contents heights ys)]
    1.68 -         (draw! c 0 y *width* h))))
    1.69 +         (draw! c 0 (* scale y) *width* (* scale h)))))
    1.70     (geometry [_]
    1.71       (reduce #(->Size (max (width %1) (width %2))
    1.72                        (+ (height %1) (height %2)))