object.dart 15.7 KB
Newer Older
1 2 3 4 5
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../node.dart';
A
Adam Barth 已提交
6
import '../scheduler.dart' as scheduler;
7
import 'dart:sky' as sky;
8 9
import 'dart:sky' show Point, Size, Rect, Color, Paint, Path;
export 'dart:sky' show Point, Size, Rect, Color, Paint, Path;
10 11 12 13 14 15 16 17 18 19

class ParentData {
  void detach() {
    detachSiblings();
  }
  void detachSiblings() { } // workaround for lack of inter-class mixins in Dart
  void merge(ParentData other) {
    // override this in subclasses to merge in data from other into this
    assert(other.runtimeType == this.runtimeType);
  }
H
Hixie 已提交
20
  String toString() => '<none>';
21 22 23 24
}

const kLayoutDirections = 4;

H
Hixie 已提交
25 26
class RenderObjectDisplayList extends sky.PictureRecorder {
  RenderObjectDisplayList(double width, double height) : super(width, height);
27
  void paintChild(RenderObject child, Point position) {
28 29
    translate(position.x, position.y);
    child.paint(this);
A
Adam Barth 已提交
30
    translate(-position.x, -position.y);
31 32 33
  }
}

H
Hixie 已提交
34
abstract class RenderObject extends AbstractNode {
35 36 37

  // LAYOUT

H
Hixie 已提交
38
  // parentData is only for use by the RenderObject that actually lays this
39 40
  // node out, and any other nodes who happen to know exactly what
  // kind of node that is.
41
  dynamic parentData; // TODO(ianh): change the type of this back to ParentData once the analyzer is cleverer
H
Hixie 已提交
42
  void setParentData(RenderObject child) {
43
    // override this to setup .parentData correctly for your class
44
    assert(!debugDoingLayout);
45
    assert(!debugDoingPaint);
46 47 48 49
    if (child.parentData is! ParentData)
      child.parentData = new ParentData();
  }

H
Hixie 已提交
50
  void adoptChild(RenderObject child) { // only for use by subclasses
51
    // call this whenever you decide a node is a child
52
    assert(!debugDoingLayout);
53
    assert(!debugDoingPaint);
54 55 56
    assert(child != null);
    setParentData(child);
    super.adoptChild(child);
H
Hixie 已提交
57
    markNeedsLayout();
58
  }
H
Hixie 已提交
59
  void dropChild(RenderObject child) { // only for use by subclasses
60
    assert(!debugDoingLayout);
61
    assert(!debugDoingPaint);
62 63 64
    assert(child != null);
    assert(child.parentData != null);
    child.parentData.detach();
H
Hixie 已提交
65 66 67 68
    if (child._relayoutSubtreeRoot != child) {
      child._relayoutSubtreeRoot = null;
      child._needsLayout = true;
    }
69
    super.dropChild(child);
H
Hixie 已提交
70
    markNeedsLayout();
71 72
  }

H
Hixie 已提交
73
  static List<RenderObject> _nodesNeedingLayout = new List<RenderObject>();
74
  static bool _debugDoingLayout = false;
75
  static bool get debugDoingLayout => _debugDoingLayout;
76 77
  bool _needsLayout = true;
  bool get needsLayout => _needsLayout;
H
Hixie 已提交
78
  RenderObject _relayoutSubtreeRoot;
79 80 81 82 83
  dynamic _constraints;
  dynamic get constraints => _constraints;
  bool debugAncestorsAlreadyMarkedNeedsLayout() {
    if (_relayoutSubtreeRoot == null)
      return true; // we haven't yet done layout even once, so there's nothing for us to do
H
Hixie 已提交
84
    RenderObject node = this;
85 86 87
    while (node != _relayoutSubtreeRoot) {
      assert(node._relayoutSubtreeRoot == _relayoutSubtreeRoot);
      assert(node.parent != null);
H
Hixie 已提交
88
      node = node.parent as RenderObject;
89 90 91 92 93 94 95
      if (!node._needsLayout)
        return false;
    }
    assert(node._relayoutSubtreeRoot == node);
    return true;
  }
  void markNeedsLayout() {
96
    assert(!debugDoingLayout);
97 98 99 100 101 102 103 104
    assert(!debugDoingPaint);
    if (_needsLayout) {
      assert(debugAncestorsAlreadyMarkedNeedsLayout());
      return;
    }
    _needsLayout = true;
    assert(_relayoutSubtreeRoot != null);
    if (_relayoutSubtreeRoot != this) {
105
      final parent = this.parent; // TODO(ianh): Remove this once the analyzer is cleverer
H
Hixie 已提交
106
      assert(parent is RenderObject);
107
      parent.markNeedsLayout();
108
      assert(parent == this.parent); // TODO(ianh): Remove this once the analyzer is cleverer
109 110
    } else {
      _nodesNeedingLayout.add(this);
111
      scheduler.ensureVisualUpdate();
112 113
    }
  }
A
Adam Barth 已提交
114 115 116 117 118 119 120 121
  void scheduleInitialLayout() {
    assert(attached);
    assert(parent == null);
    assert(_relayoutSubtreeRoot == null);
    _relayoutSubtreeRoot = this;
    _nodesNeedingLayout.add(this);
    scheduler.ensureVisualUpdate();
  }
122 123
  static void flushLayout() {
    _debugDoingLayout = true;
H
Hixie 已提交
124 125
    List<RenderObject> dirtyNodes = _nodesNeedingLayout;
    _nodesNeedingLayout = new List<RenderObject>();
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    dirtyNodes..sort((a, b) => a.depth - b.depth)..forEach((node) {
      if (node._needsLayout && node.attached)
        node._doLayout();
    });
    _debugDoingLayout = false;
  }
  void _doLayout() {
    try {
      assert(_relayoutSubtreeRoot == this);
      performLayout();
    } catch (e, stack) {
      print('Exception raised during layout of ${this}: ${e}');
      print(stack);
      return;
    }
    _needsLayout = false;
  }
  void layout(dynamic constraints, { bool parentUsesSize: false }) {
144
    final parent = this.parent; // TODO(ianh): Remove this once the analyzer is cleverer
H
Hixie 已提交
145 146
    RenderObject relayoutSubtreeRoot;
    if (!parentUsesSize || sizedByParent || parent is! RenderObject)
147 148 149
      relayoutSubtreeRoot = this;
    else
      relayoutSubtreeRoot = parent._relayoutSubtreeRoot;
150
    assert(parent == this.parent); // TODO(ianh): Remove this once the analyzer is cleverer
151 152 153 154 155 156 157 158 159
    if (!needsLayout && constraints == _constraints && relayoutSubtreeRoot == _relayoutSubtreeRoot)
      return;
    _constraints = constraints;
    _relayoutSubtreeRoot = relayoutSubtreeRoot;
    if (sizedByParent)
      performResize();
    performLayout();
    _needsLayout = false;
    markNeedsPaint();
160
    assert(parent == this.parent); // TODO(ianh): Remove this once the analyzer is cleverer
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
  }
  bool get sizedByParent => false; // return true if the constraints are the only input to the sizing algorithm (in particular, child nodes have no impact)
  void performResize(); // set the local dimensions, using only the constraints (only called if sizedByParent is true)
  void performLayout();
    // Override this to perform relayout without your parent's
    // involvement.
    //
    // This is called during layout. If sizedByParent is true, then
    // performLayout() should not change your dimensions, only do that
    // in performResize(). If sizedByParent is false, then set both
    // your dimensions and do your children's layout here.
    //
    // When calling layout() on your children, pass in
    // "parentUsesSize: true" if your size or layout is dependent on
    // your child's size.

  // when the parent has rotated (e.g. when the screen has been turned
  // 90 degrees), immediately prior to layout() being called for the
  // new dimensions, rotate() is called with the old and new angles.
  // The next time paint() is called, the coordinate space will have
  // been rotated N quarter-turns clockwise, where:
  //    N = newAngle-oldAngle
  // ...but the rendering is expected to remain the same, pixel for
  // pixel, on the output device. Then, the layout() method or
  // equivalent will be invoked.

  void rotate({
    int oldAngle, // 0..3
    int newAngle, // 0..3
    Duration time
  }) { }


  // PAINTING

  static bool debugDoingPaint = false;
  void markNeedsPaint() {
    assert(!debugDoingPaint);
A
Adam Barth 已提交
199
    scheduler.ensureVisualUpdate();
200
  }
H
Hixie 已提交
201
  void paint(RenderObjectDisplayList canvas) { }
202 203


A
Adam Barth 已提交
204
  // EVENTS
205

206
  void handleEvent(sky.Event event, HitTestEntry entry) {
207
    // override this if you have a client, to hand it to the client
A
Adam Barth 已提交
208
    // override this if you want to do anything with the event
209 210
  }

A
Adam Barth 已提交
211 212 213

  // HIT TESTING

H
Hixie 已提交
214
  // RenderObject subclasses are expected to have a method like the
215 216
  // following (with the signature being whatever passes for coordinates
  // for this particular class):
217
  // bool hitTest(HitTestResult result, { Point position }) {
218 219 220 221 222 223 224 225 226 227 228 229 230
  //   // If (x,y) is not inside this node, then return false. (You
  //   // can assume that the given coordinate is inside your
  //   // dimensions. You only need to check this if you're an
  //   // irregular shape, e.g. if you have a hole.)
  //   // Otherwise:
  //   // For each child that intersects x,y, in z-order starting from the top,
  //   // call hitTest() for that child, passing it /result/, and the coordinates
  //   // converted to the child's coordinate origin, and stop at the first child
  //   // that returns true.
  //   // Then, add yourself to /result/, and return true.
  // }
  // You must not add yourself to /result/ if you return false.

H
Hixie 已提交
231 232

  String toString([String prefix = '']) {
233 234 235 236 237 238 239 240 241 242 243 244 245 246
    String header = '${runtimeType}';
    if (_relayoutSubtreeRoot != null && _relayoutSubtreeRoot != this) {
      int count = 1;
      RenderObject target = parent;
      while (target != null && target != _relayoutSubtreeRoot) {
        target = target.parent as RenderObject;
        count += 1;
      }
      header += ' relayoutSubtreeRoot=up$count';
    }
    if (_needsLayout)
      header += ' NEEDS-LAYOUT';
    if (!attached)
      header += ' DETACHED';
H
Hixie 已提交
247
    prefix += '  ';
248
    return '${header}\n${debugDescribeSettings(prefix)}${debugDescribeChildren(prefix)}';
H
Hixie 已提交
249
  }
250
  String debugDescribeSettings(String prefix) => '${prefix}parentData: ${parentData}\n';
H
Hixie 已提交
251 252
  String debugDescribeChildren(String prefix) => '';

253 254
}

255 256 257 258 259
class HitTestEntry {
  const HitTestEntry(this.target);

  final RenderObject target;
}
260

261 262
class HitTestResult {
  final List<HitTestEntry> path = new List<HitTestEntry>();
263

264 265
  void add(HitTestEntry data) {
    path.add(data);
266 267 268 269 270 271
  }
}


// GENERIC MIXIN FOR RENDER NODES WITH ONE CHILD

272
abstract class RenderObjectWithChildMixin<ChildType extends RenderObject> implements RenderObject {
273 274 275 276 277 278 279 280 281
  ChildType _child;
  ChildType get child => _child;
  void set child (ChildType value) {
    if (_child != null)
      dropChild(_child);
    _child = value;
    if (_child != null)
      adoptChild(_child);
  }
282 283 284 285 286 287 288 289
  void attachChildren() {
    if (_child != null)
      _child.attach();
  }
  void detachChildren() {
    if (_child != null)
      _child.detach();
  }
290 291 292 293 294
  String debugDescribeChildren(String prefix) {
    if (child != null)
      return '${prefix}child: ${child.toString(prefix)}';
    return '';
  }
295 296 297 298 299
}


// GENERIC MIXIN FOR RENDER NODES WITH A LIST OF CHILDREN

H
Hixie 已提交
300
abstract class ContainerParentDataMixin<ChildType extends RenderObject> {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  ChildType previousSibling;
  ChildType nextSibling;
  void detachSiblings() {
    if (previousSibling != null) {
      assert(previousSibling.parentData is ContainerParentDataMixin<ChildType>);
      assert(previousSibling != this);
      assert(previousSibling.parentData.nextSibling == this);
      previousSibling.parentData.nextSibling = nextSibling;
    }
    if (nextSibling != null) {
      assert(nextSibling.parentData is ContainerParentDataMixin<ChildType>);
      assert(nextSibling != this);
      assert(nextSibling.parentData.previousSibling == this);
      nextSibling.parentData.previousSibling = previousSibling;
    }
    previousSibling = null;
    nextSibling = null;
  }
}

H
Hixie 已提交
321
abstract class ContainerRenderObjectMixin<ChildType extends RenderObject, ParentDataType extends ContainerParentDataMixin<ChildType>> implements RenderObject {
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
  // abstract class that has only InlineNode children

  bool _debugUltimatePreviousSiblingOf(ChildType child, { ChildType equals }) {
    assert(child.parentData is ParentDataType);
    while (child.parentData.previousSibling != null) {
      assert(child.parentData.previousSibling != child);
      child = child.parentData.previousSibling;
      assert(child.parentData is ParentDataType);
    }
    return child == equals;
  }
  bool _debugUltimateNextSiblingOf(ChildType child, { ChildType equals }) {
    assert(child.parentData is ParentDataType);
    while (child.parentData.nextSibling != null) {
      assert(child.parentData.nextSibling != child);
      child = child.parentData.nextSibling;
      assert(child.parentData is ParentDataType);
    }
    return child == equals;
  }

  ChildType _firstChild;
  ChildType _lastChild;
  void add(ChildType child, { ChildType before }) {
    assert(child != this);
    assert(before != this);
    assert(child != before);
    assert(child != _firstChild);
    assert(child != _lastChild);
    adoptChild(child);
    assert(child.parentData is ParentDataType);
    assert(child.parentData.nextSibling == null);
    assert(child.parentData.previousSibling == null);
    if (before == null) {
      // append at the end (_lastChild)
      child.parentData.previousSibling = _lastChild;
      if (_lastChild != null) {
        assert(_lastChild.parentData is ParentDataType);
        _lastChild.parentData.nextSibling = child;
      }
      _lastChild = child;
      if (_firstChild == null)
        _firstChild = child;
    } else {
      assert(_firstChild != null);
      assert(_lastChild != null);
      assert(_debugUltimatePreviousSiblingOf(before, equals: _firstChild));
      assert(_debugUltimateNextSiblingOf(before, equals: _lastChild));
      assert(before.parentData is ParentDataType);
      if (before.parentData.previousSibling == null) {
        // insert at the start (_firstChild); we'll end up with two or more children
        assert(before == _firstChild);
        child.parentData.nextSibling = before;
        before.parentData.previousSibling = child;
        _firstChild = child;
      } else {
        // insert in the middle; we'll end up with three or more children
        // set up links from child to siblings
        child.parentData.previousSibling = before.parentData.previousSibling;
        child.parentData.nextSibling = before;
        // set up links from siblings to child
        assert(child.parentData.previousSibling.parentData is ParentDataType);
        assert(child.parentData.nextSibling.parentData is ParentDataType);
        child.parentData.previousSibling.parentData.nextSibling = child;
        child.parentData.nextSibling.parentData.previousSibling = child;
        assert(before.parentData.previousSibling == child);
      }
    }
  }
  void remove(ChildType child) {
    assert(child.parentData is ParentDataType);
    assert(_debugUltimatePreviousSiblingOf(child, equals: _firstChild));
    assert(_debugUltimateNextSiblingOf(child, equals: _lastChild));
    if (child.parentData.previousSibling == null) {
      assert(_firstChild == child);
      _firstChild = child.parentData.nextSibling;
    } else {
      assert(child.parentData.previousSibling.parentData is ParentDataType);
      child.parentData.previousSibling.parentData.nextSibling = child.parentData.nextSibling;
    }
    if (child.parentData.nextSibling == null) {
      assert(_lastChild == child);
      _lastChild = child.parentData.previousSibling;
    } else {
      assert(child.parentData.nextSibling.parentData is ParentDataType);
      child.parentData.nextSibling.parentData.previousSibling = child.parentData.previousSibling;
    }
    child.parentData.previousSibling = null;
    child.parentData.nextSibling = null;
    dropChild(child);
  }
  void redepthChildren() {
    ChildType child = _firstChild;
    while (child != null) {
      redepthChild(child);
      assert(child.parentData is ParentDataType);
      child = child.parentData.nextSibling;
    }
  }
  void attachChildren() {
    ChildType child = _firstChild;
    while (child != null) {
      child.attach();
      assert(child.parentData is ParentDataType);
      child = child.parentData.nextSibling;
    }
  }
  void detachChildren() {
    ChildType child = _firstChild;
    while (child != null) {
      child.detach();
      assert(child.parentData is ParentDataType);
      child = child.parentData.nextSibling;
    }
  }

  ChildType get firstChild => _firstChild;
  ChildType get lastChild => _lastChild;
  ChildType childAfter(ChildType child) {
    assert(child.parentData is ParentDataType);
    return child.parentData.nextSibling;
  }
H
Hixie 已提交
444 445 446 447 448 449 450 451 452 453 454 455

  String debugDescribeChildren(String prefix) {
    String result = '';
    int count = 1;
    ChildType child = _firstChild;
    while (child != null) {
      result += '${prefix}child ${count}: ${child.toString(prefix)}';
      count += 1;
      child = child.parentData.nextSibling;
    }
    return result;
  }
456
}