import 'dart:math' as math; import 'package:cutdex_core/timeline/layout.dart'; import 'package:cutdex_core/timeline/model.dart'; /// Immutable dimensions and content bounds of one viewport layout. typedef TimelineCameraBounds = ({ double width, double height, int end, double top, double bottom, }); /// Placement requested by the viewport lifecycle or an explicit focus action. enum TimelineCameraPlacement { preserve, initialize, focusPrimary, resetPrimary, } /// Resolves viewport placement without changing state or notifying listeners. /// /// Initialization fits an empty timeline to twenty seconds. The controller /// requests it only for the first valid layout of an uninitialized view. /// Preserved views retain their scale, including restored empty timelines. /// When content fits vertically, [restingVerticalOffset] keeps navigation from /// moving the centered lane while allowing resize to constrain its position. TimelineCamera resolveTimelineCamera({ required TimelineCamera camera, required TimelineCameraBounds? bounds, required double primaryCenter, required double restingVerticalOffset, TimelineCameraPlacement placement = TimelineCameraPlacement.preserve, }) { if (bounds == null || bounds.width <= 0 || bounds.height <= 0) return camera; final initializing = placement == TimelineCameraPlacement.initialize; final scale = initializing && bounds.end == 0 ? bounds.width / (20 * TimelineClock.ticksPerSecond) : camera.pixelsPerTick; final visible = bounds.width / scale; final wholeVisible = visible.ceil(); final end = timelineNavigationEnd( contentEnd: bounds.end, width: bounds.width, pixelsPerTick: scale, ); final maxBase = math.max(0, end - wholeVisible); final maxFraction = end >= wholeVisible ? wholeVisible - visible : 0.0; final reset = placement == TimelineCameraPlacement.resetPrimary || initializing && bounds.end == 0; final exceedsEnd = camera.baseTick > maxBase || (camera.baseTick == maxBase && camera.fractionalTick > maxFraction); final overflows = bounds.bottom - bounds.top > bounds.height; final verticalOffset = placement != TimelineCameraPlacement.preserve ? primaryCenter - bounds.height / 2 : overflows ? camera.verticalOffset.clamp(bounds.top, bounds.bottom - bounds.height) : restingVerticalOffset.clamp(bounds.bottom - bounds.height, bounds.top); return TimelineCamera( baseTick: reset ? 0 : exceedsEnd ? maxBase : camera.baseTick, fractionalTick: reset ? 0 : exceedsEnd ? maxFraction : camera.fractionalTick, pixelsPerTick: scale, verticalOffset: verticalOffset, ); } /// Includes half a viewport beyond content without changing authored duration. int timelineNavigationEnd({ required int contentEnd, required double width, required double pixelsPerTick, }) { if (contentEnd == 0 || width <= 0) return contentEnd; final padding = (width / pixelsPerTick / 2).ceil(); return contentEnd + math.min(padding, 0x7fffffffffffffff - contentEnd); } /// Restricts editing views to the nonnegative time domain. TimelineCamera clampTimelineCameraOrigin(TimelineCamera camera) => camera.baseTick < 0 ? TimelineCamera( baseTick: 0, pixelsPerTick: camera.pixelsPerTick, verticalOffset: camera.verticalOffset, ) : camera;