Skip to content

Commit 7153e87

Browse files
committed
Enable chained edge pins!
1 parent ecc96ff commit 7153e87

File tree

2 files changed

+104
-83
lines changed

2 files changed

+104
-83
lines changed

lib/src/api/models/alignment.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class AlignmentModel with EquatableMixin, SerializableMixin {
2222
/// Equivalent to having no alignment at all.
2323
static const AlignmentModel none = AlignmentModel(null);
2424

25+
/// Returns true if this alignment's data is not null (is not [none]).
26+
bool get hasData => data != null;
27+
2528
/// Represents the top-left corner.
2629
/// Top o----------------
2730
/// Left | | |
@@ -136,6 +139,18 @@ class AlignmentModel with EquatableMixin, SerializableMixin {
136139
data == topCenter.data ||
137140
data == bottomCenter.data;
138141

142+
/// Whether this alignment contains a cardinal top y value.
143+
bool get hasTop => data?.y == -1;
144+
145+
/// Whether this alignment contains a cardinal bottom y value.
146+
bool get hasBottom => data?.y == 1;
147+
148+
/// Whether this alignment contains a cardinal left x value.
149+
bool get hasLeft => data?.x == -1;
150+
151+
/// Whether this alignment contains a cardinal right x value.
152+
bool get hasRight => data?.x == 1;
153+
139154
/// All standard alignment values for all for corners, edges and the center.
140155
static const List<AlignmentModel> values = [
141156
AlignmentModel.topLeft,

lib/src/api/models/pins_model.dart

Lines changed: 89 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,28 @@ enum EdgePin {
2525
bottom;
2626

2727
/// Returns opposite side edge pin of this edge pin.
28-
EdgePin opposite() {
29-
switch (this) {
30-
case EdgePin.left:
31-
return EdgePin.right;
32-
case EdgePin.top:
33-
return EdgePin.bottom;
34-
case EdgePin.right:
35-
return EdgePin.left;
36-
case EdgePin.bottom:
37-
return EdgePin.top;
38-
}
39-
}
28+
EdgePin get opposite => switch (this) {
29+
EdgePin.left => EdgePin.right,
30+
EdgePin.top => EdgePin.bottom,
31+
EdgePin.right => EdgePin.left,
32+
EdgePin.bottom => EdgePin.top
33+
};
4034

4135
/// Converts edge pin to [AxisC].
42-
AxisC axis() {
43-
switch (this) {
44-
case EdgePin.left:
45-
case EdgePin.right:
46-
return AxisC.horizontal;
47-
case EdgePin.top:
48-
case EdgePin.bottom:
49-
return AxisC.vertical;
50-
}
51-
}
36+
AxisC get axis => switch (this) {
37+
EdgePin.left => AxisC.horizontal,
38+
EdgePin.right => AxisC.horizontal,
39+
EdgePin.top => AxisC.vertical,
40+
EdgePin.bottom => AxisC.vertical,
41+
};
5242

5343
/// Converts edge pin to [AlignmentModel].
54-
AlignmentModel alignment() {
55-
switch (this) {
56-
case EdgePin.left:
57-
return AlignmentModel.centerLeft;
58-
case EdgePin.top:
59-
return AlignmentModel.topCenter;
60-
case EdgePin.right:
61-
return AlignmentModel.centerRight;
62-
case EdgePin.bottom:
63-
return AlignmentModel.bottomCenter;
64-
}
65-
}
44+
AlignmentModel get alignment => switch (this) {
45+
EdgePin.left => AlignmentModel.centerLeft,
46+
EdgePin.top => AlignmentModel.topCenter,
47+
EdgePin.right => AlignmentModel.centerRight,
48+
EdgePin.bottom => AlignmentModel.bottomCenter,
49+
};
6650
}
6751

6852
/// Edge pins are the edges of a node that are pinned to its parent. Edges refer
@@ -120,22 +104,52 @@ class EdgePinsModel with EquatableMixin, SerializableMixin {
120104
static const EdgePinsModel standard =
121105
EdgePinsModel(left: 0, top: 0, right: null, bottom: null);
122106

107+
/// Creates an invalid [EdgePinsModel] that has no non-null pins.
108+
/// An invalid pins model is one which has no pin on a given axis.
109+
static const EdgePinsModel invalid =
110+
EdgePinsModel(left: null, top: null, right: null, bottom: null);
111+
112+
/// Creates an [EdgePinsModel] that fills the parent's entire space.
113+
static const EdgePinsModel fill =
114+
EdgePinsModel(left: 0, top: 0, right: 0, bottom: 0);
115+
123116
/// Whether this [EdgePinsModel] follows the standard convention of pinning
124117
/// the node to parent's top-left corner.
125118
bool get isStandard =>
126119
left != null && top != null && right == null && bottom == null;
127120

121+
/// Whether this [EdgePinsModel] has an invalid axis horizontally. An invalid
122+
/// pins model is one which has no pin on a given axis.
123+
bool get isInvalidHorizontally => left == null && right == null;
124+
125+
/// Whether this [EdgePinsModel] has an invalid axis vertically. An invalid
126+
/// pins model is one which has no pin on a given axis.
127+
bool get isInvalidVertically => top == null && bottom == null;
128+
129+
/// Whether this [EdgePinsModel] is invalid on at least one axis.
130+
/// An invalid pins model is one which has no pin on a given axis.
131+
bool get isOneAxisInvalid => isInvalidHorizontally || isInvalidVertically;
132+
133+
/// Whether this [EdgePinsModel] is invalid on both axes.
134+
/// An invalid pins model is one which has no pin on a given axis.
135+
bool get areBothAxesInvalid => isInvalidHorizontally && isInvalidVertically;
136+
128137
/// Whether the node is symmetric-chained on both axes, i.e., none of the pins
129-
/// is null.
130-
bool get isDoubleChained => isHorizontalChained && isVerticalChained;
138+
/// are null.
139+
bool get isBothExpanded => isHorizontallyExpanded && isVerticallyExpanded;
140+
141+
/// Whether the node is symmetric-chained on at least one axis, i.e., at least
142+
/// one of the pins is not null.
143+
bool get isOneOrBothExpanded =>
144+
isHorizontallyExpanded || isVerticallyExpanded;
131145

132146
/// Whether the node is symmetric-chained on the horizontal axis, i.e., left
133147
/// and right pins are not null.
134-
bool get isHorizontalChained => left != null && right != null;
148+
bool get isHorizontallyExpanded => left != null && right != null;
135149

136150
/// Whether the node is symmetric-chained on the vertical axis, i.e., top and
137151
/// bottom pins are not null.
138-
bool get isVerticalChained => top != null && bottom != null;
152+
bool get isVerticallyExpanded => top != null && bottom != null;
139153

140154
/// Due to the null-pattern of the pins, default copyWith method does not
141155
/// work. So, there's a separate copyWith method for each pin.
@@ -176,61 +190,53 @@ class EdgePinsModel with EquatableMixin, SerializableMixin {
176190
/// class.
177191
///
178192
/// Takes a [pin] and a [value] and returns the new [EdgePinesModel].
179-
EdgePinsModel copyWithPin(EdgePin pin, double? value) {
180-
switch (pin) {
181-
case EdgePin.left:
182-
return copyWithLeft(value);
183-
case EdgePin.top:
184-
return copyWithTop(value);
185-
case EdgePin.right:
186-
return copyWithRight(value);
187-
case EdgePin.bottom:
188-
return copyWithBottom(value);
189-
}
190-
}
193+
EdgePinsModel copyWithPin(EdgePin pin, double? value) => switch (pin) {
194+
EdgePin.left => copyWithLeft(value),
195+
EdgePin.top => copyWithTop(value),
196+
EdgePin.right => copyWithRight(value),
197+
EdgePin.bottom => copyWithBottom(value)
198+
};
191199

192200
/// Get a pin from this instance of [EdgePinsModel], given a [pin] enum.
193201
///
194202
/// This is useful for abstraction and polymorphic behavior.
195-
double? getPin(EdgePin pin) {
196-
switch (pin) {
197-
case EdgePin.left:
198-
return left;
199-
case EdgePin.top:
200-
return top;
201-
case EdgePin.right:
202-
return right;
203-
case EdgePin.bottom:
204-
return bottom;
205-
}
206-
}
203+
double? getPin(EdgePin pin) => switch (pin) {
204+
EdgePin.left => left,
205+
EdgePin.top => top,
206+
EdgePin.right => right,
207+
EdgePin.bottom => bottom
208+
};
207209

208210
/// Whether the given [pin] is null or not.
209-
bool containsPin(EdgePin? pin) {
210-
if (pin == null) return false;
211-
switch (pin) {
212-
case EdgePin.left:
213-
return left != null;
214-
case EdgePin.top:
215-
return top != null;
216-
case EdgePin.right:
217-
return right != null;
218-
case EdgePin.bottom:
219-
return bottom != null;
220-
}
221-
}
211+
bool containsPin(EdgePin pin) => switch (pin) {
212+
EdgePin.left => left != null,
213+
EdgePin.top => top != null,
214+
EdgePin.right => right != null,
215+
EdgePin.bottom => bottom != null
216+
};
217+
218+
/// Whether this [EdgePinsModel] contains any of the given [pins].
219+
bool containsAnyPin(Iterable<EdgePin> pins) => pins.any(containsPin);
220+
221+
/// Whether this [EdgePinsModel] contains all of the given [pins].
222+
bool containsEveryPin(Iterable<EdgePin> pins) => pins.every(containsPin);
222223

223224
/// Whether this instance of [EdgePinsModel] contains symmetric
224225
/// chains on the given [axis]. IE: If both pins on the given [axis] are
225226
/// set to non-null values.
226-
bool chainedOnAxis(AxisC axis) {
227-
switch (axis) {
228-
case AxisC.horizontal:
229-
return isHorizontalChained;
230-
case AxisC.vertical:
231-
return isVerticalChained;
232-
}
233-
}
227+
bool chainedOnAxis(AxisC axis) => switch (axis) {
228+
AxisC.horizontal => isHorizontallyExpanded,
229+
AxisC.vertical => isVerticallyExpanded
230+
};
231+
232+
/// [returns] a set of pins that are enabled on this instance of
233+
/// [EdgePinsModel]. An enabled pin is one that has a non-null value.
234+
Set<EdgePin> get enabledPins => {
235+
if (left != null) EdgePin.left,
236+
if (top != null) EdgePin.top,
237+
if (right != null) EdgePin.right,
238+
if (bottom != null) EdgePin.bottom,
239+
};
234240

235241
@override
236242
List<Object?> get props => [left, top, right, bottom];

0 commit comments

Comments
 (0)