simple_render_tree.dart 1.8 KB
Newer Older
A
Adam Barth 已提交
1 2 3 4 5 6 7 8
// 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:math';
import 'dart:sky';
import 'package:sky/framework/layout2.dart';

A
Adam Barth 已提交
9
class RenderSolidColor extends RenderDecoratedBox {
10 11
  final int backgroundColor;

A
Adam Barth 已提交
12
  RenderSolidColor(int backgroundColor)
13 14
      : super(new BoxDecoration(backgroundColor: backgroundColor)),
        backgroundColor = backgroundColor;
A
Adam Barth 已提交
15 16 17 18 19 20 21 22 23

  BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) {
    return new BoxDimensions.withConstraints(constraints, height: 200.0);
  }

  void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) {
    setWidth(constraints, constraints.maxWidth);
    setHeight(constraints, 200.0);
    layoutDone();
A
Adam Barth 已提交
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

  bool handlePointer(PointerEvent event, { double x: 0.0, double y: 0.0 }) {
    if (event.type == 'pointerdown') {
      setBoxDecoration(new BoxDecoration(backgroundColor: 0xFFFF0000));
      return true;
    }

    if (event.type == 'pointerup') {
      setBoxDecoration(new BoxDecoration(backgroundColor: backgroundColor));
      return true;
    }

    return false;
  }
}

RenderView renderView;

void beginFrame(double timeStamp) {
  RenderNode.flushLayout();

  renderView.paintFrame();
}

bool handleEvent(Event event) {
  if (event is! PointerEvent)
    return false;
  return renderView.handlePointer(event, x: event.x, y: event.y);
A
Adam Barth 已提交
53 54 55
}

void main() {
56 57 58
  view.setEventCallback(handleEvent);
  view.setBeginFrameCallback(beginFrame);

A
Adam Barth 已提交
59 60 61 62 63 64
  var root = new RenderBlock(
      decoration: new BoxDecoration(backgroundColor: 0xFF00FFFF));

  root.add(new RenderSolidColor(0xFF00FF00));
  root.add(new RenderSolidColor(0xFF0000FF));

65
  renderView = new RenderView(root: root);
A
Adam Barth 已提交
66
  renderView.layout(newWidth: view.width, newHeight: view.height);
67 68

  view.scheduleFrame();
A
Adam Barth 已提交
69
}