Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minVideoLength to TrimViewer parameters #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/src/trim_viewer/fixed_viewer/fixed_trim_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class FixedTrimViewer extends StatefulWidget {
/// For defining the maximum length of the output video.
final Duration maxVideoLength;

/// For defining the minimal length of the output video
final Duration minVideoLength;

/// For showing the start and the end point of the
/// video on top of the trimmer area.
///
Expand Down Expand Up @@ -119,6 +122,7 @@ class FixedTrimViewer extends StatefulWidget {
this.viewerWidth = 50.0 * 8,
this.viewerHeight = 50,
this.maxVideoLength = const Duration(milliseconds: 0),
this.minVideoLength = const Duration(milliseconds: 0),
this.showDuration = true,
this.durationTextStyle = const TextStyle(color: Colors.white),
this.durationStyle = DurationStyle.FORMAT_HH_MM_SS,
Expand Down Expand Up @@ -161,6 +165,7 @@ class _FixedTrimViewerState extends State<FixedTrimViewer>

double? fraction;
double? maxLengthPixels;
double? minLengthPixels;

FixedThumbnailViewer? thumbnailWidget;

Expand Down Expand Up @@ -228,6 +233,10 @@ class _FixedTrimViewerState extends State<FixedTrimViewer>
maxLengthPixels = _thumbnailViewerW;
}

minLengthPixels = maxLengthPixels! *
(widget.minVideoLength.inMilliseconds /
widget.maxVideoLength.inMilliseconds);

_videoEndPos = fraction != null
? _videoDuration.toDouble() * fraction!
: _videoDuration.toDouble();
Expand Down Expand Up @@ -345,7 +354,8 @@ class _FixedTrimViewerState extends State<FixedTrimViewer>
_startCircleSize = widget.editorProperties.circleSizeOnDrag;
if ((_startPos.dx + details.delta.dx >= 0) &&
(_startPos.dx + details.delta.dx <= _endPos.dx) &&
!(_endPos.dx - _startPos.dx - details.delta.dx > maxLengthPixels!)) {
!(_endPos.dx - _startPos.dx - details.delta.dx > maxLengthPixels!) &&
!(_endPos.dx - _startPos.dx - details.delta.dx < minLengthPixels!)) {
_startPos += details.delta;
_onStartDragged();
}
Expand All @@ -363,7 +373,8 @@ class _FixedTrimViewerState extends State<FixedTrimViewer>
_endCircleSize = widget.editorProperties.circleSizeOnDrag;
if ((_endPos.dx + details.delta.dx <= _thumbnailViewerW) &&
(_endPos.dx + details.delta.dx >= _startPos.dx) &&
!(_endPos.dx - _startPos.dx + details.delta.dx > maxLengthPixels!)) {
!(_endPos.dx - _startPos.dx + details.delta.dx > maxLengthPixels!) &&
!(_endPos.dx - _startPos.dx + details.delta.dx < minLengthPixels!)) {
_endPos += details.delta;
_onEndDragged();
}
Expand Down
15 changes: 13 additions & 2 deletions lib/src/trim_viewer/scrollable_viewer/scrollable_trim_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ScrollableTrimViewer extends StatefulWidget {
/// For defining the maximum length of the output video.
final Duration maxVideoLength;

/// For defining the minimal length of the output video.
final Duration minVideoLength;

/// For showing the start and the end point of the
/// video on top of the trimmer area.
///
Expand Down Expand Up @@ -122,6 +125,7 @@ class ScrollableTrimViewer extends StatefulWidget {
super.key,
required this.trimmer,
required this.maxVideoLength,
required this.minVideoLength,
required this.onThumbnailLoadingComplete,
this.viewerWidth = 50 * 8,
this.viewerHeight = 50,
Expand Down Expand Up @@ -175,6 +179,7 @@ class _ScrollableTrimViewerState extends State<ScrollableTrimViewer>

double? fraction;
double? maxLengthPixels;
double? minLengthPixels;

ScrollableThumbnailViewer? thumbnailWidget;

Expand Down Expand Up @@ -302,6 +307,8 @@ class _ScrollableTrimViewerState extends State<ScrollableTrimViewer>
log('Total Video Length: $totalDuration');
final maxVideoLength = widget.maxVideoLength;
log('Max Video Length: $maxVideoLength');
final minVideoLength = widget.minVideoLength;
log('Min Video Length: $minVideoLength');
final paddingFraction = widget.paddingFraction;
log('Padding Fraction: $paddingFraction');
// trimAreaTime = maxVideoLength + (paddingFraction * maxVideoLength) * 2
Expand Down Expand Up @@ -359,6 +366,8 @@ class _ScrollableTrimViewerState extends State<ScrollableTrimViewer>
log('trimmerFraction: $trimmerFraction');
final trimmerCover = trimmerFraction * trimAreaLength;
maxLengthPixels = trimmerCover;
minLengthPixels = maxLengthPixels! *
(minVideoLength.inMilliseconds / maxVideoLength.inMilliseconds);
_endPos = Offset(trimmerCover, thumbnailHeight);
log('START: $_startPos, END: $_endPos');

Expand Down Expand Up @@ -476,7 +485,8 @@ class _ScrollableTrimViewerState extends State<ScrollableTrimViewer>
_startCircleSize = widget.editorProperties.circleSizeOnDrag;
if ((_startPos.dx + details.delta.dx >= 0) &&
(_startPos.dx + details.delta.dx <= _endPos.dx) &&
!(_endPos.dx - _startPos.dx - details.delta.dx > maxLengthPixels!)) {
!(_endPos.dx - _startPos.dx - details.delta.dx > maxLengthPixels!) &&
!(_endPos.dx - _startPos.dx - details.delta.dx < minLengthPixels!)) {
_startPos += details.delta;
_onStartDragged();
}
Expand All @@ -494,7 +504,8 @@ class _ScrollableTrimViewerState extends State<ScrollableTrimViewer>
_endCircleSize = widget.editorProperties.circleSizeOnDrag;
if ((_endPos.dx + details.delta.dx <= _thumbnailViewerW) &&
(_endPos.dx + details.delta.dx >= _startPos.dx) &&
!(_endPos.dx - _startPos.dx + details.delta.dx > maxLengthPixels!)) {
!(_endPos.dx - _startPos.dx + details.delta.dx > maxLengthPixels!)&&
!(_endPos.dx - _startPos.dx + details.delta.dx < minLengthPixels!)) {
_endPos += details.delta;
_onEndDragged();
}
Expand Down
12 changes: 12 additions & 0 deletions lib/src/trim_viewer/trim_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class TrimViewer extends StatefulWidget {
/// specifying this property is mandatory.
final Duration maxVideoLength;

/// For defining the minimal length of the output video.
///
/// **NOTE:** When explicitly setting the `type` to `scrollable`,
/// specifying this property is mandatory.
final Duration minVideoLength;

/// For showing the start and the end point of the
/// video on top of the trimmer area.
///
Expand Down Expand Up @@ -119,6 +125,10 @@ class TrimViewer extends StatefulWidget {
///
/// * [maxVideoLength] for specifying the maximum length of the
/// output video.
///
///
/// * [minVideoLength] for specifying the minimal length of the
/// output video.
///
///
/// * [circlePaintColor] for specifying a color to the circle.
Expand Down Expand Up @@ -171,6 +181,7 @@ class TrimViewer extends StatefulWidget {
Key? key,
required this.trimmer,
this.maxVideoLength = const Duration(milliseconds: 0),
this.minVideoLength = const Duration(milliseconds: 0),
this.type = ViewerType.auto,
this.viewerWidth = 50 * 8,
this.viewerHeight = 50,
Expand Down Expand Up @@ -223,6 +234,7 @@ class _TrimViewerState extends State<TrimViewer> with TickerProviderStateMixin {
final scrollableViewer = ScrollableTrimViewer(
trimmer: widget.trimmer,
maxVideoLength: widget.maxVideoLength,
minVideoLength: widget.minVideoLength,
viewerWidth: widget.viewerWidth,
viewerHeight: widget.viewerHeight,
showDuration: widget.showDuration,
Expand Down