From 52552112ff4158d0cb29b287071462917855632a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 26 Mar 2015 14:08:10 -0700 Subject: [PATCH] Move fakesky.dart from examples to benchmark/resources It's not really an example of how to use Sky. It's a resource for benchmarking. TBR=raf@chromium.org Review URL: https://codereview.chromium.org/1030423002 --- .../flutter/benchmarks/resources/fakesky.dart | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 engine/src/flutter/benchmarks/resources/fakesky.dart diff --git a/engine/src/flutter/benchmarks/resources/fakesky.dart b/engine/src/flutter/benchmarks/resources/fakesky.dart new file mode 100644 index 0000000000..b927f1452f --- /dev/null +++ b/engine/src/flutter/benchmarks/resources/fakesky.dart @@ -0,0 +1,160 @@ +import 'dart:async'; + +void assertHasParentNode(Node n) { assert(n.parentNode != null); } +void assertHasParentNodes(List list) { + for (var n in list) { + assertHasParentNode(n); + } +} + +class Node { + + ParentNode parentNode; + Node nextSibling; + Node previousSibling; + Node(); + + void insertBefore(List nodes) { + int count = nodes.length; + while (count-- > 0) { + parentNode._insertBefore(nodes[count], this); + } + + assertHasParentNodes(nodes); + } + + remove() { + if (parentNode == null) { + return; + } + + if (nextSibling != null) { + nextSibling.previousSibling = previousSibling; + } else { + parentNode.lastChild = previousSibling; + } + + if (previousSibling != null) { + previousSibling.nextSibling = nextSibling; + } else { + parentNode.firstChild = nextSibling; + } + + parentNode = null; + nextSibling = null; + previousSibling = null; + } +} + +class Text extends Node { + String data; + Text(this.data) : super(); +} + +class ParentNode extends Node { + Node firstChild; + Node lastChild; + + ParentNode() : super(); + + Node setChild(Node node) { + firstChild = node; + lastChild = node; + node.parentNode = this; + assertHasParentNode(node); + return node; + } + + Node _insertBefore(Node node, Node ref) { + assert(ref == null || ref.parentNode == this); + + if (node.parentNode != null) { + node.remove(); + } + + node.parentNode = this; + + if (firstChild == null && lastChild == null) { + firstChild = node; + lastChild = node; + } else if (ref == null) { + node.previousSibling = lastChild; + lastChild.nextSibling = node; + lastChild = node; + } else { + if (ref == firstChild) { + assert(ref.previousSibling == null); + firstChild = node; + } + node.previousSibling = ref.previousSibling; + ref.previousSibling = node; + node.nextSibling = ref; + } + + assertHasParentNode(node); + return node; + } + + Node appendChild(Node node) { + return _insertBefore(node, null); + } +} + +class Element extends ParentNode { + void addEventListener(String type, EventListener listener, [bool useCapture = false]) {} + void removeEventListener(String type, EventListener listener) {} + void setAttribute(String name, [String value]) {} +} + +class Document extends ParentNode { + Document(); + Element createElement(String tagName) { + switch (tagName) { + case 'img' : return new HTMLImageElement(); + default : return new Element(); + } + } +} + +class HTMLImageElement extends Element { + HTMLImageElement(); + String src; + Object style = {}; +} + +class Event {} + +class PointerEvent extends Event {} +class GestureEvent extends Event {} +class WheelEvent extends Event {} + +typedef EventListener(Event event); + +void _callRAF(Function fn) { + fn(new DateTime.now().millisecondsSinceEpoch.toDouble()); +} + +class Window { + int requestAnimationFrame(Function fn) { + new Timer(const Duration(milliseconds: 16), () { + _callRAF(fn); + }); + return 1; + } + + void cancelAnimationFrame(int id) { + } +} + +Document document = new Document(); + +Window window = new Window(); + +class ClientRect { + double top; + double right; + double bottomr; + double left; + double width; + double height; +}