Files
pipelines/app/lib/features/chat/conversation/chat_escape_stop.dart
T
alan f3a469c356 Animate chat composer while turns run
Replace the running Stop button with an animated composer border and task spinner. Add Escape, slash-command, and touch-menu stopping paths with focused widget coverage, including reduced-motion behavior.
2026-11-12 02:24:46 +00:00

120 lines
3.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// The longest gap between two Escape presses that still stops a running turn.
const Duration chatEscapeStopWindow = Duration(seconds: 1);
/// The hint shown after the first Escape while a turn runs.
const String chatEscapeStopHint = 'Press Esc again to stop';
/// Two presses of Escape stop a running turn: the first shows [chatEscapeStopHint]
/// and the second, inside [chatEscapeStopWindow], stops it. Pure timing, so the
/// clock is the caller's.
class ChatEscapeStopTracker {
DateTime? _firstAt;
/// Records one plain Escape press at [now]. Returns true when it completes a
/// pair; a press that completes nothing starts a new pair.
bool press(DateTime now) {
final first = _firstAt;
if (first != null && now.difference(first) <= chatEscapeStopWindow) {
_firstAt = null;
return true;
}
_firstAt = now;
return false;
}
/// Forgets a pending first press, for example once the turn has ended.
void reset() => _firstAt = null;
}
/// Lets two presses of Escape stop the turn while focus is anywhere inside
/// [child] (the composer or the transcript). It only consumes Escape while
/// [running]; an idle chat, and any Escape a descendant already handled (a
/// command palette, a notice), pass through untouched.
class ChatEscapeStop extends StatefulWidget {
const ChatEscapeStop({
super.key,
required this.running,
required this.onStop,
required this.child,
this.clock = DateTime.now,
});
final bool running;
/// Stops the running turn; called on the second press.
final VoidCallback onStop;
/// Injected time source, so tests control the window.
final DateTime Function() clock;
final Widget child;
@override
State<ChatEscapeStop> createState() => _ChatEscapeStopState();
}
class _ChatEscapeStopState extends State<ChatEscapeStop> {
final ChatEscapeStopTracker _tracker = ChatEscapeStopTracker();
@override
void didUpdateWidget(covariant ChatEscapeStop oldWidget) {
super.didUpdateWidget(oldWidget);
if (!widget.running) _tracker.reset();
}
KeyEventResult _onKeyEvent(FocusNode node, KeyEvent event) {
if (event.logicalKey != LogicalKeyboardKey.escape || !widget.running) {
return KeyEventResult.ignored;
}
final keyboard = HardwareKeyboard.instance;
if (keyboard.isControlPressed ||
keyboard.isMetaPressed ||
keyboard.isShiftPressed ||
keyboard.isAltPressed) {
return KeyEventResult.ignored;
}
// Holding Escape is one press, and releasing it is not a press at all.
if (event is! KeyDownEvent) {
return event is KeyRepeatEvent
? KeyEventResult.handled
: KeyEventResult.ignored;
}
if (_tracker.press(widget.clock())) {
_tracker.reset();
_hideHint();
widget.onStop();
} else {
_showHint();
}
return KeyEventResult.handled;
}
void _showHint() {
final messenger = ScaffoldMessenger.maybeOf(context);
if (messenger == null) return;
messenger
..hideCurrentSnackBar()
..showSnackBar(
const SnackBar(
key: Key('chat-escape-stop-hint'),
content: Text(chatEscapeStopHint),
duration: chatEscapeStopWindow,
behavior: SnackBarBehavior.floating,
),
);
}
void _hideHint() => ScaffoldMessenger.maybeOf(context)?.hideCurrentSnackBar();
@override
Widget build(BuildContext context) => Focus(
canRequestFocus: false,
skipTraversal: true,
onKeyEvent: _onKeyEvent,
child: widget.child,
);
}