       (.setTitle frame "SimRunner (c) 2014 DEINC")
       (.show frame))))
 
-(defn- start-rendering! [app-state]
-  (when (-> @app-state :ui :dirty?)
-    (swap! app-state #(assoc-in % [:ui :rendering?] true))))
+(defn- start-rendering [app-state]
+  (assoc-in app-state [:ui :model :rendering?] true))
 
-(defn- stop-rendering! [app-state]
-  (when (-> @app-state :ui :rendering?)
-    (swap! app-state #(-> % 
-                          (assoc-in [:ui :rendering?] false)
-                          (assoc-in [:ui :dirty?] false)))))
+(defn- start-rendering! [{app-state :state}]
+  (when (-> @app-state :ui :model :dirty?)
+    (swap! app-state start-rendering)))
 
-(defn render [{:keys [state] :as app}]
+(defn- stop-rendering [app-state]
+  (-> app-state 
+      (assoc-in [:ui :model :rendering?] false)
+      (assoc-in [:ui :model :dirty?] false)))
+
+(defn- stop-rendering! [{app-state :state}]
+  (when (-> @app-state :ui :model :rendering?)
+    (swap! app-state stop-rendering)))
+
+(defn render [app]
   (gui/gui-do
-    (when (start-rendering! state)
+    (when (start-rendering! app)
       (try
-        ;; TODO: render the view model!
         (io! "Do not update the ui in a transaction!")
         (finally
-          (stop-rendering! state))))))
+          (stop-rendering! app))))))
 

  
    :author "Frank Mosebach"}
  (:require
    [fm.simrunner.gui (core :as gui) 
                      (view :as view) 
                      (wiring :as wiring)]))

(defmulti on-action {:private true} (fn [action & _] action))

(defmethod on-action :open-config [_ app & args]
  (println (format "open-config{args: %s}" args))
  (gui/choose-file (-> args first :widget) :title "Open Config"))  

(defmethod on-action :default [action app & args]
  (println (format "on-action{action: %s args: %s}" action args)))

(defmulti on-event {:private true} (fn [_ event-id & _] event-id))

(defmethod on-event :action-performed [app _ & args]
  (on-action (-> args first meta :action) app args))

(defmethod on-event :default [app event-id & args]
  (println (format "on-event{id: %s args: %s}" event-id args)))

(defn- app [config view]
  {:config config
   :worker (agent nil)
   :state  (atom {:ui    {:view  view
                          :model {:inputs     {}
                                  :dirty?     false
                                  :locked     0
                                  :rendering? false}}
                  :model {}})})

(defn- event-handler [app]
  (fn [& args]
    (apply on-event app args)))

(defn start [config]
  @(gui/gui-do
