Files
pipelines/app/lib/features/terminal/terminal_float.dart
T
alan 30934aa71c Toggle the terminal with Alt/Ctrl/Cmd+T, animate it, focus it, and cycle its tabs with Ctrl+Tab
Alt+T, Ctrl+T and Cmd+T join Ctrl+` as early focus-manager key handlers, so
they toggle the panel, window or narrow page from anywhere without reaching
the shell or a text field. Opening with no terminal starts one. The shown
terminal takes the keyboard (also on reload and tab changes) and hiding gives
it back. The panel slides from the bottom edge as the app shrinks, the window
scales and fades, both on Motion.standard and off under reduced motion.
While shown, Ctrl+Tab/Ctrl+Shift+Tab move between terminal tabs instead of
chat panes. The tooltip and menu list the shortcuts the platform delivers.
2026-11-09 23:43:56 +00:00

221 lines
6.4 KiB
Dart

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<double> 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<double>(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,
),
),
),
),
);
}