view.dart 9.54 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:developer';
6
import 'dart:io' show Platform;
7
import 'dart:ui' as ui show Scene, SceneBuilder, window;
8

9
import 'package:flutter/foundation.dart';
10
import 'package:flutter/services.dart';
11
import 'package:vector_math/vector_math_64.dart';
12

13
import 'binding.dart';
14
import 'box.dart';
15
import 'debug.dart';
16 17 18
import 'layer.dart';
import 'object.dart';

19
/// The layout constraints for the root render object.
20
@immutable
21
class ViewConfiguration {
22 23 24
  /// Creates a view configuration.
  ///
  /// By default, the view has zero [size] and a [devicePixelRatio] of 1.0.
25
  const ViewConfiguration({
26 27
    this.size = Size.zero,
    this.devicePixelRatio = 1.0,
28
  });
29

30
  /// The size of the output surface.
31
  final Size size;
32

33 34 35 36 37 38 39 40
  /// The pixel density of the output surface.
  final double devicePixelRatio;

  /// Creates a transformation matrix that applies the [devicePixelRatio].
  Matrix4 toMatrix() {
    return new Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0);
  }

41
  @override
42
  String toString() => '$size at ${devicePixelRatio}x';
43 44
}

45
/// The root of the render tree.
46 47
///
/// The view represents the total output surface of the render tree and handles
48
/// bootstrapping the rendering pipeline. The view has a unique child
49
/// [RenderBox], which is required to fill the entire output surface.
50
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
51 52 53
  /// Creates the root of the render tree.
  ///
  /// Typically created by the binding (e.g., [RendererBinding]).
54 55
  ///
  /// The [configuration] must not be null.
56 57
  RenderView({
    RenderBox child,
58 59 60
    @required ViewConfiguration configuration,
  }) : assert(configuration != null),
       _configuration = configuration {
61 62 63
    this.child = child;
  }

64
  /// The current layout size of the view.
65
  Size get size => _size;
66
  Size _size = Size.zero;
67

68
  /// The constraints used for the root layout.
69 70
  ViewConfiguration get configuration => _configuration;
  ViewConfiguration _configuration;
71 72 73 74
  /// The configuration is initially set by the `configuration` argument
  /// passed to the constructor.
  ///
  /// Always call [scheduleInitialFrame] before changing the configuration.
75
  set configuration(ViewConfiguration value) {
76
    assert(value != null);
77
    if (configuration == value)
78
      return;
79
    _configuration = value;
80 81
    replaceRootLayer(_updateMatricesAndCreateNewRootLayer());
    assert(_rootTransform != null);
82 83 84
    markNeedsLayout();
  }

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  /// Whether Flutter should automatically compute the desired system UI.
  ///
  /// When this setting is enabled, Flutter will hit-test the layer tree at the
  /// top and bottom of the screen on each frame looking for an
  /// [AnnotatedRegionLayer] with an instance of a [SystemUiOverlayStyle]. The
  /// hit-test result from the top of the screen provides the status bar settings
  /// and the hit-test result from the bottom of the screen provides the system
  /// nav bar settings.
  ///
  /// Setting this to false does not cause previous automatic adjustments to be
  /// reset, nor does setting it to true cause the app to update immediately.
  ///
  /// If you want to imperatively set the system ui style instead, it is
  /// recommended that [automaticSystemUiAdjustment] is set to false.
  ///
  /// See also:
  ///
  ///   * [AnnotatedRegion], for placing [SystemUiOverlayStyle] in the layer tree.
  ///   * [SystemChrome.setSystemUIOverlayStyle], for imperatively setting the system ui style.
  bool automaticSystemUiAdjustment = true;

106
  /// Bootstrap the rendering pipeline by scheduling the first frame.
107 108 109 110
  ///
  /// This should only be called once, and must be called before changing
  /// [configuration]. It is typically called immediately after calling the
  /// constructor.
111
  void scheduleInitialFrame() {
112
    assert(owner != null);
113
    assert(_rootTransform == null);
114
    scheduleInitialLayout();
115 116
    scheduleInitialPaint(_updateMatricesAndCreateNewRootLayer());
    assert(_rootTransform != null);
117
    owner.requestVisualUpdate();
118 119
  }

120 121 122 123 124 125 126 127 128 129
  Matrix4 _rootTransform;

  Layer _updateMatricesAndCreateNewRootLayer() {
    _rootTransform = configuration.toMatrix();
    final ContainerLayer rootLayer = new TransformLayer(transform: _rootTransform);
    rootLayer.attach(this);
    assert(_rootTransform != null);
    return rootLayer;
  }

130 131
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
132
  @override
133
  void debugAssertDoesMeetConstraints() { assert(false); }
134

135
  @override
136 137 138 139
  void performResize() {
    assert(false);
  }

140
  @override
141
  void performLayout() {
142
    assert(_rootTransform != null);
143
    _size = configuration.size;
144
    assert(_size.isFinite);
145 146 147 148 149

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

150
  @override
151 152 153 154
  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()
  }

155 156 157 158 159 160
  /// Determines the set of render objects located at the given position.
  ///
  /// Returns true if the given point is contained in this render object or one
  /// of its descendants. Adds any render objects that contain the point to the
  /// given hit test result.
  ///
161 162 163 164
  /// The [position] argument is in the coordinate system of the render view,
  /// which is to say, in logical pixels. This is not necessarily the same
  /// coordinate system as that expected by the root [Layer], which will
  /// normally be in physical (device) pixels.
165
  bool hitTest(HitTestResult result, { Offset position }) {
166 167
    if (child != null)
      child.hitTest(result, position: position);
168 169 170 171
    result.add(new HitTestEntry(this));
    return true;
  }

172
  @override
173
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
174

175
  @override
176 177
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
Adam Barth's avatar
Adam Barth committed
178
      context.paintChild(child, offset);
179 180
  }

181 182 183 184 185 186 187
  @override
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
    assert(_rootTransform != null);
    transform.multiply(_rootTransform);
    super.applyPaintTransform(child, transform);
  }

188
  /// Uploads the composited layer tree to the engine.
189 190
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
191
  void compositeFrame() {
192
    Timeline.startSync('Compositing', arguments: timelineWhitelistArguments);
193
    try {
194
      final ui.SceneBuilder builder = new ui.SceneBuilder();
195
      layer.addToScene(builder, Offset.zero);
196
      final ui.Scene scene = builder.build();
197 198
      if (automaticSystemUiAdjustment)
        _updateSystemChrome();
199 200
      ui.window.render(scene);
      scene.dispose();
201
      assert(() {
202
        if (debugRepaintRainbowEnabled || debugRepaintTextRainbowEnabled)
203
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue((debugCurrentRepaintColor.hue + 2.0) % 360.0);
204
        return true;
205
      }());
206
    } finally {
207
      Timeline.finishSync();
208 209 210
    }
  }

211 212 213 214 215 216
  void _updateSystemChrome() {
    final Rect bounds = paintBounds;
    final Offset top = new Offset(bounds.center.dx, ui.window.padding.top / ui.window.devicePixelRatio);
    final Offset bottom = new Offset(bounds.center.dx, bounds.center.dy - ui.window.padding.bottom / ui.window.devicePixelRatio);
    final SystemUiOverlayStyle upperOverlayStyle = layer.find<SystemUiOverlayStyle>(top);
    // Only android has a customizable system navigation bar.
217
    SystemUiOverlayStyle lowerOverlayStyle;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        lowerOverlayStyle = layer.find<SystemUiOverlayStyle>(bottom);
        break;
      case TargetPlatform.iOS:
      case TargetPlatform.fuchsia:
        break;
    }
    // If there are no overlay styles in the UI don't bother updating.
    if (upperOverlayStyle != null || lowerOverlayStyle != null) {
      final SystemUiOverlayStyle overlayStyle = new SystemUiOverlayStyle(
        statusBarBrightness: upperOverlayStyle?.statusBarBrightness,
        statusBarIconBrightness: upperOverlayStyle?.statusBarIconBrightness,
        statusBarColor: upperOverlayStyle?.statusBarColor,
        systemNavigationBarColor: lowerOverlayStyle?.systemNavigationBarColor,
        systemNavigationBarDividerColor: lowerOverlayStyle?.systemNavigationBarDividerColor,
        systemNavigationBarIconBrightness: lowerOverlayStyle?.systemNavigationBarIconBrightness,
      );
      SystemChrome.setSystemUIOverlayStyle(overlayStyle);
    }
  }

240
  @override
241
  Rect get paintBounds => Offset.zero & (size * configuration.devicePixelRatio);
242 243

  @override
244 245 246 247
  Rect get semanticBounds {
    assert(_rootTransform != null);
    return MatrixUtils.transformRect(_rootTransform, Offset.zero & size);
  }
Hixie's avatar
Hixie committed
248

249
  @override
250
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
251 252 253
    // call to ${super.debugFillProperties(description)} is omitted because the
    // root superclasses don't include any interesting information for this
    // class
254
    assert(() {
255
      properties.add(new DiagnosticsNode.message('debug mode enabled - ${Platform.operatingSystem}'));
256
      return true;
257
    }());
258 259 260
    properties.add(new DiagnosticsProperty<Size>('window size', ui.window.physicalSize, tooltip: 'in physical pixels'));
    properties.add(new DoubleProperty('device pixel ratio', ui.window.devicePixelRatio, tooltip: 'physical pixels per logical pixel'));
    properties.add(new DiagnosticsProperty<ViewConfiguration>('configuration', configuration, tooltip: 'in logical pixels'));
261
    if (ui.window.semanticsEnabled)
262
      properties.add(new DiagnosticsNode.message('semantics enabled'));
263
  }
264
}