-
Notifications
You must be signed in to change notification settings - Fork 303
/
positioned.dart
453 lines (401 loc) · 17.9 KB
/
positioned.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (C) 2021-present The Kraken authors. All rights reserved.
*/
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:kraken/css.dart';
import 'package:kraken/dom.dart';
import 'package:kraken/rendering.dart';
// CSS Positioned Layout: https://drafts.csswg.org/css-position/
// RenderPositionHolder may be affected by overflow: scroller offset.
// We need to reset these offset to keep positioned elements render at their original position.
// @NOTE: Attention that renderObjects in tree may not all subtype of RenderBoxModel, use `is` to identify.
Offset? _getRenderPositionHolderScrollOffset(RenderPositionPlaceholder holder, RenderObject root) {
AbstractNode? current = holder.parent;
while (current != null && current != root) {
if (current is RenderBoxModel) {
if (current.clipX || current.clipY) {
return Offset(current.scrollLeft, current.scrollTop);
}
}
current = current.parent;
}
return null;
}
// Get the offset of the RenderPlaceholder of positioned element to its parent RenderBoxModel.
Offset _getPlaceholderToParentOffset(RenderPositionPlaceholder placeholder, RenderBoxModel parent) {
if (!placeholder.attached) {
return Offset.zero;
}
Offset positionHolderScrollOffset = _getRenderPositionHolderScrollOffset(placeholder, parent) ?? Offset.zero;
Offset placeholderOffset = placeholder.localToGlobal(positionHolderScrollOffset, ancestor: parent);
return placeholderOffset;
}
// Margin auto has special rules for positioned element
// which will override the default position rule
// https://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width
Offset _getAutoMarginPositionedElementOffset(double? x, double? y, RenderBoxModel child, Size parentSize) {
RenderStyle childRenderStyle = child.renderStyle;
CSSLengthValue marginLeft = childRenderStyle.marginLeft;
CSSLengthValue marginRight = childRenderStyle.marginRight;
CSSLengthValue marginTop = childRenderStyle.marginTop;
CSSLengthValue marginBottom = childRenderStyle.marginBottom;
CSSLengthValue width = childRenderStyle.width;
CSSLengthValue height = childRenderStyle.height;
CSSLengthValue left = childRenderStyle.left;
CSSLengthValue right = childRenderStyle.right;
CSSLengthValue top = childRenderStyle.top;
CSSLengthValue bottom = childRenderStyle.bottom;
// 'left' + 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right'
// + 'border-right-width' + 'margin-right' + 'right' = width of containing block
if (left.isNotAuto && right.isNotAuto &&
(child is! RenderReplaced || width.isNotAuto)) {
if (marginLeft.isAuto) {
double leftValue = left.computedValue;
double rightValue = right.computedValue;
double remainingSpace = parentSize.width - child.boxSize!.width - leftValue - rightValue;
if (marginRight.isAuto) {
x = leftValue + remainingSpace / 2;
} else {
x = leftValue + remainingSpace;
}
}
}
if (top.isNotAuto && bottom.isNotAuto &&
(child is! RenderReplaced || height.isNotAuto)) {
if (marginTop.isAuto) {
double topValue = top.computedValue;
double bottomValue = bottom.computedValue;
double remainingSpace = parentSize.height - child.boxSize!.height - topValue - bottomValue;
if (marginBottom.isAuto) {
y = topValue + remainingSpace / 2;
} else {
y = topValue + remainingSpace;
}
}
}
return Offset(x ?? 0, y ?? 0);
}
class CSSPositionedLayout {
static RenderLayoutParentData getPositionParentData(RenderBoxModel renderBoxModel, RenderLayoutParentData parentData) {
CSSPositionType positionType = renderBoxModel.renderStyle.position;
parentData.isPositioned = positionType == CSSPositionType.absolute || positionType == CSSPositionType.fixed;
return parentData;
}
static Offset? getRelativeOffset(RenderStyle renderStyle) {
CSSLengthValue left = renderStyle.left;
CSSLengthValue right = renderStyle.right;
CSSLengthValue top = renderStyle.top;
CSSLengthValue bottom = renderStyle.bottom;
if (renderStyle.position == CSSPositionType.relative) {
double? dx;
double? dy;
if (left.isNotAuto) {
dx = renderStyle.left.computedValue;
} else if (right.isNotAuto) {
double _dx = renderStyle.right.computedValue;
dx = -_dx;
}
if (top.isNotAuto) {
dy = renderStyle.top.computedValue;
} else if (bottom.isNotAuto) {
double _dy = renderStyle.bottom.computedValue;
dy = -_dy;
}
if (dx != null || dy != null) {
return Offset(dx ?? 0, dy ?? 0);
}
}
return null;
}
static void applyRelativeOffset(Offset? relativeOffset, RenderBox renderBox) {
RenderLayoutParentData? boxParentData = renderBox.parentData as RenderLayoutParentData?;
if (boxParentData != null) {
Offset? styleOffset;
// Text node does not have relative offset
if (renderBox is RenderBoxModel) {
styleOffset = getRelativeOffset(renderBox.renderStyle);
}
if (relativeOffset != null) {
if (styleOffset != null) {
boxParentData.offset = relativeOffset.translate(styleOffset.dx, styleOffset.dy);
} else {
boxParentData.offset = relativeOffset;
}
} else {
boxParentData.offset = styleOffset!;
}
}
}
static bool isSticky(RenderBoxModel child) {
final renderStyle = child.renderStyle;
return renderStyle.position == CSSPositionType.sticky &&
(renderStyle.top.isNotAuto ||
renderStyle.left.isNotAuto ||
renderStyle.bottom.isNotAuto ||
renderStyle.right.isNotAuto);
}
/// Set horizontal offset of sticky element
static bool _applyStickyChildHorizontalOffset(
RenderBoxModel scrollContainer,
RenderBoxModel child,
Offset childOriginalOffset,
Offset childToScrollContainerOffset,
) {
bool isHorizontalFixed = false;
double offsetX = childOriginalOffset.dx;
double childWidth = child.boxSize!.width;
double scrollContainerWidth = scrollContainer.boxSize!.width;
// Dynamic offset to scroll container
double offsetLeftToScrollContainer = childToScrollContainerOffset.dx - scrollContainer.scrollLeft;
double offsetRightToScrollContainer = scrollContainerWidth - childWidth - offsetLeftToScrollContainer;
RenderStyle childRenderStyle = child.renderStyle;
RenderStyle? scrollContainerRenderStyle = scrollContainer.renderStyle;
if (childRenderStyle.left.isNotAuto) {
// Sticky offset to scroll container must include padding and border
double stickyLeft = childRenderStyle.left.computedValue +
scrollContainerRenderStyle.paddingLeft.computedValue +
scrollContainerRenderStyle.effectiveBorderLeftWidth.computedValue;
isHorizontalFixed = offsetLeftToScrollContainer < stickyLeft;
if (isHorizontalFixed) {
offsetX += stickyLeft - offsetLeftToScrollContainer;
// Sticky child can not exceed the left boundary of its parent container
RenderBoxModel parentContainer = child.parent as RenderBoxModel;
double maxOffsetX = parentContainer.boxSize!.width - childWidth;
if (offsetX > maxOffsetX) {
offsetX = maxOffsetX;
}
}
} else if (childRenderStyle.left.isNotAuto) {
// Sticky offset to scroll container must include padding and border
double stickyRight = childRenderStyle.right.computedValue +
scrollContainerRenderStyle.paddingRight.computedValue +
scrollContainerRenderStyle.effectiveBorderRightWidth.computedValue;
isHorizontalFixed = offsetRightToScrollContainer < stickyRight;
if (isHorizontalFixed) {
offsetX += offsetRightToScrollContainer - stickyRight;
// Sticky element can not exceed the right boundary of its parent container
double minOffsetX = 0;
if (offsetX < minOffsetX) {
offsetX = minOffsetX;
}
}
}
RenderLayoutParentData boxParentData = child.parentData as RenderLayoutParentData;
boxParentData.offset = Offset(
offsetX,
boxParentData.offset.dy,
);
return isHorizontalFixed;
}
/// Set vertical offset of sticky element
static bool _applyStickyChildVerticalOffset(
RenderBoxModel scrollContainer,
RenderBoxModel child,
Offset childOriginalOffset,
Offset childToScrollContainerOffset,
) {
bool isVerticalFixed = false;
double offsetY = childOriginalOffset.dy;
double childHeight = child.boxSize!.height;
double scrollContainerHeight = scrollContainer.boxSize!.height;
// Dynamic offset to scroll container
double offsetTopToScrollContainer = childToScrollContainerOffset.dy;
double offsetBottomToScrollContainer = scrollContainerHeight - childHeight - offsetTopToScrollContainer;
RenderStyle childRenderStyle = child.renderStyle;
RenderStyle? scrollContainerRenderStyle = scrollContainer.renderStyle;
if (childRenderStyle.top.isNotAuto) {
// Sticky offset to scroll container must include padding and border
double stickyTop = childRenderStyle.top.computedValue +
scrollContainerRenderStyle.paddingTop.computedValue +
scrollContainerRenderStyle.effectiveBorderTopWidth.computedValue;
isVerticalFixed = offsetTopToScrollContainer < stickyTop;
if (isVerticalFixed) {
offsetY += stickyTop - offsetTopToScrollContainer;
// Sticky child can not exceed the bottom boundary of its parent container
RenderBoxModel parentContainer = child.parent as RenderBoxModel;
double maxOffsetY = parentContainer.boxSize!.height - childHeight;
if (offsetY > maxOffsetY) {
offsetY = maxOffsetY;
}
}
} else if (childRenderStyle.bottom.isNotAuto) {
// Sticky offset to scroll container must include padding and border
double stickyBottom = childRenderStyle.bottom.computedValue +
scrollContainerRenderStyle.paddingBottom.computedValue +
scrollContainerRenderStyle.effectiveBorderBottomWidth.computedValue;
isVerticalFixed = offsetBottomToScrollContainer < stickyBottom;
if (isVerticalFixed) {
offsetY += offsetBottomToScrollContainer - stickyBottom;
// Sticky child can not exceed the upper boundary of its parent container
double minOffsetY = 0;
if (offsetY < minOffsetY) {
offsetY = minOffsetY;
}
}
}
RenderLayoutParentData boxParentData = child.parentData as RenderLayoutParentData;
boxParentData.offset = Offset(
boxParentData.offset.dx,
offsetY,
);
return isVerticalFixed;
}
/// Set sticky child offset according to scroll offset and direction,
/// when axisDirection param is null compute the both axis direction.
/// Sticky positioning is similar to relative positioning except
/// the offsets are automatically calculated in reference to the nearest scrollport.
/// https://www.w3.org/TR/css-position-3/#stickypos-insets
static void applyStickyChildOffset(RenderBoxModel scrollContainer, RenderBoxModel child) {
RenderPositionPlaceholder childRenderPositionHolder = child.renderPositionPlaceholder!;
RenderLayoutParentData childPlaceHolderParentData = childRenderPositionHolder.parentData as RenderLayoutParentData;
// Original offset of sticky child in relative status
Offset childOriginalOffset = childPlaceHolderParentData.offset;
// Offset of sticky child to scroll container
Offset childToScrollContainerOffset =
childRenderPositionHolder.localToGlobal(Offset.zero, ancestor: scrollContainer);
bool isVerticalFixed = false;
bool isHorizontalFixed = false;
RenderStyle childRenderStyle = child.renderStyle;
if (childRenderStyle.left.isNotAuto || childRenderStyle.right.isNotAuto) {
isHorizontalFixed = _applyStickyChildHorizontalOffset(
scrollContainer, child, childOriginalOffset, childToScrollContainerOffset
);
}
if (childRenderStyle.top.isNotAuto || childRenderStyle.bottom.isNotAuto) {
isVerticalFixed = _applyStickyChildVerticalOffset(
scrollContainer, child, childOriginalOffset, childToScrollContainerOffset
);
}
if (isVerticalFixed || isHorizontalFixed) {
// Change sticky status to fixed
child.stickyStatus = StickyPositionType.fixed;
child.markNeedsPaint();
} else {
// Change sticky status to relative
if (child.stickyStatus == StickyPositionType.fixed) {
child.stickyStatus = StickyPositionType.relative;
// Reset child offset to its original offset
child.markNeedsPaint();
}
}
}
static void layoutPositionedChild(
RenderBoxModel parent,
RenderBoxModel child,
{bool needsRelayout = false}
) {
BoxConstraints childConstraints = child.getConstraints();
// Whether child need to layout
bool isChildNeedsLayout = true;
if (child.hasSize &&
!needsRelayout &&
(childConstraints == child.constraints) &&
(!child.needsLayout)
) {
isChildNeedsLayout = false;
}
if (isChildNeedsLayout) {
late DateTime childLayoutStartTime;
if (kProfileMode) {
childLayoutStartTime = DateTime.now();
}
// Should create relayoutBoundary for positioned child.
child.layout(childConstraints, parentUsesSize: false);
if (kProfileMode) {
DateTime childLayoutEndTime = DateTime.now();
parent.childLayoutDuration += (childLayoutEndTime.microsecondsSinceEpoch - childLayoutStartTime.microsecondsSinceEpoch);
}
}
}
static void applyPositionedChildOffset(
RenderBoxModel parent,
RenderBoxModel child,
) {
final RenderLayoutParentData childParentData = child.parentData as RenderLayoutParentData;
Size? parentSize = parent.boxSize;
if (parent.isScrollingContentBox) {
RenderLayoutBox overflowContainerBox = parent.parent as RenderLayoutBox;
if(overflowContainerBox.widthSizeType == BoxSizeType.specified && overflowContainerBox.heightSizeType == BoxSizeType.specified) {
parentSize = Size(
overflowContainerBox.renderStyle.width.computedValue,
overflowContainerBox.renderStyle.height.computedValue
);
} else {
parentSize = parent.boxSize;
}
} else {
parentSize = parent.boxSize;
}
// Calc x,y by parentData.
double? x, y;
double? childMarginTop = 0;
double? childMarginBottom = 0;
double? childMarginLeft = 0;
double? childMarginRight = 0;
RenderStyle childRenderStyle = child.renderStyle;
childMarginTop = childRenderStyle.marginTop.computedValue;
childMarginBottom = childRenderStyle.marginBottom.computedValue;
childMarginLeft = childRenderStyle.marginLeft.computedValue;
childMarginRight = childRenderStyle.marginRight.computedValue;
// Offset to global coordinate system of base.
if (childParentData.isPositioned) {
EdgeInsets borderEdge = parent.renderStyle.border;
double borderLeft = borderEdge.left;
double borderRight = borderEdge.right;
double borderTop = borderEdge.top;
double borderBottom = borderEdge.bottom;
RenderStyle childRenderStyle = child.renderStyle;
Offset? placeholderOffset;
// ScrollTop and scrollLeft will be added to offset of renderBox in the paint stage
// for positioned fixed element.
if (child.renderStyle.position == CSSPositionType.fixed) {
Element rootElement = parent.renderStyle.target;
child.scrollingOffsetX = rootElement.scrollLeft;
child.scrollingOffsetY = rootElement.scrollTop;
}
double top;
if (childRenderStyle.top.isNotAuto) {
top = childRenderStyle.top.computedValue + borderTop + childMarginTop;
if (parent.isScrollingContentBox) {
RenderLayoutBox overflowContainingBox = parent.parent as RenderLayoutBox;
top -= overflowContainingBox.renderStyle.paddingTop.computedValue;
}
} else if (childRenderStyle.bottom.isNotAuto) {
top = parentSize!.height - child.boxSize!.height - borderBottom - childMarginBottom - childRenderStyle.bottom.computedValue;
if (parent.isScrollingContentBox) {
RenderLayoutBox overflowContainingBox = parent.parent as RenderLayoutBox;
top -= (overflowContainingBox.renderStyle.effectiveBorderTopWidth.computedValue + overflowContainingBox.renderStyle.effectiveBorderBottomWidth.computedValue
+ overflowContainingBox.renderStyle.paddingTop.computedValue);
}
} else {
placeholderOffset = _getPlaceholderToParentOffset(child.renderPositionPlaceholder!, parent);
// Use original offset in normal flow if no top and bottom is set.
top = placeholderOffset.dy;
}
double left;
if (childRenderStyle.left.isNotAuto) {
left = childRenderStyle.left.computedValue + borderLeft + childMarginLeft;
if (parent.isScrollingContentBox) {
RenderLayoutBox overflowContainingBox = parent.parent as RenderLayoutBox;
left -= overflowContainingBox.renderStyle.paddingLeft.computedValue;
}
} else if (childRenderStyle.right.isNotAuto) {
left = parentSize!.width - child.boxSize!.width - borderRight - childMarginRight - childRenderStyle.right.computedValue;
if (parent.isScrollingContentBox) {
RenderLayoutBox overflowContainingBox = parent.parent as RenderLayoutBox;
left -= (overflowContainingBox.renderStyle.effectiveBorderLeftWidth.computedValue + overflowContainingBox.renderStyle.effectiveBorderRightWidth.computedValue
+ overflowContainingBox.renderStyle.paddingLeft.computedValue);
}
} else {
placeholderOffset ??= _getPlaceholderToParentOffset(child.renderPositionPlaceholder!, parent);
// Use original offset in normal flow if no left and right is set.
left = placeholderOffset.dx;
}
x = left;
y = top;
}
Offset offset = _getAutoMarginPositionedElementOffset(x, y, child, parentSize!);
childParentData.offset = offset;
}
}