OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 library fn; | 5 library fn; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:sky' as sky; | 9 import 'dart:sky' as sky; |
10 import 'reflect.dart' as reflect; | 10 import 'reflect.dart' as reflect; |
(...skipping 26 matching lines...) Expand all Loading... |
37 _handlers.add(new EventHandler(type, listener)); | 37 _handlers.add(new EventHandler(type, listener)); |
38 } | 38 } |
39 | 39 |
40 void addAll(EventMap events) { | 40 void addAll(EventMap events) { |
41 _handlers.addAll(events._handlers); | 41 _handlers.addAll(events._handlers); |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 class Style { | 45 class Style { |
46 final String _className; | 46 final String _className; |
| 47 final String _styles; |
| 48 final List<String> _classList; |
| 49 |
47 static final Map<String, Style> _cache = new HashMap<String, Style>(); | 50 static final Map<String, Style> _cache = new HashMap<String, Style>(); |
48 | 51 |
49 static int nextStyleId = 1; | 52 static int _nextStyleId = 1; |
50 | 53 |
51 static String nextClassName(String styles) { | 54 static String _getNextClassName() { return "style${_nextStyleId++}"; } |
52 assert(sky.document != null); | |
53 String className = "style$nextStyleId"; | |
54 nextStyleId++; | |
55 | 55 |
56 sky.Element styleNode = sky.document.createElement('style'); | 56 factory Style(String styles) { |
57 styleNode.setChild(new sky.Text(".$className { $styles }")); | 57 return _getOrCreateStyle(styles, null); |
58 sky.document.appendChild(styleNode); | |
59 | |
60 return className; | |
61 } | 58 } |
62 | 59 |
63 factory Style(String styles) { | 60 Style extendFromStyles(String styles) { |
64 return _cache.putIfAbsent(styles, () { | 61 return _getOrCreateStyle(styles, _classList); |
65 return new Style._internal(nextClassName(styles)); | 62 } |
| 63 |
| 64 Style extend(Style other) { |
| 65 return this.extendFromStyles(other._styles); |
| 66 } |
| 67 |
| 68 static Style _getOrCreateStyle(String styles, List<String> inherited) { |
| 69 var cacheId = inherited != null ? "${inherited.join('.')}-$styles" |
| 70 : styles; |
| 71 return _cache.putIfAbsent(cacheId, () { |
| 72 var className = _getNextClassName(); |
| 73 List<String> classList = inherited == null ? [ className ] |
| 74 : new List.from(inherited)..add(className); |
| 75 |
| 76 var selector = ".${classList.join('.')}"; |
| 77 sky.Element styleNode = sky.document.createElement('style'); |
| 78 styleNode.setChild(new sky.Text("$selector { $styles }")); |
| 79 sky.document.appendChild(styleNode); |
| 80 |
| 81 return new Style._internal(styles, classList); |
66 }); | 82 }); |
67 } | 83 } |
68 | 84 |
69 Style._internal(this._className); | 85 Style._internal(this._styles, List<String> classList) |
| 86 : _classList = classList, |
| 87 _className = classList.join(' '); |
70 } | 88 } |
71 | 89 |
72 void _parentInsertBefore(sky.ParentNode parent, | 90 void _parentInsertBefore(sky.ParentNode parent, |
73 sky.Node node, | 91 sky.Node node, |
74 sky.Node ref) { | 92 sky.Node ref) { |
75 if (ref != null) { | 93 if (ref != null) { |
76 ref.insertBefore([node]); | 94 ref.insertBefore([node]); |
77 } else { | 95 } else { |
78 parent.appendChild(node); | 96 parent.appendChild(node); |
79 } | 97 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 Element get _emptyElement; | 151 Element get _emptyElement; |
134 | 152 |
135 String inlineStyle; | 153 String inlineStyle; |
136 | 154 |
137 List<Node> _children = null; | 155 List<Node> _children = null; |
138 String _class = ''; | 156 String _class = ''; |
139 | 157 |
140 Element({ | 158 Element({ |
141 Object key, | 159 Object key, |
142 List<Node> children, | 160 List<Node> children, |
143 List<Style> styles, | 161 Style style, |
144 | 162 |
145 this.inlineStyle | 163 this.inlineStyle |
146 }) : super(key:key) { | 164 }) : super(key:key) { |
147 _class = styles == null ? '' : styles.map((s) => s._className).join(' '); | 165 _class = style == null ? '' : style._className; |
148 _children = children == null ? _emptyList : children; | 166 _children = children == null ? _emptyList : children; |
149 | 167 |
150 if (_isInCheckedMode) { | 168 if (_isInCheckedMode) { |
151 _debugReportDuplicateIds(); | 169 _debugReportDuplicateIds(); |
152 } | 170 } |
153 } | 171 } |
154 | 172 |
155 void _remove() { | 173 void _remove() { |
156 super._remove(); | 174 super._remove(); |
157 if (_children != null) { | 175 if (_children != null) { |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 class Container extends Element { | 423 class Container extends Element { |
406 | 424 |
407 String get _tagName => 'div'; | 425 String get _tagName => 'div'; |
408 | 426 |
409 static final Container _emptyContainer = new Container(); | 427 static final Container _emptyContainer = new Container(); |
410 Element get _emptyElement => _emptyContainer; | 428 Element get _emptyElement => _emptyContainer; |
411 | 429 |
412 Container({ | 430 Container({ |
413 Object key, | 431 Object key, |
414 List<Node> children, | 432 List<Node> children, |
415 List<Style> styles, | 433 Style style, |
416 String inlineStyle | 434 String inlineStyle |
417 }) : super( | 435 }) : super( |
418 key: key, | 436 key: key, |
419 children: children, | 437 children: children, |
420 styles: styles, | 438 style: style, |
421 inlineStyle: inlineStyle | 439 inlineStyle: inlineStyle |
422 ); | 440 ); |
423 } | 441 } |
424 | 442 |
425 class Image extends Element { | 443 class Image extends Element { |
426 | 444 |
427 String get _tagName => 'img'; | 445 String get _tagName => 'img'; |
428 | 446 |
429 static final Image _emptyImage = new Image(); | 447 static final Image _emptyImage = new Image(); |
430 Element get _emptyElement => _emptyImage; | 448 Element get _emptyElement => _emptyImage; |
431 | 449 |
432 String src; | 450 String src; |
433 int width; | 451 int width; |
434 int height; | 452 int height; |
435 | 453 |
436 Image({ | 454 Image({ |
437 Object key, | 455 Object key, |
438 List<Node> children, | 456 List<Node> children, |
439 List<Style> styles, | 457 Style style, |
440 String inlineStyle, | 458 String inlineStyle, |
441 this.width, | 459 this.width, |
442 this.height, | 460 this.height, |
443 this.src | 461 this.src |
444 }) : super( | 462 }) : super( |
445 key: key, | 463 key: key, |
446 children: children, | 464 children: children, |
447 styles: styles, | 465 style: style, |
448 inlineStyle: inlineStyle | 466 inlineStyle: inlineStyle |
449 ); | 467 ); |
450 | 468 |
451 void _syncNode([Element old]) { | 469 void _syncNode([Element old]) { |
452 super._syncNode(old); | 470 super._syncNode(old); |
453 | 471 |
454 Image oldImage = old != null ? old : _emptyImage; | 472 Image oldImage = old != null ? old : _emptyImage; |
455 sky.HTMLImageElement skyImage = _root as sky.HTMLImageElement; | 473 sky.HTMLImageElement skyImage = _root as sky.HTMLImageElement; |
456 if (src != oldImage.src) { | 474 if (src != oldImage.src) { |
457 skyImage.src = src; | 475 skyImage.src = src; |
(...skipping 15 matching lines...) Expand all Loading... |
473 static final Anchor _emptyAnchor = new Anchor(); | 491 static final Anchor _emptyAnchor = new Anchor(); |
474 Element get _emptyElement => _emptyAnchor; | 492 Element get _emptyElement => _emptyAnchor; |
475 | 493 |
476 String href; | 494 String href; |
477 int width; | 495 int width; |
478 int height; | 496 int height; |
479 | 497 |
480 Anchor({ | 498 Anchor({ |
481 Object key, | 499 Object key, |
482 List<Node> children, | 500 List<Node> children, |
483 List<Style> styles, | 501 Style style, |
484 String inlineStyle, | 502 String inlineStyle, |
485 this.width, | 503 this.width, |
486 this.height, | 504 this.height, |
487 this.href | 505 this.href |
488 }) : super( | 506 }) : super( |
489 key: key, | 507 key: key, |
490 children: children, | 508 children: children, |
491 styles: styles, | 509 style: style, |
492 inlineStyle: inlineStyle | 510 inlineStyle: inlineStyle |
493 ); | 511 ); |
494 | 512 |
495 void _syncNode([Element old]) { | 513 void _syncNode([Element old]) { |
496 Anchor oldAnchor = old != null ? old as Anchor : _emptyAnchor; | 514 Anchor oldAnchor = old != null ? old as Anchor : _emptyAnchor; |
497 super._syncNode(oldAnchor); | 515 super._syncNode(oldAnchor); |
498 | 516 |
499 sky.HTMLAnchorElement skyAnchor = _root as sky.HTMLAnchorElement; | 517 sky.HTMLAnchorElement skyAnchor = _root as sky.HTMLAnchorElement; |
500 if (href != oldAnchor.href) { | 518 if (href != oldAnchor.href) { |
501 skyAnchor.href = href; | 519 skyAnchor.href = href; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 | 696 |
679 _sync(null, _host, null); | 697 _sync(null, _host, null); |
680 assert(_root is sky.Node); | 698 assert(_root is sky.Node); |
681 | 699 |
682 sw.stop(); | 700 sw.stop(); |
683 if (_shouldLogRenderDuration) | 701 if (_shouldLogRenderDuration) |
684 print("Initial build: ${sw.elapsedMicroseconds} microseconds"); | 702 print("Initial build: ${sw.elapsedMicroseconds} microseconds"); |
685 }); | 703 }); |
686 } | 704 } |
687 } | 705 } |
OLD | NEW |