view.dart 3.24 KB
Newer Older
1 2 3 4 5 6
// 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 'dart:sky' as sky;

7
import 'package:sky/base/scheduler.dart' as scheduler;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import 'package:sky/rendering/layer.dart';
import 'package:sky/rendering/object.dart';
import 'package:sky/rendering/box.dart';
import 'package:vector_math/vector_math.dart';

class ViewConstraints {
  const ViewConstraints({
    this.size: Size.zero,
    this.orientation
  });
  final Size size;
  final int orientation;
}

class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
  RenderView({
    RenderBox child,
25
    this.devicePixelRatio,
26 27 28 29 30
    this.timeForRotation: const Duration(microseconds: 83333)
  }) {
    this.child = child;
  }

31 32 33
  final double devicePixelRatio;
  Duration timeForRotation;

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  Size _size = Size.zero;
  Size get size => _size;

  int _orientation; // 0..3
  int get orientation => _orientation;

  ViewConstraints _rootConstraints;
  ViewConstraints get rootConstraints => _rootConstraints;
  void set rootConstraints(ViewConstraints value) {
    if (_rootConstraints == value)
      return;
    _rootConstraints = value;
    markNeedsLayout();
  }

49
  void scheduleInitialFrame() {
50
    Matrix4 logicalToDeviceZoom = new Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0);
51
    scheduleInitialLayout();
52
    scheduleInitialPaint(new TransformLayer(transform: logicalToDeviceZoom));
53
    scheduler.ensureVisualUpdate();
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
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
  bool debugDoesMeetConstraints() { assert(false); return false; }

  void performResize() {
    assert(false);
  }

  void performLayout() {
    if (_rootConstraints.orientation != _orientation) {
      if (_orientation != null && child != null)
        child.rotate(oldAngle: _orientation, newAngle: _rootConstraints.orientation, time: timeForRotation);
      _orientation = _rootConstraints.orientation;
    }
    _size = _rootConstraints.size;
    assert(!_size.isInfinite);

    if (child != null)
      child.layout(new BoxConstraints.tight(_size));
  }

  void rotate({ int oldAngle, int newAngle, Duration time }) {
    assert(false); // nobody tells the screen to rotate, the whole rotate() dance is started from our performResize()
  }

  bool hitTest(HitTestResult result, { Point position }) {
    if (child != null) {
      Rect childBounds = Point.origin & child.size;
      if (childBounds.contains(position))
        child.hitTest(result, position: position);
    }
    result.add(new HitTestEntry(this));
    return true;
  }

Hixie's avatar
Hixie committed
91 92
  bool get hasLayer => true;

93 94 95 96 97 98 99 100
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
      context.paintChild(child, offset.toPoint());
  }

  void compositeFrame() {
    sky.tracing.begin('RenderView.compositeFrame');
    try {
101 102 103 104
      Rect bounds = Point.origin & (size * sky.view.devicePixelRatio);
      sky.SceneBuilder builder = new sky.SceneBuilder(bounds);
      layer.addToScene(builder, Offset.zero);
      sky.view.scene = builder.build();
105 106 107 108 109 110 111
    } finally {
      sky.tracing.end('RenderView.compositeFrame');
    }
  }

  Rect get paintBounds => Point.origin & size;
}