debug.dart 4.35 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:async';
6
import 'dart:convert' show JSON;
7
import 'dart:developer' as developer;
8

9
import 'package:flutter/painting.dart';
Devon Carew's avatar
Devon Carew committed
10
import 'package:flutter/rendering.dart';
11
import 'package:flutter/scheduler.dart';
12 13

export 'package:flutter/services.dart' show debugPrint;
14

15 16
/// Causes each RenderBox to paint a box around its bounds, and some extra
/// boxes, such as RenderPadding, to draw construction lines.
17
bool debugPaintSizeEnabled = false;
Adam Barth's avatar
Adam Barth committed
18 19

/// The color to use when painting RenderObject bounds.
20
Color debugPaintSizeColor = const Color(0xFF00FFFF);
21

22 23
/// The color to use when painting some boxes that just add space (e.g. an empty
/// RenderConstrainedBox or RenderPadding).
24
Color debugPaintSpacingColor = const Color(0x90909090);
25 26

/// The color to use when painting RenderPadding edges.
27
Color debugPaintPaddingColor = const Color(0x900090FF);
28 29

/// The color to use when painting RenderPadding edges.
30
Color debugPaintPaddingInnerEdgeColor = const Color(0xFF0090FF);
31 32

/// The color to use when painting the arrows used to show RenderPositionedBox alignment.
33
Color debugPaintArrowColor = const Color(0xFFFFFF00);
34

Adam Barth's avatar
Adam Barth committed
35
/// Causes each RenderBox to paint a line at each of its baselines.
36
bool debugPaintBaselinesEnabled = false;
Adam Barth's avatar
Adam Barth committed
37 38

/// The color to use when painting alphabetic baselines.
39
Color debugPaintAlphabeticBaselineColor = const Color(0xFF00FF00);
Adam Barth's avatar
Adam Barth committed
40 41

/// The color ot use when painting ideographic baselines.
42
Color debugPaintIdeographicBaselineColor = const Color(0xFFFFD000);
43

Adam Barth's avatar
Adam Barth committed
44
/// Causes each Layer to paint a box around its bounds.
45
bool debugPaintLayerBordersEnabled = false;
Adam Barth's avatar
Adam Barth committed
46 47

/// The color to use when painting Layer borders.
48
Color debugPaintLayerBordersColor = const Color(0xFFFF9800);
49

Hixie's avatar
Hixie committed
50
/// Causes RenderBox objects to flash while they are being tapped.
51 52 53 54 55
bool debugPaintPointersEnabled = false;

/// The color to use when reporting pointers.
int debugPaintPointersColorValue = 0x00BBBB;

56
/// Overlay a rotating set of colors when repainting layers in checked mode.
57
bool debugRepaintRainbowEnabled = false;
58 59 60 61 62

/// The current color to overlay when repainting a layer.
HSVColor debugCurrentRepaintColor = const HSVColor.fromAHSV(0.4, 60.0, 1.0, 1.0);

/// The amount to increment the hue of the current repaint color.
63
double debugRepaintRainbowHueIncrement = 2.0;
64

65 66 67 68 69 70
/// Log the call stacks that mark render objects as needing paint.
bool debugPrintMarkNeedsPaintStacks = false;

/// Log the call stacks that mark render objects as needing layout.
bool debugPrintMarkNeedsLayoutStacks = false;

71 72 73 74 75 76
List<String> debugDescribeTransform(Matrix4 transform) {
  List<String> matrix = transform.toString().split('\n').map((String s) => '  $s').toList();
  matrix.removeLast();
  return matrix;
}

77 78 79 80 81 82 83 84
bool _extensionsInitialized = false;

void initServiceExtensions() {
  if (_extensionsInitialized)
    return;

  _extensionsInitialized = true;

Devon Carew's avatar
Devon Carew committed
85
  assert(() {
86 87
    developer.registerExtension('ext.flutter.debugPaint', _debugPaint);
    developer.registerExtension('ext.flutter.timeDilation', _timeDilation);
Devon Carew's avatar
Devon Carew committed
88 89 90

    return true;
  });
91 92 93
}

/// Toggle the [debugPaintSizeEnabled] setting.
94
Future<developer.ServiceExtensionResponse> _debugPaint(String method, Map<String, String> parameters) {
95
  if (parameters.containsKey('enabled')) {
96
    debugPaintSizeEnabled = parameters['enabled'] == 'true';
Devon Carew's avatar
Devon Carew committed
97 98 99 100 101 102 103

    // Redraw everything - mark the world as dirty.
    RenderObjectVisitor visitor;
    visitor = (RenderObject child) {
      child.markNeedsPaint();
      child.visitChildren(visitor);
    };
104
    Renderer.instance?.renderView?.visitChildren(visitor);
105 106
  }

107 108 109 110 111 112 113
  return new Future<developer.ServiceExtensionResponse>.value(
    new developer.ServiceExtensionResponse.result(JSON.encode({
      'type': '_extensionType',
      'method': method,
      'enabled': debugPaintSizeEnabled
    }))
  );
114 115 116
}

/// Manipulate the scheduler's [timeDilation] field.
117
Future<developer.ServiceExtensionResponse> _timeDilation(String method, Map<String, String> parameters) {
118
  if (parameters.containsKey('timeDilation')) {
119
    timeDilation = double.parse(parameters['timeDilation']);
120 121
  }

122 123 124 125 126 127 128
  return new Future<developer.ServiceExtensionResponse>.value(
    new developer.ServiceExtensionResponse.result(JSON.encode({
      'type': '_extensionType',
      'method': method,
      'timeDilation': '$timeDilation'
    }))
  );
129
}