import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; import '../layout/workspace_layout_metrics.dart'; enum WorkspaceResizeBoundary { sidebar, media, timeline } /// Shares feedback for boundaries that meet at a resize junction. class WorkspaceResizeHighlightScope extends StatefulWidget { const WorkspaceResizeHighlightScope({super.key, required this.child}); final Widget child; @override State createState() => _WorkspaceResizeHighlightScopeState(); } class _WorkspaceResizeHighlightScopeState extends State { final _controller = _ResizeHighlightController(); @override Widget build(BuildContext context) => _ResizeHighlightScope( controller: context .dependOnInheritedWidgetOfExactType<_ResizeHighlightScope>() ?.notifier ?? _controller, child: widget.child, ); @override void dispose() { _controller.dispose(); super.dispose(); } } class _ResizeHighlightScope extends InheritedNotifier<_ResizeHighlightController> { const _ResizeHighlightScope({ required super.child, required _ResizeHighlightController controller, }) : super(notifier: controller); } class _ResizeHighlightController extends ChangeNotifier { final _sources = >{}; bool _notificationPending = false; bool _disposed = false; bool highlights(Set boundaries) => _sources.values.any((active) => active.any(boundaries.contains)); void update( Object source, Set boundaries, bool active, ) { if (_disposed) return; final next = active && boundaries.isNotEmpty ? boundaries : null; if (setEquals(_sources[source], next)) return; if (next == null) { _sources.remove(source); } else { _sources[source] = next; } // Handles can disappear while the layout is rebuilding (collapse/unmount). // Notify surviving handles after that frame instead of during their build. if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) { if (_notificationPending) return; _notificationPending = true; WidgetsBinding.instance.addPostFrameCallback((_) { _notificationPending = false; if (!_disposed) notifyListeners(); }); } else { notifyListeners(); } } @override void dispose() { _disposed = true; super.dispose(); } } /// 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, this.highlightTargets = const {}, this.highlightOffset = Offset.zero, }) : _orthogonalInitialValue = null, _onOrthogonalResize = null, _orthogonalDirection = 1; const WorkspaceResizeHandle.junction({ super.key, required double initialWidth, required double initialHeight, required ValueChanged onWidthChanged, required ValueChanged onHeightChanged, double verticalDirection = 1, this.onStart, this.onEnd, this.highlightTargets = const {}, this.highlightOffset = Offset.zero, }) : axis = Axis.horizontal, initialValue = initialWidth, onResize = onWidthChanged, direction = 1, _orthogonalInitialValue = initialHeight, _onOrthogonalResize = onHeightChanged, _orthogonalDirection = verticalDirection; final Axis axis; final double initialValue; final double direction; final VoidCallback? onStart; final ValueChanged onResize; final VoidCallback? onEnd; final Set highlightTargets; /// Aligns feedback with an edge while keeping the full resize hit target. final Offset highlightOffset; final double? _orthogonalInitialValue; final ValueChanged? _onOrthogonalResize; final double _orthogonalDirection; @override State createState() => _WorkspaceResizeHandleState(); } class _WorkspaceResizeHandleState extends State { OverlayEntry? _cursorOverlay; Offset? _startPosition; double _startValue = 0; double _orthogonalStartValue = 0; bool _hovered = false; final _highlightSource = Object(); _ResizeHighlightController? _highlightController; bool get _locallyHighlighted => _hovered || _startPosition != null; bool get _highlighted => _locallyHighlighted || !_isJunction && (_highlightController?.highlights(widget.highlightTargets) ?? false); void _publishHighlight() => _highlightController?.update( _highlightSource, widget.highlightTargets, _locallyHighlighted, ); void _hover(bool value) { if (_hovered == value) return; setState(() => _hovered = value); _publishHighlight(); } @override void didChangeDependencies() { super.didChangeDependencies(); final controller = context .dependOnInheritedWidgetOfExactType<_ResizeHighlightScope>() ?.notifier; if (_highlightController == controller) return; _highlightController?.update(_highlightSource, const {}, false); _highlightController = controller; _publishHighlight(); } @override void didUpdateWidget(covariant WorkspaceResizeHandle oldWidget) { super.didUpdateWidget(oldWidget); if (!setEquals(oldWidget.highlightTargets, widget.highlightTargets)) { _publishHighlight(); } } bool get _isJunction => widget._onOrthogonalResize != null; MouseCursor get _cursor => _isJunction ? SystemMouseCursors.grab : widget.axis == Axis.horizontal ? SystemMouseCursors.resizeColumn : SystemMouseCursors.resizeRow; double _coordinate(Offset position) => widget.axis == Axis.horizontal ? position.dx : position.dy; void _start(DragStartDetails details) { setState(() => _startPosition = details.globalPosition); _publishHighlight(); _startValue = widget.initialValue; _orthogonalStartValue = widget._orthogonalInitialValue ?? 0; widget.onStart?.call(); _releaseCursor(); final cursor = _isJunction ? SystemMouseCursors.grabbing : _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) - _coordinate(start)), ); widget._onOrthogonalResize?.call( _orthogonalStartValue + widget._orthogonalDirection * (details.globalPosition.dy - start.dy), ); } void _releaseCursor() { _cursorOverlay?.remove(); _cursorOverlay?.dispose(); _cursorOverlay = null; } void _finish() { if (_startPosition == null) return; setState(() => _startPosition = null); _publishHighlight(); _releaseCursor(); widget.onEnd?.call(); } @override void dispose() { _highlightController?.update(_highlightSource, const {}, false); _releaseCursor(); super.dispose(); } @override Widget build(BuildContext context) => MouseRegion( cursor: _cursor, onEnter: (_) => _hover(true), onExit: (_) => _hover(false), child: GestureDetector( behavior: HitTestBehavior.opaque, dragStartBehavior: DragStartBehavior.down, onHorizontalDragStart: !_isJunction && widget.axis == Axis.horizontal ? _start : null, onHorizontalDragUpdate: !_isJunction && widget.axis == Axis.horizontal ? _update : null, onHorizontalDragEnd: !_isJunction && widget.axis == Axis.horizontal ? (_) => _finish() : null, onVerticalDragStart: !_isJunction && widget.axis == Axis.vertical ? _start : null, onVerticalDragUpdate: !_isJunction && widget.axis == Axis.vertical ? _update : null, onVerticalDragEnd: !_isJunction && widget.axis == Axis.vertical ? (_) => _finish() : null, onHorizontalDragCancel: !_isJunction && widget.axis == Axis.horizontal ? _finish : null, onVerticalDragCancel: !_isJunction && widget.axis == Axis.vertical ? _finish : null, onPanStart: _isJunction ? _start : null, onPanUpdate: _isJunction ? _update : null, onPanEnd: _isJunction ? (_) => _finish() : null, onPanCancel: _isJunction ? _finish : null, child: IgnorePointer( child: Transform.translate( offset: widget.highlightOffset, child: Center( child: TweenAnimationBuilder( tween: Tween(begin: 0, end: _highlighted ? 1 : 0), duration: MediaQuery.disableAnimationsOf(context) ? Duration.zero : Duration(milliseconds: _highlighted ? 180 : 140), curve: Curves.easeInOutCubic, builder: (context, progress, child) => Opacity( opacity: progress, child: Transform.scale( scaleX: _isJunction || widget.axis == Axis.horizontal ? .5 + .5 * progress : .98 + .02 * progress, scaleY: _isJunction || widget.axis == Axis.vertical ? .5 + .5 * progress : .98 + .02 * progress, child: child, ), ), child: Container( width: _isJunction || widget.axis == Axis.horizontal ? WorkspaceLayoutMetrics.desktopResizeHighlightThickness : double.infinity, height: _isJunction || widget.axis == Axis.vertical ? WorkspaceLayoutMetrics.desktopResizeHighlightThickness : double.infinity, decoration: BoxDecoration( color: Theme.of(context).colorScheme.primary, borderRadius: BorderRadius.circular( WorkspaceLayoutMetrics.desktopResizeHighlightThickness / 2, ), ), ), ), ), ), ), ), ); }