From f6399cf63e2fffe777eca72eb9f7630e95dec8ff Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Mon, 29 Apr 2024 06:02:52 +0200 Subject: [PATCH] Work on #8257 --- src/libse/Common/FfmpegMediaInfo.cs | 2 +- src/ui/Forms/JoinSubtitles.cs | 35 +++++++++++++++++++++-------- src/ui/Logic/ListViewSorter.cs | 18 +++++++++------ 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/libse/Common/FfmpegMediaInfo.cs b/src/libse/Common/FfmpegMediaInfo.cs index c6bfe1050..d4ff98e53 100644 --- a/src/libse/Common/FfmpegMediaInfo.cs +++ b/src/libse/Common/FfmpegMediaInfo.cs @@ -68,7 +68,7 @@ namespace Nikse.SubtitleEdit.Core.Common if (info.VideoWidth == 0 && parts.Length == 2 && int.TryParse(parts[0], out var w) && - int.TryParse(parts[0], out var h)) + int.TryParse(parts[1], out var h)) { info.VideoWidth = w; info.VideoHeight = h; diff --git a/src/ui/Forms/JoinSubtitles.cs b/src/ui/Forms/JoinSubtitles.cs index ccaec13ce..7668c57ef 100644 --- a/src/ui/Forms/JoinSubtitles.cs +++ b/src/ui/Forms/JoinSubtitles.cs @@ -94,15 +94,21 @@ namespace Nikse.SubtitleEdit.Forms private void listViewParts_DragDrop(object sender, DragEventArgs e) { var files = (string[])e.Data.GetData(DataFormats.FileDrop); - foreach (var fileName in files) - { - if (!_fileNamesToJoin.Any(file => file.Equals(fileName, StringComparison.OrdinalIgnoreCase))) - { - _fileNamesToJoin.Add(fileName); - } - } - SortAndLoad(); + TaskDelayHelper.RunDelayed(TimeSpan.FromMilliseconds(1), () => + { + var fileNames = files.ToList(); + fileNames.Sort(ListViewSorter.NaturalComparer); + foreach (var fileName in fileNames) + { + if (!_fileNamesToJoin.Any(file => file.Equals(fileName, StringComparison.OrdinalIgnoreCase))) + { + _fileNamesToJoin.Add(fileName); + } + } + + SortAndLoad(); + }); } private void SortAndLoad() @@ -289,6 +295,7 @@ namespace Nikse.SubtitleEdit.Forms { var sub = subtitles[i]; var lvi = new ListViewItem($"{sub.Paragraphs.Count:#,###,###}"); + lvi.Tag = fileName; if (sub.Paragraphs.Count > 0) { lvi.SubItems.Add(sub.Paragraphs[0].StartTime.ToString()); @@ -371,7 +378,9 @@ namespace Nikse.SubtitleEdit.Forms if (openFileDialog1.ShowDialog(this) == DialogResult.OK) { var sb = new StringBuilder(); - foreach (string fileName in openFileDialog1.FileNames) + var fileNames = openFileDialog1.FileNames.ToList(); + fileNames.Sort(ListViewSorter.NaturalComparer); + foreach (var fileName in fileNames) { Application.DoEvents(); if (File.Exists(fileName)) @@ -390,6 +399,7 @@ namespace Nikse.SubtitleEdit.Forms } } } + SortAndLoad(); if (sb.Length > 0) { @@ -650,6 +660,13 @@ namespace Nikse.SubtitleEdit.Forms lv.Sort(); ListViewSorter.SetSortArrow(listViewParts.Columns[e.Column], sorter.Descending ? SortOrder.Descending : SortOrder.Ascending); + + _fileNamesToJoin.Clear(); + foreach (ListViewItem item in listViewParts.Items) + { + _fileNamesToJoin.Add((string)item.Tag); + } + SortAndLoad(); } } } diff --git a/src/ui/Logic/ListViewSorter.cs b/src/ui/Logic/ListViewSorter.cs index 11de406db..21916d47a 100644 --- a/src/ui/Logic/ListViewSorter.cs +++ b/src/ui/Logic/ListViewSorter.cs @@ -13,8 +13,8 @@ namespace Nikse.SubtitleEdit.Logic public bool IsDisplayFileSize { get; set; } public bool Descending { get; set; } - private readonly Regex _numbers = new Regex(@"\d+", RegexOptions.Compiled); - private readonly Regex _invariantNumber = new Regex(@"\d+\.{1,2}", RegexOptions.Compiled); + private static readonly Regex Numbers = new Regex(@"\d+", RegexOptions.Compiled); + private static readonly Regex InvariantNumber = new Regex(@"\d+\.{1,2}", RegexOptions.Compiled); public int Compare(object o1, object o2) { @@ -33,7 +33,7 @@ namespace Nikse.SubtitleEdit.Logic var s1 = lvi1.SubItems[ColumnNumber].Text; var s2 = lvi2.SubItems[ColumnNumber].Text; - if (_invariantNumber.IsMatch(s1) && _invariantNumber.IsMatch(s2) && + if (InvariantNumber.IsMatch(s1) && InvariantNumber.IsMatch(s2) && int.TryParse(s1, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var i1) && int.TryParse(s2, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var i2)) { @@ -54,10 +54,7 @@ namespace Nikse.SubtitleEdit.Logic return i1 > i2 ? 1 : i1 == i2 ? 0 : -1; } - // use natural sort order - var str2 = _numbers.Replace(lvi2.SubItems[ColumnNumber].Text, m => m.Value.PadLeft(10, '0')).RemoveChar(' '); - var str1 = _numbers.Replace(lvi1.SubItems[ColumnNumber].Text, m => m.Value.PadLeft(10, '0')).RemoveChar(' '); - return string.Compare(str2, str1, StringComparison.Ordinal); + return NaturalComparer(lvi2.SubItems[ColumnNumber].Text, lvi1.SubItems[ColumnNumber].Text); } public static void SetSortArrow(ColumnHeader columnHeader, SortOrder sortOrder) @@ -80,5 +77,12 @@ namespace Nikse.SubtitleEdit.Logic break; } } + + public static int NaturalComparer(string x, string y) + { + var str2 = Numbers.Replace(x, m => m.Value.PadLeft(10, '0')).RemoveChar(' '); + var str1 = Numbers.Replace(y, m => m.Value.PadLeft(10, '0')).RemoveChar(' '); + return string.Compare(str2, str1, StringComparison.OrdinalIgnoreCase); + } } }