-
Notifications
You must be signed in to change notification settings - Fork 302
/
border.dart
434 lines (377 loc) · 13.6 KB
/
border.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
/*
* Copyright (C) 2019-present Alibaba Inc. All rights reserved.
* Author: Kraken Team.
*/
import 'dart:core';
import 'dart:ui';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:kraken/css.dart';
final RegExp _splitRegExp = RegExp(r'\s+');
// Initial border value: medium
final CSSLengthValue _mediumWidth = CSSLengthValue(3, CSSLengthType.PX);
enum CSSBorderStyleType {
none,
hidden,
dotted,
dashed,
solid,
double,
groove,
ridge,
inset,
outset,
}
mixin CSSBorderMixin on RenderStyle {
// Effective border widths. These are used to calculate the
// dimensions of the border box.
@override
EdgeInsets get border {
// If has border, render padding should subtracting the edge of the border
return EdgeInsets.fromLTRB(
effectiveBorderLeftWidth.computedValue,
effectiveBorderTopWidth.computedValue,
effectiveBorderRightWidth.computedValue,
effectiveBorderBottomWidth.computedValue,
);
}
Size wrapBorderSize(Size innerSize) {
return Size(border.left + innerSize.width + border.right,
border.top + innerSize.height + border.bottom);
}
BoxConstraints deflateBorderConstraints(BoxConstraints constraints) {
return constraints.deflate(border);
}
@override
List<BorderSide>? get borderSides {
BorderSide? leftSide = CSSBorderSide._getBorderSide(this, CSSBorderSide.LEFT);
BorderSide? topSide = CSSBorderSide._getBorderSide(this, CSSBorderSide.TOP);
BorderSide? rightSide = CSSBorderSide._getBorderSide(this, CSSBorderSide.RIGHT);
BorderSide? bottomSide = CSSBorderSide._getBorderSide(this, CSSBorderSide.BOTTOM);
bool hasBorder = leftSide != null ||
topSide != null ||
rightSide != null ||
bottomSide != null;
return hasBorder ? [
leftSide ?? CSSBorderSide.none,
topSide ?? CSSBorderSide.none,
rightSide ?? CSSBorderSide.none,
bottomSide ?? CSSBorderSide.none] : null;
}
/// Shorted border property:
/// border:<line-width> || <line-style> || <color>
/// (<line-width> = <length> | thin | medium | thick), support length now.
/// Seperated properties:
/// borderWidth: <line-width>{1,4}
/// borderStyle: none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset
/// (PS. Only support solid now.)
/// borderColor: <color>
/// Border-width = <length> | thin | medium | thick
CSSLengthValue? _borderTopWidth;
set borderTopWidth(CSSLengthValue? value) {
if (value == _borderTopWidth) return;
_borderTopWidth = value;
renderBoxModel?.markNeedsLayout();
}
@override
CSSLengthValue? get borderTopWidth => _borderTopWidth;
@override
CSSLengthValue get effectiveBorderTopWidth => borderTopStyle == BorderStyle.none ? CSSLengthValue.zero : (_borderTopWidth ?? _mediumWidth);
CSSLengthValue? _borderRightWidth;
set borderRightWidth(CSSLengthValue? value) {
if (value == _borderRightWidth) return;
_borderRightWidth = value;
renderBoxModel?.markNeedsLayout();
}
@override
CSSLengthValue? get borderRightWidth => _borderRightWidth;
@override
CSSLengthValue get effectiveBorderRightWidth => borderRightStyle == BorderStyle.none ? CSSLengthValue.zero : (_borderRightWidth ?? _mediumWidth);
CSSLengthValue? _borderBottomWidth;
set borderBottomWidth(CSSLengthValue? value) {
if (value == _borderBottomWidth) return;
_borderBottomWidth = value;
renderBoxModel?.markNeedsLayout();
}
@override
CSSLengthValue? get borderBottomWidth => _borderBottomWidth;
@override
CSSLengthValue get effectiveBorderBottomWidth => borderBottomStyle == BorderStyle.none ? CSSLengthValue.zero : (_borderBottomWidth ?? _mediumWidth);
CSSLengthValue? _borderLeftWidth;
set borderLeftWidth(CSSLengthValue? value) {
if (value == _borderLeftWidth) return;
_borderLeftWidth = value;
renderBoxModel?.markNeedsLayout();
}
@override
CSSLengthValue? get borderLeftWidth => _borderLeftWidth;
@override
CSSLengthValue get effectiveBorderLeftWidth => borderLeftStyle == BorderStyle.none ? CSSLengthValue.zero : (_borderLeftWidth ?? _mediumWidth);
/// Border-color
@override
Color get borderTopColor => _borderTopColor ?? currentColor;
Color? _borderTopColor;
set borderTopColor(Color? value) {
if (value == _borderTopColor) return;
_borderTopColor = value;
renderBoxModel?.markNeedsPaint();
}
@override
Color get borderRightColor => _borderRightColor ?? currentColor;
Color? _borderRightColor;
set borderRightColor(Color? value) {
if (value == _borderRightColor) return;
_borderRightColor = value;
renderBoxModel?.markNeedsPaint();
}
@override
Color get borderBottomColor => _borderBottomColor ?? currentColor;
Color? _borderBottomColor;
set borderBottomColor(Color? value) {
if (value == _borderBottomColor) return;
_borderBottomColor = value;
renderBoxModel?.markNeedsPaint();
}
@override
Color get borderLeftColor => _borderLeftColor ?? currentColor;
Color? _borderLeftColor;
set borderLeftColor(Color? value) {
if (value == _borderLeftColor) return;
_borderLeftColor = value;
renderBoxModel?.markNeedsPaint();
}
/// Border-style
@override
BorderStyle get borderTopStyle => _borderTopStyle ?? BorderStyle.none;
BorderStyle? _borderTopStyle;
set borderTopStyle(BorderStyle? value) {
if (value == _borderTopStyle) return;
_borderTopStyle = value;
renderBoxModel?.markNeedsPaint();
}
@override
BorderStyle get borderRightStyle => _borderRightStyle ?? BorderStyle.none;
BorderStyle? _borderRightStyle;
set borderRightStyle(BorderStyle? value) {
if (value == _borderRightStyle) return;
_borderRightStyle = value;
renderBoxModel?.markNeedsPaint();
}
@override
BorderStyle get borderBottomStyle => _borderBottomStyle ?? BorderStyle.none;
BorderStyle? _borderBottomStyle;
set borderBottomStyle(BorderStyle? value) {
if (value == _borderBottomStyle) return;
_borderBottomStyle = value;
renderBoxModel?.markNeedsPaint();
}
BorderStyle? _borderLeftStyle;
@override
BorderStyle get borderLeftStyle => _borderLeftStyle ?? BorderStyle.none;
set borderLeftStyle(BorderStyle? value) {
if (value == _borderLeftStyle) return;
_borderLeftStyle = value;
renderBoxModel?.markNeedsPaint();
}
}
class CSSBorderSide {
// border default width 3.0
static double defaultBorderWidth = 3.0;
static Color defaultBorderColor = CSSColor.initial;
static const String LEFT = 'Left';
static const String RIGHT = 'Right';
static const String TOP = 'Top';
static const String BOTTOM = 'Bottom';
static BorderStyle resolveBorderStyle(String input) {
BorderStyle borderStyle;
switch (input) {
case SOLID:
borderStyle = BorderStyle.solid;
break;
case NONE:
default:
borderStyle = BorderStyle.none;
break;
}
return borderStyle;
}
static final CSSLengthValue _thinWidth = CSSLengthValue(1, CSSLengthType.PX);
static final CSSLengthValue _mediumWidth = CSSLengthValue(3, CSSLengthType.PX);
static final CSSLengthValue _thickWidth = CSSLengthValue(5, CSSLengthType.PX);
static CSSLengthValue? resolveBorderWidth(String input, RenderStyle renderStyle, String propertyName) {
// https://drafts.csswg.org/css2/#border-width-properties
// The interpretation of the first three values depends on the user agent.
// The following relationships must hold, however:
// thin ≤ medium ≤ thick.
CSSLengthValue? borderWidth;
switch (input) {
case THIN:
borderWidth = _thinWidth;
break;
case MEDIUM:
borderWidth = _mediumWidth;
break;
case THICK:
borderWidth = _thickWidth;
break;
default:
borderWidth = CSSLength.parseLength(input, renderStyle, propertyName);
}
return borderWidth;
}
static bool isValidBorderStyleValue(String value) {
return value == SOLID || value == NONE;
}
static bool isValidBorderWidthValue(String value) {
return CSSLength.isNonNegativeLength(value) || value == THIN || value == MEDIUM || value == THICK;
}
static BorderSide none = BorderSide(color: defaultBorderColor, width: 0.0, style: BorderStyle.none);
static BorderSide? _getBorderSide(RenderStyle renderStyle, String side) {
BorderStyle? borderStyle;
CSSLengthValue? borderWidth;
Color? borderColor;
switch (side) {
case LEFT:
borderStyle = renderStyle.borderLeftStyle;
borderWidth = renderStyle.effectiveBorderLeftWidth;
borderColor = renderStyle.borderLeftColor;
break;
case RIGHT:
borderStyle = renderStyle.borderRightStyle;
borderWidth = renderStyle.effectiveBorderRightWidth;
borderColor = renderStyle.borderRightColor;
break;
case TOP:
borderStyle = renderStyle.borderTopStyle;
borderWidth = renderStyle.effectiveBorderTopWidth;
borderColor = renderStyle.borderTopColor;
break;
case BOTTOM:
borderStyle = renderStyle.borderBottomStyle;
borderWidth = renderStyle.effectiveBorderBottomWidth;
borderColor = renderStyle.borderBottomColor;
break;
}
// Flutter will print border event if width is 0.0. So we needs to set borderStyle to none to prevent this.
if (borderStyle == BorderStyle.none || borderWidth!.isZero) {
return null;
} else {
return BorderSide(
width: borderWidth.computedValue,
style: borderStyle!,
color: borderColor!
);
}
}
}
class CSSBorderRadius {
final CSSLengthValue x;
final CSSLengthValue y;
const CSSBorderRadius(this.x, this.y);
static CSSBorderRadius zero = CSSBorderRadius(CSSLengthValue.zero, CSSLengthValue.zero);
static CSSBorderRadius? parseBorderRadius(String radius, RenderStyle renderStyle, String propertyName) {
if (radius.isNotEmpty) {
// border-top-left-radius: horizontal vertical
List<String> values = radius.split(_splitRegExp);
if (values.length == 1 || values.length == 2) {
String horizontalRadius = values[0];
// The first value is the horizontal radius, the second the vertical radius.
// If the second value is omitted it is copied from the first.
// https://www.w3.org/TR/css-backgrounds-3/#border-radius
String verticalRadius = values.length == 1 ? values[0] : values[1];
CSSLengthValue x = CSSLength.parseLength(horizontalRadius, renderStyle, propertyName, Axis.horizontal);
CSSLengthValue y = CSSLength.parseLength(verticalRadius, renderStyle, propertyName, Axis.vertical);
return CSSBorderRadius(x, y);
}
}
return null;
}
Radius get computedRadius {
return Radius.elliptical(x.computedValue, y.computedValue);
}
}
class KrakenBoxShadow extends BoxShadow {
/// Creates a box shadow.
///
/// By default, the shadow is solid black with zero [offset], [blurRadius],
/// and [spreadRadius].
const KrakenBoxShadow({
Color color = const Color(0xFF000000),
Offset offset = Offset.zero,
double blurRadius = 0.0,
double spreadRadius = 0.0,
this.inset = false,
}) : super(color: color, offset: offset, blurRadius: blurRadius, spreadRadius: spreadRadius);
final bool inset;
}
// ignore: must_be_immutable
class CSSBoxShadow {
CSSBoxShadow({
this.color,
this.offsetX,
this.offsetY,
this.blurRadius,
this.spreadRadius,
this.inset = false,
});
bool inset = false;
Color? color;
CSSLengthValue? offsetX;
CSSLengthValue? offsetY;
CSSLengthValue? blurRadius;
CSSLengthValue? spreadRadius;
KrakenBoxShadow get computedBoxShadow {
color ??= const Color(0xFF000000);
offsetX ??= CSSLengthValue.zero;
offsetY ??= CSSLengthValue.zero;
blurRadius ??= CSSLengthValue.zero;
spreadRadius ??= CSSLengthValue.zero;
return KrakenBoxShadow(
color: color!,
offset: Offset(offsetX!.computedValue, offsetY!.computedValue),
blurRadius: blurRadius!.computedValue,
spreadRadius: spreadRadius!.computedValue,
inset: inset,
);
}
static List<CSSBoxShadow>? parseBoxShadow(String present, RenderStyle renderStyle, String propertyName) {
var shadows = CSSStyleProperty.getShadowValues(present);
if (shadows != null) {
List<CSSBoxShadow>? boxShadow = [];
for (var shadowDefinitions in shadows) {
// Specifies the color of the shadow. If the color is absent, it defaults to currentColor.
String colorDefinition = shadowDefinitions[0] ?? CURRENT_COLOR;
Color? color = CSSColor.resolveColor(colorDefinition, renderStyle, propertyName);
CSSLengthValue? offsetX;
if (shadowDefinitions[1] != null) {
offsetX = CSSLength.parseLength(shadowDefinitions[1]!, renderStyle, propertyName);
}
CSSLengthValue? offsetY;
if (shadowDefinitions[2] != null) {
offsetY = CSSLength.parseLength(shadowDefinitions[2]!, renderStyle, propertyName);
}
CSSLengthValue? blurRadius;
if (shadowDefinitions[3] != null) {
blurRadius = CSSLength.parseLength(shadowDefinitions[3]!, renderStyle, propertyName);
}
CSSLengthValue? spreadRadius;
if (shadowDefinitions[4] != null) {
spreadRadius = CSSLength.parseLength(shadowDefinitions[4]!, renderStyle, propertyName);
}
bool inset = shadowDefinitions[5] == INSET;
if (color != null) {
boxShadow.add(CSSBoxShadow(
offsetX: offsetX,
offsetY: offsetY,
blurRadius: blurRadius,
spreadRadius: spreadRadius,
color: color,
inset: inset,
));
}
}
return boxShadow;
}
return null;
}
}