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.

Window zoom: Ctrl + wheel, Ctrl + plus / minus and Ctrl + 0 make every window larger or smaller, remembered

flexeykinDev5a37a82b4086
diff --git a/src/Filee.App/App.axaml.cs b/src/Filee.App/App.axaml.cs
index 618e0d55..d6d79a73 100644
--- a/src/Filee.App/App.axaml.cs
+++ b/src/Filee.App/App.axaml.cs
@@ -257,6 +257,20 @@ public partial class App : Application
var custom = CustomTheme.Load(AppHost.Get<UserDataStore>().Directory, AppHost.Get<ILogger<App>>());
AppHost.Get<ThemeService>().Apply(settings.Theme, loc.Language, custom);
+ // Ctrl + wheel / Ctrl +/- / Ctrl+0: everything larger or smaller, remembered.
+ UiZoom.Configure(settings.UiScale, scale =>
+ {
+ var store = AppHost.Get<UserDataStore>();
+ store.Settings.UiScale = scale;
+ try
+ {
+ store.SaveSettings();
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ AppHost.Get<ILogger<App>>().LogWarning(ex, "Could not remember the window zoom");
+ }
+ });
AppHost.Get<TriggerService>().Apply(settings);
AppHost.Get<ConverterCatalog>().Priority = settings.EnginePriority;
if (ownCopiesChanged || routesChanged)
diff --git a/src/Filee.App/Services/UiZoom.cs b/src/Filee.App/Services/UiZoom.cs
new file mode 100644
index 00000000..20c7eb72
--- /dev/null
+++ b/src/Filee.App/Services/UiZoom.cs
@@ -0,0 +1,100 @@
+// Everything in Filee's windows larger or smaller, as a browser zooms a page: Ctrl + mouse wheel, Ctrl + plus / minus,
+// Ctrl + 0 back to normal. Each tracked window's content is scaled by a layout transform (so layouts that adapt to their
+// width see the room they really have), every window at once, and the size is remembered.
+
+using Avalonia.Controls;
+using Avalonia.Input;
+using Avalonia.Interactivity;
+using Avalonia.Media;
+
+namespace Filee.App.Services;
+
+public static class UiZoom
+{
+ /// <summary>The smallest and largest size, and one step.</summary>
+ public const double Min = 0.7, Max = 1.8, Step = 0.1;
+
+ private static readonly List<WeakReference<Window>> Windows = [];
+ private static Action<double>? _save;
+
+ /// <summary>How large everything is drawn: 1 as designed.</summary>
+ public static double Scale { get; private set; } = 1;
+
+ /// <summary>The size remembered and how to remember a new one.</summary>
+ public static void Configure(double scale, Action<double> save)
+ {
+ _save = save;
+ Set(scale, remember: false);
+ }
+
+ /// <summary>Scales <paramref name="window"/>'s content and gives it the zoom keys and wheel.</summary>
+ public static void Track(Window window)
+ {
+ lock (Windows)
+ {
+ Windows.RemoveAll(w => !w.TryGetTarget(out _));
+ Windows.Add(new WeakReference<Window>(window));
+ }
+ window.AddHandler(InputElement.PointerWheelChangedEvent, OnWheel, RoutingStrategies.Tunnel);
+ window.AddHandler(InputElement.KeyDownEvent, OnKey, RoutingStrategies.Tunnel);
+ Apply(window);
+ }
+
+ /// <summary>Sets the size (kept between <see cref="Min"/> and <see cref="Max"/>) in every window.</summary>
+ public static void Set(double scale, bool remember = true)
+ {
+ var value = Math.Round(Math.Clamp(double.IsFinite(scale) ? scale : 1, Min, Max), 2);
+ var changed = Math.Abs(value - Scale) > 1e-9;
+ Scale = value;
+ List<Window> windows;
+ lock (Windows)
+ windows = Windows.Select(w => w.TryGetTarget(out var window) ? window : null).OfType<Window>().ToList();
+ foreach (var window in windows)
+ Apply(window);
+ if (changed && remember)
+ _save?.Invoke(value);
+ }
+
+ private static void Apply(Window window)
+ {
+ if (window.Content is not Control content)
+ return;
+ if (content is not LayoutTransformControl { Tag: "ui-zoom" } scaled)
+ {
+ window.Content = null;
+ scaled = new LayoutTransformControl { Tag = "ui-zoom", Child = content };
+ window.Content = scaled;
+ }
+ scaled.LayoutTransform = Math.Abs(Scale - 1) < 1e-9 ? null : new ScaleTransform(Scale, Scale);
+ }
+
+ private static void OnWheel(object? sender, PointerWheelEventArgs e)
+ {
+ // Ctrl, or Cmd on a Mac.
+ if (!(e.KeyModifiers.HasFlag(KeyModifiers.Control) || e.KeyModifiers.HasFlag(KeyModifiers.Meta)) || e.Delta.Y == 0)
+ return;
+ Set(Scale + Math.Sign(e.Delta.Y) * Step);
+ e.Handled = true;
+ }
+
+ private static void OnKey(object? sender, KeyEventArgs e)
+ {
+ if (e.KeyModifiers is not (KeyModifiers.Control or KeyModifiers.Meta))
+ return;
+ switch (e.Key)
+ {
+ case Key.OemPlus or Key.Add:
+ Set(Scale + Step);
+ e.Handled = true;
+ break;
+ case Key.OemMinus or Key.Subtract:
+ Set(Scale - Step);
+ e.Handled = true;
+ break;
+ case Key.D0 or Key.NumPad0:
+ Set(1);
+ e.Handled = true;
+ break;
+ }
+ }
+}
diff --git a/src/Filee.App/Views/EngineSetupWindow.axaml.cs b/src/Filee.App/Views/EngineSetupWindow.axaml.cs
index 23f5e020..eb43d323 100644
--- a/src/Filee.App/Views/EngineSetupWindow.axaml.cs
+++ b/src/Filee.App/Views/EngineSetupWindow.axaml.cs
@@ -10,6 +10,7 @@ public partial class EngineSetupWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
DataContextChanged += (_, _) =>
{
if (DataContext is EngineSetupViewModel vm)
diff --git a/src/Filee.App/Views/GroupEditorWindow.axaml.cs b/src/Filee.App/Views/GroupEditorWindow.axaml.cs
index 5b981e4b..b6e28279 100644
--- a/src/Filee.App/Views/GroupEditorWindow.axaml.cs
+++ b/src/Filee.App/Views/GroupEditorWindow.axaml.cs
@@ -13,6 +13,7 @@ public partial class GroupEditorWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
Opened += (_, _) => NameBox.Focus();
// Before the ComboBox, which would take Enter to open its list again.
AddBox.AddHandler(KeyDownEvent, OnAddKeyDown, RoutingStrategies.Tunnel);
diff --git a/src/Filee.App/Views/LibreOfficeOptionsWindow.axaml.cs b/src/Filee.App/Views/LibreOfficeOptionsWindow.axaml.cs
index 37ed5140..8411f5e5 100644
--- a/src/Filee.App/Views/LibreOfficeOptionsWindow.axaml.cs
+++ b/src/Filee.App/Views/LibreOfficeOptionsWindow.axaml.cs
@@ -11,6 +11,7 @@ public partial class LibreOfficeOptionsWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
}
private void OnClose(object? sender, RoutedEventArgs e) => Close();
diff --git a/src/Filee.App/Views/MainWindow.axaml.cs b/src/Filee.App/Views/MainWindow.axaml.cs
index f5d743a5..e0ecab17 100644
--- a/src/Filee.App/Views/MainWindow.axaml.cs
+++ b/src/Filee.App/Views/MainWindow.axaml.cs
@@ -20,6 +20,7 @@ public partial class MainWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
}
private async void OnOpenGitHub(object? sender, RoutedEventArgs e)
diff --git a/src/Filee.App/Views/MergeOrderWindow.axaml.cs b/src/Filee.App/Views/MergeOrderWindow.axaml.cs
index 46aa011e..430c21c4 100644
--- a/src/Filee.App/Views/MergeOrderWindow.axaml.cs
+++ b/src/Filee.App/Views/MergeOrderWindow.axaml.cs
@@ -11,6 +11,7 @@ public partial class MergeOrderWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
}
public IReadOnlyList<string>? Result { get; private set; }
diff --git a/src/Filee.App/Views/PresetEditorWindow.axaml.cs b/src/Filee.App/Views/PresetEditorWindow.axaml.cs
index cc0d0b38..92f93dcd 100644
--- a/src/Filee.App/Views/PresetEditorWindow.axaml.cs
+++ b/src/Filee.App/Views/PresetEditorWindow.axaml.cs
@@ -12,6 +12,7 @@ public partial class PresetEditorWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
}
/// <summary>True after the user saved (also available when the window was not shown as a dialog).</summary>
diff --git a/src/Filee.App/Views/RenameToolWindow.axaml.cs b/src/Filee.App/Views/RenameToolWindow.axaml.cs
index 05c123cc..fe5a8567 100644
--- a/src/Filee.App/Views/RenameToolWindow.axaml.cs
+++ b/src/Filee.App/Views/RenameToolWindow.axaml.cs
@@ -13,6 +13,7 @@ public partial class RenameToolWindow : Window
{
InitializeComponent();
Motion.Track(this);
+ UiZoom.Track(this);
}
/// <summary>Double-clicking a ready-made replacement adds it as a step, like its + button.</summary>
diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs
index 4f488dcb..b51a9747 100644
--- a/src/Filee.Core/Settings/AppSettings.cs
+++ b/src/Filee.Core/Settings/AppSettings.cs
@@ -241,6 +241,9 @@ public sealed class AppSettings
/// <summary>Every geospatial conversion is saved as a ZIP, whatever the preset says (see <see cref="OutputRule.ZipResult"/>).</summary>
public bool ZipGeoResults { get; set; }
+ /// <summary>How large Filee's windows draw everything (Ctrl + mouse wheel, Ctrl +/-, Ctrl+0): 1 as designed.</summary>
+ public double UiScale { get; set; } = 1;
+
/// <summary>
/// Returns <paramref name="preset"/> itself, or a copy with the app-wide output settings applied: it saves to
/// <see cref="DefaultOutput"/> when the preset's location is <see cref="OutputLocation.Default"/>, and keeps the
diff --git a/tests/Filee.App.Tests/UiZoomTests.cs b/tests/Filee.App.Tests/UiZoomTests.cs
new file mode 100644
index 00000000..aab6b741
--- /dev/null
+++ b/tests/Filee.App.Tests/UiZoomTests.cs
@@ -0,0 +1,43 @@
+// Ctrl + wheel, Ctrl + plus / minus and Ctrl + 0 zoom every window.
+
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Headless;
+using Avalonia.Headless.XUnit;
+using Avalonia.Input;
+using Avalonia.Media;
+using Filee.App.Services;
+
+namespace Filee.App.Tests;
+
+public class UiZoomTests
+{
+ [AvaloniaFact]
+ public void Ctrl_and_the_wheel_or_plus_minus_zoom_every_window()
+ {
+ TestServices.EnsureInitialized("en");
+ var window = new Window { Content = new Border { Width = 100, Height = 100 }, Width = 300, Height = 300 };
+ UiZoom.Track(window);
+ window.Show();
+ try
+ {
+ UiZoom.Set(1, remember: false);
+ var scaled = Assert.IsType<LayoutTransformControl>(window.Content);
+ window.KeyPress(Key.OemPlus, RawInputModifiers.Control, PhysicalKey.None, null);
+ Assert.Equal(1.1, UiZoom.Scale, 6);
+ Assert.Equal(1.1, Assert.IsType<ScaleTransform>(scaled.LayoutTransform).ScaleX, 6);
+ window.MouseWheel(new Point(50, 50), new Vector(0, 1), RawInputModifiers.Control);
+ Assert.Equal(1.2, UiZoom.Scale, 6);
+ window.KeyPress(Key.D0, RawInputModifiers.Control, PhysicalKey.None, null);
+ Assert.Equal(1, UiZoom.Scale, 6);
+ Assert.Null(scaled.LayoutTransform);
+ UiZoom.Set(99, remember: false);
+ Assert.Equal(UiZoom.Max, UiZoom.Scale);
+ }
+ finally
+ {
+ UiZoom.Set(1, remember: false);
+ window.Close();
+ }
+ }
+}