Files
pipelines/app/lib/features/chat/ui/transcript/chat_code_hover.dart
T
alan ca2f7eee32 App: add a selectable app design with a Terminal look
AppDesign builds each design's light and dark ThemeData with an
AppDesignTokens extension (corner radius, flat surfaces, frame colour,
ANSI accents, prompt glyph). Settings gains a Design picker stored per
device; the theme hosts rebuild live. Terminal is monospace (bundled
RobotoMono), near-black or paper, flat 1px frames with 3px corners, and
draws the chat transcript as prompt lines, tool dot/result lines and
bracketed follow-up lines. Default keeps its exact themes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 16:43:59 +00:00

194 lines
5.7 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../../../ui/theme/app_design_tokens.dart';
import 'package:flutter/rendering.dart';
import 'chat_copy_button.dart';
/// Keeps inline code as a TextSpan: only its hover control lives in an overlay.
/// No recognizers or embedded widgets intercept text selection or wrapping.
class ChatCodeHover {
OverlayEntry? _entry;
Timer? _exit;
Object? _active;
bool _disposed = false;
void show(BuildContext context, Object token, String text, Offset pointer) {
if (_disposed) return;
hide();
final overlay = Overlay.maybeOf(context, rootOverlay: true);
final box = overlay?.context.findRenderObject();
if (overlay == null || box is! RenderBox) return;
_active = token;
Rect anchor = Rect.fromCenter(center: pointer, width: 1, height: 48);
RenderParagraph? paragraph;
void findParagraph(RenderObject object) {
if (object is RenderParagraph) {
paragraph = object;
return;
}
object.visitChildren(findParagraph);
}
final render = context.findRenderObject();
if (render != null) findParagraph(render);
final textRender = paragraph;
if (textRender != null) {
final position = textRender.getPositionForOffset(
textRender.globalToLocal(pointer),
);
final boxes = textRender.getBoxesForSelection(
TextSelection(
baseOffset: position.offset,
extentOffset: position.offset + 1,
),
);
if (boxes.isNotEmpty) {
anchor = boxes.first.toRect().shift(
textRender.localToGlobal(Offset.zero),
);
}
}
final localAnchor = anchor.shift(-box.localToGlobal(Offset.zero));
_entry = OverlayEntry(
builder: (context) => Positioned.fill(
child: CustomSingleChildLayout(
delegate: _CopyPosition(localAnchor),
child: MouseRegion(
onEnter: (_) => _exit?.cancel(),
onExit: (_) => scheduleHide(token),
child: Material(
elevation: 3,
borderRadius: BorderRadius.circular(8),
child: ChatCopyButton(text: text, showLabel: true),
),
),
),
),
);
overlay.insert(_entry!);
}
/// A short grace period lets the mouse cross from text to its Copy control.
void scheduleHide(Object token) {
if (_active != token) return;
_exit?.cancel();
_exit = Timer(const Duration(milliseconds: 180), hide);
}
void hide() {
_exit?.cancel();
_exit = null;
_entry?.remove();
_entry?.dispose();
_entry = null;
_active = null;
}
void dispose() {
_disposed = true;
hide();
}
}
class _CopyPosition extends SingleChildLayoutDelegate {
_CopyPosition(this.anchor);
final Rect anchor;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) =>
constraints.loosen();
@override
Offset getPositionForChild(Size size, Size childSize) {
final above = anchor.top - childSize.height - 6;
final y = above >= 4 ? above : anchor.bottom + 6;
return Offset(
anchor.left.clamp(
4.0,
(size.width - childSize.width - 4).clamp(4.0, double.infinity),
),
y.clamp(
4.0,
(size.height - childSize.height - 4).clamp(4.0, double.infinity),
),
);
}
@override
bool shouldRelayout(_CopyPosition oldDelegate) =>
anchor != oldDelegate.anchor;
}
/// A fenced code block as one framed piece: a header strip with the
/// language label and Copy, then the code on its own fill.
///
/// Copy is always in the header (a touch screen has no hover to reveal it),
/// outside the selectable document, so selecting the code never takes the
/// button's label with it. Light mode keeps the code fill light, framed by a
/// hairline, so code text keeps its full contrast; dark mode lifts it a step.
class ChatCodeCopyRegion extends StatelessWidget {
const ChatCodeCopyRegion({
super.key,
required this.code,
required this.label,
required this.child,
});
final String code;
final Widget label, child;
/// The block's corner radius.
static const double radius = 8;
/// The code body's fill.
static Color fill(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return scheme.brightness == Brightness.light
? scheme.surfaceContainerLow
: scheme.surfaceContainerHighest;
}
/// The header strip's fill, one step from the body's.
static Color headerFill(BuildContext context) =>
Theme.of(context).colorScheme.surfaceContainerHigh;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final corners = AppDesignTokens.of(
context,
).corners(BorderRadius.circular(radius));
final header = ColoredBox(
key: const Key('chat-code-header'),
color: headerFill(context),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 12, end: 4),
child: Row(
children: [
Expanded(child: label),
SelectionContainer.disabled(
child: ChatCopyButton(text: code, showLabel: true),
),
],
),
),
);
return DecoratedBox(
position: DecorationPosition.foreground,
decoration: BoxDecoration(
border: Border.all(color: scheme.outlineVariant),
borderRadius: corners,
),
child: ClipRRect(
borderRadius: corners,
child: ColoredBox(
key: const Key('chat-code-body'),
color: fill(context),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [header, child],
),
),
),
);
}
}