view.dart 10.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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/gestures.dart' show MouseTrackerAnnotation;
11
import 'package:flutter/services.dart';
12
import 'package:vector_math/vector_math_64.dart';
13

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

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

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

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

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

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

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

67
  /// The current layout size of the view.
68
  Size get size => _size;
69
  Size _size = Size.zero;
70

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

88
  final ui.Window _window;
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  /// 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:
  ///
107 108
  ///  * [AnnotatedRegion], for placing [SystemUiOverlayStyle] in the layer tree.
  ///  * [SystemChrome.setSystemUIOverlayStyle], for imperatively setting the system ui style.
109 110
  bool automaticSystemUiAdjustment = true;

111
  /// Bootstrap the rendering pipeline by scheduling the first frame.
112
  ///
113 114
  /// Deprecated. Call [prepareInitialFrame] followed by a call to
  /// [PipelineOwner.requestVisualUpdate] on [owner] instead.
115 116 117 118
  @Deprecated(
    'Call prepareInitialFrame followed by owner.requestVisualUpdate() instead. '
    'This feature was deprecated after v1.10.0.'
  )
119 120 121 122 123 124 125
  void scheduleInitialFrame() {
    prepareInitialFrame();
    owner.requestVisualUpdate();
  }

  /// Bootstrap the rendering pipeline by preparing the first frame.
  ///
126 127 128
  /// This should only be called once, and must be called before changing
  /// [configuration]. It is typically called immediately after calling the
  /// constructor.
129 130 131 132
  ///
  /// This does not actually schedule the first frame. Call
  /// [PipelineOwner.requestVisualUpdate] on [owner] to do that.
  void prepareInitialFrame() {
133
    assert(owner != null);
134
    assert(_rootTransform == null);
135
    scheduleInitialLayout();
136 137
    scheduleInitialPaint(_updateMatricesAndCreateNewRootLayer());
    assert(_rootTransform != null);
138 139
  }

140 141
  Matrix4 _rootTransform;

142
  TransformLayer _updateMatricesAndCreateNewRootLayer() {
143
    _rootTransform = configuration.toMatrix();
144
    final TransformLayer rootLayer = TransformLayer(transform: _rootTransform);
145 146 147 148 149
    rootLayer.attach(this);
    assert(_rootTransform != null);
    return rootLayer;
  }

150 151
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
152
  @override
153
  void debugAssertDoesMeetConstraints() { assert(false); }
154

155
  @override
156 157 158 159
  void performResize() {
    assert(false);
  }

160
  @override
161
  void performLayout() {
162
    assert(_rootTransform != null);
163
    _size = configuration.size;
164
    assert(_size.isFinite);
165 166

    if (child != null)
167
      child.layout(BoxConstraints.tight(_size));
168 169
  }

170
  @override
171 172 173 174
  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()
  }

175 176 177 178 179 180
  /// 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.
  ///
181 182 183 184
  /// 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.
185
  bool hitTest(HitTestResult result, { Offset position }) {
186
    if (child != null)
187
      child.hitTest(BoxHitTestResult.wrap(result), position: position);
188
    result.add(HitTestEntry(this));
189 190 191
    return true;
  }

192 193 194 195
  /// Determines the set of mouse tracker annotations at the given position.
  ///
  /// See also:
  ///
196 197
  ///  * [Layer.findAllAnnotations], which is used by this method to find all
  ///    [AnnotatedRegionLayer]s annotated for mouse tracking.
198 199 200 201
  Iterable<MouseTrackerAnnotation> hitTestMouseTrackers(Offset position) {
    // Layer hit testing is done using device pixels, so we have to convert
    // the logical coordinates of the event location back to device pixels
    // here.
202
    return layer.findAllAnnotations<MouseTrackerAnnotation>(
203 204
      position * configuration.devicePixelRatio
    ).annotations;
205 206
  }

207
  @override
208
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
209

210
  @override
211 212
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
Adam Barth's avatar
Adam Barth committed
213
      context.paintChild(child, offset);
214 215
  }

216 217 218 219 220 221 222
  @override
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
    assert(_rootTransform != null);
    transform.multiply(_rootTransform);
    super.applyPaintTransform(child, transform);
  }

223
  /// Uploads the composited layer tree to the engine.
224 225
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
226
  void compositeFrame() {
227
    Timeline.startSync('Compositing', arguments: timelineWhitelistArguments);
228
    try {
229
      final ui.SceneBuilder builder = ui.SceneBuilder();
230
      final ui.Scene scene = layer.buildScene(builder);
231 232
      if (automaticSystemUiAdjustment)
        _updateSystemChrome();
233
      _window.render(scene);
234
      scene.dispose();
235
      assert(() {
236
        if (debugRepaintRainbowEnabled || debugRepaintTextRainbowEnabled)
237
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue((debugCurrentRepaintColor.hue + 2.0) % 360.0);
238
        return true;
239
      }());
240
    } finally {
241
      Timeline.finishSync();
242 243 244
    }
  }

245 246
  void _updateSystemChrome() {
    final Rect bounds = paintBounds;
247 248
    final Offset top = Offset(bounds.center.dx, _window.padding.top / _window.devicePixelRatio);
    final Offset bottom = Offset(bounds.center.dx, bounds.center.dy - _window.padding.bottom / _window.devicePixelRatio);
249
    final SystemUiOverlayStyle upperOverlayStyle = layer.find<SystemUiOverlayStyle>(top);
250
    // Only android has a customizable system navigation bar.
251
    SystemUiOverlayStyle lowerOverlayStyle;
252 253
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
254
        lowerOverlayStyle = layer.find<SystemUiOverlayStyle>(bottom);
255 256
        break;
      case TargetPlatform.fuchsia:
257 258
      case TargetPlatform.iOS:
      case TargetPlatform.macOS:
259 260 261 262
        break;
    }
    // If there are no overlay styles in the UI don't bother updating.
    if (upperOverlayStyle != null || lowerOverlayStyle != null) {
263
      final SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle(
264 265 266 267 268 269 270 271 272 273 274
        statusBarBrightness: upperOverlayStyle?.statusBarBrightness,
        statusBarIconBrightness: upperOverlayStyle?.statusBarIconBrightness,
        statusBarColor: upperOverlayStyle?.statusBarColor,
        systemNavigationBarColor: lowerOverlayStyle?.systemNavigationBarColor,
        systemNavigationBarDividerColor: lowerOverlayStyle?.systemNavigationBarDividerColor,
        systemNavigationBarIconBrightness: lowerOverlayStyle?.systemNavigationBarIconBrightness,
      );
      SystemChrome.setSystemUIOverlayStyle(overlayStyle);
    }
  }

275
  @override
276
  Rect get paintBounds => Offset.zero & (size * configuration.devicePixelRatio);
277 278

  @override
279 280 281 282
  Rect get semanticBounds {
    assert(_rootTransform != null);
    return MatrixUtils.transformRect(_rootTransform, Offset.zero & size);
  }
Hixie's avatar
Hixie committed
283

284
  @override
285
  // ignore: MUST_CALL_SUPER
286
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
287 288 289
    // call to ${super.debugFillProperties(description)} is omitted because the
    // root superclasses don't include any interesting information for this
    // class
290
    assert(() {
291
      properties.add(DiagnosticsNode.message('debug mode enabled - ${kIsWeb ? 'Web' :  Platform.operatingSystem}'));
292
      return true;
293
    }());
294 295
    properties.add(DiagnosticsProperty<Size>('window size', _window.physicalSize, tooltip: 'in physical pixels'));
    properties.add(DoubleProperty('device pixel ratio', _window.devicePixelRatio, tooltip: 'physical pixels per logical pixel'));
296
    properties.add(DiagnosticsProperty<ViewConfiguration>('configuration', configuration, tooltip: 'in logical pixels'));
297
    if (_window.semanticsEnabled)
298
      properties.add(DiagnosticsNode.message('semantics enabled'));
299
  }
300
}