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.

Main window: open where it was after a restart, always fitting its screen (#586)

cre8ive-marketing7a7cf8feecc2
diff --git a/src/Filee.App/Services/WindowService.cs b/src/Filee.App/Services/WindowService.cs
index 90a21e0f..63de248f 100644
--- a/src/Filee.App/Services/WindowService.cs
+++ b/src/Filee.App/Services/WindowService.cs
@@ -294,17 +294,19 @@ public sealed class WindowService(ILocalizer loc, IServiceProvider services, Mem
var model = ActivatorUtilities.CreateInstance<MainWindowViewModel>(services);
var main = new MainWindow { DataContext = model };
PrepareWindow(main);
- if (_mainPlacement is { } placement)
- {
- placement.ApplyTo(main);
- if (page is null)
- model.Navigate(placement.Page);
- }
+ var store = services.GetService<UserDataStore>();
+ // Where it was last time: this run's, else the one settings.json kept from the last run (#586).
+ var placement = _mainPlacement ?? MainWindowPlacement.From(store?.Settings.MainWindow);
+ placement.ApplyTo(main);
+ if (page is null && placement.Page.Length > 0)
+ model.Navigate(placement.Page);
main.Closing += (_, _) =>
{
// While it is still on screen: the release closes it hidden, when its position may no longer be known.
- if (main.IsVisible)
- _mainPlacement = MainWindowPlacement.Of(main, model, _mainPlacement);
+ if (!main.IsVisible)
+ return;
+ _mainPlacement = MainWindowPlacement.Of(main, model, _mainPlacement);
+ store?.SaveMainWindow(_mainPlacement.ToBounds());
};
main.Closed += (_, _) =>
{
@@ -669,9 +671,18 @@ public sealed class WindowService(ILocalizer loc, IServiceProvider services, Mem
private static Window? MainWindowOrNull() =>
(Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
- /// <summary>Where the main window was and which page it showed, for the next one.</summary>
- private sealed record MainWindowPlacement(PixelPoint? Position, Size? Size, bool Maximized, string Page)
+ /// <summary>
+ /// Where the main window was and which page it showed, for the next one. The page stays within this run; the rest is
+ /// also kept in settings.json for the next start (<see cref="AppSettings.MainWindow"/>).
+ /// </summary>
+ internal sealed record MainWindowPlacement(PixelPoint? Position, Size? Size, bool Maximized, string Page)
{
+ /// <summary>Room left between the window and the edges of its screen's working area.</summary>
+ internal const double Margin = 8;
+
+ /// <summary>The title bar and borders around the content, which the size (the content's) doesn't count.</summary>
+ internal const double Frame = 40;
+
/// <summary>The window's placement. Maximized or minimized, it keeps the position and size it had before.</summary>
public static MainWindowPlacement Of(MainWindow window, MainWindowViewModel vm, MainWindowPlacement? last) =>
window.WindowState switch
@@ -681,21 +692,74 @@ public sealed class WindowService(ILocalizer loc, IServiceProvider services, Mem
_ => (last ?? new(null, null, false, "")) with { Page = vm.SelectedItem.Key },
};
+ /// <summary>The placement settings.json kept, without a page; nothing (the designed size) when it kept none.</summary>
+ public static MainWindowPlacement From(WindowBounds? bounds)
+ {
+ if (bounds is null)
+ return new(null, null, false, "");
+ PixelPoint? position = bounds is { X: { } x, Y: { } y } ? new PixelPoint(x, y) : null;
+ // A size settings.json can't have come from (hand-edited, damaged) is left out.
+ Size? size = bounds is { Width: { } w, Height: { } h } && double.IsFinite(w) && double.IsFinite(h) && w >= 100 && h >= 100
+ ? new Size(w, h)
+ : null;
+ return new(position, size, bounds.Maximized, "");
+ }
+
+ public WindowBounds ToBounds() => new()
+ {
+ X = Position?.X,
+ Y = Position?.Y,
+ Width = Size?.Width,
+ Height = Size?.Height,
+ Maximized = Maximized,
+ };
+
public void ApplyTo(MainWindow window)
{
- // Only on a screen that is still there (a monitor may have been unplugged since).
- if (Position is { } position && window.Screens.ScreenFromPoint(position) is not null)
+ // On a screen that is still there (a monitor may have been unplugged since), else the primary one, where the
+ // window then opens centred.
+ var screen = Position is { } at ? window.Screens.ScreenFromPoint(at) : null;
+ var place = screen is null ? null : Position;
+ screen ??= window.Screens.Primary;
+ var size = Size ?? new Size(window.Width, window.Height);
+ if (screen is { Scaling: > 0 })
{
- window.WindowStartupLocation = WindowStartupLocation.Manual;
- window.Position = position;
+ var fitted = Fit(screen.WorkingArea, screen.Scaling, place, size, new Size(window.MinWidth, window.MinHeight));
+ window.MinWidth = fitted.Min.Width;
+ window.MinHeight = fitted.Min.Height;
+ size = fitted.Size;
+ place = fitted.Position;
}
- if (Size is { } size)
+ if (place is { } position)
{
- window.Width = size.Width;
- window.Height = size.Height;
+ window.WindowStartupLocation = WindowStartupLocation.Manual;
+ window.Position = position;
}
+ window.Width = size.Width;
+ window.Height = size.Height;
if (Maximized)
window.WindowState = WindowState.Maximized;
}
+
+ /// <summary>
+ /// The main window on a screen whose working area is <paramref name="area"/> (pixels, at <paramref name="scaling"/>):
+ /// <paramref name="size"/> made to fit the area less <see cref="Margin"/> and <see cref="Frame"/>, the
+ /// <paramref name="min"/>imum lowered where the area is smaller than it, and <paramref name="position"/> moved
+ /// back inside the area where the window would stick out (null stays null: the window opens centred).
+ /// </summary>
+ internal static (Size Size, Size Min, PixelPoint? Position) Fit(PixelRect area, double scaling, PixelPoint? position, Size size, Size min)
+ {
+ var room = new Size(Math.Max(1, area.Width / scaling - 2 * Margin), Math.Max(1, area.Height / scaling - 2 * Margin - Frame));
+ var fitted = new Size(Math.Min(size.Width, room.Width), Math.Min(size.Height, room.Height));
+ var fittedMin = new Size(Math.Min(min.Width, fitted.Width), Math.Min(min.Height, fitted.Height));
+ if (position is not { } at)
+ return (fitted, fittedMin, null);
+ // The whole window in pixels, frame included, and the corner that keeps it inside the area.
+ var width = (int)Math.Ceiling(fitted.Width * scaling);
+ var height = (int)Math.Ceiling((fitted.Height + Frame) * scaling);
+ var x = Math.Clamp(at.X, area.X, Math.Max(area.X, area.Right - width));
+ var y = Math.Clamp(at.Y, area.Y, Math.Max(area.Y, area.Bottom - height));
+ return (fitted, fittedMin, new PixelPoint(x, y));
+ }
}
}
diff --git a/src/Filee.Core/Settings/AppSettings.cs b/src/Filee.Core/Settings/AppSettings.cs
index bede3d9c..9ec379c2 100644
--- a/src/Filee.Core/Settings/AppSettings.cs
+++ b/src/Filee.Core/Settings/AppSettings.cs
@@ -261,6 +261,12 @@ public sealed class AppSettings
/// </summary>
public double UiScale { get; set; } = 1;
+ /// <summary>
+ /// Where the main window was when it last closed, so the next start opens it there (#586). Null until it has closed
+ /// once. A Filee from before it skips the property, so it needs no schema step.
+ /// </summary>
+ public WindowBounds? MainWindow { get; set; }
+
/// <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
@@ -306,6 +312,19 @@ internal sealed class IconPreferenceJsonConverter : JsonConverter<bool>
writer.WriteBooleanValue(value);
}
+/// <summary>
+/// A window's place (<see cref="AppSettings.MainWindow"/>): its top-left corner in screen pixels and its size in
+/// device-independent pixels, as Avalonia gives them; a part not known is null. Maximized keeps the size it had before.
+/// </summary>
+public sealed class WindowBounds
+{
+ public int? X { get; set; }
+ public int? Y { get; set; }
+ public double? Width { get; set; }
+ public double? Height { get; set; }
+ public bool Maximized { get; set; }
+}
+
/// <summary>The save location presets set to "Default" use. Starts as "next to the source file", as before 1.4.</summary>
public sealed class DefaultOutputSettings
{
diff --git a/src/Filee.Core/Settings/UserDataStore.cs b/src/Filee.Core/Settings/UserDataStore.cs
index f9c47a37..d77a28c0 100644
--- a/src/Filee.Core/Settings/UserDataStore.cs
+++ b/src/Filee.Core/Settings/UserDataStore.cs
@@ -106,6 +106,16 @@ public sealed class UserDataStore
Write(SettingsPath, Settings, FileeJsonContext.Default.AppSettings);
}
+ /// <summary>
+ /// Remembers where the main window was (<see cref="AppSettings.MainWindow"/>), quietly as <see cref="SaveUiScale"/>:
+ /// nothing else depends on it.
+ /// </summary>
+ public void SaveMainWindow(WindowBounds bounds)
+ {
+ Settings.MainWindow = bounds;
+ Write(SettingsPath, Settings, FileeJsonContext.Default.AppSettings);
+ }
+
public void SaveLibrary()
{
EnsureConsistency();
diff --git a/tests/Filee.App.Tests/MainWindowPlacementTests.cs b/tests/Filee.App.Tests/MainWindowPlacementTests.cs
new file mode 100644
index 00000000..4299d2e9
--- /dev/null
+++ b/tests/Filee.App.Tests/MainWindowPlacementTests.cs
@@ -0,0 +1,127 @@
+// The main window opens where it was, also after Filee restarts (#586), and always fits the screen it opens on: a
+// small or scaled screen gets a window that fits instead of one that runs off its bottom.
+
+using Avalonia;
+using Avalonia.Headless.XUnit;
+using Avalonia.Threading;
+using Filee.App.Services;
+using Filee.Core.Settings;
+using Microsoft.Extensions.DependencyInjection;
+using Placement = Filee.App.Services.WindowService.MainWindowPlacement;
+
+namespace Filee.App.Tests;
+
+[Collection("MainWindow")]
+public class MainWindowPlacementTests
+{
+ private static readonly PixelRect FullHd = new(0, 0, 1920, 1040); // 1920 × 1080 less the taskbar
+ private static readonly Size Designed = new(1080, 760);
+ private static readonly Size DesignedMin = new(820, 560);
+
+ [Fact]
+ public void A_window_that_fits_its_screen_keeps_its_size_and_place()
+ {
+ var (size, min, position) = Placement.Fit(FullHd, 1, new PixelPoint(200, 100), Designed, DesignedMin);
+
+ Assert.Equal(Designed, size);
+ Assert.Equal(DesignedMin, min);
+ Assert.Equal(new PixelPoint(200, 100), position);
+ }
+
+ [Fact]
+ public void On_a_small_scaled_screen_the_window_and_its_minimum_fit_the_working_area()
+ {
+ // 1366 × 768 at 125 %: 1093 × 584 device-independent pixels of working area (the taskbar takes 38).
+ var area = new PixelRect(0, 0, 1366, 730);
+
+ var (size, min, position) = Placement.Fit(area, 1.25, null, Designed, DesignedMin);
+
+ var room = new Size(1366 / 1.25 - 2 * Placement.Margin, 730 / 1.25 - 2 * Placement.Margin - Placement.Frame);
+ Assert.Equal(room, size); // 1076.8 × 528: a little narrower, and as tall as there is room
+ Assert.Equal(new Size(DesignedMin.Width, room.Height), min); // the minimum gives way only where it must: height
+ Assert.Null(position); // the first start: centred by the system
+ }
+
+ [Fact]
+ public void A_window_that_would_stick_out_moves_back_inside_its_screen()
+ {
+ // Saved at the right edge of a screen that is smaller now.
+ var (size, _, position) = Placement.Fit(FullHd, 1, new PixelPoint(1500, 900), Designed, DesignedMin);
+
+ var at = Assert.IsType<PixelPoint>(position);
+ Assert.Equal(1920 - (int)Designed.Width, at.X);
+ Assert.Equal(1040 - (int)(Designed.Height + Placement.Frame), at.Y);
+ Assert.Equal(Designed, size);
+
+ // A second monitor on the left: its coordinates are negative.
+ var left = new PixelRect(-1280, 0, 1280, 984);
+ var (_, _, onLeft) = Placement.Fit(left, 1, new PixelPoint(-1300, 20), new Size(900, 600), DesignedMin);
+ Assert.Equal(new PixelPoint(-1280, 20), onLeft);
+ }
+
+ [Fact]
+ public void Settings_json_gives_back_only_what_it_can_have_written()
+ {
+ var empty = Placement.From(null);
+ Assert.Equal((null, null, false, ""), (empty.Position, empty.Size, empty.Maximized, empty.Page));
+
+ var kept = Placement.From(new WindowBounds { X = -1200, Y = 40, Width = 1000, Height = 700, Maximized = true });
+ Assert.Equal(new PixelPoint(-1200, 40), kept.Position);
+ Assert.Equal(new Size(1000, 700), kept.Size);
+ Assert.True(kept.Maximized);
+ Assert.Equal("", kept.Page); // the page stays within one run
+
+ // Hand-edited or damaged: the size is left out, the window takes its designed one.
+ Assert.Null(Placement.From(new WindowBounds { Width = 0, Height = 700 }).Size);
+ Assert.Null(Placement.From(new WindowBounds { Width = double.NaN, Height = 700 }).Size);
+ Assert.Null(Placement.From(new WindowBounds { Width = 1000 }).Size);
+ Assert.Null(Placement.From(new WindowBounds { X = 10 }).Position);
+
+ var bounds = kept.ToBounds();
+ Assert.Equal((-1200, 40, 1000.0, 700.0, true), (bounds.X, bounds.Y, bounds.Width, bounds.Height, bounds.Maximized));
+ }
+
+ [AvaloniaFact]
+ public void The_main_window_opens_where_it_was_after_filee_restarts()
+ {
+ TestServices.EnsureInitialized("en");
+ var store = AppHost.Get<UserDataStore>();
+ var before = store.Settings.MainWindow;
+ var first = ActivatorUtilities.CreateInstance<WindowService>(AppHost.Services);
+ var second = ActivatorUtilities.CreateInstance<WindowService>(AppHost.Services); // the next start
+ try
+ {
+ store.Settings.MainWindow = null;
+ first.ShowMain();
+ var main = first.Main!;
+ main.Width = 900;
+ main.Height = 640;
+ Pump();
+ main.Close(); // to the tray: closing while still on screen is when it's remembered
+ Pump();
+
+ var saved = Assert.IsType<WindowBounds>(store.Settings.MainWindow);
+ Assert.Equal((900.0, 640.0, false), (saved.Width, saved.Height, saved.Maximized));
+ var reloaded = new UserDataStore(store.Directory);
+ reloaded.Load();
+ Assert.Equal(900, reloaded.Settings.MainWindow?.Width);
+
+ second.ShowMain();
+ Pump();
+ Assert.Equal((900.0, 640.0), (second.Main!.Width, second.Main.Height));
+ second.Main.Close();
+ Pump();
+ }
+ finally
+ {
+ store.Settings.MainWindow = before;
+ store.SaveSettings();
+ }
+ }
+
+ private static void Pump()
+ {
+ for (var i = 0; i < 10; i++)
+ Dispatcher.UIThread.RunJobs();
+ }
+}