102 lines
3.0 KiB
Dart
102 lines
3.0 KiB
Dart
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Keeps the resize cursor active across the window while a split is dragged.
|
|
class WorkspaceResizeHandle extends StatefulWidget {
|
|
const WorkspaceResizeHandle({
|
|
super.key,
|
|
required this.axis,
|
|
required this.initialValue,
|
|
required this.onResize,
|
|
this.direction = 1,
|
|
this.onEnd,
|
|
});
|
|
|
|
final Axis axis;
|
|
final double initialValue;
|
|
final double direction;
|
|
final ValueChanged<double> onResize;
|
|
final VoidCallback? onEnd;
|
|
|
|
@override
|
|
State<WorkspaceResizeHandle> createState() => _WorkspaceResizeHandleState();
|
|
}
|
|
|
|
class _WorkspaceResizeHandleState extends State<WorkspaceResizeHandle> {
|
|
OverlayEntry? _cursorOverlay;
|
|
double? _startPosition;
|
|
double _startValue = 0;
|
|
|
|
MouseCursor get _cursor => widget.axis == Axis.horizontal
|
|
? SystemMouseCursors.resizeColumn
|
|
: SystemMouseCursors.resizeRow;
|
|
|
|
double _coordinate(Offset position) =>
|
|
widget.axis == Axis.horizontal ? position.dx : position.dy;
|
|
|
|
void _start(DragStartDetails details) {
|
|
_startPosition = _coordinate(details.globalPosition);
|
|
_startValue = widget.initialValue;
|
|
_releaseCursor();
|
|
final cursor = _cursor;
|
|
_cursorOverlay = OverlayEntry(
|
|
builder: (_) => Positioned.fill(
|
|
child: MouseRegion(
|
|
cursor: cursor,
|
|
// Register the cursor while letting pointer hits reach widgets below.
|
|
hitTestBehavior: HitTestBehavior.translucent,
|
|
child: const SizedBox.expand(),
|
|
),
|
|
),
|
|
);
|
|
Overlay.of(context, rootOverlay: true).insert(_cursorOverlay!);
|
|
}
|
|
|
|
void _update(DragUpdateDetails details) {
|
|
final start = _startPosition;
|
|
if (start == null) return;
|
|
widget.onResize(
|
|
_startValue +
|
|
widget.direction * (_coordinate(details.globalPosition) - start),
|
|
);
|
|
}
|
|
|
|
void _releaseCursor() {
|
|
_cursorOverlay?.remove();
|
|
_cursorOverlay?.dispose();
|
|
_cursorOverlay = null;
|
|
}
|
|
|
|
void _finish() {
|
|
_startPosition = null;
|
|
_releaseCursor();
|
|
widget.onEnd?.call();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_releaseCursor();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => MouseRegion(
|
|
cursor: _cursor,
|
|
child: GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
dragStartBehavior: DragStartBehavior.down,
|
|
onHorizontalDragStart: widget.axis == Axis.horizontal ? _start : null,
|
|
onHorizontalDragUpdate: widget.axis == Axis.horizontal ? _update : null,
|
|
onHorizontalDragEnd: widget.axis == Axis.horizontal
|
|
? (_) => _finish()
|
|
: null,
|
|
onVerticalDragStart: widget.axis == Axis.vertical ? _start : null,
|
|
onVerticalDragUpdate: widget.axis == Axis.vertical ? _update : null,
|
|
onVerticalDragEnd: widget.axis == Axis.vertical ? (_) => _finish() : null,
|
|
onHorizontalDragCancel: widget.axis == Axis.horizontal ? _finish : null,
|
|
onVerticalDragCancel: widget.axis == Axis.vertical ? _finish : null,
|
|
child: const SizedBox.expand(),
|
|
),
|
|
);
|
|
}
|