Files
pipelines/app/lib/features/chat/conversation/chat_background_tasks.dart
T
alan cdd54c113c Float several chats and keep them working on every tab
Floating chats were stuck on other destinations: the hidden Chat tab
muted the window's hide animation, so the window stayed painted but took
no input. The reveal now uses its own ticker, each floating pane's focus
scope hangs from a FloatingFocusRoot above the destination stack, and the
windows show over every destination like the terminal.

Any number of chats can float; the shortcut, a window's X and the menu
show or hide them together. Each pane keeps its own frame (also while
docked), saved in record version 2; version 1 records still load. Drags
read the live frame so fast pointer events no longer lose movement.
2026-11-10 15:26:38 +00:00

225 lines
7.7 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'chat_background_tasks_controller.dart';
import 'chat_background_tasks_dialog.dart';
/// Compact live task count: a quiet control inside the composer beside Send,
/// shown only while tasks run. Details live only in its modal.
class ChatBackgroundTasks extends StatefulWidget {
const ChatBackgroundTasks({
super.key,
required this.gateway,
required this.sessionId,
this.syncToken = 0,
});
final ApiGateway gateway;
final String sessionId;
final int syncToken;
@override
State<ChatBackgroundTasks> createState() => _ChatBackgroundTasksState();
}
class _ChatBackgroundTasksState extends State<ChatBackgroundTasks> {
late final controller = ChatBackgroundTasksController(
widget.gateway,
widget.sessionId,
);
TransitionRoute<void>? _modal;
@override
void didUpdateWidget(covariant ChatBackgroundTasks oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.gateway != widget.gateway ||
oldWidget.sessionId != widget.sessionId) {
_closeModal();
controller.replace(widget.gateway, widget.sessionId, notify: false);
} else if (oldWidget.syncToken != widget.syncToken) {
controller.reconnect(notify: false);
}
}
void _closeModal() {
final route = _modal;
_modal = null;
if (route == null || !route.isActive) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (route.isActive) route.navigator?.removeRoute(route);
});
}
void _open() {
if (_modal != null) return;
unawaited(controller.refreshAutomations());
FocusManager.instance.primaryFocus?.unfocus();
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
Widget dialog(BuildContext context) => ChatBackgroundTasksDialog(
key: const Key('chat-background-tasks-modal'),
controller: controller,
onClose: () => Navigator.of(context, rootNavigator: true).pop(),
);
final TransitionRoute<void> route = cupertino
? CupertinoModalPopupRoute<void>(
barrierDismissible: true,
builder: dialog,
)
: DialogRoute<void>(context: context, builder: dialog);
_modal = route;
Navigator.of(context, rootNavigator: true).push(route);
route.completed.then((_) {
if (_modal == route) _modal = null;
});
}
@override
void dispose() {
_closeModal();
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => LayoutBuilder(
builder: (context, constraints) => ListenableBuilder(
listenable: controller,
builder: (context, _) {
final count = controller.running.length;
final automationCount = controller.automationStatus.count;
final failed = controller.error != null;
// Nothing to show without running tasks: never a "0 tasks" caption. A
// list that cannot be read stays reachable so its refusal is reported.
if (count == 0 &&
(automationCount == null || automationCount == 0) &&
!failed) {
return const SizedBox.shrink();
}
final labels = <String>[];
if (count > 0) labels.add('$count ${count == 1 ? 'task' : 'tasks'}');
if (automationCount != null && automationCount > 0) {
labels.add(
'$automationCount ${automationCount == 1 ? 'automation' : 'automations'}',
);
}
final label = failed && labels.isEmpty ? 'Tasks' : labels.join(', ');
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final labelStyle = cupertino
? CupertinoTheme.of(context).textTheme.tabLabelTextStyle
: Theme.of(context).textTheme.labelSmall;
final painter = TextPainter(
text: TextSpan(text: label, style: labelStyle),
textDirection: Directionality.of(context),
textScaler: MediaQuery.textScalerOf(context),
)..layout();
final compact =
constraints.maxWidth.isFinite &&
painter.width + (cupertino ? 32 : 40) > constraints.maxWidth;
painter.dispose();
Widget caption() => compact
? Wrap(
spacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
if (count > 0) Text('$count', style: labelStyle),
if ((automationCount ?? 0) > 0)
Row(
mainAxisSize: MainAxisSize.min,
children: [
if (count > 0) ...[
Icon(
cupertino ? CupertinoIcons.clock : Icons.schedule,
size: 16,
),
const SizedBox(width: 2),
],
Text('$automationCount', style: labelStyle),
],
),
if (labels.isEmpty) Text('Tasks', style: labelStyle),
],
)
: Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: labelStyle,
);
final automationOnly = count == 0 && (automationCount ?? 0) > 0;
final Widget button;
if (cupertino) {
final color = CupertinoColors.secondaryLabel.resolveFrom(context);
button = CupertinoButton(
key: const Key('chat-background-tasks-button'),
sizeStyle: CupertinoButtonSize.small,
padding: const EdgeInsets.symmetric(horizontal: 6),
onPressed: _open,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
automationOnly
? CupertinoIcons.clock
: CupertinoIcons.square_stack_3d_up,
size: 16,
color: color,
),
const SizedBox(width: 4),
Flexible(
child: DefaultTextStyle.merge(
style: TextStyle(color: color),
child: caption(),
),
),
],
),
);
} else {
final theme = Theme.of(context);
button = TextButton.icon(
key: const Key('chat-background-tasks-button'),
style: TextButton.styleFrom(
foregroundColor: theme.colorScheme.onSurfaceVariant,
padding: EdgeInsets.symmetric(horizontal: compact ? 4 : 8),
visualDensity: VisualDensity.compact,
),
onPressed: _open,
icon: Icon(
automationOnly ? Icons.schedule : Icons.layers_outlined,
size: 16,
),
label: caption(),
);
}
return Padding(
// Level with the field's single line, beside Send.
padding: const EdgeInsetsDirectional.only(end: 4, bottom: 4),
child: Semantics(
identifier: 'chat-background-tasks-button',
button: true,
excludeSemantics: true,
onTap: _open,
label: failed
? (labels.isEmpty
? 'Background tasks unavailable'
: 'Background work, $label; task list unavailable')
: 'Background work, $label',
child: Tooltip(
message: label,
excludeFromSemantics: true,
child: button,
),
),
);
},
),
);
}