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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Toolbar: a preset whose judging fails stays listed (#304)
SeoJCalived93a14c17e87
diff --git a/src/Filee.App/ViewModels/Pages/ToolbarPageViewModel.cs b/src/Filee.App/ViewModels/Pages/ToolbarPageViewModel.csindex 84768b73..d5891756 100644--- a/src/Filee.App/ViewModels/Pages/ToolbarPageViewModel.cs+++ b/src/Filee.App/ViewModels/Pages/ToolbarPageViewModel.cs@@ -18,6 +18,7 @@ using Filee.Core.Localization;using Filee.Core.Presets;using Filee.Core.Profiles;using Filee.Core.Settings;+using Microsoft.Extensions.Logging;namespace Filee.App.ViewModels.Pages;@@ -53,6 +54,7 @@ public sealed partial class ToolbarPageViewModel : ObservableObject, IDisposableprivate readonly WindowService _windows;private readonly PresetAvailability _availability;private readonly ConverterCatalog _catalog;+ private readonly ILogger<ToolbarPageViewModel> _log;private PresetAvailability.PaletteSession? _session;private readonly Dictionary<string, PresetJudgement> _judged = [];private object? _pass;@@ -62,13 +64,14 @@ public sealed partial class ToolbarPageViewModel : ObservableObject, IDisposableprivate bool _repositioning;public ToolbarPageViewModel(UserDataStore store, ILocalizer loc, WindowService windows,- PresetAvailability availability, ConverterCatalog catalog)+ PresetAvailability availability, ConverterCatalog catalog, ILogger<ToolbarPageViewModel> log){_store = store;_loc = loc;_windows = windows;_availability = availability;_catalog = catalog;+ _log = log;// A profile is found by its name or by an extension it handles ("png" finds the photo donut).VisibleProfiles = new FilteredList<ProfileItemViewModel>(Profiles, p => [p.Name, .. p.Profile.Extensions]);VisiblePalette = new FilteredList<PaletteChip>(Palette, c => [c.Label, c.Caption]);@@ -518,41 +521,48 @@ public sealed partial class ToolbarPageViewModel : ObservableObject, IDisposable{if (pass != _pass || _disposed)return;- try+ var profile = Profile;+ if (profile is null){- var profile = Profile;- if (profile is null)- {- _pass = null;- Rebuild();- return;- }- var placed = profile.PresetIds.Concat(profile.BackPresetIds ?? []).ToHashSet();- var pending = _store.Presets.Where(p => !p.IsGroup && !placed.Contains(p.Id) && !_judged.ContainsKey(p.Id)).ToList();- if (pending.Count > 0)- {- _session ??= _availability.ForPalette(profile.Extensions.Select(FormatRegistry.FindByExtension)- .OfType<FileFormat>().DistinctBy(f => f.Id).ToList());- var clock = Stopwatch.StartNew();- foreach (var preset in pending)- {- _judged[preset.Id] = _session.Judge(preset);- if (clock.Elapsed >= JudgeSlice)- break;- }- }- if (_store.Presets.Any(p => !p.IsGroup && !placed.Contains(p.Id) && !_judged.ContainsKey(p.Id)))- Dispatcher.UIThread.Post(() => JudgeSome(pass), DispatcherPriority.Background);- else+ _pass = null;+ Rebuild();+ return;+ }+ var placed = profile.PresetIds.Concat(profile.BackPresetIds ?? []).ToHashSet();+ var pending = _store.Presets.Where(p => !p.IsGroup && !placed.Contains(p.Id) && !_judged.ContainsKey(p.Id)).ToList();+ if (pending.Count > 0)+ {+ _session ??= _availability.ForPalette(profile.Extensions.Select(FormatRegistry.FindByExtension)+ .OfType<FileFormat>().DistinctBy(f => f.Id).ToList());+ var clock = Stopwatch.StartNew();+ foreach (var preset in pending){- _pass = null;- Rebuild();+ _judged[preset.Id] = JudgeOne(preset);+ if (clock.Elapsed >= JudgeSlice)+ break;}}- catch+ if (_store.Presets.Any(p => !p.IsGroup && !placed.Contains(p.Id) && !_judged.ContainsKey(p.Id)))+ Dispatcher.UIThread.Post(() => JudgeSome(pass), DispatcherPriority.Background);+ else{_pass = null;- throw;+ Rebuild();+ }+ }++ // A preset whose judging throws stays usable (not dimmed) and the rest go on: this runs as a dispatcher job, where an+ // exception would end Filee.+ private PresetJudgement JudgeOne(Preset preset)+ {+ try+ {+ return _session!.Judge(preset);+ }+ catch (Exception ex) when (ex is not OutOfMemoryException)+ {+ _log.LogWarning(ex, "Judging preset {Preset} for the palette failed", preset.Id);+ return new PresetJudgement(PresetUse.Usable, null);}}diff --git a/tests/Filee.App.Tests/ToolbarEditorTests.cs b/tests/Filee.App.Tests/ToolbarEditorTests.csindex b047782f..08a3ada8 100644--- a/tests/Filee.App.Tests/ToolbarEditorTests.cs+++ b/tests/Filee.App.Tests/ToolbarEditorTests.cs@@ -24,6 +24,8 @@ using Filee.Core.Presets;using Filee.Core.Profiles;using Filee.Core.Settings;using Filee.Engines.Infrastructure;+using Microsoft.Extensions.Logging;+using Microsoft.Extensions.Logging.Abstractions;namespace Filee.App.Tests;@@ -199,7 +201,7 @@ public class ToolbarPageTeststry{using var vm = new ToolbarPageViewModel(store, AppHost.Get<Filee.Core.Localization.ILocalizer>(), AppHost.Get<WindowService>(),- AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>());+ AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>(), NullLogger<ToolbarPageViewModel>.Instance);// A new profile goes before the mixed ones and starts as a normal profile.vm.AddProfileCommand.Execute(null);@@ -248,7 +250,7 @@ public class ToolbarPageTeststry{using var vm = new ToolbarPageViewModel(store, AppHost.Get<Filee.Core.Localization.ILocalizer>(), AppHost.Get<WindowService>(),- AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>());+ AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>(), NullLogger<ToolbarPageViewModel>.Instance);vm.SelectedProfile = vm.Profiles.Single(p => p.Profile.Id == "images");vm.PaletteSearch = "png"; // a search that would hide itvar pdf = vm.Profiles.Single(p => p.Profile.Id == "pdf");@@ -291,7 +293,7 @@ public class ToolbarPageTeststry{using var vm = new ToolbarPageViewModel(store, AppHost.Get<Filee.Core.Localization.ILocalizer>(), AppHost.Get<WindowService>(),- AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>());+ AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>(), NullLogger<ToolbarPageViewModel>.Instance);var item = vm.Profiles.Single(p => p.Profile.Id == "images");vm.SelectedProfile = item;Assert.True(vm.CanReset);@@ -338,7 +340,7 @@ public class ToolbarPageTestsTestServices.EnsureInitialized("en");var store = AppHost.Get<UserDataStore>();using var vm = new ToolbarPageViewModel(store, AppHost.Get<Filee.Core.Localization.ILocalizer>(), AppHost.Get<WindowService>(),- AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>());+ AppHost.Get<PresetAvailability>(), AppHost.Get<ConverterCatalog>(), NullLogger<ToolbarPageViewModel>.Instance);vm.SelectedProfile = vm.Profiles.Single(p => p.Profile.Id == "mixed");Assert.True(vm.IsMixed);@@ -380,6 +382,30 @@ public class PresetAvailabilityTestspublic bool Transforms(string from, string to) => true;}+ private sealed class ThrowingCombiner() : FakeConverter("test-throwing-combiner", Array.Empty<ConversionEdge>()), IFileCombiner+ {+ public bool CanCombineInto(string format) => throw new InvalidOperationException("broken combiner");++ public Task CombineAsync(IReadOnlyList<string> inputPaths, string outputPath, string format, Preset preset,+ string workDirectory, IProgress<double>? progress, CancellationToken cancellationToken) =>+ throw new NotSupportedException();+ }++ private sealed class PaletteLogger : ILogger<ToolbarPageViewModel>+ {+ public readonly List<(LogLevel Level, Exception? Exception, string Message)> Warnings = [];++ public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;+ public bool IsEnabled(LogLevel logLevel) => true;++ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,+ Func<TState, Exception?, string> formatter)+ {+ if (logLevel == LogLevel.Warning)+ Warnings.Add((logLevel, exception, formatter(state, exception)));+ }+ }+private sealed class Fixture : IDisposable{private readonly UserDataStore _store;@@ -394,14 +420,17 @@ public class PresetAvailabilityTestspublic readonly ILocalizer Loc;public readonly List<EnginePackageState> Packages;- public Fixture()+ public Fixture(IConverter? extra = null){TestServices.EnsureInitialized("en");_store = AppHost.Get<UserDataStore>();_presets = _store.Presets.ToList();_profiles = _store.Profiles.Select(p => p.Clone()).ToList();Loc = AppHost.Get<ILocalizer>();- Catalog = new ConverterCatalog([Built, Gated, Motion, Turner]);+ var converters = new List<IConverter> { Built, Gated, Motion, Turner };+ if (extra is not null)+ converters.Add(extra);+ Catalog = new ConverterCatalog(converters);Packages = EngineDownloads.Packages.Where(p => p.Id is "pandoc" or "ffmpeg").Select(p => new EnginePackageState(p, Loc) { Status = EnginePackageStatus.NotInstalled }).ToList();Availability = new PresetAvailability(Catalog, Loc, Packages);@@ -420,10 +449,11 @@ public class PresetAvailabilityTestspublic Preset Preset(string id) => _store.FindPreset(id)!;public UserDataStore Store => _store;- public ToolbarPageViewModel Page(ToolbarProfile profile)+ public ToolbarPageViewModel Page(ToolbarProfile profile, ILogger<ToolbarPageViewModel>? log = null){_store.Profiles.Add(profile);- var vm = new ToolbarPageViewModel(_store, Loc, AppHost.Get<WindowService>(), Availability, Catalog);+ var vm = new ToolbarPageViewModel(_store, Loc, AppHost.Get<WindowService>(), Availability, Catalog,+ log ?? NullLogger<ToolbarPageViewModel>.Instance);vm.SelectedProfile = vm.Profiles.Single(p => ReferenceEquals(p.Profile, profile));return vm;}@@ -493,6 +523,40 @@ public class PresetAvailabilityTestsf.Availability.ForPalette([Fixture.Format("mp4")]).Judge(f.Preset("tool-crop")));}+ [AvaloniaFact]+ public void A_preset_whose_judging_throws_shows_undimmed_and_the_rest_are_judged()+ {+ using var f = new Fixture(new ThrowingCombiner());+ f.Store.Presets.Add(new() { Id = "test-jpg-after", TargetFormat = "jpg" });+ f.Store.Presets.Add(new() { Id = "test-mp4-after", TargetFormat = "mp4" });+ var log = new PaletteLogger();+ using var vm = f.Page(new ToolbarProfile+ {+ Id = "test-palette",+ Extensions = ["png", "mp4"],+ PresetIds = ["zip-all"],+ BackPresetIds = [],+ }, log);++ Dispatcher.UIThread.RunJobs();++ Assert.Null(vm.Palette.Single(c => c.PresetId == "test-pack").Reason);+ Assert.Null(vm.Palette.Single(c => c.PresetId == "test-jpg-after").Reason);+ // Judged after the failure, not just shown: it still needs FFmpeg.+ var after = f.Availability.Check(f.Preset("test-mp4-after"), [Fixture.Format("png")]);+ Assert.NotNull(after);+ Assert.Equal(after, vm.Palette.Single(c => c.PresetId == "test-mp4-after").Reason);+ Assert.Null(vm.Palette.Single(c => c.PresetId == "test-jpg").Reason);+ Assert.Equal(f.Availability.Check(f.Preset("test-mp4"), [Fixture.Format("png")]),+ vm.Palette.Single(c => c.PresetId == "test-mp4").Reason);+ Assert.DoesNotContain(vm.Palette, c => c.PresetId == "test-wav");+ var warning = Assert.Single(log.Warnings);+ Assert.Equal(LogLevel.Warning, warning.Level);+ Assert.IsType<InvalidOperationException>(warning.Exception);+ Assert.Contains("test-pack", warning.Message);+ Assert.DoesNotContain(vm.Palette, c => c.PresetId == "zip-all");+ }+[AvaloniaFact]public void Check_builds_no_planner_when_it_answers_early(){