import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'terminal_body.dart'; import 'terminal_float_frame.dart'; import 'terminal_hub.dart'; import 'terminal_layout.dart'; import 'terminal_tab_strip.dart'; export 'terminal_float_frame.dart' show fitTerminalFloat, terminalFloatMargin; /// The terminal as a window over the app: moved by its title bar, resized /// from its lower right corner, minimised to its title bar or maximised to /// the app. Its frame is kept in [layout]. It grows and fades in from its /// place as [reveal] runs forward, and shrinks and fades out in reverse. class TerminalFloat extends StatelessWidget { const TerminalFloat({ super.key, required this.hub, required this.layout, required this.cupertino, required this.bounds, this.reveal = kAlwaysCompleteAnimation, this.focus, this.hiding = false, }); final TerminalHub hub; final TerminalLayout layout; final bool cupertino; final Size bounds; final Animation reveal; /// The terminal's focus scope (see [TerminalBody.focus]). final FocusScopeNode? focus; /// While the window hides it takes no input. final bool hiding; /// The scale the window grows from as it shows. static const double revealScale = 0.92; /// Where the window is drawn now. Rect get frame => layout.maximized ? (Offset.zero & bounds).deflate(terminalFloatMargin) : fitTerminalFloat(layout.floatRect, bounds); void _move(Offset delta) { if (layout.maximized) return; layout.setFloatRect(frame.shift(delta), save: false); } void _resize(Offset delta) { final rect = frame; layout.setFloatRect( fitTerminalFloat( Rect.fromLTWH( rect.left, rect.top, rect.width + delta.dx, rect.height + delta.dy, ), bounds, ), save: false, ); } @override Widget build(BuildContext context) { final rect = frame; final title = _titleBar(context); final Widget window; if (layout.minimized) { window = SizedBox(width: rect.width, child: title); } else { window = SizedBox.fromSize( size: rect.size, child: Stack( children: [ Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ title, TerminalTabStrip( hub: hub, layout: layout, cupertino: cupertino, ), Expanded( child: TerminalBody( hub: hub, cupertino: cupertino, focus: focus, ), ), ], ), if (!layout.maximized) PositionedDirectional(end: 0, bottom: 0, child: _corner()), ], ), ); } return Positioned( left: rect.left, top: rect.top, 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: 'Terminal window', child: terminalFloatChrome(context, cupertino, window), ), ), ), ), ), ); } Widget _titleBar(BuildContext context) { final label = Text( hub.selected?.title ?? 'Terminal', maxLines: 1, overflow: TextOverflow.ellipsis, style: cupertino ? CupertinoTheme.of(context).textTheme.navTitleTextStyle : Theme.of(context).textTheme.titleSmall, ); final drag = MouseRegion( cursor: layout.maximized ? SystemMouseCursors.basic : SystemMouseCursors.move, child: GestureDetector( key: const ValueKey('terminal-float-title'), behavior: HitTestBehavior.opaque, // The window follows the pointer from where it went down. dragStartBehavior: DragStartBehavior.down, onPanUpdate: (details) => _move(details.delta), onPanEnd: (_) => unawaited(layout.save()), onDoubleTap: () => layout.setMaximized(!layout.maximized), child: Padding( padding: const EdgeInsetsDirectional.only(start: 12), child: Align( alignment: AlignmentDirectional.centerStart, child: label, ), ), ), ); return Row( children: [ Expanded(child: drag), _button( layout.minimized ? 'Restore' : 'Minimise', layout.minimized ? Icons.expand_less : Icons.minimize, layout.minimized ? CupertinoIcons.chevron_up : CupertinoIcons.minus, () => layout.setMinimized(!layout.minimized), ), _button( layout.maximized ? 'Restore size' : 'Maximise', layout.maximized ? Icons.fullscreen_exit : Icons.fullscreen, layout.maximized ? CupertinoIcons.fullscreen_exit : CupertinoIcons.fullscreen, () => layout.setMaximized(!layout.maximized), ), _button( 'Hide terminal', Icons.close, CupertinoIcons.xmark, layout.hide, ), ], ); } 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 terminal window', child: MouseRegion( cursor: SystemMouseCursors.resizeDownRight, child: GestureDetector( key: const ValueKey('terminal-float-resize'), behavior: HitTestBehavior.opaque, dragStartBehavior: DragStartBehavior.down, onPanUpdate: (details) => _resize(details.delta), onPanEnd: (_) => unawaited(layout.save()), child: Padding( padding: const EdgeInsets.all(4), child: Icon( cupertino ? CupertinoIcons.arrow_down_right : Icons.south_east, ), ), ), ), ); }