From a588659f1a012021c754edf22f9197b841da0803 Mon Sep 17 00:00:00 2001 From: bartekmotyl <29923773+bartekmotyl@users.noreply.github.com> Date: Sat, 30 Jul 2022 18:14:44 +0200 Subject: [PATCH] fix for #61 --- src/SimpleVideoCutter/VideoCutterTimeline.cs | 53 ++++++++++++++------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/SimpleVideoCutter/VideoCutterTimeline.cs b/src/SimpleVideoCutter/VideoCutterTimeline.cs index 14f3b17..7e4d27f 100644 --- a/src/SimpleVideoCutter/VideoCutterTimeline.cs +++ b/src/SimpleVideoCutter/VideoCutterTimeline.cs @@ -16,6 +16,7 @@ public class Selection { public long Start; public long End; + public long Duration => End - Start; public bool Includes(long position) { @@ -158,7 +159,7 @@ public void AddSelection(long start, long end) public bool Empty => selections.Count == 0; public long? OverallStart => selections.FirstOrDefault()?.Start; public long? OverallEnd => selections.LastOrDefault()?.End; - public long OverallDuration => OverallEnd ?? 0 - OverallStart ?? 0; + public long OverallDuration => selections.Sum(sel => sel.Duration); public List AllSelections => selections; @@ -209,29 +210,51 @@ public void DeleteSelection(int index) public bool SetSelectionStart(int index, long value) { var selection = this.selections[index]; - var prev = index > 0 ? this.selections[index-1] : null; - if (prev != null && prev.End > value) + var prevStart = selection.Start; + try { - selections[index].Start = prev.End+1; - return false; + var prev = index > 0 ? this.selections[index - 1] : null; + if (prev != null && prev.End > value) + { + selections[index].Start = prev.End + 1; + return false; + } + + selections[index].Start = value > selections[index].End ? selections[index].End : value; + return true; + } + finally + { + if (selections[index].Start != prevStart) + { + OnSelectionsChanged(); + } } - - selections[index].Start = value > selections[index].End ? selections[index].End : value; - return true; } public bool SetSelectionEnd(int index, long value) { var selection = this.selections[index]; - var next = index < selections.Count - 1 ? this.selections[index + 1] : null; - if (next != null && next.Start < value) + var prevEnd = selection.End; + try { - selections[index].End = next.Start - 1; - return false; - } + var next = index < selections.Count - 1 ? this.selections[index + 1] : null; + if (next != null && next.Start < value) + { + selections[index].End = next.Start - 1; + return false; + } - selections[index].End = value < selections[index].Start ? selections[index].Start : value; - return true; + selections[index].End = value < selections[index].Start ? selections[index].Start : value; + return true; + } + finally + { + if (selections[index].End != prevEnd) + { + OnSelectionsChanged(); + } + } } public bool CanStartSelectionAtFrame(long frame)