Files
pipelines/app/lib/features/chat/workspace/chat_float_window.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

230 lines
6.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/window_margins.dart';
import '../../terminal/terminal_float_frame.dart' show terminalFloatChrome;
import 'chat_float_store.dart';
/// [rect] moved and shrunk to fit inside [bounds], at least the smallest
/// window where the bounds allow it.
Rect fitChatFloat(Rect rect, Size bounds) {
final width = rect.width
.clamp(chatFloatMinSize.width, double.infinity)
.clamp(0.0, bounds.width);
final height = rect.height
.clamp(chatFloatMinSize.height, double.infinity)
.clamp(0.0, bounds.height);
return Rect.fromLTWH(
rect.left.clamp(0.0, bounds.width - width),
rect.top.clamp(0.0, bounds.height - height),
width,
height,
);
}
/// A floating chat's window: moved by its title bar, resized from its
/// lower right corner, kept inside [bounds]. It grows and fades in from its
/// place as [reveal] runs forward and leaves the same way in reverse.
class ChatFloatWindow extends StatelessWidget {
const ChatFloatWindow({
super.key,
required this.rect,
required this.currentRect,
required this.onMove,
required this.onCommit,
required this.bounds,
required this.title,
required this.cupertino,
required this.reveal,
required this.hiding,
this.offstage = false,
required this.shortcut,
required this.onDock,
required this.onHide,
required this.child,
});
/// The saved frame; it is drawn fitted inside [bounds].
final Rect rect;
/// The frame now: several drag events can arrive before a rebuild.
final Rect Function() currentRect;
final ValueChanged<Rect> onMove;
/// A move or resize ended.
final VoidCallback onCommit;
final Size bounds;
final String title;
final bool cupertino;
final Animation<double> reveal;
/// While the window hides it takes no input.
final bool hiding;
/// Hidden and settled: kept built (the chat keeps its state) but unpainted.
final bool offstage;
final String shortcut;
final VoidCallback onDock;
final VoidCallback onHide;
final Widget child;
static const double revealScale = 0.92;
void _resize(Offset delta) {
final rect = fitChatFloat(currentRect(), bounds);
onMove(
fitChatFloat(
Rect.fromLTWH(
rect.left,
rect.top,
rect.width + delta.dx,
rect.height + delta.dy,
),
bounds,
),
);
}
@override
Widget build(BuildContext context) {
final rect = fitChatFloat(this.rect, bounds);
final window = SizedBox.fromSize(
size: rect.size,
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_titleBar(context, rect),
Expanded(
child: MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
removeLeft: true,
removeRight: true,
// A small window keeps the compact bar insets.
child: WindowMargins.compact(child: child),
),
),
],
),
PositionedDirectional(end: 0, bottom: 0, child: _corner()),
],
),
);
return Positioned(
left: rect.left,
top: rect.top,
child: Offstage(
offstage: offstage,
child: IgnorePointer(
ignoring: hiding,
child: ExcludeFocus(
excluding: hiding,
child: FadeTransition(
opacity: reveal,
child: ScaleTransition(
scale: Tween<double>(
begin: revealScale,
end: 1,
).animate(reveal),
child: Semantics(
container: true,
label: 'Floating chat: $title',
identifier: 'chat-float-window',
child: terminalFloatChrome(context, cupertino, window),
),
),
),
),
),
),
);
}
Widget _titleBar(BuildContext context, Rect rect) {
final label = Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: cupertino
? CupertinoTheme.of(context).textTheme.navTitleTextStyle
: Theme.of(context).textTheme.titleSmall,
);
final drag = MouseRegion(
cursor: SystemMouseCursors.move,
child: GestureDetector(
key: const ValueKey('chat-float-title'),
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => onMove(
fitChatFloat(
fitChatFloat(currentRect(), bounds).shift(details.delta),
bounds,
),
),
onPanEnd: (_) => onCommit(),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 12),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: label,
),
),
),
);
return Row(
children: [
Expanded(child: drag),
_button(
'Dock',
Icons.vertical_split_outlined,
CupertinoIcons.rectangle_split_3x1,
onDock,
),
_button(
'Hide floating chats ($shortcut)',
Icons.close,
CupertinoIcons.xmark,
onHide,
),
],
);
}
Widget _button(
String label,
IconData material,
IconData cupertinoIcon,
VoidCallback onPressed,
) => cupertino
? CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 8),
onPressed: onPressed,
child: Icon(cupertinoIcon, semanticLabel: label),
)
: IconButton(tooltip: label, onPressed: onPressed, icon: Icon(material));
Widget _corner() => Semantics(
label: 'Resize floating chat',
child: MouseRegion(
cursor: SystemMouseCursors.resizeDownRight,
child: GestureDetector(
key: const ValueKey('chat-float-resize'),
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => _resize(details.delta),
onPanEnd: (_) => onCommit(),
child: Padding(
padding: const EdgeInsets.all(4),
child: Icon(
cupertino ? CupertinoIcons.arrow_down_right : Icons.south_east,
),
),
),
),
);
}