-
Notifications
You must be signed in to change notification settings - Fork 24
/
layout_constraints.dart
226 lines (207 loc) · 7.76 KB
/
layout_constraints.dart
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import 'dart:math' as math;
import 'package:meta/meta.dart';
import 'package:multi_split_view/multi_split_view.dart';
import 'package:multi_split_view/src/area.dart';
import 'package:multi_split_view/src/controller.dart';
import 'package:multi_split_view/src/internal/num_util.dart';
/// Represents the layout constraints used by the [MultiSplitView].
@internal
class LayoutConstraints {
factory LayoutConstraints(
{required final int areasCount,
required final double containerSize,
required final double dividerThickness}) {
NumUtil.validateInt('childrenCount', areasCount);
NumUtil.validateDouble('dividerThickness', dividerThickness);
NumUtil.validateDouble('containerSize', containerSize);
final double totalDividerSize =
areasCount > 1 ? (areasCount - 1) * dividerThickness : 0;
final double spaceForAreas = math.max(0, containerSize - totalDividerSize);
return LayoutConstraints._(
areasCount: areasCount,
containerSize: containerSize,
dividerThickness: dividerThickness,
totalDividerSize: totalDividerSize,
spaceForAreas: spaceForAreas);
}
LayoutConstraints._(
{required int areasCount,
required double containerSize,
required double dividerThickness,
required double totalDividerSize,
required double spaceForAreas})
: areasCount = areasCount,
containerSize = containerSize,
dividerThickness = dividerThickness,
totalDividerSize = totalDividerSize,
spaceForAreas = spaceForAreas;
/// The count of visible areas on the screen.
final int areasCount;
/// The container size.
final double containerSize;
/// The divider thickness defined by the theme.
final double dividerThickness;
/// The total size of all dividers.
final double totalDividerSize;
/// The size of the container minus the size of the dividers.
final double spaceForAreas;
/// Applies the following adjustments:
///
/// * Shrinks size when the total size of the areas is greater than
/// the available space, even if a [min] limit exists.
/// * Grows size to meet the minimum value when space is available.
/// * Grows size when the total size of the areas is smaller than the
/// available space and there are no flex areas to fill the available
/// space, even if a [max] limit exists.
void adjustAreas(
{required ControllerHelper controllerHelper,
required SizeOverflowPolicy sizeOverflowPolicy,
required SizeUnderflowPolicy sizeUnderflowPolicy,
required MinSizeRecoveryPolicy minSizeRecoveryPolicy}) {
bool changed = false;
int flexCount = 0;
double totalSize = 0;
List<Area> minSizeToRecover = [];
for (Area area in controllerHelper.areas) {
if (area.size != null) {
totalSize += area.size!;
if (area.min != null && area.size! < area.min!) {
minSizeToRecover.add(area);
}
} else {
flexCount++;
}
}
if (totalSize > spaceForAreas) {
// The total size of the areas is greater than the available space.
// Need to shrink.
Iterable<Area> it = sizeOverflowPolicy == SizeOverflowPolicy.shrinkFirst
? controllerHelper.areas
: controllerHelper.areas.reversed;
for (Area area in it) {
if (totalSize <= spaceForAreas) {
break;
} else if (area.size != null) {
double excessToRemove =
math.min(area.size!, totalSize - spaceForAreas);
AreaHelper.setSize(area: area, size: area.size! - excessToRemove);
totalSize -= excessToRemove;
}
}
changed = true;
}
if (totalSize < spaceForAreas && minSizeToRecover.isNotEmpty) {
// Grows size to meet the minimum value when space is available.
for (Area area
in minSizeRecoveryPolicy == MinSizeRecoveryPolicy.firstToLast
? minSizeToRecover
: minSizeToRecover.reversed) {
double available = math.max(spaceForAreas - totalSize, 0);
if (available == 0) {
break;
}
double sizeToRecover = math.min(area.min! - area.size!, available);
totalSize += sizeToRecover;
AreaHelper.setSize(
area: area, size: math.min(area.size! + sizeToRecover, area.min!));
changed = true;
}
}
if (totalSize < spaceForAreas && flexCount == 0) {
// The total size of the areas is smaller than the available space and
// there are no flex areas to fill the available space.
// Need to stretch.
if (sizeUnderflowPolicy == SizeUnderflowPolicy.stretchFirst) {
Area area = controllerHelper.areas.first;
AreaHelper.setSize(
area: area, size: area.size! + spaceForAreas - totalSize);
} else if (sizeUnderflowPolicy == SizeUnderflowPolicy.stretchLast) {
Area area = controllerHelper.areas.last;
AreaHelper.setSize(
area: area, size: area.size! + spaceForAreas - totalSize);
} else if (sizeUnderflowPolicy == SizeUnderflowPolicy.stretchAll) {
double extraSize =
(spaceForAreas - totalSize) / controllerHelper.areas.length;
for (Area area in controllerHelper.areas) {
AreaHelper.setSize(area: area, size: area.size! + extraSize);
}
}
changed = true;
}
if (changed) {
controllerHelper.updateAreas();
Future.microtask(() => controllerHelper.notifyListeners());
}
}
double calculateAvailableSpaceForFlexAreas(
MultiSplitViewController controller) {
double space = spaceForAreas;
for (int index = 0; index < controller.areasCount; index++) {
Area area = controller.getArea(index);
if (area.size != null) {
space -= area.size!;
}
}
return math.max(space, 0);
}
double dividerStartOf(
{required int index,
required MultiSplitViewController controller,
required bool antiAliasingWorkaround}) {
double dividerStart = double.infinity;
var onDividerLayout = (
{required int index,
required double start,
required double thickness}) {
dividerStart = start;
};
performLayout(
controller: controller,
antiAliasingWorkaround: antiAliasingWorkaround,
onDividerLayout: onDividerLayout,
onlyOnIndex: index);
if (dividerStart == double.infinity) {
throw ArgumentError.value(index, 'index', 'Invalid index');
}
return dividerStart;
}
void performLayout(
{required MultiSplitViewController controller,
required bool antiAliasingWorkaround,
OnLayout? onAreaLayout,
OnLayout? onDividerLayout,
int? onlyOnIndex}) {
double start = 0;
final double availableSpaceForFlexAreas =
calculateAvailableSpaceForFlexAreas(controller);
final double pixelPerFlex =
availableSpaceForFlexAreas / controller.totalFlex;
for (int index = 0; index < controller.areasCount; index++) {
Area area = controller.getArea(index);
double thickness;
if (area.flex != null) {
thickness = area.flex! * pixelPerFlex;
} else {
thickness = area.size!;
}
if (antiAliasingWorkaround) {
thickness = thickness.roundToDouble();
}
if (onAreaLayout != null &&
(onlyOnIndex == null || onlyOnIndex == index)) {
onAreaLayout(index: index, start: start, thickness: thickness);
}
start += thickness;
if (index < controller.areasCount - 1) {
if (onDividerLayout != null &&
(onlyOnIndex == null || onlyOnIndex == index)) {
onDividerLayout(
index: index, start: start, thickness: dividerThickness);
}
start += dividerThickness;
}
}
}
}
typedef OnLayout = void Function(
{required int index, required double start, required double thickness});