Show discreet local timestamps on chat messages

This commit is contained in:
2026-11-05 22:00:13 +00:00
parent 5b0c46b27a
commit 9180e84109
8 changed files with 311 additions and 1 deletions
@@ -0,0 +1,77 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../sessions/chat_adaptive.dart';
/// The small, non-selectable metadata line for a recorded chat message time.
/// The visible value includes a local date so messages from different days are
/// not ambiguous; the tooltip and semantics expose the full localized value.
class ChatMessageTimestamp extends StatelessWidget {
const ChatMessageTimestamp({super.key, required this.createdAt});
final int? createdAt;
@override
Widget build(BuildContext context) {
final instant = createdAt == null
? null
: DateTime.fromMillisecondsSinceEpoch(
createdAt!,
isUtc: true,
).toLocal();
if (instant == null) return const SizedBox.shrink();
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final material = Localizations.of<MaterialLocalizations>(
context,
MaterialLocalizations,
);
final String date, time, full;
if (material != null) {
time = material.formatTimeOfDay(
TimeOfDay.fromDateTime(instant),
alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context),
);
date = material.formatCompactDate(instant);
full = '${material.formatFullDate(instant)}, $time';
} else {
final local = CupertinoLocalizations.of(context);
final use24 = MediaQuery.alwaysUse24HourFormatOf(context);
final hour = use24
? instant.hour
: (instant.hour % 12 == 0 ? 12 : instant.hour % 12);
final period = use24
? ''
: ' ${instant.hour < 12 ? local.anteMeridiemAbbreviation : local.postMeridiemAbbreviation}';
time =
'${local.datePickerHour(hour)}:${local.datePickerMinute(instant.minute)}$period';
date =
'${local.datePickerMediumDate(instant)} ${local.datePickerYear(instant.year)}';
full = '$date, $time';
}
final style =
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: (Theme.of(context).textTheme.bodySmall ?? const TextStyle()))
.copyWith(
color: cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: 12,
);
final label = 'Sent $full';
final text = Text('$date · $time', style: style);
return SelectionContainer.disabled(
child: Tooltip(
message: full,
child: Semantics(
container: true,
label: label,
excludeSemantics: true,
child: text,
),
),
);
}
}
@@ -8,11 +8,13 @@ import '../../data/chat_attachment_images.dart';
import '../sessions/chat_adaptive.dart';
import 'chat_attachment_gallery.dart';
import '../../domain/chat_tool_calls.dart';
import '../../domain/chat_transcript.dart';
import 'chat_copy_button.dart';
import 'chat_link_text.dart';
import 'chat_message_bubble.dart';
import 'chat_turn_activity_view.dart';
import 'native_disclosure.dart';
import 'chat_message_timestamp.dart';
/// Builds an assistant text part; the default keeps the server's text exactly.
typedef ChatMessageTextBuilder = Widget Function(BuildContext, String);
@@ -112,7 +114,11 @@ class ChatMessageView extends StatelessWidget {
for (final part in _partsOf(_userMessage))
if (_isFile(part) && chatPartIsThumbnail(part)) part,
];
if (thumbnails.isEmpty && footer.isEmpty) {
final timestamp = ChatMessageTimestamp(
createdAt: chatMessageCreatedAt(_userMessage),
);
final hasTimestamp = chatMessageCreatedAt(_userMessage) != null;
if (thumbnails.isEmpty && footer.isEmpty && !hasTimestamp) {
return ChatUserBubble(cupertino: cupertino, child: body);
}
return Column(
@@ -139,6 +145,7 @@ class ChatMessageView extends StatelessWidget {
children: footer,
),
],
if (hasTimestamp) ...[const SizedBox(height: 2), timestamp],
],
);
}
@@ -196,6 +203,8 @@ class ChatMessageView extends StatelessWidget {
color: _secondaryColor(context, _isCupertino(context)),
),
),
if (chatMessageCreatedAt(row) case final time?)
ChatMessageTimestamp(createdAt: time),
];
return Column(
mainAxisSize: MainAxisSize.min,