Files
pipelines/app/lib/features/chat/conversation/chat_background_tasks.dart
T
alan 9695100faa Build chat panes through one content builder
Phase 2.2 of docs/plans/floating-windows.md: buildChatPaneContent wraps a
conversation in the scopes it needs and keys it by the runtime, so the
tiling and (next) a floating window build the same subtree. The tiling uses
it; behaviour is unchanged.
2026-11-10 21:05:41 +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,
),
),
);
},
),
);
}