Compiler now accepts precompiled instances.

This commit is contained in:
Daniel Ziltener 2015-01-19 14:04:57 +00:00
parent 22dffc8fda
commit df28a1c96b
3 changed files with 60 additions and 23 deletions

View file

@ -1,4 +1,4 @@
(ns clojurefx.clojurefx
1(ns clojurefx.clojurefx
(:refer-clojure :exclude [atom doseq let fn defn ref dotimes defprotocol loop for send meta with-meta])
(:require [clojure.core.typed :refer :all]
[clojure.core.typed.unsafe :refer [ignore-with-unchecked-cast]]

View file

@ -77,21 +77,24 @@
eval)
(apply dissoc props mandatory))))
(ann resolv-o-matic [(U String Keyword Symbol Class) -> Class])
(defn resolv-o-matic [thing]
(cond
(symbol? thing) (ns-resolve (the-ns 'clojurefx.clojurefx) thing)
(keyword? thing) (recur (name thing))
(string? thing) (recur (symbol thing))
:else thing))
;; (ann resolv-o-matic [(U String Keyword Symbol Class) -> Class])
;; (defn resolv-o-matic [thing]
;; (cond
;; (symbol? thing) (ns-resolve (the-ns 'clojurefx.clojurefx) thing)
;; (keyword? thing) (recur (name thing))
;; (string? thing) (recur (symbol thing))
;; :else thing))
(ann compile [(Vec Any) -> Any])
(defn compile [[obj params & other]]
(assert (map? params))
(let [obj (build-node (resolv-o-matic obj) params)]
(if (empty? other)
obj
(flatten (conj (list obj) (compile other))))))
(defn compile
([args] (compile args []))
([[obj & other] accu]
(cond
(nil? obj) accu
(and (empty? other) (empty? accu)) obj
(and (empty? (rest other)) (empty? accu)) (build-node obj (first other))
(class? obj) (recur (rest other) (conj accu (build-node obj (first other))))
:else (recur other (conj accu obj)))))
(ann compile-o-matic [Any -> Any])
(defn compile-o-matic [thing]
@ -101,3 +104,23 @@
thing)
thing))
(comment
(import (javafx.scene.layout VBox)
(javafx.scene.control Button ScrollPane Label))
(def example-graph
[VBox {:id "topBox"
:children [Button {:id "button"
:text "Close"}
ScrollPane {:content [Label {:id "label"
:text "This rocks."}]}]}])
(def example-graph2
[VBox {:id "topBox"
:children [Button {:id "button"
:text "Close"}
(new javafx.scene.control.Label "Precompiled")
Button {:id "button2"
:text "OK"}
ScrollPane {:content [Label {:id "label"
:text "This rocks."}]}]}]))

View file

@ -21,11 +21,25 @@
ScrollPane {:content [Label {:id "label"
:text "This rocks."}]}]}])
(def example-graph2
[VBox {:id "topBox"
:children [Button {:id "button"
:text "Close"}
(new javafx.scene.control.Label "Precompiled")
Button {:id "button2"
:text "OK"}
ScrollPane {:content [Label {:id "label"
:text "This rocks."}]}]}])
(def scene-graph (atom nil))
(facts "Vector compilation"
(fact "Simple element"
(type (compile [Label {:text "Hello ClojureFX"}])) => javafx.scene.control.Label)
(fact "Simple precompiled element"
(type (compile [(new Label "Hello Precompiled")])) => javafx.scene.control.Label)
(fact "Nested structure"
(type (reset! scene-graph (compile example-graph))) => javafx.scene.layout.VBox
(get-id @scene-graph) => "topBox"))
(get-id @scene-graph) => "topBox")
(fact "Partially precompiled nested structure"
(type (reset! scene-graph (compile example-graph2))) => javafx.scene.layout.VBox))