binding.dart 2.82 KB
Newer Older
1 2 3 4
// 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.

5
import 'dart:ui' as ui;
6

7
import 'package:flutter/gestures.dart';
8
import 'package:flutter/scheduler.dart';
Ian Hickson's avatar
Ian Hickson committed
9
import 'package:flutter/services.dart';
10 11

import 'box.dart';
12
import 'debug.dart';
13 14
import 'object.dart';
import 'view.dart';
15

Ian Hickson's avatar
Ian Hickson committed
16
export 'package:flutter/gestures.dart' show HitTestResult;
17

Florian Loitsch's avatar
Florian Loitsch committed
18
/// The glue between the render tree and the Flutter engine.
Ian Hickson's avatar
Ian Hickson committed
19 20
abstract class Renderer extends Scheduler
  implements HitTestable {
21

Ian Hickson's avatar
Ian Hickson committed
22 23
  void initInstances() {
    super.initInstances();
24
    _instance = this;
Ian Hickson's avatar
Ian Hickson committed
25 26
    ui.window.onMetricsChanged = handleMetricsChanged;
    initRenderView();
27
    assert(renderView != null);
28 29 30 31
    assert(() {
      initServiceExtensions();
      return true;
    });
Ian Hickson's avatar
Ian Hickson committed
32 33
    addPersistentFrameCallback(_handlePersistentFrameCallback);
  }
34

Ian Hickson's avatar
Ian Hickson committed
35 36
  static Renderer _instance;
  static Renderer get instance => _instance;
37

Ian Hickson's avatar
Ian Hickson committed
38 39 40 41
  void initRenderView() {
    if (renderView == null) {
      renderView = new RenderView();
      renderView.scheduleInitialFrame();
42
    }
43
    handleMetricsChanged(); // configures renderView's metrics
44 45
  }

Florian Loitsch's avatar
Florian Loitsch committed
46
  /// The render tree that's attached to the output surface.
47
  RenderView get renderView => _renderView;
48
  RenderView _renderView;
Ian Hickson's avatar
Ian Hickson committed
49
  void set renderView(RenderView value) {
50
    assert(value != null);
Ian Hickson's avatar
Ian Hickson committed
51 52 53 54 55
    if (_renderView == value)
      return;
    if (_renderView != null)
      _renderView.detach();
    _renderView = value;
56
    _renderView.attach();
57 58
  }

Ian Hickson's avatar
Ian Hickson committed
59
  void handleMetricsChanged() {
60
    assert(renderView != null);
61
    renderView.configuration = new ViewConfiguration(size: ui.window.size);
62 63
  }

64 65 66 67
  void _handlePersistentFrameCallback(Duration timeStamp) {
    beginFrame();
  }

Ian Hickson's avatar
Ian Hickson committed
68
  /// Pump the rendering pipeline to generate a frame.
69
  void beginFrame() {
70
    assert(renderView != null);
71
    RenderObject.flushLayout();
72
    RenderObject.flushCompositingBits();
73
    RenderObject.flushPaint();
74
    renderView.compositeFrame();
75 76
  }

Ian Hickson's avatar
Ian Hickson committed
77
  void hitTest(HitTestResult result, Point position) {
78 79
    assert(renderView != null);
    renderView.hitTest(result, position: position);
Ian Hickson's avatar
Ian Hickson committed
80
    super.hitTest(result, position);
81
  }
82
}
83

Florian Loitsch's avatar
Florian Loitsch committed
84
/// Prints a textual representation of the entire render tree.
85
void debugDumpRenderTree() {
86
  debugPrint(Renderer.instance?.renderView?.toStringDeep());
87
}
88

Florian Loitsch's avatar
Florian Loitsch committed
89
/// Prints a textual representation of the entire layer tree.
90
void debugDumpLayerTree() {
91
  debugPrint(Renderer.instance?.renderView?.layer?.toStringDeep());
Ian Hickson's avatar
Ian Hickson committed
92 93 94 95
}

/// A concrete binding for applications that use the Rendering framework
/// directly. This is the glue that binds the framework to the Flutter engine.
96
class RenderingFlutterBinding extends BindingBase with Scheduler, Gesturer, Renderer {
Ian Hickson's avatar
Ian Hickson committed
97
  RenderingFlutterBinding({ RenderBox root }) {
98
    assert(renderView != null);
Ian Hickson's avatar
Ian Hickson committed
99 100
    renderView.child = root;
  }
101
}