1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.constraintlayout.core.state.helpers;
18 
19 import static androidx.constraintlayout.core.widgets.ConstraintWidget.UNKNOWN;
20 
21 import androidx.constraintlayout.core.state.ConstraintReference;
22 import androidx.constraintlayout.core.state.State;
23 import androidx.constraintlayout.core.widgets.ConstraintWidget;
24 
25 public class HorizontalChainReference extends ChainReference {
26 
HorizontalChainReference(State state)27     public HorizontalChainReference(State state) {
28         super(state, State.Helper.HORIZONTAL_CHAIN);
29     }
30 
31     // @TODO: add description
32     @Override
apply()33     public void apply() {
34         ConstraintReference first = null;
35         ConstraintReference previous = null;
36         for (Object key : mReferences) {
37             ConstraintReference reference = mHelperState.constraints(key);
38             reference.clearHorizontal();
39         }
40 
41         for (Object key : mReferences) {
42             ConstraintReference reference = mHelperState.constraints(key);
43 
44             if (first == null) {
45                 first = reference;
46                 if (mStartToStart != null) {
47                     first.startToStart(mStartToStart)
48                             .margin(mMarginStart)
49                             .marginGone(mMarginStartGone);
50                 } else if (mStartToEnd != null) {
51                     first.startToEnd(mStartToEnd).margin(mMarginStart).marginGone(mMarginStartGone);
52                 } else if (mLeftToLeft != null) {
53                     // TODO: Hack until we support RTL properly
54                     first.startToStart(mLeftToLeft).margin(mMarginLeft).marginGone(mMarginLeftGone);
55                 } else if (mLeftToRight != null) {
56                     // TODO: Hack until we support RTL properly
57                     first.startToEnd(mLeftToRight).margin(mMarginLeft).marginGone(mMarginLeftGone);
58                 } else {
59                     // No constraint declared, default to Parent.
60                     String refKey = reference.getKey().toString();
61                     first.startToStart(State.PARENT).margin(getPreMargin(refKey)).marginGone(
62                             getPreGoneMargin(refKey));
63                 }
64             }
65             if (previous != null) {
66                 String preKey = previous.getKey().toString();
67                 String refKey = reference.getKey().toString();
68                 previous.endToStart(reference.getKey()).margin(getPostMargin(preKey)).marginGone(
69                         getPostGoneMargin(preKey));
70                 reference.startToEnd(previous.getKey()).margin(getPreMargin(refKey)).marginGone(
71                         getPreGoneMargin(refKey));
72             }
73             float weight = getWeight(key.toString());
74             if (weight != UNKNOWN) {
75                 reference.setHorizontalChainWeight(weight);
76             }
77             previous = reference;
78         }
79 
80         if (previous != null) {
81             if (mEndToStart != null) {
82                 previous.endToStart(mEndToStart).margin(mMarginEnd).marginGone(mMarginEndGone);
83             } else if (mEndToEnd != null) {
84                 previous.endToEnd(mEndToEnd).margin(mMarginEnd).marginGone(mMarginEndGone);
85             } else if (mRightToLeft != null) {
86                 // TODO: Hack until we support RTL properly
87                 previous.endToStart(mRightToLeft).margin(mMarginRight).marginGone(mMarginRightGone);
88             } else if (mRightToRight != null) {
89                 // TODO: Hack until we support RTL properly
90                 previous.endToEnd(mRightToRight).margin(mMarginRight).marginGone(mMarginRightGone);
91             } else {
92                 // No constraint declared, default to Parent.
93                 String preKey = previous.getKey().toString();
94                 previous.endToEnd(State.PARENT).margin(getPostMargin(preKey)).marginGone(
95                         getPostGoneMargin(preKey));
96             }
97         }
98 
99         if (first == null) {
100             return;
101         }
102 
103         if (mBias != 0.5f) {
104             first.horizontalBias(mBias);
105         }
106 
107         switch (mStyle) {
108             case SPREAD: {
109                 first.setHorizontalChainStyle(ConstraintWidget.CHAIN_SPREAD);
110             }
111             break;
112             case SPREAD_INSIDE: {
113                 first.setHorizontalChainStyle(ConstraintWidget.CHAIN_SPREAD_INSIDE);
114             }
115             break;
116             case PACKED: {
117                 first.setHorizontalChainStyle(ConstraintWidget.CHAIN_PACKED);
118             }
119         }
120     }
121 
122 }
123