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 wrap: on
line diff
--- a/src/indyvon/component.clj	Wed Nov 12 15:44:17 2014 +0300
+++ b/src/indyvon/component.clj	Mon Nov 17 10:42:09 2014 +0300
@@ -29,7 +29,7 @@
 (defn- paint-component [^Component c ^Graphics g scene]
   (let [w (.getWidth c)
         h (.getHeight c)]
-    (.setColor g (:back-color *theme*))
+    (.setColor g (theme-get :back-color))
     (.fillRect g 0 0 w h)
     (draw-scene! scene g w h)))
 
@@ -50,7 +50,7 @@
         panel
         {"paintComponent" #(paint-component %1 %2 scene)
          "getPreferredSize" #(preferred-size % scene)})
-       (.setBackground panel (:back-color *theme*))
+       (.setBackground panel (theme-get :back-color))
        (add-observer panel scene (fn [w _]
                                    ;; Use the first observer argument
                                    ;; instead of closing over panel to
--- a/src/indyvon/core.clj	Wed Nov 12 15:44:17 2014 +0300
+++ b/src/indyvon/core.clj	Mon Nov 17 10:42:09 2014 +0300
@@ -381,6 +381,24 @@
       (finally
        (.dispose clip-g)))))
 
+(defn- theme-get*
+  ([theme key]
+     (theme-get* theme key nil))
+  ([theme key not-found]
+     (if-let [e (find theme key)]
+       (loop [k (val e)]
+         (if-let [e1 (and (keyword? k)
+                          (find theme k))]
+           (recur (val e1))
+           k))
+       not-found)))
+
+(defn theme-get
+  ([key]
+     (theme-get* *theme* key))
+  ([key not-found]
+     (theme-get* *theme* key not-found)))
+
 (defn ^Graphics2D apply-theme
   "Set graphics' color and font to match theme.
   Modifies and returns the first argument."
@@ -388,9 +406,9 @@
      (apply-theme *graphics* *theme*))
   ([^Graphics2D graphics theme]
      (doto graphics
-       (.setColor (:fore-color theme))
-       (.setBackground (:back-color theme))
-       (.setFont (:font theme)))))
+       (.setColor (theme-get :fore-color))
+       (.setBackground (theme-get :back-color))
+       (.setFont (theme-get :font)))))
 
 (defn- ^Graphics2D create-graphics
   ([]
@@ -445,7 +463,7 @@
 
 (defmacro with-color [color-or-key & body]
   `(let [color# ~color-or-key
-         color# (get *theme* color# color#)
+         color# (theme-get color# color#)
          g# *graphics*
          old-color# (.getColor g#)]
      (try
@@ -454,6 +472,17 @@
        (finally
          (.setColor g# old-color#)))))
 
+(defmacro with-font [font-or-key & body]
+  `(let [font# ~font-or-key
+         font# (theme-get font# font#)
+         g# *graphics*
+         old-font# (.getFont g#)]
+     (try
+       (.setFont g# font#)
+       ~@body
+       (finally
+         (.setColor g# old-font#)))))
+
 (defmacro with-stroke [stroke & body]
   `(let [g# *graphics*
          old-stroke# (.getStroke g#)]
--- a/src/indyvon/demo.clj	Wed Nov 12 15:44:17 2014 +0300
+++ b/src/indyvon/demo.clj	Mon Nov 17 10:42:09 2014 +0300
@@ -35,11 +35,11 @@
           padding 4
           border-width 1
           offset (if (picked? id) (/ shadow-offset 2) 0)
-          ^Color color (:alt-back-color *theme*)
+          ^Color color (theme-get :alt-back-color)
           color (if (hovered? id) (.brighter color) color)
           width (- *width* shadow-offset)
           height (- *height* shadow-offset)]
-      (with-color (:shadow-color *theme*)
+      (with-color :shadow-color
         (.fillRect *graphics* shadow-offset shadow-offset width height))
       (with-color color
         (.fillRect *graphics* offset offset width height))
@@ -99,11 +99,11 @@
           (let [hovered (hovered? button)
                 offset (if (picked? button) (/ shadow-offset 2) 0)
                 color (combine-colors
-                       (:alt-back-color *theme*) Color/WHITE
+                       (theme-get :alt-back-color) Color/WHITE
                        (animate highlight 0.0 1.0 @animation-speed))
                 width (- *width* shadow-offset)
                 height (- *height* shadow-offset)]
-            (with-color (:shadow-color *theme*)
+            (with-color :shadow-color
               (.fillRect *graphics*
                          shadow-offset shadow-offset
                          width height))
--- a/src/indyvon/views.clj	Wed Nov 12 15:44:17 2014 +0300
+++ b/src/indyvon/views.clj	Mon Nov 17 10:42:09 2014 +0300
@@ -206,7 +206,7 @@
                   (.draw layout *graphics* x (+ y ascent))
                   (recur (next layouts) (+ y lh)))))))
         (geometry [view]
-          (let [layouts (layout-text lines (:font *theme*) (font-context))
+          (let [layouts (layout-text lines (theme-get :font) (font-context))
                 w (text-width layouts)
                 h (text-height layouts)]
             (->Size w h)))))))