 
 (defn- sort-lines [lines] (map (fn [l] (sort component-comparator l)) lines))
 
-(defn- group-by-lines [accepting-children]
+(fg/defaccessorfn group-by-lines [component accepting-children]
   (let [cnt (count accepting-children)]
     (loop [line-y1 []
            line-y2 []

                           (count lines))
              existing-line (< line-index (count lines))
              line (if existing-line (nth lines line-index) #{})
              cy2 (+ cy (m/y (:clip-size c)))]
          (recur
            (if existing-line line-y1 (assoc line-y1 line-index cy))
            (assoc line-y2 line-index (if existing-line (max cy2 (nth line-y2 line-index)) cy2))

;;; Sorts focusable components so that they are traversed in left->right, up->down direction.
;;; To make such ad order, arranges components in a vector of horizontal "lines"
(fg/defevolverfn :focus-traversal-order
  (let [accepting-children (get-accepting-children component)]
    (if (pos? (count accepting-children))
      (let [lines (group-by-lines component accepting-children)
            result (mapcat (fn [i] (map :id (nth lines i))) (range 0 (count lines)))]
        result)
      [])))

(fg/defaccessorfn get-child-ids-in-traversal-order [component]
  (if-let [focus-traversal-order (get-property component [:this] :focus-traversal-order)]
    focus-traversal-order
    (let [accepting-children (get-accepting-children component)]
      (mapv :id accepting-children))))

(fg/defaccessorfn get-in-cycle [component dir c-id]
  (let [child-ids (get-child-ids-in-traversal-order component)
        child-count (count child-ids)
        closed (:closed-focus-root component)
        cycle-keeper (fn [i]
                       (cond
                         (>= i child-count) (if closed 0)
                         (< i 0) (if closed (dec child-count))
                         :else i))
        index-of-c (if c-id (.indexOf child-ids c-id))]
    (cond
      (= child-count 0) nil
      (and (= child-count 1) closed) (nth child-ids 0)
      :else (case dir
              :next (if-let [new-index (cycle-keeper (inc index-of-c))] (nth child-ids new-index))
              :prev (if-let [new-index (cycle-keeper (dec index-of-c))] (nth child-ids new-index))
              :first (nth child-ids 0)
              :last (nth child-ids (dec child-count))))))

(def clean-state {:mode :none
                  :focused-child nil})
