view.dart 12.1 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, FlutterView;
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
  /// The pixel density of the output surface.
  final double devicePixelRatio;

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

41
  @override
42
  String toString() => '$size at ${debugFormatDouble(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
  RenderView({
57 58
    RenderBox? child,
    required ViewConfiguration configuration,
59
    required ui.FlutterView window,
60
  }) : assert(configuration != null),
61 62
       _configuration = configuration,
       _window = window {
63 64 65
    this.child = child;
  }

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

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

87
  final ui.FlutterView _window;
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:
  ///
106 107
  ///  * [AnnotatedRegion], for placing [SystemUiOverlayStyle] in the layer tree.
  ///  * [SystemChrome.setSystemUIOverlayStyle], for imperatively setting the system ui style.
108 109
  bool automaticSystemUiAdjustment = true;

110 111
  /// Bootstrap the rendering pipeline by preparing the first frame.
  ///
112 113 114
  /// This should only be called once, and must be called before changing
  /// [configuration]. It is typically called immediately after calling the
  /// constructor.
115 116 117 118
  ///
  /// This does not actually schedule the first frame. Call
  /// [PipelineOwner.requestVisualUpdate] on [owner] to do that.
  void prepareInitialFrame() {
119
    assert(owner != null);
120
    assert(_rootTransform == null);
121
    scheduleInitialLayout();
122 123
    scheduleInitialPaint(_updateMatricesAndCreateNewRootLayer());
    assert(_rootTransform != null);
124 125
  }

126
  Matrix4? _rootTransform;
127

128
  TransformLayer _updateMatricesAndCreateNewRootLayer() {
129
    _rootTransform = configuration.toMatrix();
130
    final TransformLayer rootLayer = TransformLayer(transform: _rootTransform);
131 132 133 134 135
    rootLayer.attach(this);
    assert(_rootTransform != null);
    return rootLayer;
  }

136 137
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
138
  @override
139
  void debugAssertDoesMeetConstraints() { assert(false); }
140

141
  @override
142 143 144 145
  void performResize() {
    assert(false);
  }

146
  @override
147
  void performLayout() {
148
    assert(_rootTransform != null);
149
    _size = configuration.size;
150
    assert(_size.isFinite);
151 152

    if (child != null)
153
      child!.layout(BoxConstraints.tight(_size));
154 155
  }

156
  @override
157
  void rotate({ int? oldAngle, int? newAngle, Duration? time }) {
158 159 160
    assert(false); // nobody tells the screen to rotate, the whole rotate() dance is started from our performResize()
  }

161 162 163 164 165 166
  /// 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.
  ///
167 168 169 170
  /// 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.
171
  bool hitTest(HitTestResult result, { required Offset position }) {
172
    if (child != null)
173
      child!.hitTest(BoxHitTestResult.wrap(result), position: position);
174
    result.add(HitTestEntry(this));
175 176 177
    return true;
  }

178 179 180 181
  /// Determines the set of mouse tracker annotations at the given position.
  ///
  /// See also:
  ///
182 183
  ///  * [Layer.findAllAnnotations], which is used by this method to find all
  ///    [AnnotatedRegionLayer]s annotated for mouse tracking.
184
  HitTestResult hitTestMouseTrackers(Offset position) {
185
    assert(position != null);
186 187 188
    // 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.
189
    final BoxHitTestResult result = BoxHitTestResult();
190 191
    hitTest(result, position: position);
    return result;
192 193
  }

194
  @override
195
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
196

197
  @override
198 199
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
200
      context.paintChild(child!, offset);
201 202
  }

203 204 205
  @override
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
    assert(_rootTransform != null);
206
    transform.multiply(_rootTransform!);
207 208 209
    super.applyPaintTransform(child, transform);
  }

210
  /// Uploads the composited layer tree to the engine.
211 212
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
213
  void compositeFrame() {
214
    Timeline.startSync('Compositing', arguments: timelineArgumentsIndicatingLandmarkEvent);
215
    try {
216
      final ui.SceneBuilder builder = ui.SceneBuilder();
217
      final ui.Scene scene = layer!.buildScene(builder);
218 219
      if (automaticSystemUiAdjustment)
        _updateSystemChrome();
220
      _window.render(scene);
221
      scene.dispose();
222
      assert(() {
223
        if (debugRepaintRainbowEnabled || debugRepaintTextRainbowEnabled)
224
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue((debugCurrentRepaintColor.hue + 2.0) % 360.0);
225
        return true;
226
      }());
227
    } finally {
228
      Timeline.finishSync();
229 230 231
    }
  }

232
  void _updateSystemChrome() {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    // Take overlay style from the place where a system status bar and system
    // navigation bar are placed to update system style overlay.
    // The center of the system navigation bar and the center of the status bar
    // are used to get SystemUiOverlayStyle's to update system overlay appearance.
    //
    //         Horizontal center of the screen
    //                 V
    //    ++++++++++++++++++++++++++
    //    |                        |
    //    |    System status bar   |  <- Vertical center of the status bar
    //    |                        |
    //    ++++++++++++++++++++++++++
    //    |                        |
    //    |        Content         |
    //    ~                        ~
    //    |                        |
    //    ++++++++++++++++++++++++++
    //    |                        |
    //    |  System navigation bar | <- Vertical center of the navigation bar
    //    |                        |
    //    ++++++++++++++++++++++++++ <- bounds.bottom
254
    final Rect bounds = paintBounds;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    // Center of the status bar
    final Offset top = Offset(
      // Horizontal center of the screen
      bounds.center.dx,
      // The vertical center of the system status bar. The system status bar
      // height is kept as top window padding.
      _window.padding.top / 2.0,
    );
    // Center of the navigation bar
    final Offset bottom = Offset(
      // Horizontal center of the screen
      bounds.center.dx,
      // Vertical center of the system navigation bar. The system navigation bar
      // height is kept as bottom window padding. The "1" needs to be subtracted
      // from the bottom because available pixels are in (0..bottom) range.
      // I.e. for a device with 1920 height, bound.bottom is 1920, but the most
      // bottom drawn pixel is at 1919 position.
      bounds.bottom - 1.0 - _window.padding.bottom / 2.0,
    );
274
    final SystemUiOverlayStyle? upperOverlayStyle = layer!.find<SystemUiOverlayStyle>(top);
275
    // Only android has a customizable system navigation bar.
276
    SystemUiOverlayStyle? lowerOverlayStyle;
277 278
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
279
        lowerOverlayStyle = layer!.find<SystemUiOverlayStyle>(bottom);
280 281
        break;
      case TargetPlatform.fuchsia:
282
      case TargetPlatform.iOS:
283
      case TargetPlatform.linux:
284
      case TargetPlatform.macOS:
285
      case TargetPlatform.windows:
286 287 288 289
        break;
    }
    // If there are no overlay styles in the UI don't bother updating.
    if (upperOverlayStyle != null || lowerOverlayStyle != null) {
290
      final SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle(
291 292 293 294 295 296 297 298 299 300 301
        statusBarBrightness: upperOverlayStyle?.statusBarBrightness,
        statusBarIconBrightness: upperOverlayStyle?.statusBarIconBrightness,
        statusBarColor: upperOverlayStyle?.statusBarColor,
        systemNavigationBarColor: lowerOverlayStyle?.systemNavigationBarColor,
        systemNavigationBarDividerColor: lowerOverlayStyle?.systemNavigationBarDividerColor,
        systemNavigationBarIconBrightness: lowerOverlayStyle?.systemNavigationBarIconBrightness,
      );
      SystemChrome.setSystemUIOverlayStyle(overlayStyle);
    }
  }

302
  @override
303
  Rect get paintBounds => Offset.zero & (size * configuration.devicePixelRatio);
304 305

  @override
306 307
  Rect get semanticBounds {
    assert(_rootTransform != null);
308
    return MatrixUtils.transformRect(_rootTransform!, Offset.zero & size);
309
  }
Hixie's avatar
Hixie committed
310

311
  @override
312
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
313 314 315
    // call to ${super.debugFillProperties(description)} is omitted because the
    // root superclasses don't include any interesting information for this
    // class
316
    assert(() {
317
      properties.add(DiagnosticsNode.message('debug mode enabled - ${kIsWeb ? 'Web' :  Platform.operatingSystem}'));
318
      return true;
319
    }());
320 321
    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'));
322
    properties.add(DiagnosticsProperty<ViewConfiguration>('configuration', configuration, tooltip: 'in logical pixels'));
323
    if (_window.platformDispatcher.semanticsEnabled)
324
      properties.add(DiagnosticsNode.message('semantics enabled'));
325
  }
326
}