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.

Allow several mixed-selection profiles in the profile selector

KnifeLemon504cf1110b86
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index d0f651b2..ae6f814c 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -32,7 +32,9 @@
A drag gesture (modifier + drag past the threshold) opens the donut *before* files are known.
2. **Donut** – `RadialWindow` is transparent, topmost and not activated, so Explorer's drag continues.
The first `DragEnter` delivers the file list; `RadialViewModel` picks a `ToolbarProfile` by extension
- (`ProfileSelector`) and asks `PresetAvailability` which presets can run (disabled slices get a reason).
+ (`ProfileSelector`: the first normal profile that has every file's extension; for a mix of types the first
+ mixed-selection profile whose checked extensions cover the files, else the one with nothing checked) and asks
+ `PresetAvailability` which presets can run (disabled slices get a reason).
3. **Drop** – the slice under the cursor is a preset. `ConversionService.Start` enqueues a `ConversionJob`.
The drop is always reported as *Copy* so Explorer never deletes the source files.
4. **Plan** – for every file, `RoutePlanner` finds the cheapest chain of converter edges from the source format to
diff --git a/src/Filee.Core/Profiles/ToolbarProfile.cs b/src/Filee.Core/Profiles/ToolbarProfile.cs
index da5764e9..95406ab1 100644
--- a/src/Filee.Core/Profiles/ToolbarProfile.cs
+++ b/src/Filee.Core/Profiles/ToolbarProfile.cs
@@ -19,12 +19,17 @@ public sealed class ToolbarProfile
public string? NameKey { get; set; }
- /// <summary>Source extensions (without dot) this profile is used for.</summary>
+ /// <summary>
+ /// Source extensions (without dot). For a normal profile: the files it is used for. For a mixed-selection profile
+ /// (<see cref="IsFallback"/>): the extensions a mixed selection must stay within ("every selected file is one
+ /// of ..."); empty means any mixed selection.
+ /// </summary>
public List<string> Extensions { get; set; } = [];
/// <summary>
- /// Used when dropped files match no other profile, or when they span several profiles.
- /// Exactly one profile should be the fallback.
+ /// A "mixed files" profile: used when the dropped files span several profiles (or match none). Several may exist,
+ /// each limited to its <see cref="Extensions"/>; one with no extensions takes every other mixed selection
+ /// (see <see cref="ProfileSelector.Select"/>). The name is kept for compatibility with saved profiles.
/// </summary>
public bool IsFallback { get; set; }
@@ -50,8 +55,12 @@ public sealed class ToolbarProfile
public static class ProfileSelector
{
/// <summary>
- /// Picks the profile matching every file's extension. If the files match different profiles
- /// (or none), the fallback profile is returned.
+ /// Picks the profile for the given files, in this order:
+ /// <list type="number">
+ /// <item>the first normal profile that has every file's extension;</item>
+ /// <item>otherwise the first mixed-selection profile (list order) whose extensions include every file's;</item>
+ /// <item>otherwise the catch-all (see <see cref="CatchAll"/>).</item>
+ /// </list>
/// </summary>
public static ToolbarProfile? Select(IReadOnlyList<ToolbarProfile> profiles, IEnumerable<string> filePaths)
{
@@ -62,14 +71,25 @@ public static class ProfileSelector
if (extensions.Count > 0)
{
- var match = profiles.FirstOrDefault(p => !p.IsFallback && extensions.All(p.Matches));
+ var match = profiles.FirstOrDefault(p => !p.IsFallback && extensions.All(p.Matches))
+ ?? profiles.FirstOrDefault(p => p.IsFallback && p.Extensions.Count > 0 && extensions.All(p.Matches));
if (match is not null)
return match;
}
- return profiles.FirstOrDefault(p => p.IsFallback) ?? profiles.FirstOrDefault();
+ return CatchAll(profiles);
}
+ /// <summary>
+ /// The profile used for selections no other profile takes: the first mixed-selection profile with no extensions.
+ /// If the user limited every mixed profile to some extensions, the first mixed profile is still better than an
+ /// unrelated single-type donut; the first profile is the last resort (no mixed profile at all).
+ /// </summary>
+ public static ToolbarProfile? CatchAll(IReadOnlyList<ToolbarProfile> profiles) =>
+ profiles.FirstOrDefault(p => p.IsFallback && p.Extensions.Count == 0)
+ ?? profiles.FirstOrDefault(p => p.IsFallback)
+ ?? profiles.FirstOrDefault();
+
/// <summary>Formats of the given files that Filee recognises.</summary>
public static IReadOnlyList<FileFormat> DetectFormats(IEnumerable<string> filePaths) =>
filePaths.Select(FormatRegistry.Detect).OfType<FileFormat>().Distinct().ToList();
diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs
index 129e1500..ba27b8ba 100644
--- a/src/Filee.Core/Settings/UserDataStore.cs
+++ b/src/Filee.Core/Settings/UserDataStore.cs
@@ -138,17 +138,18 @@ public sealed class UserDataStore
Presets.RemoveAll(p => p.Id == preset.Id);
Presets.Add(preset);
}
+ // Mixed-selection profiles stay mixed: several may exist, and the catch-all is the first one with nothing
+ // checked, so an imported one added at the end never replaces ours.
foreach (var profile in bundle.Profiles)
{
Profiles.RemoveAll(p => p.Id == profile.Id);
- profile.IsFallback = false; // keep our own fallback
Profiles.Add(profile);
}
SaveLibrary();
return bundle.Presets.Count + bundle.Profiles.Count;
}
- /// <summary>Removes dangling preset references and guarantees exactly one fallback profile.</summary>
+ /// <summary>Removes dangling preset references and guarantees at least one mixed-selection profile.</summary>
private void EnsureConsistency()
{
var ids = Presets.Select(p => p.Id).ToHashSet();
@@ -157,15 +158,13 @@ public sealed class UserDataStore
profile.PresetIds = profile.PresetIds.Where(ids.Contains).Distinct().Take(ToolbarProfile.MaxSlices).ToList();
}
- var fallbacks = Profiles.Where(p => p.IsFallback).ToList();
- if (fallbacks.Count == 0)
+ // Several mixed profiles are fine (each limited to the extensions checked on it); none is not.
+ if (!Profiles.Any(p => p.IsFallback))
{
var mixed = BuiltInData.CreateProfiles().First(p => p.IsFallback);
mixed.PresetIds = mixed.PresetIds.Where(ids.Contains).ToList();
Profiles.Add(mixed);
}
- foreach (var extra in fallbacks.Skip(1))
- extra.IsFallback = false;
}
private T? Read<T>(string path, JsonTypeInfo<T> type) where T : class
@@ -263,7 +262,7 @@ internal static class SettingsMigrations
var changed = false;
// v4: Markdown / TXT / HTML files get their own donut instead of the "mixed files" fallback. Skipped when
// the user already routes Markdown through a profile of their own.
- if (from < 4 && !profiles.Any(p => p.Id == "text" || p.Extensions.Contains("md", StringComparer.OrdinalIgnoreCase)))
+ if (from < 4 && !profiles.Any(p => p.Id == "text" || (!p.IsFallback && p.Extensions.Contains("md", StringComparer.OrdinalIgnoreCase))))
{
var text = BuiltInData.TextProfile();
var ids = presets.Select(p => p.Id).ToHashSet();
@@ -294,7 +293,7 @@ internal static class SettingsMigrations
// The new donut takes over these extensions from the built-in documents donut, but not from
// profiles the user made.
changed |= office?.Extensions.RemoveAll(split.Extensions.Contains) > 0;
- if (profiles.Any(p => p.Extensions.Intersect(split.Extensions, StringComparer.OrdinalIgnoreCase).Any()))
+ if (profiles.Any(p => !p.IsFallback && p.Extensions.Intersect(split.Extensions, StringComparer.OrdinalIgnoreCase).Any()))
continue;
insertAt = insertAt < 0 ? profiles.Count : insertAt;
profiles.Insert(insertAt++, split);
@@ -324,7 +323,8 @@ internal static class SettingsMigrations
}
var presetIds = presets.Select(p => p.Id).ToHashSet();
- var claimed = profiles.SelectMany(p => p.Extensions).ToHashSet(StringComparer.OrdinalIgnoreCase);
+ // Extensions checked on a mixed-selection profile only limit that profile; they don't claim files.
+ var claimed = profiles.Where(p => !p.IsFallback).SelectMany(p => p.Extensions).ToHashSet(StringComparer.OrdinalIgnoreCase);
var defaults = BuiltInData.CreateProfiles().ToDictionary(p => p.Id);
foreach (var profile in profiles.Where(p => !p.IsFallback && defaults.ContainsKey(p.Id)))
{
diff --git a/tests/Filee.Core.Tests/ProfileSelectorTests.cs b/tests/Filee.Core.Tests/ProfileSelectorTests.cs
new file mode 100644
index 00000000..2c852bad
--- /dev/null
+++ b/tests/Filee.Core.Tests/ProfileSelectorTests.cs
@@ -0,0 +1,152 @@
+// Which donut opens for a set of files: normal profiles first, then mixed-selection profiles limited to checked
+// extensions, then the catch-all.
+
+using Filee.Core.Presets;
+using Filee.Core.Profiles;
+
+namespace Filee.Core.Tests;
+
+public class ProfileSelectorTests
+{
+ private readonly List<ToolbarProfile> _builtIn = BuiltInData.CreateProfiles();
+
+ private static ToolbarProfile Normal(string id, params string[] extensions) => new() { Id = id, Extensions = [.. extensions] };
+
+ private static ToolbarProfile Mixed(string id, params string[] extensions) =>
+ new() { Id = id, IsFallback = true, Extensions = [.. extensions] };
+
+ private static string? Pick(IReadOnlyList<ToolbarProfile> profiles, params string[] files) =>
+ ProfileSelector.Select(profiles, files)?.Id;
+
+ [Fact]
+ public void Picks_profile_matching_all_files() => Assert.Equal("images", Pick(_builtIn, "a.jpg", "b.PNG"));
+
+ [Theory]
+ [InlineData("pdf", "a.pdf")]
+ [InlineData("archives", "a.tar.gz", "b.zip")]
+ [InlineData("video", @"C:\clips\a.MP4", @"C:\clips\b.mkv")]
+ [InlineData("text", "notes.md", "readme.txt")]
+ public void Built_in_profiles_take_their_files(string expected, params string[] files) =>
+ Assert.Equal(expected, Pick(_builtIn, files));
+
+ [Fact]
+ public void Mixed_files_use_the_fallback() => Assert.Equal("mixed", Pick(_builtIn, "a.jpg", "b.docx"));
+
+ [Fact]
+ public void Unknown_files_use_the_fallback() => Assert.Equal("mixed", Pick(_builtIn, "a.xyz"));
+
+ [Fact]
+ public void No_files_use_the_fallback() => Assert.Equal("mixed", Pick(_builtIn));
+
+ [Fact]
+ public void Folders_and_files_without_extension_use_the_fallback()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Mixed("mixed"), Mixed("jpg-and-more", "jpg", "pdf")];
+ Assert.Equal("mixed", Pick(profiles, @"C:\photos", "a.jpg"));
+ }
+
+ [Fact]
+ public void A_mixed_profile_takes_a_selection_within_its_checked_extensions()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg", "png"), Normal("pdf", "pdf"), Mixed("mixed"), Mixed("scans", "jpg", "png", "pdf")];
+
+ Assert.Equal("scans", Pick(profiles, "a.jpg", "b.pdf"));
+ Assert.Equal("scans", Pick(profiles, "a.jpg", "b.png", "c.pdf"));
+ }
+
+ [Fact]
+ public void Every_file_must_be_checked_on_the_mixed_profile()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Normal("pdf", "pdf"), Normal("office", "docx"), Mixed("mixed"), Mixed("scans", "jpg", "pdf")];
+ Assert.Equal("mixed", Pick(profiles, "a.jpg", "b.pdf", "c.docx"));
+ }
+
+ [Fact]
+ public void A_normal_profile_wins_over_a_mixed_one_listing_the_same_extensions()
+ {
+ // Single-type selections are unchanged: the mixed profile only takes what no normal profile takes.
+ List<ToolbarProfile> profiles = [Mixed("scans", "jpg", "pdf"), Normal("images", "jpg"), Mixed("mixed")];
+ Assert.Equal("images", Pick(profiles, "a.jpg", "b.JPG"));
+
+ // A normal profile holding every extension of the selection wins as well.
+ profiles.Insert(0, Normal("mine", "jpg", "pdf"));
+ Assert.Equal("mine", Pick(profiles, "a.jpg", "b.pdf"));
+ }
+
+ [Fact]
+ public void The_first_matching_mixed_profile_in_list_order_wins()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Normal("pdf", "pdf"), Mixed("broad", "jpg", "png", "pdf", "docx"), Mixed("narrow", "jpg", "pdf")];
+ Assert.Equal("broad", Pick(profiles, "a.jpg", "b.pdf"));
+
+ profiles.Reverse();
+ Assert.Equal("narrow", Pick(profiles, "a.jpg", "b.pdf"));
+ }
+
+ [Fact]
+ public void The_catch_all_position_does_not_matter_for_limited_profiles()
+ {
+ List<ToolbarProfile> profiles = [Mixed("mixed"), Normal("images", "jpg"), Normal("pdf", "pdf"), Mixed("scans", "jpg", "pdf")];
+ Assert.Equal("scans", Pick(profiles, "a.jpg", "b.pdf"));
+ }
+
+ [Fact]
+ public void Checked_extensions_are_compared_without_case_and_dots()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Normal("pdf", "pdf"), Mixed("mixed"), Mixed("scans", "JPG", "Pdf")];
+ Assert.Equal("scans", Pick(profiles, "A.Jpg", "b.PDF"));
+ }
+
+ [Fact]
+ public void Two_part_extensions_are_matched_as_a_whole()
+ {
+ List<ToolbarProfile> profiles = [Normal("archives", "zip", "tar.gz"), Normal("images", "png"), Mixed("mixed"), Mixed("backup", "tar.gz", "png")];
+
+ Assert.Equal("backup", Pick(profiles, "site.tar.gz", "logo.png"));
+ Assert.Equal("mixed", Pick(profiles, "site.gz", "logo.png")); // plain .gz is not .tar.gz
+ }
+
+ [Fact]
+ public void Unknown_extensions_can_be_checked_on_a_mixed_profile()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Mixed("mixed"), Mixed("raw-and-sidecar", "jpg", "xmp")];
+
+ Assert.Equal("raw-and-sidecar", Pick(profiles, "a.jpg", "a.xmp"));
+ Assert.Equal("raw-and-sidecar", Pick(profiles, "a.xmp")); // no normal profile takes .xmp
+ }
+
+ [Fact]
+ public void A_normal_profile_without_extensions_is_never_picked()
+ {
+ List<ToolbarProfile> profiles = [Normal("new"), Normal("images", "jpg"), Mixed("mixed")];
+
+ Assert.Equal("images", Pick(profiles, "a.jpg"));
+ Assert.Equal("mixed", Pick(profiles, "a.xyz"));
+ }
+
+ [Fact]
+ public void The_catch_all_is_the_first_mixed_profile_with_nothing_checked()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Mixed("scans", "jpg", "pdf"), Mixed("mixed"), Mixed("second")];
+
+ Assert.Equal("mixed", Pick(profiles, "a.jpg", "b.docx"));
+ Assert.Equal("mixed", ProfileSelector.CatchAll(profiles)!.Id);
+ }
+
+ [Fact]
+ public void Without_a_catch_all_the_first_mixed_profile_takes_the_rest()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Normal("office", "docx"), Mixed("scans", "jpg", "pdf"), Mixed("office-mix", "docx", "xlsx")];
+ Assert.Equal("scans", Pick(profiles, "a.jpg", "b.docx"));
+ }
+
+ [Fact]
+ public void Without_any_mixed_profile_the_first_profile_is_used()
+ {
+ List<ToolbarProfile> profiles = [Normal("images", "jpg"), Normal("office", "docx")];
+ Assert.Equal("images", Pick(profiles, "a.jpg", "b.docx"));
+ }
+
+ [Fact]
+ public void Nothing_to_pick_from_is_null() => Assert.Null(Pick([], "a.jpg"));
+}
diff --git a/tests/Filee.Core.Tests/SmallParsersTests.cs b/tests/Filee.Core.Tests/SmallParsersTests.cs
index 2c0ded75..4f1dfcca 100644
--- a/tests/Filee.Core.Tests/SmallParsersTests.cs
+++ b/tests/Filee.Core.Tests/SmallParsersTests.cs
@@ -1,7 +1,6 @@
using Filee.Core.Conversion;
using Filee.Core.Formats;
using Filee.Core.Presets;
-using Filee.Core.Profiles;
namespace Filee.Core.Tests;
@@ -40,23 +39,6 @@ public class FormatRegistryTests
FormatRegistry.Known.SelectMany(f => f.Extensions).Distinct(StringComparer.OrdinalIgnoreCase).Count());
}
-public class ProfileSelectorTests
-{
- private readonly List<ToolbarProfile> _profiles = BuiltInData.CreateProfiles();
-
- [Fact]
- public void Picks_profile_matching_all_files() =>
- Assert.Equal("images", ProfileSelector.Select(_profiles, ["a.jpg", "b.PNG"])!.Id);
-
- [Fact]
- public void Mixed_files_use_the_fallback() =>
- Assert.Equal("mixed", ProfileSelector.Select(_profiles, ["a.jpg", "b.docx"])!.Id);
-
- [Fact]
- public void Unknown_files_use_the_fallback() =>
- Assert.Equal("mixed", ProfileSelector.Select(_profiles, ["a.xyz"])!.Id);
-}
-
public class BuiltInDataTests
{
[Fact]
diff --git a/tests/Filee.Core.Tests/UserDataStoreTests.cs b/tests/Filee.Core.Tests/UserDataStoreTests.cs
index 8c8c81e1..bafed175 100644
--- a/tests/Filee.Core.Tests/UserDataStoreTests.cs
+++ b/tests/Filee.Core.Tests/UserDataStoreTests.cs
@@ -131,6 +131,63 @@ public class UserDataStoreTests
Assert.Equal(all.Count, all.Distinct(StringComparer.OrdinalIgnoreCase).Count());
}
+ [Fact]
+ public void Catalog_migration_ignores_extensions_checked_on_mixed_profiles()
+ {
+ // Schema-5 data where the user already limited a mixed-files donut to MP3 and JPG: those checks must not
+ // count as "taken" when the catalog donuts and extensions are added.
+ var presets = BuiltInData.CreatePresets().Where(p => BuiltInData.CatalogPresets().All(c => c.Id != p.Id)).ToList();
+ var catalogIds = BuiltInData.CatalogProfiles().Select(p => p.Id).ToHashSet();
+ var profiles = BuiltInData.CreateProfiles().Where(p => !catalogIds.Contains(p.Id)).ToList();
+ profiles.Single(p => p.Id == "images").Extensions = ["png"];
+ profiles.Add(new ToolbarProfile { Id = "music-mix", Name = "Music mix", IsFallback = true, Extensions = ["mp3", "jpg"] });
+
+ Assert.True(SettingsMigrations.ApplyToLibrary(5, presets, profiles));
+
+ Assert.Contains("mp3", profiles.Single(p => p.Id == "audio").Extensions);
+ Assert.Contains("jpg", profiles.Single(p => p.Id == "images").Extensions);
+ Assert.Equal(["mp3", "jpg"], profiles.Single(p => p.Id == "music-mix").Extensions);
+ }
+
+ [Fact]
+ public void Several_mixed_profiles_are_kept_and_one_is_added_when_none_is_left()
+ {
+ using var dir = new TempDir();
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+ store.Profiles.Add(new ToolbarProfile { Id = "scans", Name = "Scans", IsFallback = true, Extensions = ["jpg", "pdf"] });
+ store.SaveLibrary();
+
+ var reloaded = new UserDataStore(dir.Path);
+ reloaded.Load();
+ Assert.Equal(["mixed", "scans"], reloaded.Profiles.Where(p => p.IsFallback).Select(p => p.Id));
+
+ reloaded.Profiles.RemoveAll(p => p.IsFallback);
+ reloaded.SaveLibrary();
+ Assert.Equal("mixed", Assert.Single(reloaded.Profiles, p => p.IsFallback).Id);
+ }
+
+ [Fact]
+ public void Imported_mixed_profiles_stay_mixed()
+ {
+ using var dir = new TempDir();
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+ store.Profiles.Add(new ToolbarProfile { Id = "scans", Name = "Scans", IsFallback = true, Extensions = ["jpg", "pdf"] });
+ var bundle = Path.Combine(dir.Path, "bundle.json");
+ store.Export(bundle);
+
+ using var other = new TempDir();
+ var target = new UserDataStore(other.Path);
+ target.Load();
+ target.Import(bundle);
+
+ Assert.True(target.Profiles.Single(p => p.Id == "scans").IsFallback);
+ Assert.Single(target.Profiles, p => p.Id == "mixed");
+ Assert.Equal("scans", ProfileSelector.Select(target.Profiles, ["a.jpg", "b.pdf"])!.Id);
+ Assert.Equal("mixed", ProfileSelector.Select(target.Profiles, ["a.jpg", "b.docx"])!.Id);
+ }
+
[Fact]
public void Spreadsheet_profile_is_not_added_when_a_user_profile_takes_spreadsheets()
{