OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of layout; | 5 part of layout; |
6 | 6 |
7 // This file has classes representing the grid tracks and grid template | 7 // This file has classes representing the grid tracks and grid template |
8 | 8 |
9 /** | 9 /** |
10 * The data structure representing the grid-rows or grid-columns | 10 * The data structure representing the grid-rows or grid-columns |
11 * properties. | 11 * properties. |
12 */ | 12 */ |
13 class GridTrackList { | 13 class GridTrackList { |
14 /** The set of tracks defined in CSS via grid-rows and grid-columns */ | 14 /** The set of tracks defined in CSS via grid-rows and grid-columns */ |
15 final List<GridTrack> tracks; | 15 final List<GridTrack> tracks; |
16 | 16 |
17 /** | 17 /** |
18 * Maps edge names to the corresponding track. Depending on whether the index | 18 * Maps edge names to the corresponding track. Depending on whether the index |
19 * is used as a start or end, it might be interpreted exclusively or | 19 * is used as a start or end, it might be interpreted exclusively or |
20 * inclusively. | 20 * inclusively. |
21 */ | 21 */ |
22 final Map<String, int> lineNames; | 22 final Map<String, int> lineNames; |
23 | 23 |
24 GridTrackList(this.tracks, this.lineNames) {} | 24 GridTrackList(this.tracks, this.lineNames) {} |
25 } | 25 } |
26 | 26 |
27 | |
28 /** Represents a row or a column. */ | 27 /** Represents a row or a column. */ |
29 class GridTrack { | 28 class GridTrack { |
30 /** | 29 /** |
31 * The start position of this track. Equal to the sum of previous track's | 30 * The start position of this track. Equal to the sum of previous track's |
32 * usedBreadth. | 31 * usedBreadth. |
33 */ | 32 */ |
34 num start; | 33 num start; |
35 | 34 |
36 /** The final computed breadth of this track. */ | 35 /** The final computed breadth of this track. */ |
37 num usedBreadth; | 36 num usedBreadth; |
(...skipping 17 matching lines...) Expand all Loading... |
55 SizingFunction get minSizing => sizing.min; | 54 SizingFunction get minSizing => sizing.min; |
56 | 55 |
57 /** The min sizing function for the track. */ | 56 /** The min sizing function for the track. */ |
58 SizingFunction get maxSizing => sizing.max; | 57 SizingFunction get maxSizing => sizing.max; |
59 | 58 |
60 num get end => start + usedBreadth; | 59 num get end => start + usedBreadth; |
61 | 60 |
62 bool get isFractional => minSizing.isFraction || maxSizing.isFraction; | 61 bool get isFractional => minSizing.isFraction || maxSizing.isFraction; |
63 } | 62 } |
64 | 63 |
65 | |
66 /** Represents the grid-row-align or grid-column-align. */ | 64 /** Represents the grid-row-align or grid-column-align. */ |
67 class GridItemAlignment { | 65 class GridItemAlignment { |
68 // TODO(jmesserly): should this be stored as an int for performance? | 66 // TODO(jmesserly): should this be stored as an int for performance? |
69 final String value; | 67 final String value; |
70 | 68 |
71 // 'start' | 'end' | 'center' | 'stretch' | 69 // 'start' | 'end' | 'center' | 'stretch' |
72 GridItemAlignment.fromString(String value) | 70 GridItemAlignment.fromString(String value) |
73 : this.value = (value == null) ? 'stretch' : value { | 71 : this.value = (value == null) ? 'stretch' : value { |
74 | 72 switch (this.value) { |
75 switch (this.value) { | 73 case 'start': |
76 case 'start': case 'end': case 'center': case 'stretch': | 74 case 'end': |
| 75 case 'center': |
| 76 case 'stretch': |
77 break; | 77 break; |
78 default: | 78 default: |
79 throw new UnsupportedError( | 79 throw new UnsupportedError('invalid row/column alignment "$value"'); |
80 'invalid row/column alignment "$value"'); | |
81 } | 80 } |
82 } | 81 } |
83 | 82 |
84 _GridLocation align(_GridLocation span, int size) { | 83 _GridLocation align(_GridLocation span, int size) { |
85 switch (value) { | 84 switch (value) { |
86 case 'start': | 85 case 'start': |
87 return new _GridLocation(span.start, size); | 86 return new _GridLocation(span.start, size); |
88 case 'end': | 87 case 'end': |
89 return new _GridLocation(span.end - size, size); | 88 return new _GridLocation(span.end - size, size); |
90 case 'center': | 89 case 'center': |
91 size = Math.min(size, span.length); | 90 size = Math.min(size, span.length); |
92 num center = span.start + span.length / 2; | 91 num center = span.start + span.length / 2; |
93 num left = center - size / 2; | 92 num left = center - size / 2; |
94 return new _GridLocation(left.round(), size); | 93 return new _GridLocation(left.round(), size); |
95 case 'stretch': | 94 case 'stretch': |
96 return span; | 95 return span; |
97 } | 96 } |
98 } | 97 } |
99 } | 98 } |
100 | 99 |
101 | |
102 /** | 100 /** |
103 * Represents a grid-template. Used in conjunction with a grid-cell to | 101 * Represents a grid-template. Used in conjunction with a grid-cell to |
104 * place cells in the grid, without needing to specify the exact row/column. | 102 * place cells in the grid, without needing to specify the exact row/column. |
105 */ | 103 */ |
106 class GridTemplate { | 104 class GridTemplate { |
107 final Map<int, _GridTemplateRect> _rects; | 105 final Map<int, _GridTemplateRect> _rects; |
108 final int _numRows; | 106 final int _numRows; |
109 | 107 |
110 GridTemplate(List<String> rows) | 108 GridTemplate(List<String> rows) |
111 : _rects = new Map<int, _GridTemplateRect>(), | 109 : _rects = new Map<int, _GridTemplateRect>(), |
112 _numRows = rows.length { | 110 _numRows = rows.length { |
113 _buildRects(rows); | 111 _buildRects(rows); |
114 } | 112 } |
115 | 113 |
116 /** Scans the template strings and computes bounds for each one. */ | 114 /** Scans the template strings and computes bounds for each one. */ |
117 void _buildRects(List<String> templateRows) { | 115 void _buildRects(List<String> templateRows) { |
118 for (int r = 0; r < templateRows.length; r++) { | 116 for (int r = 0; r < templateRows.length; r++) { |
119 String row = templateRows[r]; | 117 String row = templateRows[r]; |
120 for (int c = 0; c < row.length; c++) { | 118 for (int c = 0; c < row.length; c++) { |
121 int cell = row.codeUnitAt(c); | 119 int cell = row.codeUnitAt(c); |
122 final rect = _rects[cell]; | 120 final rect = _rects[cell]; |
(...skipping 15 matching lines...) Expand all Loading... |
138 * Looks up the given cell in the template, and returns the rect. | 136 * Looks up the given cell in the template, and returns the rect. |
139 */ | 137 */ |
140 _GridTemplateRect lookupCell(String cell) { | 138 _GridTemplateRect lookupCell(String cell) { |
141 if (cell.length != 1) { | 139 if (cell.length != 1) { |
142 throw new UnsupportedError( | 140 throw new UnsupportedError( |
143 'grid-cell "$cell" must be a one character string'); | 141 'grid-cell "$cell" must be a one character string'); |
144 } | 142 } |
145 final rect = _rects[cell.codeUnitAt(0)]; | 143 final rect = _rects[cell.codeUnitAt(0)]; |
146 if (rect == null) { | 144 if (rect == null) { |
147 throw new UnsupportedError( | 145 throw new UnsupportedError( |
148 'grid-cell "$cell" not found in parent\'s grid-template'); | 146 'grid-cell "$cell" not found in parent\'s grid-template'); |
149 } | 147 } |
150 return rect; | 148 return rect; |
151 } | 149 } |
152 } | 150 } |
153 | 151 |
154 /** Used by GridTemplate to track a single cell's bounds. */ | 152 /** Used by GridTemplate to track a single cell's bounds. */ |
155 class _GridTemplateRect { | 153 class _GridTemplateRect { |
156 int row, column, rowSpan, columnSpan, _count, _char; | 154 int row, column, rowSpan, columnSpan, _count, _char; |
157 _GridTemplateRect(this._char, this.row, this.column) | 155 _GridTemplateRect(this._char, this.row, this.column) |
158 : rowSpan = 1, columnSpan = 1, _count = 1 {} | 156 : rowSpan = 1, |
| 157 columnSpan = 1, |
| 158 _count = 1 {} |
159 | 159 |
160 void add(int r, int c) { | 160 void add(int r, int c) { |
161 assert (r >= row && c >= column); | 161 assert(r >= row && c >= column); |
162 _count++; | 162 _count++; |
163 rowSpan = Math.max(rowSpan, r - row + 1); | 163 rowSpan = Math.max(rowSpan, r - row + 1); |
164 columnSpan = Math.max(columnSpan, c - column + 1); | 164 columnSpan = Math.max(columnSpan, c - column + 1); |
165 } | 165 } |
166 | 166 |
167 void checkValid() { | 167 void checkValid() { |
168 int expected = rowSpan * columnSpan; | 168 int expected = rowSpan * columnSpan; |
169 if (expected != _count) { | 169 if (expected != _count) { |
170 // TODO(jmesserly): not sure if we should throw here, due to CSS's | 170 // TODO(jmesserly): not sure if we should throw here, due to CSS's |
171 // permissiveness. At the moment we're noisy about errors. | 171 // permissiveness. At the moment we're noisy about errors. |
172 String cell = new String.fromCharCodes([_char]); | 172 String cell = new String.fromCharCodes([_char]); |
173 throw new UnsupportedError('grid-template "$cell"' | 173 throw new UnsupportedError('grid-template "$cell"' |
174 ' is not square, expected $expected cells but got $_count'); | 174 ' is not square, expected $expected cells but got $_count'); |
175 } | 175 } |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 | |
180 /** | 179 /** |
181 * Used to return a row/column and span during parsing of grid-row and | 180 * Used to return a row/column and span during parsing of grid-row and |
182 * grid-column during parsing. | 181 * grid-column during parsing. |
183 */ | 182 */ |
184 class _GridLocation { | 183 class _GridLocation { |
185 final int start, length; | 184 final int start, length; |
186 _GridLocation(this.start, this.length) {} | 185 _GridLocation(this.start, this.length) {} |
187 | 186 |
188 int get end => start + length; | 187 int get end => start + length; |
189 } | 188 } |
OLD | NEW |