diff --git a/common/src/app/common/geom/shapes/constraints.cljc b/common/src/app/common/geom/shapes/constraints.cljc index b3c8b05971..173e6378a0 100644 --- a/common/src/app/common/geom/shapes/constraints.cljc +++ b/common/src/app/common/geom/shapes/constraints.cljc @@ -148,6 +148,9 @@ ((if (= :x axis) center-horizontal-vector center-vertical-vector) child-points parent-points)) (defn displacement + "The move that gives a child the vector `before-v` to its parent's side + again, where it now has `after-v`. A child on the side has no direction + to it, so the parent's side gives the one `before-v` had." [before-v after-v before-parent-side-v after-parent-side-v] (let [before-angl (gpt/angle-with-other before-v before-parent-side-v) @@ -155,8 +158,17 @@ sign (if (mth/close? before-angl after-angl) 1 -1) length (* sign (gpt/length before-v))] - (if (mth/almost-zero? length) + (cond + (mth/almost-zero? length) after-v + + (mth/almost-zero? (gpt/length after-v)) + (let [;; the parent's side, pointing as `before-v` did along it + direction (cond-> (gpt/unit after-parent-side-v) + (not (mth/close? before-angl 0)) (gpt/negate))] + (gpt/subtract after-v (gpt/scale direction (gpt/length before-v)))) + + :else (gpt/subtract after-v (gpt/scale (gpt/unit after-v) length))))) (defn side-vector diff --git a/common/test/common_tests/geom_shapes_constraints_test.cljc b/common/test/common_tests/geom_shapes_constraints_test.cljc index 0dd38b5f33..28efacfa2a 100644 --- a/common/test/common_tests/geom_shapes_constraints_test.cljc +++ b/common/test/common_tests/geom_shapes_constraints_test.cljc @@ -6,6 +6,7 @@ (ns common-tests.geom-shapes-constraints-test (:require + [app.common.geom.point :as gpt] [app.common.geom.shapes.constraints :as gsc] [app.common.types.modifiers :as ctm] [clojure.test :as t])) @@ -23,3 +24,30 @@ (t/testing ":default method can be added to modifiers" (let [result (gsc/constraint-modifier :scale :y nil nil nil nil)] (t/is (ctm/empty? (ctm/add-modifiers (ctm/empty) result)))))) + +;; ---- constraint-modifier :start ---- + +(defn- rect-points + [x y w h] + [(gpt/point x y) (gpt/point (+ x w) y) (gpt/point (+ x w) (+ y h)) (gpt/point x (+ y h))]) + +(defn- move-vector + [modifiers] + (reduce gpt/add (gpt/point 0 0) + (keep #(when (= :move (:type %)) (:vector %)) (:geometry-child modifiers)))) + +(t/deftest start-constraint-moves-a-child-onto-the-start-of-its-squeezed-parent + (t/testing "a child at the end of its parent keeps its start offset when the + parent is squeezed to zero and moved onto it" + (let [child (rect-points 100 0 100 100) + before (rect-points 0 0 100 100) + after (rect-points 100 0 0.01 100) + result (gsc/constraint-modifier :start :x child before child after)] + (t/is (gpt/close? (gpt/point 100 0) (move-vector result))))) + + (t/testing "a child the moved parent passes over keeps its start offset" + (let [child (rect-points 50 0 100 100) + before (rect-points 0 0 100 100) + after (rect-points 100 0 0.01 100) + result (gsc/constraint-modifier :start :x child before child after)] + (t/is (gpt/close? (gpt/point 100 0) (move-vector result)))))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index d5f32d48ba..158383a160 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2209,6 +2209,16 @@ [:combine 18] [:make-variant] [:switch 18] [:combine] [:rotate 8] [:update-main]]})))) +(t/deftest nesting-a-copy-that-squeezes-a-fill-frame-moves-the-child-at-its-end + (t/is (map? (cg/run {:world [3 2 {:variants true :text-styles false :layout :flex :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:add-variant] [:delete 10] [:restore] [:delete 10] [:restore] [:combine 12] + [:combine 9] [:combine] [:style 0 29 1] [:add-variant] [:add-variant] + [:make-variant] [:combine] [:add-variant] [:add-variant] [:add-variant] + [:add-variant] [:combine] [:add-variant] [:combine] [:update-main] + [:nest 13]]})))) + (t/deftest pushing-a-grid-direction-orders-the-copies-like-the-reflowed-main (t/is (map? (cg/run {:world [3 2 {:variants true :text-styles false :layout :grid :variant-diff false :layout-item-margins false :grid-square true :rich-variants false