debug.dart 5.19 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
import 'dart:ui' as ui;
9

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

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

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

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

23 24 25 26 27 28 29 30 31 32 33 34 35
/// The color to use when painting some boxes that just add space (e.g. an empty
/// RenderConstrainedBox or RenderPadding).
ui.Color debugPaintSpacingColor = const ui.Color(0x90909090);

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

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

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

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

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

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

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

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

51 52 53 54 55 56
/// Causes RenderBox objects to flash while they are being tapped
bool debugPaintPointersEnabled = false;

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

57
/// The color to use when painting RenderError boxes in checked mode.
58
ui.Color debugErrorBoxColor = const ui.Color(0xFFFF0000);
59

60 61 62 63 64 65 66 67 68
/// Overlay a rotating set of colors when repainting layers in checked mode.
bool debugEnableRepaintRainbox = false;

/// 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.
double debugRepaintRainboxHueIncrement = 2.0;

69 70 71 72 73 74
/// 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;

75 76 77 78 79 80
List<String> debugDescribeTransform(Matrix4 transform) {
  List<String> matrix = transform.toString().split('\n').map((String s) => '  $s').toList();
  matrix.removeLast();
  return matrix;
}

81 82 83 84 85 86 87 88
bool _extensionsInitialized = false;

void initServiceExtensions() {
  if (_extensionsInitialized)
    return;

  _extensionsInitialized = true;

Devon Carew's avatar
Devon Carew committed
89
  assert(() {
90 91 92
    developer.registerExtension('flutter', _flutter);
    developer.registerExtension('flutter.debugPaint', _debugPaint);
    developer.registerExtension('flutter.timeDilation', _timeDilation);
Devon Carew's avatar
Devon Carew committed
93 94 95

    // Emit an info level log message; this tells the debugger that the Flutter
    // service extensions are registered.
96
    developer.log('Flutter initialized', name: 'flutter', level: 800);
Devon Carew's avatar
Devon Carew committed
97 98 99

    return true;
  });
100 101 102 103
}

/// Just respond to the request. Clients can use the existence of this call to
/// know that the debug client is a Flutter app.
104
Future<developer.ServiceExtensionResponse> _flutter(String method, Map<String, String> parameters) {
105 106 107 108 109 110
  return new Future<developer.ServiceExtensionResponse>.value(
    new developer.ServiceExtensionResponse.result(JSON.encode({
      'type': '_extensionType',
      'method': method
    }))
  );
111 112 113
}

/// Toggle the [debugPaintSizeEnabled] setting.
114
Future<developer.ServiceExtensionResponse> _debugPaint(String method, Map<String, String> parameters) {
115
  if (parameters.containsKey('enabled')) {
116
    debugPaintSizeEnabled = parameters['enabled'] == 'true';
Devon Carew's avatar
Devon Carew committed
117 118 119 120 121 122 123

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

127 128 129 130 131 132 133
  return new Future<developer.ServiceExtensionResponse>.value(
    new developer.ServiceExtensionResponse.result(JSON.encode({
      'type': '_extensionType',
      'method': method,
      'enabled': debugPaintSizeEnabled
    }))
  );
134 135 136
}

/// Manipulate the scheduler's [timeDilation] field.
137
Future<developer.ServiceExtensionResponse> _timeDilation(String method, Map<String, String> parameters) {
138
  if (parameters.containsKey('timeDilation')) {
139
    timeDilation = double.parse(parameters['timeDilation']);
140 141
  }

142 143 144 145 146 147 148
  return new Future<developer.ServiceExtensionResponse>.value(
    new developer.ServiceExtensionResponse.result(JSON.encode({
      'type': '_extensionType',
      'method': method,
      'timeDilation': '$timeDilation'
    }))
  );
149
}