error.dart 4.13 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 6
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphStyle, TextStyle;

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 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
  /// Constructs a RenderErrorBox render object.
  ///
  /// 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 58 59 60
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    return constraints.constrainWidth(0.0);
  }

61
  @override
62 63 64 65
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    return constraints.constrainWidth(_kMaxWidth);
  }

66
  @override
67 68 69 70
  double getMinIntrinsicHeight(BoxConstraints constraints) {
    return constraints.constrainHeight(0.0);
  }

71
  @override
72 73 74 75
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    return constraints.constrainHeight(_kMaxHeight);
  }

76
  @override
77 78
  bool get sizedByParent => true;

79
  @override
Adam Barth's avatar
Adam Barth committed
80 81
  bool hitTestSelf(Point position) => true;

82
  @override
83 84 85 86
  void performResize() {
    size = constraints.constrain(const Size(_kMaxWidth, _kMaxHeight));
  }

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  /// 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
  );

102
  @override
103
  void paint(PaintingContext context, Offset offset) {
104 105 106 107 108 109 110 111 112 113 114 115
    try {
      context.canvas.drawRect(offset & size, new Paint() .. color = backgroundColor);
      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;
          _paragraph.maxWidth = parentBox.size.width;
        } else {
          _paragraph.maxWidth = size.width;
        }
        _paragraph.layout();
Adam Barth's avatar
Adam Barth committed
116
        context.canvas.drawParagraph(_paragraph, offset);
117 118
      }
    } catch (e) { }
119 120
  }
}