Show a terminal shortcut below the top bar on phones

While shells are running and the full-screen terminal is closed, a small
floating button under the right end of the top bar reopens it, with a count
when more than one shell runs. The surface's stack is always present so the
app never remounts when the shortcut comes or goes.
This commit is contained in:
2026-11-10 00:23:53 +00:00
parent b44938d028
commit 070f9e205e
3 changed files with 131 additions and 3 deletions
@@ -0,0 +1,67 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../ui/adaptive/segmented_bar_pieces.dart';
/// On a phone, a small floating button below the top bar's right end that
/// reopens the full-screen terminal while a shell is running there. It shows
/// how many shells are running when there is more than one.
class TerminalShortcutBubble extends StatelessWidget {
const TerminalShortcutBubble({
super.key,
required this.running,
required this.cupertino,
required this.onOpen,
});
final int running;
final bool cupertino;
final VoidCallback onOpen;
/// The top edge: below the safe area and the top bar (its 8 px top inset
/// and its pieces), with an 8 px gap.
static double topOf(BuildContext context, {required bool cupertino}) =>
MediaQuery.paddingOf(context).top +
8 +
segmentedPieceHeight(cupertino) +
8;
@override
Widget build(BuildContext context) {
final label = running == 1
? 'Open terminal'
: 'Open terminal, $running running';
final icon = Icon(
cupertino
? CupertinoIcons.chevron_left_slash_chevron_right
: Icons.terminal,
size: 20,
);
final content = running > 1
? Badge.count(count: running, child: icon)
: icon;
final button = cupertino
? CupertinoButton.tinted(
key: const Key('terminal-shortcut-bubble'),
sizeStyle: CupertinoButtonSize.small,
padding: const EdgeInsets.all(10),
borderRadius: BorderRadius.circular(22),
onPressed: onOpen,
child: content,
)
: FloatingActionButton.small(
key: const Key('terminal-shortcut-bubble'),
heroTag: null,
tooltip: label,
onPressed: onOpen,
child: content,
);
return Semantics(
button: true,
label: label,
excludeSemantics: true,
onTap: onOpen,
child: button,
);
}
}
@@ -11,6 +11,7 @@ import 'terminal_layout.dart';
import 'terminal_open_action.dart';
import 'terminal_page.dart';
import 'terminal_scope.dart';
import 'terminal_shortcut_bubble.dart';
import 'terminal_shortcuts.dart';
import 'terminal_stage.dart';
@@ -169,14 +170,43 @@ class _TerminalSurfaceState extends State<TerminalSurface>
return refusal;
}
Future<void> _openPage() {
Future<void> _openPage() async {
unawaited(openTerminalIfNone(widget.hub));
return _page.show(
setState(() {});
await _page.show(
context,
hub: widget.hub,
layout: widget.layout,
cupertino: widget.cupertino,
);
if (mounted) setState(() {});
}
/// Running shells on a phone with the terminal page closed get a small
/// shortcut below the top bar's right end. The stack is always there so the
/// app below never remounts when the shortcut comes or goes.
Widget _withShortcut(Widget child) {
final running = widget.hub.tabs.where((tab) => tab.info.running).length;
final show = _narrow && running > 0 && !_page.open;
return Stack(
fit: StackFit.passthrough,
children: [
child,
if (show)
PositionedDirectional(
end: 12,
top: TerminalShortcutBubble.topOf(
context,
cupertino: widget.cupertino,
),
child: TerminalShortcutBubble(
running: running,
cupertino: widget.cupertino,
onOpen: () => unawaited(_openPage()),
),
),
],
);
}
@override
@@ -192,7 +222,7 @@ class _TerminalSurfaceState extends State<TerminalSurface>
present: !_narrow && (widget.layout.visible || !_reveal.isDismissed),
reveal: _curve,
focus: _focus.scope,
child: child!,
child: _withShortcut(child!),
),
child: widget.child,
),
@@ -258,6 +258,37 @@ void main() {
expect(hub.tabs, hasLength(2));
},
);
desktopTestWidgets(
'a phone with running shells shows a shortcut below the top bar that '
'reopens the terminal',
(tester) async {
await pumpApp(tester, size: const Size(390, 800));
final bubble = find.byKey(const Key('terminal-shortcut-bubble'));
// The shells load when the terminal is first shown.
await tester.tap(find.byTooltip(TerminalToggleButton.label));
await tester.pumpAndSettle();
expect(bubble, findsNothing, reason: 'hidden while the page is open');
await tester.pageBack();
await tester.pumpAndSettle();
expect(bubble, findsOneWidget);
expect(find.bySemanticsLabel('Open terminal, 2 running'), findsOneWidget);
final rect = tester.getRect(bubble);
expect(rect.right, greaterThan(390 - 80), reason: 'on the right');
expect(rect.top, greaterThan(40), reason: 'below the top bar');
expect(rect.top, lessThan(160));
await tester.tap(bubble);
await tester.pumpAndSettle();
expect(find.widgetWithText(AppBar, 'Terminal'), findsOneWidget);
expect(bubble, findsNothing);
// Wide windows use the panel instead, so no shortcut there.
await tester.pageBack();
await tester.pumpAndSettle();
tester.view.physicalSize = const Size(1200, 800);
await tester.pumpAndSettle();
expect(bubble, findsNothing);
},
);
}
/// Hardware keys reach the terminal on desktop; phones type through the