Colour the turn change chip's counts

"3 files changed +9 −2" now shows additions in green and removals in red,
using the run Changes colours, in light and dark themes.
This commit is contained in:
2026-11-09 19:01:09 +00:00
parent 73e55b0b0c
commit 15939fcf09
3 changed files with 75 additions and 6 deletions
@@ -16,6 +16,7 @@ class ChatTurnChip extends StatelessWidget {
this.sheet,
this.open,
this.ellipsizeLabel = false,
this.labelSpans,
}) : assert(sheet != null || open != null);
final Key chipKey;
@@ -34,6 +35,10 @@ class ChatTurnChip extends StatelessWidget {
/// bounds its width (a turn footer's Wrap, an aligned row).
final bool ellipsizeLabel;
/// Styled pieces of [label] (e.g. coloured counts); [label] stays the
/// plain text. Pieces without their own style inherit the chip's style.
final List<InlineSpan> Function(BuildContext context)? labelSpans;
@override
Widget build(BuildContext context) {
final cupertino =
@@ -87,12 +92,21 @@ class ChatTurnChip extends StatelessWidget {
);
}
Text _label({int? maxLines, TextOverflow? overflow}) => Text(
label,
maxLines: maxLines,
overflow: overflow,
style: TextStyle(color: color, fontSize: 13),
);
Widget _label({int? maxLines, TextOverflow? overflow}) {
final style = TextStyle(color: color, fontSize: 13);
final spans = labelSpans;
if (spans == null) {
return Text(label, maxLines: maxLines, overflow: overflow, style: style);
}
return Builder(
builder: (context) => Text.rich(
TextSpan(children: spans(context)),
maxLines: maxLines,
overflow: overflow,
style: style,
),
);
}
Future<void> _open(BuildContext context, bool cupertino) {
if (open case final open?) return open(context, cupertino);
@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../../shell/list_detail_policy.dart';
import '../../../changes/widgets/change_style.dart'
show addedColor, removedColor;
import '../../domain/chat_turn_diffs.dart';
import '../sessions/chat_adaptive.dart';
import 'chat_turn_chip.dart';
@@ -38,6 +40,23 @@ class ChatTurnDiffChip extends StatelessWidget {
color: color,
),
label: chatTurnDiffLabel(summary),
// The counts take the run Changes colours: additions green, removals red.
labelSpans: (context) {
final files = chatTurnDiffFiles(summary);
const bold = FontWeight.w600;
return [
TextSpan(text: '${files == 1 ? '1 file' : '$files files'} changed '),
TextSpan(
text: '+${chatTurnDiffAdditions(summary)}',
style: TextStyle(color: addedColor(context), fontWeight: bold),
),
const TextSpan(text: ' '),
TextSpan(
text: '−${chatTurnDiffDeletions(summary)}',
style: TextStyle(color: removedColor(context), fontWeight: bold),
),
];
},
color: color,
semanticsLabel: chatTurnDiffSemantics(summary),
ellipsizeLabel: true,
@@ -1,3 +1,4 @@
import 'package:pipelines_app/features/changes/widgets/change_style.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -210,4 +211,39 @@ void main() {
expect(find.text('lib/a.dart'), findsOneWidget);
});
}
testWidgets('additions are green and removals red in the chip', (
tester,
) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatTurnDiffChip(
summary: const {'files': 3, 'additions': 9, 'deletions': 2},
load: () async => const {},
),
),
),
);
final context = tester.element(find.byType(ChatTurnDiffChip));
final rich = tester.widget<RichText>(
find.descendant(
of: find.byType(ChatTurnDiffChip),
matching: find.byWidgetPredicate(
(w) => w is RichText && w.text.toPlainText().contains('changed'),
),
),
);
expect(rich.text.toPlainText(), '3 files changed +9 −2');
final colours = <String, Color?>{};
rich.text.visitChildren((span) {
if (span is TextSpan && span.text != null) {
colours[span.text!] = span.style?.color;
}
return true;
});
expect(colours['+9'], addedColor(context));
expect(colours['−2'], removedColor(context));
expect(addedColor(context), isNot(removedColor(context)));
});
}