error.dart 3.87 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.

Adam Barth's avatar
Adam Barth committed
5
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle, TextStyle;
6

7 8
import 'box.dart';
import 'object.dart';
9 10 11 12

const double _kMaxWidth = 100000.0;
const double _kMaxHeight = 100000.0;

13 14 15 16 17 18 19 20 21 22 23 24 25 26
/// A render object used as a placeholder when an error occurs.
///
/// The box will be painted in the color given by the
/// [RenderErrorBox.backgroundColor] static property.
///
/// A message can be provided. To simplify the class and thus help reduce the
/// likelihood of this class itself being the source of errors, the message
/// cannot be changed once the object has been created. If provided, the text
/// will be painted on top of the background, using the styles given by the
/// [RenderErrorBox.textStyle] and [RenderErrorBox.paragraphStyle] static
/// properties.
///
/// Again to help simplify the class, this box tries to be 100000.0 pixels wide
/// and high, to approximate being infinitely high but without using infinities.
27
class RenderErrorBox extends RenderBox {
28
  /// Creates a RenderErrorBox render object.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  ///
  /// A message can optionally be provided. If a message is provided, an attempt
  /// will be made to render the message when the box paints.
  RenderErrorBox([ this.message = '' ]) {
    try {
      if (message != '') {
        // This class is intentionally doing things using the low-level
        // primitives to avoid depending on any subsystems that may have ended
        // up in an unstable state -- after all, this class is mainly used when
        // things have gone wrong.
        //
        // Generally, the much better way to draw text in a RenderObject is to
        // use the TextPainter class. If you're looking for code to crib from,
        // see the paragraph.dart file and the RenderParagraph class.
        ui.ParagraphBuilder builder = new ui.ParagraphBuilder();
        builder.pushStyle(textStyle);
        builder.addText(message);
        _paragraph = builder.build(paragraphStyle);
      }
    } catch (e) { }
  }

  /// The message to attempt to display at paint time.
  final String message;

  ui.Paragraph _paragraph;
55

56
  @override
57
  double computeMaxIntrinsicWidth(double height) {
58
    return _kMaxWidth;
59 60
  }

61
  @override
62
  double computeMaxIntrinsicHeight(double width) {
63
    return _kMaxHeight;
64 65
  }

66
  @override
67 68
  bool get sizedByParent => true;

69
  @override
Adam Barth's avatar
Adam Barth committed
70 71
  bool hitTestSelf(Point position) => true;

72
  @override
73 74 75 76
  void performResize() {
    size = constraints.constrain(const Size(_kMaxWidth, _kMaxHeight));
  }

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  /// The color to use when painting the background of [RenderErrorBox] objects.
  static Color backgroundColor = const Color(0xF0900000);

  /// The text style to use when painting [RenderErrorBox] objects.
  static ui.TextStyle textStyle = new ui.TextStyle(
    color: const Color(0xFFFFFF00),
    fontFamily: 'monospace',
    fontSize: 7.0
  );

  /// The paragraph style to use when painting [RenderErrorBox] objects.
  static ui.ParagraphStyle paragraphStyle = new ui.ParagraphStyle(
    lineHeight: 0.25 // TODO(ianh): https://github.com/flutter/flutter/issues/2460 will affect this
  );

92
  @override
93
  void paint(PaintingContext context, Offset offset) {
94 95
    try {
      context.canvas.drawRect(offset & size, new Paint() .. color = backgroundColor);
Adam Barth's avatar
Adam Barth committed
96
      double width;
97 98 99 100 101
      if (_paragraph != null) {
        // See the comment in the RenderErrorBox constructor. This is not the
        // code you want to be copying and pasting. :-)
        if (parent is RenderBox) {
          RenderBox parentBox = parent;
Adam Barth's avatar
Adam Barth committed
102
          width = parentBox.size.width;
103
        } else {
Adam Barth's avatar
Adam Barth committed
104
          width = size.width;
105
        }
Adam Barth's avatar
Adam Barth committed
106
        _paragraph.layout(new ui.ParagraphConstraints(width: width));
Adam Barth's avatar
Adam Barth committed
107
        context.canvas.drawParagraph(_paragraph, offset);
108 109
      }
    } catch (e) { }
110 111
  }
}