Expose horizontal chat overflow with native scrollbars and keyboard controls
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../../../../ui/layout/wheel_scroll_forwarding.dart';
|
||||
|
||||
/// Native, persistent overflow affordances for a code block or table. The
|
||||
/// footer is outside the selectable document and never overlays its content.
|
||||
class ChatHorizontalViewport extends StatefulWidget {
|
||||
const ChatHorizontalViewport({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.label = 'content',
|
||||
});
|
||||
final Widget child;
|
||||
final String label;
|
||||
@override
|
||||
State<ChatHorizontalViewport> createState() => _ChatHorizontalViewportState();
|
||||
}
|
||||
|
||||
class _ChatHorizontalViewportState extends State<ChatHorizontalViewport> {
|
||||
final _controller = ScrollController();
|
||||
final _focus = FocusNode(debugLabel: 'Horizontal chat content');
|
||||
bool _overflow = false, _left = false, _right = false, _focused = false;
|
||||
bool _scheduled = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller.addListener(_scheduleMetrics);
|
||||
}
|
||||
|
||||
void _scheduleMetrics() {
|
||||
if (_scheduled) return;
|
||||
_scheduled = true;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_scheduled = false;
|
||||
if (!mounted || !_controller.hasClients) return;
|
||||
final p = _controller.position;
|
||||
final overflow = p.maxScrollExtent - p.minScrollExtent > .5;
|
||||
final reverse = axisDirectionIsReversed(p.axisDirection);
|
||||
final before = overflow && p.pixels > p.minScrollExtent + .5;
|
||||
final after = overflow && p.pixels < p.maxScrollExtent - .5;
|
||||
final left = reverse ? after : before;
|
||||
final right = reverse ? before : after;
|
||||
if (_overflow == overflow && _left == left && _right == right) return;
|
||||
setState(() {
|
||||
_overflow = overflow;
|
||||
_left = left;
|
||||
_right = right;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void _move(double physicalDelta) {
|
||||
if (!_controller.hasClients) return;
|
||||
final p = _controller.position;
|
||||
final delta = axisDirectionIsReversed(p.axisDirection)
|
||||
? -physicalDelta
|
||||
: physicalDelta;
|
||||
_controller.jumpTo(
|
||||
(p.pixels + delta).clamp(p.minScrollExtent, p.maxScrollExtent),
|
||||
);
|
||||
}
|
||||
|
||||
KeyEventResult _key(FocusNode node, KeyEvent event) {
|
||||
if (!_focus.hasPrimaryFocus ||
|
||||
!_overflow ||
|
||||
(event is! KeyDownEvent && event is! KeyRepeatEvent) ||
|
||||
HardwareKeyboard.instance.isShiftPressed ||
|
||||
HardwareKeyboard.instance.isControlPressed ||
|
||||
HardwareKeyboard.instance.isMetaPressed ||
|
||||
HardwareKeyboard.instance.isAltPressed) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
||||
_move(-80);
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
|
||||
_move(80);
|
||||
} else {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.removeListener(_scheduleMetrics);
|
||||
_controller.dispose();
|
||||
_focus.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final color = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
final focusColor = cupertino
|
||||
? CupertinoTheme.of(context).primaryColor
|
||||
: Theme.of(context).colorScheme.primary;
|
||||
final view = Padding(
|
||||
padding: EdgeInsets.only(bottom: _overflow ? 12 : 0),
|
||||
child: HorizontalScrollView(controller: _controller, child: widget.child),
|
||||
);
|
||||
final scrollbar = cupertino
|
||||
? CupertinoScrollbar(
|
||||
controller: _controller,
|
||||
thumbVisibility: _overflow,
|
||||
child: view,
|
||||
)
|
||||
: Scrollbar(
|
||||
controller: _controller,
|
||||
thumbVisibility: _overflow,
|
||||
trackVisibility: _overflow,
|
||||
child: view,
|
||||
);
|
||||
return NotificationListener<ScrollMetricsNotification>(
|
||||
onNotification: (notification) {
|
||||
if (notification.metrics.axis == Axis.horizontal) _scheduleMetrics();
|
||||
return false;
|
||||
},
|
||||
child: Focus(
|
||||
focusNode: _focus,
|
||||
canRequestFocus: _overflow,
|
||||
onFocusChange: (value) => setState(() => _focused = value),
|
||||
onKeyEvent: _key,
|
||||
child: Semantics(
|
||||
label: _overflow ? 'Horizontally scrollable ${widget.label}' : null,
|
||||
hint: _overflow
|
||||
? 'Swipe sideways or focus and use the left and right arrow keys.'
|
||||
: null,
|
||||
onScrollLeft: _left ? () => _move(-80) : null,
|
||||
onScrollRight: _right ? () => _move(80) : null,
|
||||
child: DecoratedBox(
|
||||
position: DecorationPosition.foreground,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: _focused ? focusColor : Colors.transparent,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
scrollbar,
|
||||
if (_overflow)
|
||||
SelectionContainer.disabled(
|
||||
child: ExcludeSemantics(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
_left && _right
|
||||
? Icons.swap_horiz
|
||||
: _left
|
||||
? Icons.arrow_back
|
||||
: Icons.arrow_forward,
|
||||
size: 16,
|
||||
textDirection: TextDirection.ltr,
|
||||
color: color,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Flexible(
|
||||
child: Text(
|
||||
'Scroll horizontally',
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -193,6 +193,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
);
|
||||
case MarkdownTable(header: final header, rows: final rows):
|
||||
return ChatTranscriptHorizontalScrollView(
|
||||
label: 'table',
|
||||
child: Table(
|
||||
defaultColumnWidth: const IntrinsicColumnWidth(),
|
||||
border: TableBorder.all(color: _dividerColor(context, cupertino)),
|
||||
@@ -231,6 +232,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: ChatTranscriptHorizontalScrollView(
|
||||
label: 'code block',
|
||||
child: Text(
|
||||
code,
|
||||
style: const TextStyle(fontFamily: appMonospaceFamily),
|
||||
|
||||
@@ -5,7 +5,7 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../../../../ui/layout/wheel_scroll_forwarding.dart';
|
||||
import 'chat_horizontal_viewport.dart';
|
||||
|
||||
/// The selection controller shared by all link spans in one transcript.
|
||||
///
|
||||
@@ -114,8 +114,13 @@ class _ChatTranscriptSelectionAreaState
|
||||
/// mean after, not before. Give the vertical position priority outside its
|
||||
/// height, while keeping normal horizontal selection and scrolling inside.
|
||||
class ChatTranscriptHorizontalScrollView extends StatefulWidget {
|
||||
const ChatTranscriptHorizontalScrollView({super.key, required this.child});
|
||||
const ChatTranscriptHorizontalScrollView({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.label = 'content',
|
||||
});
|
||||
final Widget child;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
State<ChatTranscriptHorizontalScrollView> createState() =>
|
||||
@@ -134,7 +139,10 @@ class _ChatTranscriptHorizontalScrollViewState
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final view = HorizontalScrollView(child: widget.child);
|
||||
final view = ChatHorizontalViewport(
|
||||
label: widget.label,
|
||||
child: widget.child,
|
||||
);
|
||||
if (ChatTranscriptSelectionScope.maybeOf(context) == null) return view;
|
||||
return SelectionContainer(delegate: _delegate, child: view);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user