 
      :else nil)))
 
+(defn handle-text-layer-finished [state bbox]
+  (-> state
+    (update-in [:drawing :layers 0 :bbox] #(or bbox %))
+    (finalize-layer)))
+
 (defmethod control-event :text-layer-finished
   [browser-state message {:keys [bbox]} state]
-  (-> state
-    (update-in [:drawing :layers 0 :bbox] #(or bbox %))
-    (finalize-layer)))
-
-(defmethod post-control-event! :text-layer-finished
-  [browser-state message _ previous-state current-state]
-  (let [cast! #(put! (get-in current-state [:comms :controls]) %)
-        db           (:db current-state)
-        layer        (utils/remove-map-nils (get-in current-state [:drawing :finished-layers 0]))]
+  (handle-text-layer-finished state bbox))
+
+(defn handle-text-layer-finished-after [current-state]
+  (let [db (:db current-state)
+        layer (utils/remove-map-nils (get-in current-state [:drawing :finished-layers 0]))]
     (when (layer-model/detectable? layer)
       (d/transact! db [layer] {:can-undo? true}))
     (maybe-notify-subscribers! current-state nil nil)))
+
+(defmethod post-control-event! :text-layer-finished
+  [browser-state message _ previous-state current-state]
+  (handle-text-layer-finished-after current-state))
 
 (defmethod control-event :deleted-selected
   [browser-state message _ state]

  (let [points (map js/parseInt (str/split (subs path 1) #" "))]
    (map (fn [[rx ry]] {:rx rx :ry ry}) (partition 2 points))))

(defmethod control-event :drawing-started
  [browser-state message [x y] state]
  (let [[rx ry] (cameras/screen->point (:camera state) x y)
        [snap-x snap-y] (cameras/snap-to-grid (:camera state) rx ry)
        {:keys [entity-id state]} (frontend.db/get-entity-id state)

      (assoc-in [:mouse-down] false)
      (assoc-in [:editing-eids :editing-eids] #{}))))

(defn mouse-depressed-intents [state button ctrl?]
  (let [tool (get-in state state/current-tool-path)
        drawing-text? (and (keyword-identical? :text tool)
                           (get-in state [:drawing :in-progress?]))]
    (concat
     ;; If you click while writing text, you probably wanted to place it there
     ;; You also want the right-click menu to open
     (when drawing-text? [:finish-text-layer])
     (cond
       (= button 2) [:open-menu]
       (and (= button 0) ctrl?) [:open-menu]
       (get-in state [:layer-properties-menu :opened?]) [:submit-layer-properties]
       (contains? #{:pen :rect :circle :line :select} tool) [:start-drawing]
       (and (keyword-identical? tool :text) (not drawing-text?)) [:start-drawing]
       :else nil))))

(declare handle-menu-opened)
(declare handle-menu-opened-after)
(declare handle-layer-properties-submitted)
(declare handle-layer-properties-submitted-after)
(declare handle-text-layer-finished)
(declare handle-text-layer-finished-after)

(defmethod control-event :mouse-depressed
  [browser-state message [x y {:keys [button type ctrl?]}] state]
  (let [intents (mouse-depressed-intents state button ctrl?)
        new-state (-> state
                    (update-mouse x y)
                    (assoc-in [:mouse-down] true)
                    (assoc-in [:mouse-type] (if (= type "mousedown") :mouse :touch)))]
    (reduce (fn [s intent]
              (case intent
                :finish-text-layer (handle-text-layer-finished s)
                :open-menu (handle-menu-opened s)
                :start-drawing (handle-drawing-started s x y)
                :submit-layer-properties (handle-layer-properties-submitted s)
                s))
            new-state intents)))

(defmethod post-control-event! :mouse-depressed
  [browser-state message [x y {:keys [button ctrl?]}] previous-state current-state]
  ;; use previous state so that we're consistent with the control-event
  (let [intents (mouse-depressed-intents previous-state button ctrl?)]
    (doseq [intent intents]
      (case intent
        :finish-text-layer (handle-text-layer-finished-after current-state)
        :open-menu (handle-menu-opened-after current-state previous-state)
        :start-drawing nil
        :submit-layer-properties (handle-layer-properties-submitted-after current-state)
        nil))))

(defn detectable-movement?
  "Checks to make sure we moved the layer from its starting position"
