sector_layout.dart 22.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

import 'package:flutter/rendering.dart';

9
const double kTwoPi = 2 * math.pi;
Hixie's avatar
Hixie committed
10 11 12

class SectorConstraints extends Constraints {
  const SectorConstraints({
13 14 15
    this.minDeltaRadius = 0.0,
    this.maxDeltaRadius = double.infinity,
    this.minDeltaTheta = 0.0,
16
    this.maxDeltaTheta = kTwoPi,
17 18
  }) : assert(maxDeltaRadius >= minDeltaRadius),
       assert(maxDeltaTheta >= minDeltaTheta);
Hixie's avatar
Hixie committed
19

20
  const SectorConstraints.tight({ double deltaRadius = 0.0, double deltaTheta = 0.0 })
Hixie's avatar
Hixie committed
21 22 23 24 25 26 27 28 29 30 31
    : minDeltaRadius = deltaRadius,
      maxDeltaRadius = deltaRadius,
      minDeltaTheta = deltaTheta,
      maxDeltaTheta = deltaTheta;

  final double minDeltaRadius;
  final double maxDeltaRadius;
  final double minDeltaTheta;
  final double maxDeltaTheta;

  double constrainDeltaRadius(double deltaRadius) {
32
    return deltaRadius.clamp(minDeltaRadius, maxDeltaRadius);
Hixie's avatar
Hixie committed
33 34 35
  }

  double constrainDeltaTheta(double deltaTheta) {
36
    return deltaTheta.clamp(minDeltaTheta, maxDeltaTheta);
Hixie's avatar
Hixie committed
37 38
  }

39
  @override
Hixie's avatar
Hixie committed
40
  bool get isTight => minDeltaTheta >= maxDeltaTheta && minDeltaTheta >= maxDeltaTheta;
41

42
  @override
43
  bool get isNormalized => minDeltaRadius <= maxDeltaRadius && minDeltaTheta <= maxDeltaTheta;
44 45

  @override
46
  bool debugAssertIsValid({
47
    bool isAppliedConstraint = false,
48
    InformationCollector? informationCollector,
49
  }) {
50 51 52
    assert(isNormalized);
    return isNormalized;
  }
Hixie's avatar
Hixie committed
53 54 55
}

class SectorDimensions {
56
  const SectorDimensions({ this.deltaRadius = 0.0, this.deltaTheta = 0.0 });
Hixie's avatar
Hixie committed
57 58

  factory SectorDimensions.withConstraints(
59 60 61 62
    SectorConstraints constraints, {
    double deltaRadius = 0.0,
    double deltaTheta = 0.0,
  }) {
63
    return SectorDimensions(
Hixie's avatar
Hixie committed
64
      deltaRadius: constraints.constrainDeltaRadius(deltaRadius),
65
      deltaTheta: constraints.constrainDeltaTheta(deltaTheta),
Hixie's avatar
Hixie committed
66 67 68 69 70 71 72 73 74 75 76 77
    );
  }

  final double deltaRadius;
  final double deltaTheta;
}

class SectorParentData extends ParentData {
  double radius = 0.0;
  double theta = 0.0;
}

78 79 80 81 82 83 84 85 86 87 88
/// Base class for [RenderObject]s that live in a polar coordinate space.
///
/// In a polar coordinate system each point on a plane is determined by a
/// distance from a reference point ("radius") and an angle from a reference
/// direction ("theta").
///
/// See also:
///
///  * <https://en.wikipedia.org/wiki/Polar_coordinate_system>, which defines
///    the polar coordinate space.
///  * [RenderBox], which is the base class for [RenderObject]s that live in a
89
///    Cartesian coordinate space.
Hixie's avatar
Hixie committed
90 91
abstract class RenderSector extends RenderObject {

92
  @override
Hixie's avatar
Hixie committed
93
  void setupParentData(RenderObject child) {
94
    if (child.parentData is! SectorParentData) {
95
      child.parentData = SectorParentData();
96
    }
Hixie's avatar
Hixie committed
97 98 99 100
  }

  // RenderSectors always use SectorParentData subclasses, as they need to be
  // able to read their position information for painting and hit testing.
101
  @override
102
  SectorParentData? get parentData => super.parentData as SectorParentData?;
Hixie's avatar
Hixie committed
103 104

  SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
105
    return SectorDimensions.withConstraints(constraints);
Hixie's avatar
Hixie committed
106 107
  }

108
  @override
109
  SectorConstraints get constraints => super.constraints as SectorConstraints;
110 111

  @override
112
  void debugAssertDoesMeetConstraints() {
113 114
    assert(deltaRadius < double.infinity);
    assert(deltaTheta < double.infinity);
115 116 117 118
    assert(constraints.minDeltaRadius <= deltaRadius);
    assert(deltaRadius <= math.max(constraints.minDeltaRadius, constraints.maxDeltaRadius));
    assert(constraints.minDeltaTheta <= deltaTheta);
    assert(deltaTheta <= math.max(constraints.minDeltaTheta, constraints.maxDeltaTheta));
Hixie's avatar
Hixie committed
119
  }
120 121

  @override
Hixie's avatar
Hixie committed
122
  void performResize() {
123
    // default behavior for subclasses that have sizedByParent = true
Hixie's avatar
Hixie committed
124 125 126
    deltaRadius = constraints.constrainDeltaRadius(0.0);
    deltaTheta = constraints.constrainDeltaTheta(0.0);
  }
127 128

  @override
Hixie's avatar
Hixie committed
129 130 131 132 133 134 135
  void performLayout() {
    // descendants have to either override performLayout() to set both
    // the dimensions and lay out children, or, set sizedByParent to
    // true so that performResize()'s logic above does its thing.
    assert(sizedByParent);
  }

136
  @override
137
  Rect get paintBounds => Rect.fromLTWH(0.0, 0.0, 2.0 * deltaRadius, 2.0 * deltaRadius);
138 139

  @override
140
  Rect get semanticBounds => Rect.fromLTWH(-deltaRadius, -deltaRadius, 2.0 * deltaRadius, 2.0 * deltaRadius);
Hixie's avatar
Hixie committed
141

142 143
  bool hitTest(SectorHitTestResult result, { required double radius, required double theta }) {
    if (radius < parentData!.radius || radius >= parentData!.radius + deltaRadius ||
144
        theta < parentData!.theta || theta >= parentData!.theta + deltaTheta) {
Hixie's avatar
Hixie committed
145
      return false;
146
    }
Hixie's avatar
Hixie committed
147
    hitTestChildren(result, radius: radius, theta: theta);
148
    result.add(SectorHitTestEntry(this, radius: radius, theta: theta));
Hixie's avatar
Hixie committed
149 150
    return true;
  }
151
  void hitTestChildren(SectorHitTestResult result, { required double radius, required double theta }) { }
Hixie's avatar
Hixie committed
152

153 154
  late double deltaRadius;
  late double deltaTheta;
Hixie's avatar
Hixie committed
155 156 157 158
}

abstract class RenderDecoratedSector extends RenderSector {

159
  RenderDecoratedSector(BoxDecoration? decoration) : _decoration = decoration;
Hixie's avatar
Hixie committed
160

161 162 163
  BoxDecoration? _decoration;
  BoxDecoration? get decoration => _decoration;
  set decoration(BoxDecoration? value) {
164
    if (value == _decoration) {
Hixie's avatar
Hixie committed
165
      return;
166
    }
Hixie's avatar
Hixie committed
167 168 169 170 171
    _decoration = value;
    markNeedsPaint();
  }

  // offset must point to the center of the circle
172
  @override
Hixie's avatar
Hixie committed
173 174 175
  void paint(PaintingContext context, Offset offset) {
    assert(parentData is SectorParentData);

176
    if (_decoration == null) {
Hixie's avatar
Hixie committed
177
      return;
178
    }
Hixie's avatar
Hixie committed
179

180
    if (_decoration!.color != null) {
Adam Barth's avatar
Adam Barth committed
181
      final Canvas canvas = context.canvas;
182
      final Paint paint = Paint()..color = _decoration!.color!;
183
      final Path path = Path();
184
      final double outerRadius = parentData!.radius + deltaRadius;
185
      final Rect outerBounds = Rect.fromLTRB(offset.dx-outerRadius, offset.dy-outerRadius, offset.dx+outerRadius, offset.dy+outerRadius);
186 187
      path.arcTo(outerBounds, parentData!.theta, deltaTheta, true);
      final double innerRadius = parentData!.radius;
188
      final Rect innerBounds = Rect.fromLTRB(offset.dx-innerRadius, offset.dy-innerRadius, offset.dx+innerRadius, offset.dy+innerRadius);
189
      path.arcTo(innerBounds, parentData!.theta + deltaTheta, -deltaTheta, false);
Hixie's avatar
Hixie committed
190 191 192 193 194 195 196 197 198 199
      path.close();
      canvas.drawPath(path, paint);
    }
  }

}

class SectorChildListParentData extends SectorParentData with ContainerParentDataMixin<RenderSector> { }

class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRenderObjectMixin<RenderSector, SectorChildListParentData> {
200
  RenderSectorWithChildren(super.decoration);
Hixie's avatar
Hixie committed
201

202
  @override
203 204
  void hitTestChildren(SectorHitTestResult result, { required double radius, required double theta }) {
    RenderSector? child = lastChild;
Hixie's avatar
Hixie committed
205
    while (child != null) {
206
      if (child.hitTest(result, radius: radius, theta: theta)) {
Hixie's avatar
Hixie committed
207
        return;
208
      }
209
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
210 211 212 213
      child = childParentData.previousSibling;
    }
  }

214
  @override
Hixie's avatar
Hixie committed
215
  void visitChildren(RenderObjectVisitor visitor) {
216
    RenderSector? child = lastChild;
Hixie's avatar
Hixie committed
217 218
    while (child != null) {
      visitor(child);
219
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
220 221 222 223 224 225 226 227 228
      child = childParentData.previousSibling;
    }
  }
}

class RenderSectorRing extends RenderSectorWithChildren {
  // lays out RenderSector children in a ring

  RenderSectorRing({
229
    BoxDecoration? decoration,
230
    double deltaRadius = double.infinity,
231
    double padding = 0.0,
232 233 234 235
  }) : _padding = padding,
       assert(deltaRadius >= 0.0),
       _desiredDeltaRadius = deltaRadius,
       super(decoration);
Hixie's avatar
Hixie committed
236 237 238

  double _desiredDeltaRadius;
  double get desiredDeltaRadius => _desiredDeltaRadius;
239
  set desiredDeltaRadius(double value) {
240
    assert(value >= 0);
Hixie's avatar
Hixie committed
241 242 243 244 245 246 247 248
    if (_desiredDeltaRadius != value) {
      _desiredDeltaRadius = value;
      markNeedsLayout();
    }
  }

  double _padding;
  double get padding => _padding;
249
  set padding(double value) {
Hixie's avatar
Hixie committed
250 251 252 253 254 255 256
    // TODO(ianh): avoid code duplication
    if (_padding != value) {
      _padding = value;
      markNeedsLayout();
    }
  }

257
  @override
Hixie's avatar
Hixie committed
258 259
  void setupParentData(RenderObject child) {
    // TODO(ianh): avoid code duplication
260
    if (child.parentData is! SectorChildListParentData) {
261
      child.parentData = SectorChildListParentData();
262
    }
Hixie's avatar
Hixie committed
263 264
  }

265
  @override
Hixie's avatar
Hixie committed
266
  SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
267
    final double outerDeltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
268
    final double innerDeltaRadius = math.max(0.0, outerDeltaRadius - padding * 2.0);
269 270
    final double childRadius = radius + padding;
    final double paddingTheta = math.atan(padding / (radius + outerDeltaRadius));
Hixie's avatar
Hixie committed
271
    double innerTheta = paddingTheta; // increments with each child
272
    double remainingDeltaTheta = math.max(0.0, constraints.maxDeltaTheta - (innerTheta + paddingTheta));
273
    RenderSector? child = firstChild;
Hixie's avatar
Hixie committed
274
    while (child != null) {
275
      final SectorConstraints innerConstraints = SectorConstraints(
Hixie's avatar
Hixie committed
276
        maxDeltaRadius: innerDeltaRadius,
277
        maxDeltaTheta: remainingDeltaTheta,
Hixie's avatar
Hixie committed
278
      );
279
      final SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius);
Hixie's avatar
Hixie committed
280 281
      innerTheta += childDimensions.deltaTheta;
      remainingDeltaTheta -= childDimensions.deltaTheta;
282
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
283 284 285 286 287 288
      child = childParentData.nextSibling;
      if (child != null) {
        innerTheta += paddingTheta;
        remainingDeltaTheta -= paddingTheta;
      }
    }
289 290 291 292 293
    return SectorDimensions.withConstraints(
      constraints,
      deltaRadius: outerDeltaRadius,
      deltaTheta: innerTheta,
    );
Hixie's avatar
Hixie committed
294 295
  }

296
  @override
Hixie's avatar
Hixie committed
297
  void performLayout() {
298
    assert(parentData is SectorParentData);
Hixie's avatar
Hixie committed
299
    deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
300
    assert(deltaRadius < double.infinity);
301
    final double innerDeltaRadius = deltaRadius - padding * 2.0;
302 303
    final double childRadius = parentData!.radius + padding;
    final double paddingTheta = math.atan(padding / (parentData!.radius + deltaRadius));
Hixie's avatar
Hixie committed
304 305
    double innerTheta = paddingTheta; // increments with each child
    double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddingTheta);
306
    RenderSector? child = firstChild;
Hixie's avatar
Hixie committed
307
    while (child != null) {
308
      final SectorConstraints innerConstraints = SectorConstraints(
Hixie's avatar
Hixie committed
309
        maxDeltaRadius: innerDeltaRadius,
310
        maxDeltaTheta: remainingDeltaTheta,
Hixie's avatar
Hixie committed
311 312
      );
      assert(child.parentData is SectorParentData);
313 314
      child.parentData!.theta = innerTheta;
      child.parentData!.radius = childRadius;
Hixie's avatar
Hixie committed
315 316 317
      child.layout(innerConstraints, parentUsesSize: true);
      innerTheta += child.deltaTheta;
      remainingDeltaTheta -= child.deltaTheta;
318
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
319 320 321 322 323 324 325 326 327 328 329
      child = childParentData.nextSibling;
      if (child != null) {
        innerTheta += paddingTheta;
        remainingDeltaTheta -= paddingTheta;
      }
    }
    deltaTheta = innerTheta;
  }

  // offset must point to the center of our circle
  // each sector then knows how to paint itself at its location
330
  @override
Hixie's avatar
Hixie committed
331 332 333
  void paint(PaintingContext context, Offset offset) {
    // TODO(ianh): avoid code duplication
    super.paint(context, offset);
334
    RenderSector? child = firstChild;
Hixie's avatar
Hixie committed
335
    while (child != null) {
Adam Barth's avatar
Adam Barth committed
336
      context.paintChild(child, offset);
337
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
338 339 340 341 342 343 344 345 346 347
      child = childParentData.nextSibling;
    }
  }

}

class RenderSectorSlice extends RenderSectorWithChildren {
  // lays out RenderSector children in a stack

  RenderSectorSlice({
348
    BoxDecoration? decoration,
349
    double deltaTheta = kTwoPi,
350
    double padding = 0.0,
Hixie's avatar
Hixie committed
351 352 353 354
  }) : _padding = padding, _desiredDeltaTheta = deltaTheta, super(decoration);

  double _desiredDeltaTheta;
  double get desiredDeltaTheta => _desiredDeltaTheta;
355
  set desiredDeltaTheta(double value) {
Hixie's avatar
Hixie committed
356 357 358 359 360 361 362 363
    if (_desiredDeltaTheta != value) {
      _desiredDeltaTheta = value;
      markNeedsLayout();
    }
  }

  double _padding;
  double get padding => _padding;
364
  set padding(double value) {
Hixie's avatar
Hixie committed
365 366 367 368 369 370 371
    // TODO(ianh): avoid code duplication
    if (_padding != value) {
      _padding = value;
      markNeedsLayout();
    }
  }

372
  @override
Hixie's avatar
Hixie committed
373 374
  void setupParentData(RenderObject child) {
    // TODO(ianh): avoid code duplication
375
    if (child.parentData is! SectorChildListParentData) {
376
      child.parentData = SectorChildListParentData();
377
    }
Hixie's avatar
Hixie committed
378 379
  }

380
  @override
Hixie's avatar
Hixie committed
381
  SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
382
    assert(parentData is SectorParentData);
383
    final double paddingTheta = math.atan(padding / parentData!.radius);
384 385
    final double outerDeltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
    final double innerDeltaTheta = outerDeltaTheta - paddingTheta * 2.0;
386
    double childRadius = parentData!.radius + padding;
Hixie's avatar
Hixie committed
387
    double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0);
388
    RenderSector? child = firstChild;
Hixie's avatar
Hixie committed
389
    while (child != null) {
390
      final SectorConstraints innerConstraints = SectorConstraints(
Hixie's avatar
Hixie committed
391
        maxDeltaRadius: remainingDeltaRadius,
392
        maxDeltaTheta: innerDeltaTheta,
Hixie's avatar
Hixie committed
393
      );
394
      final SectorDimensions childDimensions = child.getIntrinsicDimensions(innerConstraints, childRadius);
Hixie's avatar
Hixie committed
395 396
      childRadius += childDimensions.deltaRadius;
      remainingDeltaRadius -= childDimensions.deltaRadius;
397
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
398 399 400 401
      child = childParentData.nextSibling;
      childRadius += padding;
      remainingDeltaRadius -= padding;
    }
402 403 404 405 406
    return SectorDimensions.withConstraints(
      constraints,
      deltaRadius: childRadius - parentData!.radius,
      deltaTheta: outerDeltaTheta,
    );
Hixie's avatar
Hixie committed
407 408
  }

409
  @override
Hixie's avatar
Hixie committed
410
  void performLayout() {
411
    assert(parentData is SectorParentData);
Hixie's avatar
Hixie committed
412 413
    deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
    assert(deltaTheta <= kTwoPi);
414 415
    final double paddingTheta = math.atan(padding / parentData!.radius);
    final double innerTheta = parentData!.theta + paddingTheta;
416
    final double innerDeltaTheta = deltaTheta - paddingTheta * 2.0;
417
    double childRadius = parentData!.radius + padding;
Hixie's avatar
Hixie committed
418
    double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0);
419
    RenderSector? child = firstChild;
Hixie's avatar
Hixie committed
420
    while (child != null) {
421
      final SectorConstraints innerConstraints = SectorConstraints(
Hixie's avatar
Hixie committed
422
        maxDeltaRadius: remainingDeltaRadius,
423
        maxDeltaTheta: innerDeltaTheta,
Hixie's avatar
Hixie committed
424
      );
425 426
      child.parentData!.theta = innerTheta;
      child.parentData!.radius = childRadius;
Hixie's avatar
Hixie committed
427 428 429
      child.layout(innerConstraints, parentUsesSize: true);
      childRadius += child.deltaRadius;
      remainingDeltaRadius -= child.deltaRadius;
430
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
431 432 433 434
      child = childParentData.nextSibling;
      childRadius += padding;
      remainingDeltaRadius -= padding;
    }
435
    deltaRadius = childRadius - parentData!.radius;
Hixie's avatar
Hixie committed
436 437 438 439
  }

  // offset must point to the center of our circle
  // each sector then knows how to paint itself at its location
440
  @override
Hixie's avatar
Hixie committed
441 442 443
  void paint(PaintingContext context, Offset offset) {
    // TODO(ianh): avoid code duplication
    super.paint(context, offset);
444
    RenderSector? child = firstChild;
Hixie's avatar
Hixie committed
445 446
    while (child != null) {
      assert(child.parentData is SectorChildListParentData);
Adam Barth's avatar
Adam Barth committed
447
      context.paintChild(child, offset);
448
      final SectorChildListParentData childParentData = child.parentData! as SectorChildListParentData;
Hixie's avatar
Hixie committed
449 450 451 452 453 454 455
      child = childParentData.nextSibling;
    }
  }

}

class RenderBoxToRenderSectorAdapter extends RenderBox with RenderObjectWithChildMixin<RenderSector> {
456
  RenderBoxToRenderSectorAdapter({ double innerRadius = 0.0, RenderSector? child })
457
    : _innerRadius = innerRadius {
Hixie's avatar
Hixie committed
458 459 460 461 462
    this.child = child;
  }

  double _innerRadius;
  double get innerRadius => _innerRadius;
463
  set innerRadius(double value) {
Hixie's avatar
Hixie committed
464 465 466 467
    _innerRadius = value;
    markNeedsLayout();
  }

468
  @override
Hixie's avatar
Hixie committed
469
  void setupParentData(RenderObject child) {
470
    if (child.parentData is! SectorParentData) {
471
      child.parentData = SectorParentData();
472
    }
Hixie's avatar
Hixie committed
473 474
  }

475
  @override
476
  double computeMinIntrinsicWidth(double height) {
477
    if (child == null) {
478
      return 0.0;
479
    }
480
    return getIntrinsicDimensions(height: height).width;
Hixie's avatar
Hixie committed
481 482
  }

483
  @override
484
  double computeMaxIntrinsicWidth(double height) {
485
    if (child == null) {
486
      return 0.0;
487
    }
488
    return getIntrinsicDimensions(height: height).width;
Hixie's avatar
Hixie committed
489 490
  }

491
  @override
492
  double computeMinIntrinsicHeight(double width) {
493
    if (child == null) {
494
      return 0.0;
495
    }
496
    return getIntrinsicDimensions(width: width).height;
Hixie's avatar
Hixie committed
497 498
  }

499
  @override
500
  double computeMaxIntrinsicHeight(double width) {
501
    if (child == null) {
502
      return 0.0;
503
    }
504
    return getIntrinsicDimensions(width: width).height;
Hixie's avatar
Hixie committed
505 506
  }

507
  Size getIntrinsicDimensions({
508
    double width = double.infinity,
509
    double height = double.infinity,
510
  }) {
Hixie's avatar
Hixie committed
511
    assert(child is RenderSector);
512
    assert(child!.parentData is SectorParentData);
513
    if (!width.isFinite && !height.isFinite) {
514
      return Size.zero;
515
    }
516
    final double maxChildDeltaRadius = math.max(0.0, math.min(width, height) / 2.0 - innerRadius);
517
    final SectorDimensions childDimensions = child!.getIntrinsicDimensions(SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius);
518
    final double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0;
519
    return Size.square(dimension);
Hixie's avatar
Hixie committed
520 521
  }

522
  @override
Hixie's avatar
Hixie committed
523
  void performLayout() {
524
    if (child == null || (!constraints.hasBoundedWidth && !constraints.hasBoundedHeight)) {
Hixie's avatar
Hixie committed
525
      size = constraints.constrain(Size.zero);
526
      child?.layout(SectorConstraints(maxDeltaRadius: innerRadius), parentUsesSize: true);
527
      return;
Hixie's avatar
Hixie committed
528
    }
529
    assert(child is RenderSector);
530
    assert(child!.parentData is SectorParentData);
531
    final double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxHeight) / 2.0 - innerRadius;
532 533 534 535
    child!.parentData!.radius = innerRadius;
    child!.parentData!.theta = 0.0;
    child!.layout(SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), parentUsesSize: true);
    final double dimension = (innerRadius + child!.deltaRadius) * 2.0;
536
    size = constraints.constrain(Size(dimension, dimension));
Hixie's avatar
Hixie committed
537 538
  }

539
  @override
Hixie's avatar
Hixie committed
540 541 542
  void paint(PaintingContext context, Offset offset) {
    super.paint(context, offset);
    if (child != null) {
543
      final Rect bounds = offset & size;
Hixie's avatar
Hixie committed
544
      // we move the offset to the center of the circle for the RenderSectors
545
      context.paintChild(child!, bounds.center);
Hixie's avatar
Hixie committed
546 547 548
    }
  }

549
  @override
550
  bool hitTest(BoxHitTestResult result, { required Offset position }) {
551
    if (child == null) {
Hixie's avatar
Hixie committed
552
      return false;
553
    }
554 555
    double x = position.dx;
    double y = position.dy;
Hixie's avatar
Hixie committed
556
    // translate to our origin
557 558
    x -= size.width / 2.0;
    y -= size.height / 2.0;
Hixie's avatar
Hixie committed
559
    // convert to radius/theta
560
    final double radius = math.sqrt(x * x + y * y);
561
    final double theta = (math.atan2(x, -y) - math.pi / 2.0) % kTwoPi;
562
    if (radius < innerRadius) {
Hixie's avatar
Hixie committed
563
      return false;
564 565
    }
    if (radius >= innerRadius + child!.deltaRadius) {
Hixie's avatar
Hixie committed
566
      return false;
567 568
    }
    if (theta > child!.deltaTheta) {
Hixie's avatar
Hixie committed
569
      return false;
570
    }
571
    child!.hitTest(SectorHitTestResult.wrap(result), radius: radius, theta: theta);
572
    result.add(BoxHitTestEntry(this, position));
Hixie's avatar
Hixie committed
573 574 575 576 577
    return true;
  }
}

class RenderSolidColor extends RenderDecoratedSector {
578 579
  RenderSolidColor(
    this.backgroundColor, {
580
    this.desiredDeltaRadius = double.infinity,
581
    this.desiredDeltaTheta = kTwoPi,
582
  }) : super(BoxDecoration(color: backgroundColor));
Hixie's avatar
Hixie committed
583 584 585 586 587

  double desiredDeltaRadius;
  double desiredDeltaTheta;
  final Color backgroundColor;

588
  @override
Hixie's avatar
Hixie committed
589
  SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
590
    return SectorDimensions.withConstraints(constraints, deltaTheta: desiredDeltaTheta);
Hixie's avatar
Hixie committed
591 592
  }

593
  @override
Hixie's avatar
Hixie committed
594 595 596 597 598
  void performLayout() {
    deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
    deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
  }

599
  @override
Ian Hickson's avatar
Ian Hickson committed
600 601
  void handleEvent(PointerEvent event, HitTestEntry entry) {
    if (event is PointerDownEvent) {
602
      decoration = const BoxDecoration(color: Color(0xFFFF0000));
Ian Hickson's avatar
Ian Hickson committed
603
    } else if (event is PointerUpEvent) {
604
      decoration = BoxDecoration(color: backgroundColor);
Ian Hickson's avatar
Ian Hickson committed
605
    }
Hixie's avatar
Hixie committed
606 607
  }
}
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

/// The result of performing a hit test on [RenderSector]s.
class SectorHitTestResult extends HitTestResult {
  /// Creates an empty hit test result for hit testing on [RenderSector].
  SectorHitTestResult() : super();

  /// Wraps `result` to create a [HitTestResult] that implements the
  /// [SectorHitTestResult] protocol for hit testing on [RenderSector]s.
  ///
  /// This method is used by [RenderObject]s that adapt between the
  /// [RenderSector]-world and the non-[RenderSector]-world to convert a (subtype of)
  /// [HitTestResult] to a [SectorHitTestResult] for hit testing on [RenderSector]s.
  ///
  /// The [HitTestEntry]s added to the returned [SectorHitTestResult] are also
  /// added to the wrapped `result` (both share the same underlying data
  /// structure to store [HitTestEntry]s).
  ///
  /// See also:
  ///
  ///  * [HitTestResult.wrap], which turns a [SectorHitTestResult] back into a
  ///    generic [HitTestResult].
629
  SectorHitTestResult.wrap(super.result) : super.wrap();
630 631 632

  // TODO(goderbauer): Add convenience methods to transform hit test positions
  //    once we have RenderSector implementations that move the origin of their
633
  //    children (e.g. RenderSectorTransform analogs to RenderTransform).
634 635 636 637 638
}

/// A hit test entry used by [RenderSector].
class SectorHitTestEntry extends HitTestEntry {
  /// Creates a box hit test entry.
639
  SectorHitTestEntry(RenderSector super.target, { required this.radius,  required this.theta });
640 641

  @override
642
  RenderSector get target => super.target as RenderSector;
643 644 645 646 647 648 649 650 651

  /// The radius component of the hit test position in the local coordinates of
  /// [target].
  final double radius;

  /// The theta component of the hit test position in the local coordinates of
  /// [target].
  final double theta;
}