Separate chunked reply paragraphs with an empty-line gap
Consecutive text parts of an agent turn (including folded messages) and markdown blocks within a part are now separated by ~0.9x the scaled body font size; a heading stays 8px from what it introduces. Adds a ?paras=1 chunked reply to the composer probe and gap tests at 1x/2x.
This commit is contained in:
@@ -6,6 +6,7 @@ import 'package:flutter/semantics.dart';
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_markdown.dart';
|
||||
import 'chat_message_bubble.dart';
|
||||
|
||||
/// Renders one assistant text part with native text widgets and the host's own
|
||||
/// theme roles. Link activation is the caller's job: [onOpenLink] receives only
|
||||
@@ -66,20 +67,31 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
if (_rendered != null) return _rendered!;
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final gap = chatParagraphGap(context);
|
||||
final blocks = parseMarkdown(widget.text);
|
||||
return _rendered = _selectable(
|
||||
context,
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 8,
|
||||
children: [
|
||||
for (final block in parseMarkdown(widget.text))
|
||||
_block(context, block, cupertino),
|
||||
for (var index = 0; index < blocks.length; index += 1)
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: index == 0 ? 0 : _gapAfter(blocks[index - 1], gap),
|
||||
),
|
||||
child: _block(context, blocks[index], cupertino),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Blocks are separated by an empty line of body text, like paragraphs;
|
||||
/// a heading stays close to the content it introduces.
|
||||
double _gapAfter(MarkdownBlock previous, double paragraphGap) =>
|
||||
previous is MarkdownHeading ? 8 : paragraphGap;
|
||||
|
||||
/// Selection is provided by the native [SelectionArea]. A Cupertino-only app
|
||||
/// has no `MaterialLocalizations`, which `SelectionArea` asserts on, so one
|
||||
/// English default localization scope is added there for its context menu.
|
||||
|
||||
@@ -66,3 +66,11 @@ TextStyle? chatMessageTextStyle(BuildContext context, bool cupertino) =>
|
||||
cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyLarge;
|
||||
|
||||
/// The empty-line gap between two paragraphs of a reply: about one line of
|
||||
/// body text at [fontSize] (else the ambient text size), scaled by the
|
||||
/// operator's text scaling, so chunks read as separate paragraphs.
|
||||
double chatParagraphGap(BuildContext context, [double? fontSize]) {
|
||||
final size = fontSize ?? DefaultTextStyle.of(context).style.fontSize ?? 14;
|
||||
return MediaQuery.textScalerOf(context).scale(size) * 0.9;
|
||||
}
|
||||
|
||||
@@ -228,10 +228,25 @@ class ChatMessageView extends StatelessWidget {
|
||||
final widgets = <Widget>[];
|
||||
final parts = _partsOf(row);
|
||||
final seen = <String, int>{};
|
||||
// Long replies arrive as several text parts (and folded messages); an
|
||||
// empty line between consecutive ones keeps them reading as paragraphs.
|
||||
final gap = chatParagraphGap(
|
||||
context,
|
||||
chatMessageTextStyle(context, _isCupertino(context))?.fontSize,
|
||||
);
|
||||
var previousText = false;
|
||||
for (var index = 0; index < parts.length; index++) {
|
||||
final part = parts[index];
|
||||
final widget = _agentPart(context, part);
|
||||
var widget = _agentPart(context, part);
|
||||
if (widget == null) continue;
|
||||
final text = _isTextPart(part);
|
||||
if (text && previousText) {
|
||||
widget = Padding(
|
||||
padding: EdgeInsets.only(top: gap),
|
||||
child: widget,
|
||||
);
|
||||
}
|
||||
previousText = text;
|
||||
// Part identity, not position, so an older assistant message that folds
|
||||
// into the same turn can prepend its parts without moving an open
|
||||
// disclosure. The enclosing row's identity is carried by ChatRowKeys; a
|
||||
@@ -319,6 +334,11 @@ List<JsonMap> _partsOf(Map<dynamic, dynamic> source) {
|
||||
.toList();
|
||||
}
|
||||
|
||||
bool _isTextPart(JsonMap part) =>
|
||||
(part['type'] == null || part['type'] == 'text') &&
|
||||
part['text'] is String &&
|
||||
(part['text'] as String).trim().isNotEmpty;
|
||||
|
||||
bool _isFile(JsonMap part) =>
|
||||
part['type'] == 'file' &&
|
||||
'${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty;
|
||||
|
||||
Reference in New Issue
Block a user