231 lines
6.9 KiB
Dart
231 lines
6.9 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_hub.dart';
|
|
import 'terminal_layout.dart';
|
|
import 'terminal_tab_strip.dart';
|
|
|
|
/// Margin a maximised window keeps from the edges of the app.
|
|
const double terminalFloatMargin = 8;
|
|
|
|
/// [rect] moved and shrunk to fit inside [bounds], at least the minimum
|
|
/// window size where the bounds allow it.
|
|
Rect fitTerminalFloat(Rect rect, Size bounds) {
|
|
final width = rect.width
|
|
.clamp(terminalFloatMinSize.width, double.infinity)
|
|
.clamp(0.0, bounds.width);
|
|
final height = rect.height
|
|
.clamp(terminalFloatMinSize.height, double.infinity)
|
|
.clamp(0.0, bounds.height);
|
|
final left = rect.left.clamp(0.0, bounds.width - width);
|
|
final top = rect.top.clamp(0.0, bounds.height - height);
|
|
return Rect.fromLTWH(left, top, width, height);
|
|
}
|
|
|
|
/// 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].
|
|
class TerminalFloat extends StatelessWidget {
|
|
const TerminalFloat({
|
|
super.key,
|
|
required this.hub,
|
|
required this.layout,
|
|
required this.cupertino,
|
|
required this.bounds,
|
|
});
|
|
|
|
final TerminalHub hub;
|
|
final TerminalLayout layout;
|
|
final bool cupertino;
|
|
final Size bounds;
|
|
|
|
/// 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),
|
|
),
|
|
],
|
|
),
|
|
if (!layout.maximized)
|
|
PositionedDirectional(end: 0, bottom: 0, child: _corner()),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
return Positioned(
|
|
left: rect.left,
|
|
top: rect.top,
|
|
child: Semantics(
|
|
container: true,
|
|
label: 'Terminal window',
|
|
child: _chrome(context, window),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _chrome(BuildContext context, Widget child) {
|
|
if (!cupertino) {
|
|
return Material(
|
|
elevation: 8,
|
|
clipBehavior: Clip.antiAlias,
|
|
borderRadius: BorderRadius.circular(12),
|
|
color: Theme.of(context).colorScheme.surfaceContainer,
|
|
child: child,
|
|
);
|
|
}
|
|
return DecoratedBox(
|
|
decoration: BoxDecoration(
|
|
color: CupertinoColors.secondarySystemBackground.resolveFrom(context),
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(
|
|
color: CupertinoColors.separator.resolveFrom(context),
|
|
),
|
|
boxShadow: const [BoxShadow(blurRadius: 16, color: Color(0x33000000))],
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: Material(type: MaterialType.transparency, child: child),
|
|
),
|
|
);
|
|
}
|
|
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|