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

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

13
// Line length to fit small phones without dynamically checking size.
14
const String _kLine = '\n\n────────────────────\n\n';
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
/// 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.
30
class RenderErrorBox extends RenderBox {
31
  /// Creates a RenderErrorBox render object.
32 33 34 35 36 37 38 39 40 41 42 43 44 45
  ///
  /// 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.
46
        final ui.ParagraphBuilder builder = new ui.ParagraphBuilder(paragraphStyle);
47
        builder.pushStyle(textStyle);
48
        builder.addText(
49 50
          '$message$_kLine$message$_kLine$message$_kLine$message$_kLine$message$_kLine$message$_kLine'
          '$message$_kLine$message$_kLine$message$_kLine$message$_kLine$message$_kLine$message'
51
        );
52
        _paragraph = builder.build();
53
      }
54
    } catch (e) { } // ignore: empty_catches
55 56 57 58 59 60
  }

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

  ui.Paragraph _paragraph;
61

62
  @override
63
  double computeMaxIntrinsicWidth(double height) {
64
    return _kMaxWidth;
65 66
  }

67
  @override
68
  double computeMaxIntrinsicHeight(double width) {
69
    return _kMaxHeight;
70 71
  }

72
  @override
73 74
  bool get sizedByParent => true;

75
  @override
76
  bool hitTestSelf(Offset position) => true;
Adam Barth's avatar
Adam Barth committed
77

78
  @override
79 80 81 82
  void performResize() {
    size = constraints.constrain(const Size(_kMaxWidth, _kMaxHeight));
  }

83 84 85 86 87
  /// 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(
88
    color: const Color(0xFFFFFF66),
89
    fontFamily: 'monospace',
90 91
    fontSize: 14.0,
    fontWeight: FontWeight.bold
92 93 94 95
  );

  /// The paragraph style to use when painting [RenderErrorBox] objects.
  static ui.ParagraphStyle paragraphStyle = new ui.ParagraphStyle(
96
    lineHeight: 1.0,
97 98
  );

99
  @override
100
  void paint(PaintingContext context, Offset offset) {
101 102
    try {
      context.canvas.drawRect(offset & size, new Paint() .. color = backgroundColor);
Adam Barth's avatar
Adam Barth committed
103
      double width;
104 105 106 107
      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) {
108
          final RenderBox parentBox = parent;
Adam Barth's avatar
Adam Barth committed
109
          width = parentBox.size.width;
110
        } else {
Adam Barth's avatar
Adam Barth committed
111
          width = size.width;
112
        }
Adam Barth's avatar
Adam Barth committed
113
        _paragraph.layout(new ui.ParagraphConstraints(width: width));
114

Adam Barth's avatar
Adam Barth committed
115
        context.canvas.drawParagraph(_paragraph, offset);
116
      }
117
    } catch (e) { } // ignore: empty_catches
118 119
  }
}