OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // This file has classes representing the grid sizing functions | |
6 | |
7 /** | |
8 * Represents the sizing function used for the min or max of a row or column. | |
9 */ | |
10 // TODO(jmesserly): rename to GridSizing, or make internal | |
11 class SizingFunction { | |
12 const SizingFunction(); | |
13 | |
14 bool get isContentSized() => isMinContentSized || isMaxContentSized; | |
15 bool get isMinContentSized() => false; | |
16 bool get isMaxContentSized() => false; | |
17 bool get isFraction() => false; | |
18 | |
19 num resolveLength(num gridSize) => 0; | |
20 | |
21 num get fractionValue() => 0; | |
22 | |
23 // TODO(jmesserly): this is only needed because FixedSizing is mutable | |
24 SizingFunction clone() => this; | |
25 } | |
26 | |
27 /** | |
28 * Fixed size reprensents a length as defined by CSS3 Values spec. | |
29 * Can also be a percentage of the Grid element's logical width (for columns) | |
30 * or logical height (for rows). When the width or height of the Grid element | |
31 * is undefined, the percentage is ignored and the Grid Track will be | |
32 * auto-sized. | |
33 */ | |
34 class FixedSizing extends SizingFunction { | |
35 final String units; | |
36 final num length; | |
37 | |
38 // TODO(jmesserly): kind of ugly to have this mutable property here, but | |
39 // we need to correctly track whether we're content sized during a layout | |
40 bool _contentSized; | |
41 | |
42 FixedSizing(this.length, [this.units = 'px']) | |
43 : super(), | |
44 _contentSized = false { | |
45 if (units != 'px' && units != '%') { | |
46 // TODO(jmesserly): support other unit types | |
47 throw new UnsupportedOperationException('Units other than px and %'); | |
48 } | |
49 } | |
50 | |
51 // TODO(jmesserly): this is only needed because of our mutable property | |
52 FixedSizing clone() => new FixedSizing(length, units); | |
53 | |
54 bool get isMinContentSized() => _contentSized; | |
55 | |
56 num resolveLength(num gridSize) { | |
57 if (units == '%') { | |
58 if (gridSize == null) { | |
59 // Use content size when the grid doesn't have an absolute size in this | |
60 // dimension | |
61 _contentSized = true; | |
62 return 0; | |
63 } | |
64 _contentSized = false; | |
65 return (length / 100) * gridSize; | |
66 } else { | |
67 return length; | |
68 } | |
69 } | |
70 | |
71 String toString() => 'FixedSizing: ${length}${units} $_contentSized'; | |
72 } | |
73 | |
74 | |
75 /** | |
76 * Fraction is a non-negative floating-point number followed by 'fr'. Each | |
77 * fraction value takes a share of the remaining space proportional to its | |
78 * number. | |
79 */ | |
80 class FractionSizing extends SizingFunction { | |
81 final num fractionValue; | |
82 FractionSizing(this.fractionValue) : super() {} | |
83 | |
84 bool get isFraction() => true; | |
85 | |
86 String toString() => 'FixedSizing: ${fractionValue}fr'; | |
87 } | |
88 | |
89 class MinContentSizing extends SizingFunction { | |
90 const MinContentSizing() : super(); | |
91 | |
92 bool get isMinContentSized() => true; | |
93 | |
94 String toString() => 'MinContentSizing'; | |
95 } | |
96 | |
97 class MaxContentSizing extends SizingFunction { | |
98 const MaxContentSizing() : super(); | |
99 | |
100 bool get isMaxContentSized() { return true; } | |
101 | |
102 String toString() => 'MaxContentSizing'; | |
103 } | |
104 | |
105 /** The min and max sizing functions for a track. */ | |
106 class TrackSizing { | |
107 /** The min sizing function for the track. */ | |
108 final SizingFunction min; | |
109 | |
110 /** The min sizing function for the track. */ | |
111 final SizingFunction max; | |
112 | |
113 const TrackSizing.auto() | |
114 : min = const MinContentSizing(), | |
115 max = const MaxContentSizing(); | |
116 | |
117 TrackSizing(this.min, this.max) {} | |
118 | |
119 // TODO(jmesserly): this is only needed because FixedSizing is mutable | |
120 TrackSizing clone() => new TrackSizing(min.clone(), max.clone()); | |
121 } | |
122 | |
123 /** Represents a GridTrack breadth property. */ | |
124 // TODO(jmesserly): these classes could be replaced with reflection/mirrors | |
125 interface _BreadthAccumulator { | |
126 void setSize(GridTrack t, num value); | |
127 num getSize(GridTrack t); | |
128 | |
129 SizingFunction getSizingFunction(GridTrack t); | |
130 } | |
131 | |
132 class _UsedBreadthAccumulator implements _BreadthAccumulator { | |
133 const _UsedBreadthAccumulator(); | |
134 | |
135 void setSize(GridTrack t, num value) { t.usedBreadth = value; } | |
136 num getSize(GridTrack t) => t.usedBreadth; | |
137 | |
138 SizingFunction getSizingFunction(GridTrack t) => t.minSizing; | |
139 } | |
140 | |
141 class _MaxBreadthAccumulator implements _BreadthAccumulator { | |
142 const _MaxBreadthAccumulator(); | |
143 | |
144 void setSize(GridTrack t, num value) { t.maxBreadth = value; } | |
145 num getSize(GridTrack t) => t.maxBreadth; | |
146 | |
147 SizingFunction getSizingFunction(GridTrack t) => t.maxSizing; | |
148 } | |
149 | |
OLD | NEW |