Files
pipelines/app/lib/features/chat/ui/transcript/boxed/boxed_quote.dart
T

137 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../../ui/theme/app_design_tokens.dart';
import '../../sessions/chat_color_palette.dart';
import '../chat_message_bubble.dart';
/// The width of the bar in the chat's colour along a boxed turn's leading
/// edge.
const double boxedQuoteBar = 6;
/// The padding inside a boxed turn, after its bar.
const double boxedQuotePadding = 12;
/// Where a boxed turn's text starts, from the row's edge: the footer under
/// it (its delivery line and time) starts here too.
const double boxedQuoteTextInset = boxedQuoteBar + boxedQuotePadding;
/// The words of the label above the operator's boxed turn.
const String boxedQuoteLabel = 'You';
/// The operator's turn under a boxed transcript (High contrast): a "You"
/// label, then the words in a box framed in the ink at the design's stroke,
/// with a thick bar in the chat's colour (the ink for a chat with none) on
/// its leading edge. Painted in one pass: no clip layer per turn.
class BoxedQuote extends StatelessWidget {
const BoxedQuote({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
final tokens = AppDesignTokens.of(context);
final bar = chatColorShade(context, ChatBubbleColor.of(context));
return Material(
key: const Key('chat-boxed-quote'),
type: MaterialType.transparency,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ExcludeSemantics(
child: SelectionContainer.disabled(
child: Padding(
padding: const EdgeInsetsDirectional.only(bottom: 4),
child: Text(
boxedQuoteLabel,
key: const Key('chat-boxed-quote-label'),
style: theme.textTheme.labelLarge?.copyWith(
color: scheme.onSurface,
fontWeight: FontWeight.w800,
),
),
),
),
),
CustomPaint(
foregroundPainter: BoxedFramePainter(
frame: scheme.outline,
bar: bar ?? scheme.onSurface,
stroke: tokens.stroke,
radius: tokens.radius ?? 6,
barWidth: boxedQuoteBar,
textDirection: Directionality.of(context),
),
child: Padding(
padding: const EdgeInsetsDirectional.fromSTEB(
boxedQuoteTextInset,
10,
boxedQuotePadding,
10,
),
child: child,
),
),
],
),
);
}
}
/// A rounded frame of [stroke] in [frame], with a [barWidth] bar in [bar]
/// along its leading edge inside the corners. Drawn with canvas clips only,
/// so a list of them adds no layers.
class BoxedFramePainter extends CustomPainter {
const BoxedFramePainter({
required this.frame,
required this.bar,
required this.stroke,
required this.radius,
required this.textDirection,
this.barWidth = 0,
});
final Color frame;
final Color bar;
final double stroke;
final double radius;
final double barWidth;
final TextDirection textDirection;
@override
void paint(Canvas canvas, Size size) {
final outer = RRect.fromRectAndRadius(
Offset.zero & size,
Radius.circular(radius),
);
if (barWidth > 0) {
final rect = textDirection == TextDirection.ltr
? Rect.fromLTWH(0, 0, barWidth, size.height)
: Rect.fromLTWH(size.width - barWidth, 0, barWidth, size.height);
canvas
..save()
..clipRRect(outer)
..drawRect(rect, Paint()..color = bar)
..restore();
}
canvas.drawRRect(
outer.deflate(stroke / 2),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = stroke
..color = frame,
);
}
@override
bool shouldRepaint(BoxedFramePainter old) =>
old.frame != frame ||
old.bar != bar ||
old.stroke != stroke ||
old.radius != radius ||
old.barWidth != barWidth ||
old.textDirection != textDirection;
}