Files
pipelines/app/lib/features/terminal/terminal_panel.dart
T

160 lines
5.1 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../ui/adaptive/resize_grip.dart';
import 'terminal_body.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_tab_strip.dart';
/// Arrow-key step of the panel's resize handle.
const double terminalPanelKeyStep = 24;
/// Space the docked panel always leaves the app above it.
const double terminalPanelAppMin = 160;
/// [height] within what a window [available] tall allows the panel.
double terminalPanelHeight(double height, double available) {
final max = (available - terminalPanelAppMin).clamp(
terminalPanelMinHeight,
double.infinity,
);
return height.clamp(terminalPanelMinHeight, max);
}
/// The terminal docked at the bottom of the window, filling the height its
/// parent gives it (the layout's, see [terminalPanelHeight]): a resize
/// handle on its top edge (drag, or the arrow keys), the tabs and the
/// selected terminal.
class TerminalPanel extends StatefulWidget {
const TerminalPanel({
super.key,
required this.hub,
required this.layout,
required this.cupertino,
required this.available,
});
final TerminalHub hub;
final TerminalLayout layout;
final bool cupertino;
/// The height of the window the panel shares with the app.
final double available;
@override
State<TerminalPanel> createState() => _TerminalPanelState();
}
class _TerminalPanelState extends State<TerminalPanel> {
bool _active = false;
double get _height =>
terminalPanelHeight(widget.layout.panelHeight, widget.available);
void _resize(double height, {bool save = true}) =>
widget.layout.setPanelHeight(
terminalPanelHeight(height, widget.available),
save: save,
);
String _describe(double height) =>
'${terminalPanelHeight(height, widget.available).round()} pixels';
KeyEventResult _key(FocusNode _, KeyEvent event) {
if (event is! KeyDownEvent && event is! KeyRepeatEvent) {
return KeyEventResult.ignored;
}
if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
_resize(_height + terminalPanelKeyStep);
} else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
_resize(_height - terminalPanelKeyStep);
} else {
return KeyEventResult.ignored;
}
return KeyEventResult.handled;
}
@override
Widget build(BuildContext context) {
final height = _height;
final handle = Semantics(
identifier: 'terminal-panel-resize',
label: 'Terminal height',
hint: 'Drag, or use the arrow keys, to resize the terminal',
slider: true,
value: _describe(height),
increasedValue: _describe(height + terminalPanelKeyStep),
decreasedValue: _describe(height - terminalPanelKeyStep),
onIncrease: () => _resize(height + terminalPanelKeyStep),
onDecrease: () => _resize(height - terminalPanelKeyStep),
child: Focus(
onKeyEvent: _key,
onFocusChange: (value) => setState(() => _active = value),
child: MouseRegion(
cursor: SystemMouseCursors.resizeRow,
child: GestureDetector(
key: const ValueKey('terminal-panel-resize'),
behavior: HitTestBehavior.opaque,
excludeFromSemantics: true,
dragStartBehavior: DragStartBehavior.down,
onVerticalDragStart: (_) => setState(() => _active = true),
onVerticalDragUpdate: (details) =>
_resize(_height - details.delta.dy, save: false),
onVerticalDragEnd: (_) {
setState(() => _active = false);
unawaited(widget.layout.save());
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Center(
child: AdaptiveResizeGrip(compact: true, emphasized: _active),
),
),
),
),
),
);
final surface = widget.cupertino
? CupertinoColors.systemBackground.resolveFrom(context)
: Theme.of(context).colorScheme.surfaceContainerLow;
final divider = widget.cupertino
? CupertinoColors.separator.resolveFrom(context)
: Theme.of(context).colorScheme.outlineVariant;
return Semantics(
container: true,
label: 'Terminal panel',
child: DecoratedBox(
decoration: BoxDecoration(
color: surface,
border: Border(top: BorderSide(color: divider)),
),
child: Material(
type: MaterialType.transparency,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
handle,
TerminalTabStrip(
hub: widget.hub,
layout: widget.layout,
cupertino: widget.cupertino,
),
Expanded(
child: TerminalBody(
hub: widget.hub,
cupertino: widget.cupertino,
),
),
],
),
),
),
);
}
}