summaryrefslogtreecommitdiff
path: root/solver/src/main/java/android/support/constraint/solver/widgets/Chain.java
blob: 777ae6428a98be6a8d45daa9e3b41353a2bbf5a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.constraint.solver.widgets;

import android.support.constraint.solver.ArrayRow;
import android.support.constraint.solver.LinearSystem;
import android.support.constraint.solver.SolverVariable;

import static android.support.constraint.solver.widgets.ConstraintWidget.*;

/**
 * Chain management and constraints creation
 */
class Chain {

    private static final boolean DEBUG = false;

    /**
     * Apply specific rules for dealing with chains of widgets.
     * Chains are defined as a list of widget linked together with bi-directional connections
     *
     * @param constraintWidgetContainer root container
     * @param system the linear system we add the equations to
     * @param orientation HORIZONTAL or VERTICAL
     */
    static void applyChainConstraints(ConstraintWidgetContainer constraintWidgetContainer, LinearSystem system, int orientation) {

        // what to do:
        // Don't skip things. Either the element is GONE or not.
        int offset = 0;
        int chainsSize = 0;
        ConstraintWidget[] chainsArray = null;
        if (orientation == ConstraintWidget.HORIZONTAL) {
            offset = 0;
            chainsSize = constraintWidgetContainer.mHorizontalChainsSize;
            chainsArray = constraintWidgetContainer.mHorizontalChainsArray;
        } else {
            offset = 2;
            chainsSize = constraintWidgetContainer.mVerticalChainsSize;
            chainsArray = constraintWidgetContainer.mVerticalChainsArray;
        }
        for (int i = 0; i < chainsSize; i++) {
            ConstraintWidget first = chainsArray[i];
            if (constraintWidgetContainer.optimizeFor(Optimizer.OPTIMIZATION_CHAIN)) {
                if (!Optimizer.applyChainOptimized(constraintWidgetContainer, system, orientation, offset, first)) {
                    applyChainConstraints(constraintWidgetContainer, system, orientation, offset, first);
                }
            } else {
                applyChainConstraints(constraintWidgetContainer, system, orientation, offset, first);
            }
        }
    }

    /**
     * Apply specific rules for dealing with chains of widgets.
     * Chains are defined as a list of widget linked together with bi-directional connections
     *
     * @param container the root container
     * @param system the linear system we add the equations to
     * @param orientation HORIZONTAL or VERTICAL
     * @param offset 0 or 2 to accomodate for HORIZONTAL / VERTICAL
     * @param first first widget of the chain
     */
    static void applyChainConstraints(ConstraintWidgetContainer container, LinearSystem system,
                                      int orientation, int offset, ConstraintWidget first) {
        ConstraintWidget widget = first;
        ConstraintWidget next = null;
        ConstraintWidget firstVisibleWidget = null;
        ConstraintWidget lastVisibleWidget = null;
        boolean done = false;
        int numMatchConstraints = 0;
        float totalWeights = 0;
        ConstraintWidget firstMatchConstraintsWidget = null;
        ConstraintWidget previousMatchConstraintsWidget = null;

        boolean isWrapContent = container.mListDimensionBehaviors[orientation] == ConstraintWidget.DimensionBehaviour.WRAP_CONTENT;
        boolean isChainSpread = false;
        boolean isChainSpreadInside = false;
        boolean isChainPacked = false;

        ConstraintWidget head = first;
        if (orientation == ConstraintWidget.HORIZONTAL && container.isRtl()) {
            // find the last widget
            while (!done) {
                // go to the next widget
                ConstraintAnchor nextAnchor = widget.mListAnchors[offset + 1].mTarget;
                if (nextAnchor != null) {
                    next = nextAnchor.mOwner;
                    if (next.mListAnchors[offset].mTarget == null
                            || next.mListAnchors[offset].mTarget.mOwner != widget) {
                        next = null;
                    }
                } else {
                    next = null;
                }
                if (next != null) {
                    widget = next;
                } else {
                    done = true;
                }
            }
            head = widget;
            widget = first;
            next = null;
            done = false;
        }

        if (orientation == ConstraintWidget.HORIZONTAL) {
            isChainSpread = head.mHorizontalChainStyle == ConstraintWidget.CHAIN_SPREAD;
            isChainSpreadInside = head.mHorizontalChainStyle == ConstraintWidget.CHAIN_SPREAD_INSIDE;
            isChainPacked = head.mHorizontalChainStyle == ConstraintWidget.CHAIN_PACKED;
        } else {
            isChainSpread = head.mVerticalChainStyle == ConstraintWidget.CHAIN_SPREAD;
            isChainSpreadInside = head.mVerticalChainStyle == ConstraintWidget.CHAIN_SPREAD_INSIDE;
            isChainPacked = head.mVerticalChainStyle == ConstraintWidget.CHAIN_PACKED;
        }

        // The first traversal will:
        // - set up some basic ordering constraints
        // - build a linked list of visible widgets
        // - build a linked list of matched constraints widgets

        while (!done) {
            // apply ordering on the current widget

            // First, let's maintain a linked list of visible widgets for the chain
            widget.mListNextVisibleWidget[orientation] = null;
            if (widget.getVisibility() != ConstraintWidget.GONE) {
                if (lastVisibleWidget != null) {
                    lastVisibleWidget.mListNextVisibleWidget[orientation] = widget;
                }
                if (firstVisibleWidget == null) {
                    firstVisibleWidget = widget;
                }
                lastVisibleWidget = widget;
            }

            ConstraintAnchor begin = widget.mListAnchors[offset];
            int strength = SolverVariable.STRENGTH_LOW;
            int margin = begin.getMargin();

            if (begin.mTarget != null && widget != first) {
                margin += begin.mTarget.getMargin();
            }

            if (isChainPacked && widget != first && widget != firstVisibleWidget) {
                strength = SolverVariable.STRENGTH_FIXED;
            }

            system.addGreaterThan(begin.mSolverVariable, begin.mTarget.mSolverVariable,
                    margin, SolverVariable.STRENGTH_FIXED);
            system.addEquality(begin.mSolverVariable, begin.mTarget.mSolverVariable, margin, strength);

            // First, let's maintain a linked list of matched widgets for the chain
            widget.mListNextMatchConstraintsWidget[orientation] = null;
            if (widget.getVisibility() != ConstraintWidget.GONE
                    && widget.mListDimensionBehaviors[orientation] == ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT) {
                numMatchConstraints++;
                totalWeights += widget.mWeight[orientation];
                if (firstMatchConstraintsWidget == null) {
                    firstMatchConstraintsWidget = widget;
                } else {
                    previousMatchConstraintsWidget.mListNextMatchConstraintsWidget[orientation] = widget;
                }
                previousMatchConstraintsWidget = widget;
                if (isWrapContent) {
                    system.addGreaterThan(widget.mListAnchors[offset + 1].mSolverVariable,
                            widget.mListAnchors[offset].mSolverVariable, 0, SolverVariable.STRENGTH_FIXED);
                }
            }

            if (isWrapContent) {
                system.addGreaterThan(widget.mListAnchors[offset].mSolverVariable,
                        container.mListAnchors[offset].mSolverVariable,
                        0, SolverVariable.STRENGTH_FIXED);
            }

            // go to the next widget
            ConstraintAnchor nextAnchor = widget.mListAnchors[offset + 1].mTarget;
            if (nextAnchor != null) {
                next = nextAnchor.mOwner;
                if (next.mListAnchors[offset].mTarget == null || next.mListAnchors[offset].mTarget.mOwner != widget) {
                    next = null;
                }
            } else {
                next = null;
            }
            if (next != null) {
                widget = next;
            } else {
                done = true;
            }
        }
        ConstraintWidget last = widget;

        // Make sure we have constraints for the last anchors / targets
        if (lastVisibleWidget != null && last.mListAnchors[offset + 1].mTarget != null) {
            ConstraintAnchor end = lastVisibleWidget.mListAnchors[offset + 1];
            system.addLowerThan(end.mSolverVariable,
                    last.mListAnchors[offset + 1].mTarget.mSolverVariable, -end.getMargin(),
                    SolverVariable.STRENGTH_FIXED);
        }

        // ... and make sure the root end is constrained in wrap content.
        if (isWrapContent) {
            system.addGreaterThan(container.mListAnchors[offset + 1].mSolverVariable,
                    last.mListAnchors[offset + 1].mSolverVariable,
                    last.mListAnchors[offset + 1].getMargin(), SolverVariable.STRENGTH_FIXED);
        }

        // Now, let's apply the centering / spreading for matched constraints widgets
        if (numMatchConstraints > 0) {
            widget = firstMatchConstraintsWidget;
            while (widget != null) {
                next = widget.mListNextMatchConstraintsWidget[orientation];
                if (next != null) {
                    float currentWeight = widget.mWeight[orientation];
                    float nextWeight = next.mWeight[orientation];
                    SolverVariable begin = widget.mListAnchors[offset].mSolverVariable;
                    SolverVariable end = widget.mListAnchors[offset + 1].mSolverVariable;
                    SolverVariable nextBegin = next.mListAnchors[offset].mSolverVariable;
                    SolverVariable nextEnd = next.mListAnchors[offset + 1].mSolverVariable;

                    boolean applyEquality;
                    int currentDimensionDefault;
                    int nextDimensionDefault;
                    if (orientation == ConstraintWidget.HORIZONTAL) {
                        currentDimensionDefault = widget.mMatchConstraintDefaultWidth;
                        nextDimensionDefault = next.mMatchConstraintDefaultWidth;
                    } else {
                        currentDimensionDefault = widget.mMatchConstraintDefaultHeight;
                        nextDimensionDefault = next.mMatchConstraintDefaultHeight;
                    }
                    applyEquality = ((currentDimensionDefault == MATCH_CONSTRAINT_SPREAD)
                            || (currentDimensionDefault == MATCH_CONSTRAINT_RATIO)) &&
                            ((nextDimensionDefault == MATCH_CONSTRAINT_SPREAD)
                                    || (nextDimensionDefault == MATCH_CONSTRAINT_RATIO));

                    if (applyEquality) {
                        ArrayRow row = system.createRow();
                        row.createRowEqualMatchDimensions(currentWeight, totalWeights, nextWeight,
                                begin, end, nextBegin, nextEnd);
                        system.addConstraint(row);
                    }

                }
                widget = next;
            }
        }

        if (DEBUG) {
            widget = firstVisibleWidget;
            while (widget != null) {
                next = widget.mListNextVisibleWidget[orientation];
                widget.mListAnchors[offset].mSolverVariable.setName("" + widget.getDebugName() + ".left");
                widget.mListAnchors[offset + 1].mSolverVariable.setName("" + widget.getDebugName() + ".right");
                widget = next;
            }
        }

        // Finally, let's apply the specific rules dealing with the different chain types

        if (firstVisibleWidget != null && (firstVisibleWidget == lastVisibleWidget || isChainPacked)) {
            ConstraintAnchor begin = first.mListAnchors[offset];
            ConstraintAnchor end = last.mListAnchors[offset + 1];
            SolverVariable beginTarget = first.mListAnchors[offset].mTarget != null ? first.mListAnchors[offset].mTarget.mSolverVariable : null;
            SolverVariable endTarget = last.mListAnchors[offset + 1].mTarget != null ? last.mListAnchors[offset + 1].mTarget.mSolverVariable : null;
            if (firstVisibleWidget == lastVisibleWidget) {
                begin = firstVisibleWidget.mListAnchors[offset];
                end = firstVisibleWidget.mListAnchors[offset + 1];
            }
            if (beginTarget != null && endTarget != null) {
                float bias = 0.5f;
                if (orientation == ConstraintWidget.HORIZONTAL) {
                    bias = head.mHorizontalBiasPercent;
                } else {
                    bias = head.mVerticalBiasPercent;
                }
                int beginMargin = begin.getMargin();
                if (lastVisibleWidget == null) {
                    // everything is hidden
                    lastVisibleWidget = last;
                }
                int endMargin = lastVisibleWidget.mListAnchors[offset + 1].getMargin();
                system.addCentering(begin.mSolverVariable, beginTarget, beginMargin, bias,
                        endTarget, end.mSolverVariable, endMargin, SolverVariable.STRENGTH_EQUALITY);
            }
        } else if (isChainSpread && firstVisibleWidget != null) {
            // for chain spread, we need to add equal dimensions in between *visible* widgets
            widget = firstVisibleWidget;
            ConstraintWidget previousVisibleWidget = firstVisibleWidget;
            while (widget != null) {
                next = widget.mListNextVisibleWidget[orientation];
                if (next != null || widget == lastVisibleWidget) {
                    ConstraintAnchor beginAnchor = widget.mListAnchors[offset];
                    SolverVariable begin = beginAnchor.mSolverVariable;
                    SolverVariable beginTarget = beginAnchor.mTarget != null ? beginAnchor.mTarget.mSolverVariable : null;
                    if (previousVisibleWidget != widget) {
                        beginTarget = previousVisibleWidget.mListAnchors[offset + 1].mSolverVariable;
                    }

                    ConstraintAnchor beginNextAnchor = null;
                    SolverVariable beginNext = null;
                    SolverVariable beginNextTarget = null;
                    int beginMargin = beginAnchor.getMargin();
                    int nextMargin = 0;

                    if (next != null) {
                        beginNextAnchor = next.mListAnchors[offset];
                        beginNext = beginNextAnchor.mSolverVariable;
                        beginNextTarget = beginNextAnchor.mTarget != null ? beginNextAnchor.mTarget.mSolverVariable : null;
                        nextMargin = beginNextAnchor.getMargin();
                    } else {
                        beginNextAnchor = last.mListAnchors[offset + 1].mTarget;
                        if (beginNextAnchor != null) {
                            beginNext = beginNextAnchor.mSolverVariable;
                        }
                        beginNextTarget = widget.mListAnchors[offset + 1].mSolverVariable;
                    }

                    if (begin != null && beginTarget != null && beginNext != null && beginNextTarget != null) {
                        int margin1 = 0;
                        if (widget == firstVisibleWidget) {
                            margin1 = firstVisibleWidget.mListAnchors[offset].getMargin();
                        }
                        int margin2 = 0;
                        if (widget == lastVisibleWidget) {
                            margin2 = lastVisibleWidget.mListAnchors[offset + 1].getMargin();
                        }
                        system.addCentering(begin, beginTarget, margin1, 0.5f,
                                beginNext, beginNextTarget, margin2,
                                SolverVariable.STRENGTH_HIGHEST);
                    }
                }
                previousVisibleWidget = widget;
                widget = next;
            }
        } else if (isChainSpreadInside && firstVisibleWidget != null) {
            // for chain spread inside, we need to add equal dimensions in between *visible* widgets
            widget = firstVisibleWidget;
            ConstraintWidget previousVisibleWidget = firstVisibleWidget;
            while (widget != null) {
                next = widget.mListNextVisibleWidget[orientation];
                if (widget != firstVisibleWidget && next != null) {
                    if (next == lastVisibleWidget) {
                        next = null;
                    }
                    ConstraintAnchor beginAnchor = widget.mListAnchors[offset];
                    SolverVariable begin = beginAnchor.mSolverVariable;
                    SolverVariable beginTarget = beginAnchor.mTarget != null ? beginAnchor.mTarget.mSolverVariable : null;
                    beginTarget = previousVisibleWidget.mListAnchors[offset + 1].mSolverVariable;
                    ConstraintAnchor beginNextAnchor = null;
                    SolverVariable beginNext = null;
                    SolverVariable beginNextTarget = null;
                    int beginMargin = beginAnchor.getMargin();
                    int nextMargin = 0;

                    if (next != null) {
                        beginNextAnchor = next.mListAnchors[offset];
                        beginNext = beginNextAnchor.mSolverVariable;
                        beginNextTarget = beginNextAnchor.mTarget != null ? beginNextAnchor.mTarget.mSolverVariable : null;
                        nextMargin = beginNextAnchor.getMargin();
                    } else {
                        beginNextAnchor = widget.mListAnchors[offset + 1].mTarget;
                        if (beginNextAnchor != null) {
                            beginNext = beginNextAnchor.mSolverVariable;
                        }
                        beginNextTarget = widget.mListAnchors[offset + 1].mSolverVariable;
                    }

                    if (begin != null && beginTarget != null && beginNext != null && beginNextTarget != null) {
                        system.addCentering(begin, beginTarget, 0, 0.5f,
                                beginNext, beginNextTarget, 0,
                                SolverVariable.STRENGTH_HIGHEST);
                    }
                }
                previousVisibleWidget = widget;
                widget = next;
            }
            ConstraintAnchor begin = firstVisibleWidget.mListAnchors[offset];
            ConstraintAnchor beginTarget = first.mListAnchors[offset].mTarget;
            ConstraintAnchor end = lastVisibleWidget.mListAnchors[offset + 1];
            ConstraintAnchor endTarget = last.mListAnchors[offset + 1].mTarget;
            if (beginTarget != null) {
                if (firstVisibleWidget != lastVisibleWidget) {
                    system.addEquality(begin.mSolverVariable, beginTarget.mSolverVariable, begin.getMargin(), SolverVariable.STRENGTH_FIXED);
                } else if (endTarget != null) {
                    system.addCentering(begin.mSolverVariable, beginTarget.mSolverVariable, begin.getMargin(), 0.5f,
                            end.mSolverVariable, endTarget.mSolverVariable, end.getMargin(), SolverVariable.STRENGTH_FIXED);
                }
            }
            if (endTarget != null && (firstVisibleWidget != lastVisibleWidget)) {
                system.addEquality(end.mSolverVariable, endTarget.mSolverVariable, -end.getMargin(), SolverVariable.STRENGTH_FIXED);
            }
        }
    }
}