Files
cutdex/lib/shared/widgets/workspace_resize_handle.dart
T
lingjhf 2e0033ce2d
Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
feat(workspace): refresh desktop editing experience and prepare 0.1.2
2026-10-09 01:56:01 +08:00

105 lines
3.1 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.onStart,
this.onEnd,
});
final Axis axis;
final double initialValue;
final double direction;
final VoidCallback? onStart;
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;
widget.onStart?.call();
_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(),
),
);
}