198 lines
6.0 KiB
Dart
198 lines
6.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../ui/adaptive/segmented_bar.dart';
|
|
import '../../../ui/adaptive/host.dart';
|
|
import '../../../ui/adaptive/floating_bar_layout.dart';
|
|
import 'graph_viewport.dart';
|
|
import 'graph_viewport_controller.dart';
|
|
|
|
/// Builds the same canvas for another viewport, with the selection that
|
|
/// presentation is showing.
|
|
typedef GraphExpandedCanvasBuilder =
|
|
Widget Function(
|
|
BuildContext context,
|
|
String? selectedStage,
|
|
ValueChanged<String> onSelected,
|
|
);
|
|
|
|
/// A native full-screen presentation of one graph canvas.
|
|
///
|
|
/// It fits the graph into the larger box (as the web does when its panel
|
|
/// expands), keeps the selected node selected, and lets a tap in the larger
|
|
/// canvas update that selection through [onSelected]. Closing pops the route;
|
|
/// the inline canvas was never changed, so it returns to its previous state.
|
|
class GraphExpandedView extends StatefulWidget {
|
|
const GraphExpandedView({
|
|
super.key,
|
|
required this.contentSize,
|
|
required this.canvasBuilder,
|
|
required this.title,
|
|
required this.onSelected,
|
|
this.selectedStage,
|
|
this.header,
|
|
});
|
|
|
|
/// The measured graph extent, as in the inline canvas.
|
|
final Size contentSize;
|
|
|
|
final GraphExpandedCanvasBuilder canvasBuilder;
|
|
|
|
/// The presentation's native title, e.g. `Run graph`.
|
|
final String title;
|
|
|
|
/// Reports a node activated in the expanded canvas.
|
|
final ValueChanged<String> onSelected;
|
|
|
|
/// Optional fixed content above the canvas, e.g. a legend that explains the
|
|
/// graph's marks. It stays outside the pan/zoom transform.
|
|
final Widget? header;
|
|
|
|
final String? selectedStage;
|
|
|
|
@override
|
|
State<GraphExpandedView> createState() => _GraphExpandedViewState();
|
|
}
|
|
|
|
class _GraphExpandedViewState extends State<GraphExpandedView> {
|
|
final GraphViewportController _controller = GraphViewportController();
|
|
late String? _selected = widget.selectedStage;
|
|
|
|
@override
|
|
void didUpdateWidget(GraphExpandedView oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.selectedStage != oldWidget.selectedStage) {
|
|
_selected = widget.selectedStage;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _select(String key) {
|
|
setState(() => _selected = key);
|
|
widget.onSelected(key);
|
|
}
|
|
|
|
void _close() => Navigator.of(context).maybePop();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final viewport = GraphViewport(
|
|
key: const Key('graph-expanded-viewport'),
|
|
controller: _controller,
|
|
contentSize: widget.contentSize,
|
|
showControls: false,
|
|
// The larger box fits the whole graph, as the web does when its panel
|
|
// expands; a later resize re-fits until the reader pans or zooms, then
|
|
// only clamps.
|
|
child: widget.canvasBuilder(context, _selected, _select),
|
|
);
|
|
final body = widget.header == null
|
|
? viewport
|
|
: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
widget.header!,
|
|
Expanded(child: viewport),
|
|
],
|
|
);
|
|
if (usesCupertinoHost(context)) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: SegmentedBar(
|
|
cupertino: true,
|
|
automaticallyImplyLeading: false,
|
|
leading: SegmentedBackButton(cupertino: true, onPressed: _close),
|
|
title: widget.title,
|
|
actions: [
|
|
_GraphFitButton(
|
|
cupertino: true,
|
|
onPressed: _controller.fitToViewport,
|
|
),
|
|
],
|
|
),
|
|
child: BelowTopBar(child: body),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
extendBodyBehindAppBar: true,
|
|
appBar: SegmentedBar(
|
|
cupertino: false,
|
|
title: widget.title,
|
|
automaticallyImplyLeading: false,
|
|
leading: SegmentedBackButton(cupertino: false, onPressed: _close),
|
|
actions: [
|
|
_GraphFitButton(
|
|
cupertino: false,
|
|
onPressed: _controller.fitToViewport,
|
|
),
|
|
],
|
|
),
|
|
body: BelowTopBar(child: body),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _GraphFitButton extends StatelessWidget {
|
|
const _GraphFitButton({required this.cupertino, required this.onPressed});
|
|
|
|
final bool cupertino;
|
|
final VoidCallback onPressed;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => MergeSemantics(
|
|
child: Semantics(
|
|
button: true,
|
|
label: graphFitLabel,
|
|
child: cupertino
|
|
? CupertinoButton(
|
|
key: const Key('graph-fit'),
|
|
padding: const EdgeInsets.all(8),
|
|
onPressed: onPressed,
|
|
child: const ExcludeSemantics(child: Icon(CupertinoIcons.resize)),
|
|
)
|
|
: Tooltip(
|
|
message: graphFitLabel,
|
|
excludeFromSemantics: true,
|
|
child: IconButton(
|
|
key: const Key('graph-fit'),
|
|
onPressed: onPressed,
|
|
icon: const ExcludeSemantics(child: Icon(Icons.fit_screen)),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Pushes [GraphExpandedView] with the host's native full-screen transition.
|
|
///
|
|
/// This is the seam a Graph tab or run detail can call: pass the measured
|
|
/// content size and a builder that draws the same canvas for the supplied
|
|
/// selection. The expanded canvas fits that size; the inline canvas is
|
|
/// untouched.
|
|
Future<void> openGraphExpanded(
|
|
BuildContext context, {
|
|
required Size contentSize,
|
|
required GraphExpandedCanvasBuilder canvasBuilder,
|
|
required String title,
|
|
required ValueChanged<String> onSelected,
|
|
String? selectedStage,
|
|
Widget? header,
|
|
}) {
|
|
final page = GraphExpandedView(
|
|
contentSize: contentSize,
|
|
canvasBuilder: canvasBuilder,
|
|
title: title,
|
|
onSelected: onSelected,
|
|
selectedStage: selectedStage,
|
|
header: header,
|
|
);
|
|
final route = usesCupertinoHost(context)
|
|
? CupertinoPageRoute<void>(fullscreenDialog: true, builder: (_) => page)
|
|
: MaterialPageRoute<void>(fullscreenDialog: true, builder: (_) => page);
|
|
return Navigator.of(context).push(route);
|
|
}
|