Files
pipelines/app/lib/features/chat/ui/transcript/notebook/notebook_glyph.dart
T

84 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
/// The small marks a notebook's sub-cells draw: the collapse chevron and the
/// call's status.
enum NotebookGlyphKind { collapsed, expanded, done, failed, running }
/// One [NotebookGlyphKind] stroked in [color] in a [size] square. Painted,
/// not an [Icon] (which builds five widgets), so a long transcript of tool
/// cells stays as cheap to scroll as the other designs'.
class NotebookGlyph extends StatelessWidget {
const NotebookGlyph(
this.kind, {
super.key,
required this.color,
this.size = 16,
});
final NotebookGlyphKind kind;
final Color color;
final double size;
@override
Widget build(BuildContext context) => CustomPaint(
size: Size.square(size),
painter: _GlyphPainter(kind, color, Directionality.of(context)),
);
}
class _GlyphPainter extends CustomPainter {
const _GlyphPainter(this.kind, this.color, this.direction);
final NotebookGlyphKind kind;
final Color color;
final TextDirection direction;
@override
void paint(Canvas canvas, Size size) {
final s = size.width;
final stroke = Paint()
..color = color
..style = PaintingStyle.stroke
..strokeWidth = (s / 10).clamp(1.2, 2.0)
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round;
// Points on a unit square, mirrored for a right-to-left chevron.
Offset p(double x, double y) => Offset(
(kind == NotebookGlyphKind.collapsed && direction == TextDirection.rtl
? 1 - x
: x) *
s,
y * s,
);
Path line(List<Offset> points) {
final path = Path()..moveTo(points.first.dx, points.first.dy);
for (final point in points.skip(1)) {
path.lineTo(point.dx, point.dy);
}
return path;
}
switch (kind) {
case NotebookGlyphKind.collapsed:
canvas.drawPath(line([p(.4, .28), p(.62, .5), p(.4, .72)]), stroke);
case NotebookGlyphKind.expanded:
canvas.drawPath(line([p(.28, .4), p(.5, .62), p(.72, .4)]), stroke);
case NotebookGlyphKind.done:
canvas.drawPath(line([p(.22, .52), p(.42, .7), p(.78, .3)]), stroke);
case NotebookGlyphKind.failed:
canvas
..drawLine(p(.28, .28), p(.72, .72), stroke)
..drawLine(p(.72, .28), p(.28, .72), stroke);
case NotebookGlyphKind.running:
final dot = Paint()..color = color;
for (final x in [.25, .5, .75]) {
canvas.drawCircle(p(x, .5), s / 14, dot);
}
}
}
@override
bool shouldRepaint(_GlyphPainter old) =>
old.kind != kind || old.color != color || old.direction != direction;
}