aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/ResizeState.java
blob: 42b9083ad5595ae7b4ea1d7524291e27a3917c7f (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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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 com.android.ide.common.layout;

import static com.android.SdkConstants.VALUE_N_DP;
import static com.android.SdkConstants.VALUE_WRAP_CONTENT;

import com.android.ide.common.api.INode;
import com.android.ide.common.api.Rect;
import com.android.ide.common.api.Segment;
import com.android.ide.common.api.SegmentType;

/** State held during resizing operations */
class ResizeState {
    /**
     * The associated rule
     */
    private final BaseLayoutRule mRule;

    /**
     * The node being resized
     */
    public final INode node;

     /**
      * The layout containing the resized node
      */
    public final INode layout;

    /** The proposed resized bounds of the node */
    public Rect bounds;

    /** The preferred wrap_content bounds of the node */
    public Rect wrapBounds;

    /** The suggested horizontal fill_parent guideline position */
    public Segment horizontalFillSegment;

    /** The suggested vertical fill_parent guideline position */
    public Segment verticalFillSegment;

    /** The type of horizontal edge being resized, or null */
    public SegmentType horizontalEdgeType;

    /** The type of vertical edge being resized, or null */
    public SegmentType verticalEdgeType;

    /** Whether the user has snapped to the wrap_content width */
    public boolean wrapWidth;

    /** Whether the user has snapped to the wrap_content height */
    public boolean wrapHeight;

    /** Whether the user has snapped to the match_parent width */
    public boolean fillWidth;

    /** Whether the user has snapped to the match_parent height */
    public boolean fillHeight;

    /** Custom field for use by subclasses */
    public Object clientData;

    /** Keyboard mask */
    public int modifierMask;

    /**
     * The actual view object for the layout containing the resizing operation,
     * or null if not known
     */
    public Object layoutView;

    /**
     * Constructs a new {@link ResizeState}
     *
     * @param rule the associated rule
     * @param layout the parent layout containing the resized node
     * @param layoutView the actual View instance for the layout, or null if not known
     * @param node the node being resized
     */
    ResizeState(BaseLayoutRule rule, INode layout, Object layoutView, INode node) {
        mRule = rule;

        this.layout = layout;
        this.node = node;
        this.layoutView = layoutView;
    }

    /**
     * Returns the width attribute to be set to match the new bounds
     *
     * @return the width string, never null
     */
    public String getWidthAttribute() {
        if (wrapWidth) {
            return VALUE_WRAP_CONTENT;
        } else if (fillWidth) {
            return mRule.getFillParentValueName();
        } else {
            return String.format(VALUE_N_DP, mRule.mRulesEngine.pxToDp(bounds.w));
        }
    }

    /**
     * Returns the height attribute to be set to match the new bounds
     *
     * @return the height string, never null
     */
    public String getHeightAttribute() {
        if (wrapHeight) {
            return VALUE_WRAP_CONTENT;
        } else if (fillHeight) {
            return mRule.getFillParentValueName();
        } else {
            return String.format(VALUE_N_DP, mRule.mRulesEngine.pxToDp(bounds.h));
        }
    }
}