Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Merge order: files start in name order, the dialog looks like Filee's others

KnifeLemon6bb9f483e2dc
diff --git a/src/Filee.App/ViewModels/MergeOrderViewModel.cs b/src/Filee.App/ViewModels/MergeOrderViewModel.cs
index 78ed4465..0cf1a743 100644
--- a/src/Filee.App/ViewModels/MergeOrderViewModel.cs
+++ b/src/Filee.App/ViewModels/MergeOrderViewModel.cs
@@ -1,22 +1,30 @@
-// The files in a merged PDF follow the order reviewed here. Moving a row changes only this copy of the selection.
+// The order of a merge into one PDF (issue #268): the files start sorted by name the way people count ("page 2" before
+// "page 10"), and the user moves them before the merge starts. Moving a row changes only this copy of the selection.
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
+using Filee.Core.Platform;
+using Filee.Core.Web;
namespace Filee.App.ViewModels;
+/// <summary>One file of the merge: its name, and the folder it is in (none for a file from a browser, not saved yet).</summary>
public sealed class MergeSource(string path)
{
public string Path { get; } = path;
public string Name => System.IO.Path.GetFileName(Path);
+ public string Folder => WebDrop.IsPlaceholder(Path) ? "" : System.IO.Path.GetDirectoryName(Path) ?? "";
}
public sealed partial class MergeOrderViewModel : ObservableObject
{
public MergeOrderViewModel(IReadOnlyList<string> files)
{
- Files = new(files.Select(file => new MergeSource(file)));
+ Files = new(files
+ .OrderBy(file => System.IO.Path.GetFileName(file), NaturalComparer.Instance)
+ .ThenBy(file => file, NaturalComparer.Instance)
+ .Select(file => new MergeSource(file)));
SelectedFile = Files.FirstOrDefault();
}
@@ -27,8 +35,11 @@ public sealed partial class MergeOrderViewModel : ObservableObject
[NotifyCanExecuteChangedFor(nameof(MoveDownCommand))]
private MergeSource? _selectedFile;
+ /// <summary>The paths in the order they are merged.</summary>
+ public IReadOnlyList<string> OrderedFiles => Files.Select(file => file.Path).ToArray();
+
private bool CanMoveUp() => SelectedFile is not null && Files.IndexOf(SelectedFile) > 0;
- private bool CanMoveDown() => SelectedFile is not null && Files.IndexOf(SelectedFile) is var index && index >= 0 && index < Files.Count - 1;
+ private bool CanMoveDown() => SelectedFile is not null && Files.IndexOf(SelectedFile) is >= 0 and var index && index < Files.Count - 1;
[RelayCommand(CanExecute = nameof(CanMoveUp))]
private void MoveUp() => Move(-1);
@@ -38,16 +49,16 @@ public sealed partial class MergeOrderViewModel : ObservableObject
private void Move(int direction)
{
- if (SelectedFile is not { } selected) return;
+ if (SelectedFile is not { } selected)
+ return;
var index = Files.IndexOf(selected);
var target = index + direction;
- if (index < 0 || target < 0 || target >= Files.Count) return;
+ if (index < 0 || target < 0 || target >= Files.Count)
+ return;
Files.Move(index, target);
- // ListBox can clear its selection while processing a collection move.
+ // The list box can drop its selection while it moves the row.
SelectedFile = selected;
MoveUpCommand.NotifyCanExecuteChanged();
MoveDownCommand.NotifyCanExecuteChanged();
}
-
- public IReadOnlyList<string> OrderedFiles => Files.Select(file => file.Path).ToArray();
}
diff --git a/src/Filee.App/Views/MergeOrderWindow.axaml b/src/Filee.App/Views/MergeOrderWindow.axaml
index 50d6f221..aa913b68 100644
--- a/src/Filee.App/Views/MergeOrderWindow.axaml
+++ b/src/Filee.App/Views/MergeOrderWindow.axaml
@@ -1,3 +1,5 @@
+<!-- Merge order: shown before files merge into one PDF from the donut (RadialController, issue #268). Merge starts the
+ conversion in the order shown; Cancel or closing the window starts nothing. -->
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Filee.App.ViewModels"
@@ -5,46 +7,54 @@
x:DataType="vm:MergeOrderViewModel"
Title="{DynamicResource merge.order_title}"
Icon="avares://Filee/Assets/Icons/filee.ico"
- Width="600" Height="480" MinWidth="440" MinHeight="320"
+ Width="560" Height="480" MinWidth="440" MinHeight="320"
WindowStartupLocation="CenterScreen">
<Window.KeyBindings>
<KeyBinding Gesture="Alt+Up" Command="{Binding MoveUpCommand}" />
<KeyBinding Gesture="Alt+Down" Command="{Binding MoveDownCommand}" />
</Window.KeyBindings>
- <DockPanel Margin="20">
- <StackPanel DockPanel.Dock="Top" Spacing="6" Margin="0,0,0,14">
- <TextBlock Classes="h2" Text="{DynamicResource merge.order_title}" />
- <TextBlock Classes="caption" TextWrapping="Wrap" Text="{DynamicResource merge.order_hint}" />
- </StackPanel>
- <Grid DockPanel.Dock="Bottom" ColumnDefinitions="Auto,*" Margin="0,14,0,0">
- <StackPanel Orientation="Horizontal" Spacing="8">
- <Button x:Name="MoveUp" Content="↑" Command="{Binding MoveUpCommand}"
- AutomationProperties.Name="{DynamicResource group.move_up}"
- ToolTip.Tip="{DynamicResource group.move_up}" />
- <Button x:Name="MoveDown" Content="↓" Command="{Binding MoveDownCommand}"
- AutomationProperties.Name="{DynamicResource group.move_down}"
- ToolTip.Tip="{DynamicResource group.move_down}" />
+ <DockPanel>
+ <Border DockPanel.Dock="Bottom" Padding="20,12" BorderThickness="0,1,0,0" BorderBrush="{DynamicResource SubtleBorderBrush}"
+ Background="{DynamicResource SurfaceBrush}">
+ <DockPanel>
+ <StackPanel DockPanel.Dock="Left" Orientation="Horizontal" Spacing="6">
+ <Button x:Name="MoveUp" Classes="icon" Command="{Binding MoveUpCommand}"
+ ToolTip.Tip="{DynamicResource group.move_up}" AutomationProperties.Name="{DynamicResource group.move_up}">
+ <PathIcon Width="14" Height="14" Data="{StaticResource Icon.ArrowUp}" />
+ </Button>
+ <Button x:Name="MoveDown" Classes="icon" Command="{Binding MoveDownCommand}"
+ ToolTip.Tip="{DynamicResource group.move_down}" AutomationProperties.Name="{DynamicResource group.move_down}">
+ <PathIcon Width="14" Height="14" Data="{StaticResource Icon.ArrowDown}" />
+ </Button>
+ </StackPanel>
+ <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Spacing="8">
+ <Button x:Name="CancelMerge" Content="{DynamicResource common.cancel}" Click="OnCancel" IsCancel="True" />
+ <Button x:Name="StartMerge" Classes="accent" Content="{DynamicResource preset.merge_pdf}" Click="OnMerge" IsDefault="True" />
+ </StackPanel>
+ </DockPanel>
+ </Border>
+ <DockPanel Margin="20,18,20,14">
+ <StackPanel DockPanel.Dock="Top" Spacing="6" Margin="0,0,0,12">
+ <TextBlock Classes="h2" Text="{DynamicResource merge.order_title}" />
+ <TextBlock Classes="caption" TextWrapping="Wrap" Text="{DynamicResource merge.order_hint}" />
</StackPanel>
- <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" Spacing="8">
- <Button x:Name="CancelMerge" Content="{DynamicResource common.cancel}" Click="OnCancel" IsCancel="True" />
- <Button x:Name="StartMerge" Classes="accent" Content="{DynamicResource preset.merge_pdf}" Click="OnMerge" IsDefault="True" />
- </StackPanel>
- </Grid>
- <ListBox x:Name="FileOrder" ItemsSource="{Binding Files}" SelectedItem="{Binding SelectedFile}"
- ScrollViewer.HorizontalScrollBarVisibility="Disabled">
- <ListBox.Styles>
- <Style Selector="ListBoxItem">
- <Setter Property="HorizontalContentAlignment" Value="Stretch" />
- </Style>
- </ListBox.Styles>
- <ListBox.ItemTemplate>
- <DataTemplate x:DataType="vm:MergeSource">
- <StackPanel Spacing="2" Margin="4,6" ToolTip.Tip="{Binding Path}">
- <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
- <TextBlock Classes="caption" Text="{Binding Path}" TextTrimming="CharacterEllipsis" />
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
+ <ListBox x:Name="FileOrder" ItemsSource="{Binding Files}" SelectedItem="{Binding SelectedFile}"
+ ScrollViewer.HorizontalScrollBarVisibility="Disabled">
+ <ListBox.Styles>
+ <Style Selector="ListBoxItem">
+ <Setter Property="HorizontalContentAlignment" Value="Stretch" />
+ </Style>
+ </ListBox.Styles>
+ <ListBox.ItemTemplate>
+ <DataTemplate x:DataType="vm:MergeSource">
+ <StackPanel Spacing="2" Margin="4,6" ToolTip.Tip="{Binding Path}">
+ <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
+ <TextBlock Classes="caption" Text="{Binding Folder}" TextTrimming="CharacterEllipsis"
+ IsVisible="{Binding Folder, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />
+ </StackPanel>
+ </DataTemplate>
+ </ListBox.ItemTemplate>
+ </ListBox>
+ </DockPanel>
</DockPanel>
</Window>
diff --git a/src/Filee.Core/Platform/NaturalComparer.cs b/src/Filee.Core/Platform/NaturalComparer.cs
new file mode 100644
index 00000000..1317e2b4
--- /dev/null
+++ b/src/Filee.Core/Platform/NaturalComparer.cs
@@ -0,0 +1,42 @@
+// Orders names the way people count: comic book pages, files to merge.
+
+namespace Filee.Core.Platform;
+
+/// <summary>
+/// Compares names the way people number pages and files: digit runs by value ("p2" &lt; "p10"), case-insensitively.
+/// </summary>
+public sealed class NaturalComparer : IComparer<string>
+{
+ public static readonly NaturalComparer Instance = new();
+
+ public int Compare(string? x, string? y)
+ {
+ if (x is null || y is null)
+ return string.CompareOrdinal(x, y);
+ int i = 0, j = 0;
+ while (i < x.Length && j < y.Length)
+ {
+ if (char.IsAsciiDigit(x[i]) && char.IsAsciiDigit(y[j]))
+ {
+ var startX = i;
+ var startY = j;
+ while (i < x.Length && char.IsAsciiDigit(x[i]))
+ i++;
+ while (j < y.Length && char.IsAsciiDigit(y[j]))
+ j++;
+ var numberX = x[startX..i].TrimStart('0');
+ var numberY = y[startY..j].TrimStart('0');
+ var byValue = numberX.Length != numberY.Length ? numberX.Length.CompareTo(numberY.Length) : string.CompareOrdinal(numberX, numberY);
+ if (byValue != 0)
+ return byValue;
+ continue;
+ }
+ var byChar = char.ToLowerInvariant(x[i]).CompareTo(char.ToLowerInvariant(y[j]));
+ if (byChar != 0)
+ return byChar;
+ i++;
+ j++;
+ }
+ return (x.Length - i).CompareTo(y.Length - j);
+ }
+}
diff --git a/src/Filee.Engines/Ebooks/ComicBook.cs b/src/Filee.Engines/Ebooks/ComicBook.cs
index e66eb832..d757a8c8 100644
--- a/src/Filee.Engines/Ebooks/ComicBook.cs
+++ b/src/Filee.Engines/Ebooks/ComicBook.cs
@@ -5,6 +5,7 @@
using System.IO.Compression;
using Filee.Core.Conversion;
+using Filee.Core.Platform;
using ImageMagick;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
@@ -175,40 +176,3 @@ internal static class ComicBook
return pages;
}
}
-
-/// <summary>Compares names the way people number pages: digit runs by value ("p2" &lt; "p10"), case-insensitively.</summary>
-internal sealed class NaturalComparer : IComparer<string>
-{
- public static readonly NaturalComparer Instance = new();
-
- public int Compare(string? x, string? y)
- {
- if (x is null || y is null)
- return string.CompareOrdinal(x, y);
- int i = 0, j = 0;
- while (i < x.Length && j < y.Length)
- {
- if (char.IsAsciiDigit(x[i]) && char.IsAsciiDigit(y[j]))
- {
- var startX = i;
- var startY = j;
- while (i < x.Length && char.IsAsciiDigit(x[i]))
- i++;
- while (j < y.Length && char.IsAsciiDigit(y[j]))
- j++;
- var numberX = x[startX..i].TrimStart('0');
- var numberY = y[startY..j].TrimStart('0');
- var byValue = numberX.Length != numberY.Length ? numberX.Length.CompareTo(numberY.Length) : string.CompareOrdinal(numberX, numberY);
- if (byValue != 0)
- return byValue;
- continue;
- }
- var byChar = char.ToLowerInvariant(x[i]).CompareTo(char.ToLowerInvariant(y[j]));
- if (byChar != 0)
- return byChar;
- i++;
- j++;
- }
- return (x.Length - i).CompareTo(y.Length - j);
- }
-}
diff --git a/tests/Filee.App.Tests/MergeOrderTests.cs b/tests/Filee.App.Tests/MergeOrderTests.cs
index cb171f07..49e1624c 100644
--- a/tests/Filee.App.Tests/MergeOrderTests.cs
+++ b/tests/Filee.App.Tests/MergeOrderTests.cs
@@ -1,4 +1,4 @@
-// Issue #268: interact with the real order chooser, then inspect the actual merged PDF's pages.
+// Merge order (issue #268): the real chooser opened from the donut, then the pages of the merged PDF.
using Avalonia;
using Avalonia.Controls;
@@ -67,6 +67,7 @@ public class MergeOrderTests
RenderTests.Pump();
var dialog = Assert.IsType<MergeOrderWindow>(windows.MergeOrder);
+ // Sorted by name, then folder: 200, 300, 400.
var vm = Assert.IsType<MergeOrderViewModel>(dialog.DataContext);
Assert.False(radial.IsVisible);
Assert.DoesNotContain(conversions.Jobs, j => !before.Contains(j.Job.Id));
@@ -125,7 +126,7 @@ public class MergeOrderTests
[InlineData("pl")]
[InlineData("hi")]
[InlineData("ru")]
- public void Chooser_renders_long_paths_at_its_minimum_width(string language)
+ public void Chooser_fits_long_paths_at_its_minimum_width(string language)
{
TestServices.EnsureInitialized(language);
var vm = new MergeOrderViewModel([
@@ -137,14 +138,39 @@ public class MergeOrderTests
{
window.Show();
RenderTests.Pump();
- var merge = window.FindControl<Button>("StartMerge")!;
- var cancel = window.FindControl<Button>("CancelMerge")!;
- var down = window.FindControl<Button>("MoveDown")!;
- Assert.True(merge.TranslatePoint(default, window)!.Value.X > down.TranslatePoint(default, window)!.Value.X + down.Bounds.Width);
- Assert.True(cancel.TranslatePoint(default, window)!.Value.X + cancel.Bounds.Width <= merge.TranslatePoint(default, window)!.Value.X);
+ Rect Box(string name)
+ {
+ var control = window.FindControl<Control>(name)!;
+ return new Rect(control.Bounds.Size).TransformToAABB(control.TransformToVisual(window)!.Value);
+ }
+ var (merge, cancel, down) = (Box("StartMerge"), Box("CancelMerge"), Box("MoveDown"));
+ // Arabic mirrors the row: the arrows go to the right, Merge to the left edge.
+ if (language == "ar")
+ {
+ Assert.True(merge.Right < down.Left);
+ Assert.True(cancel.Left >= merge.Right);
+ }
+ else
+ {
+ Assert.True(merge.Left > down.Right);
+ Assert.True(cancel.Right <= merge.Left);
+ }
+ Assert.True(merge.Right <= window.Bounds.Width && merge.Left >= 0);
Assert.True(window.FindControl<ListBox>("FileOrder")!.Bounds.Height > 80);
- RenderTests.Save(window, $"issue268-merge-order-{language}.png");
+ RenderTests.Save(window, $"merge-order-{language}.png");
}
finally { window.Close(); }
}
+
+ [Fact]
+ public void Files_start_in_name_order_counting_numbers_and_browser_files_show_no_folder()
+ {
+ var browser = Path.Combine(Filee.Core.Web.WebDrop.DefaultWorkRoot, "abc", "drop", "1", "page 1.pdf");
+ var vm = new MergeOrderViewModel([@"C:\b\page 10.pdf", @"C:\a\page 2.pdf", browser, @"C:\a\page 10.pdf"]);
+ Assert.Equal([browser, @"C:\a\page 2.pdf", @"C:\a\page 10.pdf", @"C:\b\page 10.pdf"], vm.OrderedFiles);
+ Assert.Equal("", vm.Files[0].Folder);
+ Assert.Equal(Path.GetDirectoryName(@"C:\a\page 2.pdf"), vm.Files[1].Folder);
+ Assert.False(vm.MoveUpCommand.CanExecute(null));
+ Assert.True(vm.MoveDownCommand.CanExecute(null));
+ }
}