import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import '../../terminal/terminal_float_frame.dart' show terminalFloatChrome; import 'chat_float_binding.dart'; 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, ); } /// The 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.frame, 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, }); final ChatFloatFrame frame; final Size bounds; final String title; final bool cupertino; final Animation 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(frame.rect, bounds); frame.move( fitChatFloat( Rect.fromLTWH( rect.left, rect.top, rect.width + delta.dx, rect.height + delta.dy, ), bounds, ), ); } @override Widget build(BuildContext context) { final rect = fitChatFloat(frame.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, 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( begin: revealScale, end: 1, ).animate(reveal), child: Semantics( container: true, label: 'Floating chat', 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) => frame.move( fitChatFloat( fitChatFloat(frame.rect, bounds).shift(details.delta), bounds, ), ), onPanEnd: (_) => frame.commit(), 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 chat ($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: (_) => frame.commit(), child: Padding( padding: const EdgeInsets.all(4), child: Icon( cupertino ? CupertinoIcons.arrow_down_right : Icons.south_east, ), ), ), ), ); }