Permalink
Browse files

Started working stylesheets. It's almost too boring to include, but more

advanced capabilities are problematic.
  • Loading branch information...
1 parent 75532ec commit 304ac5dd3c0c54fcb59b76633c20627745b666ba @daveray committed Oct 24, 2011
Showing with 79 additions and 0 deletions.
  1. +41 −0 src/seesaw/style.clj
  2. +38 −0 test/seesaw/test/style.clj
View
@@ -0,0 +1,41 @@
+; Copyright (c) Dave Ray, 2011. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns ^{:doc "Functions for styling apps. Prefer (seesaw.core/stylesheet) and friends."
+ :author "Dave Ray"}
+ seesaw.style
+ (:use [seesaw.config :only [config!]]
+ [seesaw.selector]))
+
+(defn apply-stylesheet
+ "ALPHA - EXPERIMENTAL AND GUARANTEED TO CHANGE
+
+ Apply a stylesheet to a widget hierarchy. A stylesheet is simple a map where
+ the keys are selectors and the values are maps from widget properties to
+ values. For example,
+
+ (apply-stylesheet frame {
+ [:#foo] { :text \"hi\" }
+ [:.important] { :background :red } })
+
+ Applying a stylesheet is a one-time operation. It does not set up any
+ kind of monitoring. Thus, if you make a change to a widget that would
+ affect the rules that apply to it (say, by changing its :class) you'll
+ need to reapply the stylesheet.
+
+ See:
+ (seesaw.core/config!)
+ (seesaw.core/select)
+ "
+ [root stylesheet]
+ (doseq [[sel style] stylesheet]
+ (apply config! (select root sel) (reduce concat style)))
+ root)
+
View
@@ -0,0 +1,38 @@
+; Copyright (c) Dave Ray, 2011. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns seesaw.test.style
+ (:use seesaw.style
+ [seesaw.core :only [border-panel label button config text]]
+ [seesaw.color :only [to-color]])
+ (:use [lazytest.describe :only (describe it testing)]
+ [lazytest.expect :only (expect)]))
+
+(describe apply-stylesheet
+
+ (it "returns its input"
+ (let [lbl (label)]
+ (expect (= lbl (apply-stylesheet lbl {})))))
+
+ (it "changes styles of widget for rules that match"
+ (let [lbl (label :id :lbl)
+ btn-a (button :class :btn)
+ btn-b (button :class :btn :id :btn-b)
+ p (border-panel :center lbl :north btn-a :south btn-b)]
+ (apply-stylesheet p
+ {[:#lbl] { :background :aliceblue
+ :text "hi"}
+ [:.btn] { :foreground :red }
+ [:#btn-b] {:text "B"}})
+ (expect (= (to-color :aliceblue) (config lbl :background)))
+ (expect (= "hi" (text lbl)))
+ (expect (= "B" (text btn-b)))
+ (expect (= (to-color :red) (config btn-a :foreground)))
+ (expect (= (to-color :red) (config btn-b :foreground))))))

0 comments on commit 304ac5d

Please sign in to comment.