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.

feat(app): add unit selector for video size limit in preset editor

Gian Abi Firdausaa64100956c0
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
index 916946e8..89ed3021 100644
--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs
+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
@@ -29,6 +29,13 @@ public enum FileSizeUnit
MB,
}
+/// <summary>The unit of a video's size limit in the preset editor (#680); the preset itself keeps megabytes or a percentage.</summary>
+public enum VideoSizeUnit
+{
+ MB,
+ Percent,
+}
+
/// <summary>A value with a localized label for combo boxes.</summary>
public sealed record Choice<T>(T Value, string Label, string? Detail = null) : IChoice
{
@@ -197,7 +204,9 @@ public sealed partial class PresetEditorViewModel : ObservableObject
var perMegabyte = _imageSizeUnit.Value == FileSizeUnit.KB ? 1000m : 1m;
_imageMaxSize = (decimal)_edit.Image.MaxSizeMb * perMegabyte;
_imageMinSize = (decimal)_edit.Image.MinSizeMb * perMegabyte;
- _videoMaxSizeMb = (decimal)_edit.Media.MaxSizeMb;
+ _videoSizeUnitChoice = _edit.Media.MaxSizePercent > 0 ? VideoSizeUnits[1] : VideoSizeUnits[0];
+ _videoMaxSizeMb = _edit.Media.MaxSizeMb > 0 ? (decimal)_edit.Media.MaxSizeMb : null;
+ _videoMaxSizePercent = _edit.Media.MaxSizePercent > 0 ? _edit.Media.MaxSizePercent : null;
_resizePercent = _edit.Image.ResizePercent;
_longEdge = _edit.Image.LongEdge;
_width = _edit.Image.Width;
@@ -463,8 +472,36 @@ public sealed partial class PresetEditorViewModel : ObservableObject
/// <summary>A minimum over the maximum, while the boxes are shown; a maximum of 0 is no maximum.</summary>
public string? SizeRangeError => ShowImageSize && ImageMaxSize > 0 && ImageMinSize > ImageMaxSize ? _loc["presets.size_range_invalid"] : null;
- /// <summary>"Size limit (MB)" for a video (0 = none, #553).</summary>
- [ObservableProperty] private decimal? _videoMaxSizeMb;
+ /// <summary>The unit of the video size limit: MB or % of original (#680).</summary>
+ public IReadOnlyList<Choice<VideoSizeUnit>> VideoSizeUnits { get; } =
+ [new(VideoSizeUnit.MB, "MB"), new(VideoSizeUnit.Percent, "%")];
+
+ [ObservableProperty] private Choice<VideoSizeUnit> _videoSizeUnitChoice;
+
+ /// <summary>Video size limit in MB (0 or empty = none, #553).</summary>
+ [ObservableProperty, NotifyPropertyChangedFor(nameof(VideoMaxSize))] private decimal? _videoMaxSizeMb;
+
+ /// <summary>Video size limit as percentage of original (1–99, 0 or empty = none, #680).</summary>
+ [ObservableProperty, NotifyPropertyChangedFor(nameof(VideoMaxSize))] private decimal? _videoMaxSizePercent;
+
+ /// <summary>The value bound to the video size NumericUpDown.</summary>
+ public decimal? VideoMaxSize
+ {
+ get => VideoSizeUnitChoice.Value == VideoSizeUnit.Percent ? VideoMaxSizePercent : VideoMaxSizeMb;
+ set
+ {
+ if (VideoSizeUnitChoice.Value == VideoSizeUnit.Percent)
+ VideoMaxSizePercent = value;
+ else
+ VideoMaxSizeMb = value;
+ OnPropertyChanged();
+ }
+ }
+
+ /// <summary>Maximum value for the video size NumericUpDown: 99 for %, 4096 for MB.</summary>
+ public decimal VideoSizeMaximum => VideoSizeUnitChoice.Value == VideoSizeUnit.Percent ? 99 : 4096;
+ public decimal VideoSizeIncrement => 1;
+ public string VideoSizeFormat => VideoSizeUnitChoice.Value == VideoSizeUnit.Percent ? "0" : "0.#";
[ObservableProperty] private int _resizePercent;
[ObservableProperty] private int _longEdge;
[ObservableProperty] private int _width;
@@ -609,6 +646,16 @@ public sealed partial class PresetEditorViewModel : ObservableObject
// A combo box that loses its selection sets null: every choice keeps its value (see Choices.Restore).
partial void OnTargetChanged(Choice<string>? oldValue, Choice<string> newValue) => Choices.Restore(oldValue, newValue, v => Target = v);
+ partial void OnVideoSizeUnitChoiceChanged(Choice<VideoSizeUnit>? oldValue, Choice<VideoSizeUnit> newValue)
+ {
+ if (Choices.Restore(oldValue, newValue, v => VideoSizeUnitChoice = v) || oldValue is null || oldValue.Value == newValue.Value)
+ return;
+ OnPropertyChanged(nameof(VideoSizeMaximum));
+ OnPropertyChanged(nameof(VideoSizeIncrement));
+ OnPropertyChanged(nameof(VideoSizeFormat));
+ OnPropertyChanged(nameof(VideoMaxSize));
+ }
+
/// <summary>
/// Both ends keep their size in the other unit: 500 KB becomes 0.5 MB. The boxes' maximum is raised before the
/// values grow and lowered after they shrink, so a box never cuts a value to the other unit's maximum.
@@ -880,7 +927,24 @@ public sealed partial class PresetEditorViewModel : ObservableObject
var media = _original.Media;
media.Quality = MediaQuality.Value;
media.MaxHeight = MaxHeight.Value;
- media.MaxSizeMb = ShowVideoMaxSize ? SizeLimit(VideoMaxSizeMb) : 0;
+ if (ShowVideoMaxSize)
+ {
+ if (VideoSizeUnitChoice.Value == VideoSizeUnit.Percent)
+ {
+ media.MaxSizeMb = 0;
+ media.MaxSizePercent = VideoMaxSizePercent is > 0 and < 100 ? (int)Math.Clamp(VideoMaxSizePercent.Value, 1, 99) : 0;
+ }
+ else
+ {
+ media.MaxSizePercent = 0;
+ media.MaxSizeMb = SizeLimit(VideoMaxSizeMb);
+ }
+ }
+ else
+ {
+ media.MaxSizeMb = 0;
+ media.MaxSizePercent = 0;
+ }
media.AudioBitrateKbps = AudioBitrate.Value;
media.RemoveAudio = RemoveAudio;
media.AudioChannels = AudioChannels.Value;
diff --git a/src/Filee.App/Views/PresetEditorView.axaml b/src/Filee.App/Views/PresetEditorView.axaml
index b462ac2b..97099fe2 100644
--- a/src/Filee.App/Views/PresetEditorView.axaml
+++ b/src/Filee.App/Views/PresetEditorView.axaml
@@ -269,8 +269,12 @@
<UniformGrid Columns="2" ColumnSpacing="12" RowSpacing="10">
<StackPanel IsVisible="{Binding ShowVideoMaxSize}">
<TextBlock Classes="label" Text="{DynamicResource presets.max_size}" />
- <NumericUpDown Value="{Binding VideoMaxSizeMb}" Minimum="0" Maximum="4096" Increment="1" FormatString="0.#"
- ToolTip.Tip="{DynamicResource presets.max_size_hint_video}" AutomationProperties.Name="{DynamicResource presets.max_size}" />
+ <Grid ColumnDefinitions="*,8,Auto">
+ <NumericUpDown Value="{Binding VideoMaxSize}" Minimum="0" Maximum="{Binding VideoSizeMaximum}" Increment="{Binding VideoSizeIncrement}" FormatString="{Binding VideoSizeFormat}"
+ ToolTip.Tip="{DynamicResource presets.max_size_hint_video}" AutomationProperties.Name="{DynamicResource presets.max_size}" />
+ <ComboBox Grid.Column="2" ItemsSource="{Binding VideoSizeUnits}" SelectedItem="{Binding VideoSizeUnitChoice}"
+ AutomationProperties.Name="{DynamicResource presets.size_unit}" />
+ </Grid>
</StackPanel>
<StackPanel IsVisible="{Binding ShowVideoCodec}">
<TextBlock Classes="label" Text="{DynamicResource presets.video_codec}" />
diff --git a/tests/Filee.App.Tests/PresetSizeRangeTests.cs b/tests/Filee.App.Tests/PresetSizeRangeTests.cs
index 99599539..4050440c 100644
--- a/tests/Filee.App.Tests/PresetSizeRangeTests.cs
+++ b/tests/Filee.App.Tests/PresetSizeRangeTests.cs
@@ -231,4 +231,58 @@ public class PresetSizeRangeTests
Assert.True(job.HasNotices);
Assert.Equal($"photo.jpg — {loc["notice.under_min_size"]}", job.Notices);
}
+
+ [AvaloniaFact]
+ public void Video_size_unit_switch_keeps_each_value_separately()
+ {
+ TestServices.EnsureInitialized("en");
+ var preset = new Preset { Name = "Video Preset", TargetFormat = "mp4", Media = { MaxSizeMb = 25 } };
+ InEditor(preset, (editor, window) =>
+ {
+ Assert.Equal(VideoSizeUnit.MB, editor.VideoSizeUnitChoice.Value);
+ Assert.Equal(25m, editor.VideoMaxSize);
+
+ // Switch to percent: value should not turn into 25%, but be null or previous percent
+ editor.VideoSizeUnitChoice = editor.VideoSizeUnits.Single(u => u.Value == VideoSizeUnit.Percent);
+ Pump();
+ Assert.Null(editor.VideoMaxSize);
+ Assert.Equal(99m, editor.VideoSizeMaximum);
+
+ // Set percent
+ editor.VideoMaxSize = 50;
+ Pump();
+ Assert.Equal(50m, editor.VideoMaxSize);
+
+ // Switch back to MB: original MB value is preserved
+ editor.VideoSizeUnitChoice = editor.VideoSizeUnits.Single(u => u.Value == VideoSizeUnit.MB);
+ Pump();
+ Assert.Equal(25m, editor.VideoMaxSize);
+ Assert.Equal(4096m, editor.VideoSizeMaximum);
+
+ // Switch back to percent: percent value is preserved
+ editor.VideoSizeUnitChoice = editor.VideoSizeUnits.Single(u => u.Value == VideoSizeUnit.Percent);
+ Pump();
+ Assert.Equal(50m, editor.VideoMaxSize);
+
+ // Apply: percent is saved, MB is zero
+ Assert.True(editor.Apply());
+ Assert.Equal(50, preset.Media.MaxSizePercent);
+ Assert.Equal(0, preset.Media.MaxSizeMb);
+ });
+ }
+
+ [AvaloniaFact]
+ public void Video_percentage_limit_applies_correctly()
+ {
+ TestServices.EnsureInitialized("en");
+ var preset = new Preset { Name = "Video Preset", TargetFormat = "mp4" };
+ InEditor(preset, (editor, window) =>
+ {
+ editor.VideoSizeUnitChoice = editor.VideoSizeUnits.Single(u => u.Value == VideoSizeUnit.Percent);
+ editor.VideoMaxSize = 75;
+ Assert.True(editor.Apply());
+ Assert.Equal(75, preset.Media.MaxSizePercent);
+ Assert.Equal(0, preset.Media.MaxSizeMb);
+ });
+ }
}