From df28a1c96be8b360333da3763b744b2a5bbcd53e Mon Sep 17 00:00:00 2001 From: "dziltener@lyrion.ch" Date: Mon, 19 Jan 2015 14:04:57 +0000 Subject: [PATCH] Compiler now accepts precompiled instances. --- src/clojurefx/clojurefx.clj | 18 ++++++------ src/clojurefx/factory.clj | 49 ++++++++++++++++++++++++--------- test/clojurefx/factory_test.clj | 16 ++++++++++- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/clojurefx/clojurefx.clj b/src/clojurefx/clojurefx.clj index fbac5b7..a2e6e94 100644 --- a/src/clojurefx/clojurefx.clj +++ b/src/clojurefx/clojurefx.clj @@ -1,12 +1,12 @@ -(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]] - [taoensso.timbre :as timbre] - [clojure.java.io :as io] - [clojure.zip :as zip] - [clojurefx.protocols :as p] - [clojure.java.io :refer :all])) +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]] + [taoensso.timbre :as timbre] + [clojure.java.io :as io] + [clojure.zip :as zip] + [clojurefx.protocols :as p] + [clojure.java.io :refer :all])) (defonce force-toolkit-init (javafx.embed.swing.JFXPanel.)) diff --git a/src/clojurefx/factory.clj b/src/clojurefx/factory.clj index 01d0775..ecc7555 100644 --- a/src/clojurefx/factory.clj +++ b/src/clojurefx/factory.clj @@ -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."}]}]}])) diff --git a/test/clojurefx/factory_test.clj b/test/clojurefx/factory_test.clj index 94c0b28..ce9b0bf 100644 --- a/test/clojurefx/factory_test.clj +++ b/test/clojurefx/factory_test.clj @@ -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))