changeset 158:e0063c1d0f7f

Allow theme entries to reference other theme keys.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 17 Nov 2014 10:42:09 +0300
parents 4fea68ec12f4
children 2a93c3ca0244
files src/indyvon/component.clj src/indyvon/core.clj src/indyvon/demo.clj src/indyvon/views.clj
diffstat 4 files changed, 40 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/src/indyvon/component.clj	Wed Nov 12 15:44:17 2014 +0300
     1.2 +++ b/src/indyvon/component.clj	Mon Nov 17 10:42:09 2014 +0300
     1.3 @@ -29,7 +29,7 @@
     1.4  (defn- paint-component [^Component c ^Graphics g scene]
     1.5    (let [w (.getWidth c)
     1.6          h (.getHeight c)]
     1.7 -    (.setColor g (:back-color *theme*))
     1.8 +    (.setColor g (theme-get :back-color))
     1.9      (.fillRect g 0 0 w h)
    1.10      (draw-scene! scene g w h)))
    1.11  
    1.12 @@ -50,7 +50,7 @@
    1.13          panel
    1.14          {"paintComponent" #(paint-component %1 %2 scene)
    1.15           "getPreferredSize" #(preferred-size % scene)})
    1.16 -       (.setBackground panel (:back-color *theme*))
    1.17 +       (.setBackground panel (theme-get :back-color))
    1.18         (add-observer panel scene (fn [w _]
    1.19                                     ;; Use the first observer argument
    1.20                                     ;; instead of closing over panel to
     2.1 --- a/src/indyvon/core.clj	Wed Nov 12 15:44:17 2014 +0300
     2.2 +++ b/src/indyvon/core.clj	Mon Nov 17 10:42:09 2014 +0300
     2.3 @@ -381,6 +381,24 @@
     2.4        (finally
     2.5         (.dispose clip-g)))))
     2.6  
     2.7 +(defn- theme-get*
     2.8 +  ([theme key]
     2.9 +     (theme-get* theme key nil))
    2.10 +  ([theme key not-found]
    2.11 +     (if-let [e (find theme key)]
    2.12 +       (loop [k (val e)]
    2.13 +         (if-let [e1 (and (keyword? k)
    2.14 +                          (find theme k))]
    2.15 +           (recur (val e1))
    2.16 +           k))
    2.17 +       not-found)))
    2.18 +
    2.19 +(defn theme-get
    2.20 +  ([key]
    2.21 +     (theme-get* *theme* key))
    2.22 +  ([key not-found]
    2.23 +     (theme-get* *theme* key not-found)))
    2.24 +
    2.25  (defn ^Graphics2D apply-theme
    2.26    "Set graphics' color and font to match theme.
    2.27    Modifies and returns the first argument."
    2.28 @@ -388,9 +406,9 @@
    2.29       (apply-theme *graphics* *theme*))
    2.30    ([^Graphics2D graphics theme]
    2.31       (doto graphics
    2.32 -       (.setColor (:fore-color theme))
    2.33 -       (.setBackground (:back-color theme))
    2.34 -       (.setFont (:font theme)))))
    2.35 +       (.setColor (theme-get :fore-color))
    2.36 +       (.setBackground (theme-get :back-color))
    2.37 +       (.setFont (theme-get :font)))))
    2.38  
    2.39  (defn- ^Graphics2D create-graphics
    2.40    ([]
    2.41 @@ -445,7 +463,7 @@
    2.42  
    2.43  (defmacro with-color [color-or-key & body]
    2.44    `(let [color# ~color-or-key
    2.45 -         color# (get *theme* color# color#)
    2.46 +         color# (theme-get color# color#)
    2.47           g# *graphics*
    2.48           old-color# (.getColor g#)]
    2.49       (try
    2.50 @@ -454,6 +472,17 @@
    2.51         (finally
    2.52           (.setColor g# old-color#)))))
    2.53  
    2.54 +(defmacro with-font [font-or-key & body]
    2.55 +  `(let [font# ~font-or-key
    2.56 +         font# (theme-get font# font#)
    2.57 +         g# *graphics*
    2.58 +         old-font# (.getFont g#)]
    2.59 +     (try
    2.60 +       (.setFont g# font#)
    2.61 +       ~@body
    2.62 +       (finally
    2.63 +         (.setColor g# old-font#)))))
    2.64 +
    2.65  (defmacro with-stroke [stroke & body]
    2.66    `(let [g# *graphics*
    2.67           old-stroke# (.getStroke g#)]
     3.1 --- a/src/indyvon/demo.clj	Wed Nov 12 15:44:17 2014 +0300
     3.2 +++ b/src/indyvon/demo.clj	Mon Nov 17 10:42:09 2014 +0300
     3.3 @@ -35,11 +35,11 @@
     3.4            padding 4
     3.5            border-width 1
     3.6            offset (if (picked? id) (/ shadow-offset 2) 0)
     3.7 -          ^Color color (:alt-back-color *theme*)
     3.8 +          ^Color color (theme-get :alt-back-color)
     3.9            color (if (hovered? id) (.brighter color) color)
    3.10            width (- *width* shadow-offset)
    3.11            height (- *height* shadow-offset)]
    3.12 -      (with-color (:shadow-color *theme*)
    3.13 +      (with-color :shadow-color
    3.14          (.fillRect *graphics* shadow-offset shadow-offset width height))
    3.15        (with-color color
    3.16          (.fillRect *graphics* offset offset width height))
    3.17 @@ -99,11 +99,11 @@
    3.18            (let [hovered (hovered? button)
    3.19                  offset (if (picked? button) (/ shadow-offset 2) 0)
    3.20                  color (combine-colors
    3.21 -                       (:alt-back-color *theme*) Color/WHITE
    3.22 +                       (theme-get :alt-back-color) Color/WHITE
    3.23                         (animate highlight 0.0 1.0 @animation-speed))
    3.24                  width (- *width* shadow-offset)
    3.25                  height (- *height* shadow-offset)]
    3.26 -            (with-color (:shadow-color *theme*)
    3.27 +            (with-color :shadow-color
    3.28                (.fillRect *graphics*
    3.29                           shadow-offset shadow-offset
    3.30                           width height))
     4.1 --- a/src/indyvon/views.clj	Wed Nov 12 15:44:17 2014 +0300
     4.2 +++ b/src/indyvon/views.clj	Mon Nov 17 10:42:09 2014 +0300
     4.3 @@ -206,7 +206,7 @@
     4.4                    (.draw layout *graphics* x (+ y ascent))
     4.5                    (recur (next layouts) (+ y lh)))))))
     4.6          (geometry [view]
     4.7 -          (let [layouts (layout-text lines (:font *theme*) (font-context))
     4.8 +          (let [layouts (layout-text lines (theme-get :font) (font-context))
     4.9                  w (text-width layouts)
    4.10                  h (text-height layouts)]
    4.11              (->Size w h)))))))