1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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
17package com.android.ide.common.layout;
18
19import static com.android.SdkConstants.VALUE_N_DP;
20import static com.android.SdkConstants.VALUE_WRAP_CONTENT;
21
22import com.android.ide.common.api.INode;
23import com.android.ide.common.api.Rect;
24import com.android.ide.common.api.Segment;
25import com.android.ide.common.api.SegmentType;
26
27/** State held during resizing operations */
28class ResizeState {
29    /**
30     * The associated rule
31     */
32    private final BaseLayoutRule mRule;
33
34    /**
35     * The node being resized
36     */
37    public final INode node;
38
39     /**
40      * The layout containing the resized node
41      */
42    public final INode layout;
43
44    /** The proposed resized bounds of the node */
45    public Rect bounds;
46
47    /** The preferred wrap_content bounds of the node */
48    public Rect wrapBounds;
49
50    /** The suggested horizontal fill_parent guideline position */
51    public Segment horizontalFillSegment;
52
53    /** The suggested vertical fill_parent guideline position */
54    public Segment verticalFillSegment;
55
56    /** The type of horizontal edge being resized, or null */
57    public SegmentType horizontalEdgeType;
58
59    /** The type of vertical edge being resized, or null */
60    public SegmentType verticalEdgeType;
61
62    /** Whether the user has snapped to the wrap_content width */
63    public boolean wrapWidth;
64
65    /** Whether the user has snapped to the wrap_content height */
66    public boolean wrapHeight;
67
68    /** Whether the user has snapped to the match_parent width */
69    public boolean fillWidth;
70
71    /** Whether the user has snapped to the match_parent height */
72    public boolean fillHeight;
73
74    /** Custom field for use by subclasses */
75    public Object clientData;
76
77    /** Keyboard mask */
78    public int modifierMask;
79
80    /**
81     * The actual view object for the layout containing the resizing operation,
82     * or null if not known
83     */
84    public Object layoutView;
85
86    /**
87     * Constructs a new {@link ResizeState}
88     *
89     * @param rule the associated rule
90     * @param layout the parent layout containing the resized node
91     * @param layoutView the actual View instance for the layout, or null if not known
92     * @param node the node being resized
93     */
94    ResizeState(BaseLayoutRule rule, INode layout, Object layoutView, INode node) {
95        mRule = rule;
96
97        this.layout = layout;
98        this.node = node;
99        this.layoutView = layoutView;
100    }
101
102    /**
103     * Returns the width attribute to be set to match the new bounds
104     *
105     * @return the width string, never null
106     */
107    public String getWidthAttribute() {
108        if (wrapWidth) {
109            return VALUE_WRAP_CONTENT;
110        } else if (fillWidth) {
111            return mRule.getFillParentValueName();
112        } else {
113            return String.format(VALUE_N_DP, mRule.mRulesEngine.pxToDp(bounds.w));
114        }
115    }
116
117    /**
118     * Returns the height attribute to be set to match the new bounds
119     *
120     * @return the height string, never null
121     */
122    public String getHeightAttribute() {
123        if (wrapHeight) {
124            return VALUE_WRAP_CONTENT;
125        } else if (fillHeight) {
126            return mRule.getFillParentValueName();
127        } else {
128            return String.format(VALUE_N_DP, mRule.mRulesEngine.pxToDp(bounds.h));
129        }
130    }
131}
132