200 lines
6.8 KiB
Dart
200 lines
6.8 KiB
Dart
// Scroll position probe for browser captures of the chat transcript.
|
|
//
|
|
// `window.chatScrollMetrics()` returns the open transcript's vertical scroll
|
|
// position as JSON: `{"pixels", "min", "max", "viewport"}`, or `null` when no
|
|
// transcript is shown. tool/capture's `eval=` and `expect=` steps read it, so a
|
|
// wheel step can be checked against the offset it should have moved.
|
|
//
|
|
// `window.chatScrollTargets()` returns, as JSON, the centre point of the
|
|
// visible part of the first instance of each content kind in the transcript (`code`,
|
|
// `wide` code, `table`, `image`, `agent` report, `notice`), so a capture can
|
|
// put the wheel over it. `window.chatSidewaysOffsets()` lists the built code
|
|
// blocks' and tables' horizontal offsets, so a sideways scroll can be seen.
|
|
import 'dart:convert';
|
|
import 'dart:js_interop';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_transcript_scroll_view.dart';
|
|
import 'package:pipelines_app/features/chat/ui/transcript/chat_attachment_gallery.dart';
|
|
import 'package:pipelines_app/features/chat/ui/transcript/chat_injected_rows.dart';
|
|
|
|
@JS('chatScrollMetrics')
|
|
external set _chatScrollMetrics(JSFunction probe);
|
|
|
|
@JS('chatPaneScrollMetrics')
|
|
external set _chatPaneScrollMetrics(JSFunction probe);
|
|
|
|
@JS('chatScrollTargets')
|
|
external set _chatScrollTargets(JSFunction probe);
|
|
|
|
@JS('chatSidewaysOffsets')
|
|
external set _chatSidewaysOffsets(JSFunction probe);
|
|
|
|
void installChatScrollProbe() {
|
|
_chatScrollMetrics = (() => _metrics()?.toJS).toJS;
|
|
_chatPaneScrollMetrics = (() => _paneMetrics().toJS).toJS;
|
|
_chatScrollTargets = (() => _targets().toJS).toJS;
|
|
_chatSidewaysOffsets = (() => _sideways().toJS).toJS;
|
|
}
|
|
|
|
/// The content kind [element] draws, or null for one that is not a target.
|
|
String? _kind(Element element) {
|
|
final widget = element.widget;
|
|
if (widget is Table) return 'table';
|
|
if (widget is ChatAttachmentGallery) return 'image';
|
|
if (widget is ChatAgentReportCard) return 'agent';
|
|
if (widget is ChatSystemNoticeLine) return 'notice';
|
|
if (widget is SingleChildScrollView &&
|
|
widget.scrollDirection == Axis.horizontal) {
|
|
// A code block; a table's own element is reported as `table`.
|
|
var table = false;
|
|
ScrollPosition? position;
|
|
void inside(Element child) {
|
|
if (child.widget is Table) table = true;
|
|
if (child is StatefulElement && child.state is ScrollableState) {
|
|
position ??= (child.state as ScrollableState).position;
|
|
}
|
|
if (!table) child.visitChildren(inside);
|
|
}
|
|
|
|
element.visitChildren(inside);
|
|
final scroll = position;
|
|
if (table || scroll == null || !scroll.hasContentDimensions) return null;
|
|
return scroll.maxScrollExtent > 0 ? 'wide' : 'code';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
String _targets() {
|
|
final points = <String, List<double>>{};
|
|
final view = WidgetsBinding.instance.platformDispatcher.views.first;
|
|
final screen = Offset.zero & (view.physicalSize / view.devicePixelRatio);
|
|
// Clear of the floating bar above and the composer below.
|
|
final region = Rect.fromLTRB(0, 80, screen.width, screen.height - 160);
|
|
void visit(Element element) {
|
|
final kind = _kind(element);
|
|
if (kind != null && !points.containsKey(kind)) {
|
|
final box = element.findRenderObject();
|
|
if (box is RenderBox && box.attached && box.hasSize) {
|
|
final rect = (box.localToGlobal(Offset.zero) & box.size).intersect(
|
|
region,
|
|
);
|
|
if (rect.width >= 20 && rect.height >= 20) {
|
|
points[kind] = [rect.center.dx, rect.center.dy];
|
|
}
|
|
}
|
|
}
|
|
element.visitChildren(visit);
|
|
}
|
|
|
|
void transcript(Element element) {
|
|
if (element.widget is ChatTranscriptScrollView) {
|
|
element.visitChildren(visit);
|
|
return;
|
|
}
|
|
element.visitChildren(transcript);
|
|
}
|
|
|
|
WidgetsBinding.instance.rootElement?.visitChildren(transcript);
|
|
return jsonEncode(points);
|
|
}
|
|
|
|
String _sideways() {
|
|
final offsets = <double>[];
|
|
void visit(Element element) {
|
|
if (element is StatefulElement && element.state is ScrollableState) {
|
|
final position = (element.state as ScrollableState).position;
|
|
if (position.axis == Axis.horizontal && position.hasPixels) {
|
|
offsets.add(position.pixels);
|
|
}
|
|
}
|
|
element.visitChildren(visit);
|
|
}
|
|
|
|
void transcript(Element element) {
|
|
if (element.widget is ChatTranscriptScrollView) {
|
|
element.visitChildren(visit);
|
|
return;
|
|
}
|
|
element.visitChildren(transcript);
|
|
}
|
|
|
|
WidgetsBinding.instance.rootElement?.visitChildren(transcript);
|
|
return jsonEncode(offsets);
|
|
}
|
|
|
|
String? _metrics() {
|
|
ScrollPosition? found;
|
|
void scrollable(Element element) {
|
|
if (found != null) return;
|
|
if (element is StatefulElement && element.state is ScrollableState) {
|
|
final position = (element.state as ScrollableState).position;
|
|
if (position.axis == Axis.vertical) {
|
|
found = position;
|
|
return;
|
|
}
|
|
}
|
|
element.visitChildren(scrollable);
|
|
}
|
|
|
|
void transcript(Element element) {
|
|
if (found != null) return;
|
|
if (element.widget is ChatTranscriptScrollView) {
|
|
scrollable(element);
|
|
return;
|
|
}
|
|
element.visitChildren(transcript);
|
|
}
|
|
|
|
WidgetsBinding.instance.rootElement?.visitChildren(transcript);
|
|
final position = found;
|
|
if (position == null || !position.hasPixels) return null;
|
|
return jsonEncode({
|
|
'pixels': position.pixels,
|
|
'min': position.minScrollExtent,
|
|
'max': position.maxScrollExtent,
|
|
'viewport': position.viewportDimension,
|
|
});
|
|
}
|
|
|
|
/// All retained transcript viewports, so a capture can prove that the inactive
|
|
/// peek reaches its actual bottom without activating a chat or mutating data.
|
|
String _paneMetrics() {
|
|
final panes = <Map<String, Object?>>[];
|
|
void visit(Element element) {
|
|
if (element.widget is ChatTranscriptScrollView) {
|
|
ScrollPosition? position;
|
|
void inside(Element child) {
|
|
if (child is StatefulElement && child.state is ScrollableState) {
|
|
final candidate = (child.state as ScrollableState).position;
|
|
if (candidate.axis == Axis.vertical) position ??= candidate;
|
|
}
|
|
child.visitChildren(inside);
|
|
}
|
|
|
|
element.visitChildren(inside);
|
|
final scroll = position;
|
|
final box = element.findRenderObject();
|
|
if (scroll != null &&
|
|
scroll.hasContentDimensions &&
|
|
box is RenderBox &&
|
|
box.hasSize) {
|
|
panes.add({
|
|
'id': (element.widget.key as ValueKey?)?.value.toString(),
|
|
'pixels': scroll.pixels,
|
|
'min': scroll.minScrollExtent,
|
|
'max': scroll.maxScrollExtent,
|
|
'viewport': scroll.viewportDimension,
|
|
'top': box.localToGlobal(Offset.zero).dy,
|
|
'height': box.size.height,
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
element.visitChildren(visit);
|
|
}
|
|
|
|
WidgetsBinding.instance.rootElement?.visitChildren(visit);
|
|
return jsonEncode(panes);
|
|
}
|