浏览代码

Add new settings to show last file on startup, or a specific file and fix wrong filehistory

Ruben 1 年之前
父节点
当前提交
eb296dc04d

+ 9 - 1
src/PicView.Core/Config/AppSettings.cs

@@ -2,7 +2,7 @@
 
 public class AppSettings
 {
-    public int Version { get; set; } = 1;
+    public double Version { get; set; } = 1;
     public WindowProperties? WindowProperties { get; set; }
     public UIProperties? UIProperties { get; set; }
     public Theme? Theme { get; set; }
@@ -10,6 +10,7 @@ public class AppSettings
     public ImageScaling? ImageScaling { get; set; }
     public Sorting? Sorting { get; set; }
     public Zoom? Zoom { get; set; }
+    public StartUp? StartUp { get; set; }
 }
 
 public class WindowProperties
@@ -75,4 +76,11 @@ public class Sorting
     public bool Ascending { get; set; } = true;
     public int SortPreference { get; set; } = 0;
     public bool IncludeSubDirectories { get; set; } = false;
+}
+
+public class StartUp
+{
+    public bool OpenLastFile { get; set; } = false;
+    public bool OpenSpecificFile { get; set; } = false;
+    public string OpenSpecificString { get; set; } = "";
 }

+ 3 - 2
src/PicView.Core/Config/SettingsHelper.cs

@@ -5,7 +5,7 @@ namespace PicView.Core.Config;
 
 public static class SettingsHelper
 {
-    private const int CurrentSettingsVersion = 1;
+    private const double CurrentSettingsVersion = 1;
 
     public static AppSettings? Settings { get; private set; }
     private static JsonSerializerOptions? _jsonSerializerOptions;
@@ -30,7 +30,8 @@ public static class SettingsHelper
                     Sorting = new Sorting(),
                     Theme = new Theme(),
                     WindowProperties = new WindowProperties(),
-                    Zoom = new Zoom()
+                    Zoom = new Zoom(),
+                    StartUp = new StartUp()
                 };
             }
         }

+ 26 - 18
src/PicView.Core/Navigation/FileHistory.cs

@@ -9,6 +9,7 @@ namespace PicView.Core.Navigation
     {
         private readonly List<string> _fileHistory;
         public const short MaxCount = 15;
+        private readonly string _fileLocation;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="FileHistory"/> class.
@@ -16,12 +17,12 @@ namespace PicView.Core.Navigation
         public FileHistory()
         {
             _fileHistory ??= new List<string>();
-            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/recent.txt");
+            _fileLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/recent.txt");
             try
             {
-                if (!File.Exists(file))
+                if (!File.Exists(_fileLocation))
                 {
-                    using var fs = File.Create(file);
+                    using var fs = File.Create(_fileLocation);
                     fs.Seek(0, SeekOrigin.Begin);
                 }
             }
@@ -41,10 +42,9 @@ namespace PicView.Core.Navigation
         public string ReadFromFile()
         {
             _fileHistory.Clear();
-            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/recent.txt");
             try
             {
-                using var reader = new StreamReader(file);
+                using var reader = new StreamReader(_fileLocation);
                 while (reader.Peek() >= 0)
                 {
                     _fileHistory.Add(reader.ReadLine());
@@ -66,10 +66,9 @@ namespace PicView.Core.Navigation
         /// <returns>An empty string if successful, otherwise an error message.</returns>
         public string WriteToFile()
         {
-            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/recent.txt");
             try
             {
-                using var writer = new StreamWriter(file);
+                using var writer = new StreamWriter(_fileLocation);
                 foreach (var item in _fileHistory)
                 {
                     writer.WriteLine(item);
@@ -89,21 +88,30 @@ namespace PicView.Core.Navigation
 
         public void Add(string fileName)
         {
-            if (string.IsNullOrWhiteSpace(fileName))
-            {
-                return;
-            }
-            if (_fileHistory.Exists(e => e.EndsWith(fileName)))
+            try
             {
-                return;
-            }
+                if (string.IsNullOrWhiteSpace(fileName))
+                {
+                    return;
+                }
+                if (_fileHistory.Exists(e => e.EndsWith(fileName)))
+                {
+                    return;
+                }
 
-            if (_fileHistory.Count >= MaxCount)
+                if (_fileHistory.Count >= MaxCount)
+                {
+                    _fileHistory.RemoveAt(0);
+                }
+
+                _fileHistory.Add(fileName);
+            }
+            catch (Exception e)
             {
-                _fileHistory.RemoveAt(0);
+#if DEBUG
+                Trace.WriteLine($"{nameof(FileHistory)}: {nameof(Add)} exception,\n{e.Message}");
+#endif
             }
-
-            _fileHistory.Add(fileName);
         }
 
         public string? GetLastFile()

+ 1 - 1
src/PicView.WPF/ChangeImage/LoadPic.cs

@@ -578,7 +578,7 @@ namespace PicView.WPF.ChangeImage
             }
 
             // Add recent files, except when browsing archive
-            if (string.IsNullOrWhiteSpace(ArchiveExtraction.TempZipFile) && Pics.Count > index)
+            if (string.IsNullOrWhiteSpace(ArchiveExtraction.TempFilePath) && Pics.Count > index)
             {
                 FileHistoryNavigation.Add(Pics[index]);
             }

+ 38 - 90
src/PicView.WPF/UILogic/Loading/StartLoading.cs

@@ -2,17 +2,13 @@
 using PicView.Core.Config;
 using PicView.WPF.ChangeImage;
 using PicView.WPF.ConfigureSettings;
-using PicView.WPF.ImageHandling;
 using PicView.WPF.Shortcuts;
 using PicView.WPF.SystemIntegration;
 using PicView.WPF.UILogic.Sizing;
 using PicView.WPF.Views.UserControls.Misc;
 using PicView.WPF.Views.Windows;
-using System.IO;
 using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
 using static PicView.WPF.UILogic.Loading.LoadContextMenus;
 using static PicView.WPF.UILogic.Loading.LoadControls;
 using static PicView.WPF.UILogic.Sizing.WindowSizing;
@@ -25,16 +21,10 @@ internal static class StartLoading
 {
     internal static async Task LoadedEvent(Startup_Window startupWindow)
     {
-        var spinner = GetSpinWaiter;
-        spinner = new SpinWaiter();
-        startupWindow.TheGrid.Children.Add(spinner);
-        Panel.SetZIndex(spinner, 99);
-        BitmapSource? bitmapSource = null;
+        GetSpinWaiter = new SpinWaiter();
+        startupWindow.TheGrid.Children.Add(GetSpinWaiter);
+        Panel.SetZIndex(GetSpinWaiter, 99);
 
-        var image = new Image
-        {
-            Stretch = Stretch.Uniform,
-        };
         // Load sizing properties
         MonitorInfo = MonitorSize.GetMonitorSize(startupWindow);
         await SettingsHelper.LoadSettingsAsync().ConfigureAwait(false);
@@ -42,86 +32,20 @@ internal static class StartLoading
         {
             SetLastWindowSize(startupWindow);
         }
-
-        var args = Environment.GetCommandLineArgs();
-        ImageSizeCalculationHelper.ImageSize? size = null;
-        MainWindow? mainWindow = null;
-
-        if (args.Length > 1)
-        {
-            await Task.Run(async () =>
-            {
-                bitmapSource = await Image2BitmapSource.ReturnBitmapSourceAsync(new FileInfo(args[1]))
-                    .ConfigureAwait(false);
-            });
-
-            await startupWindow.Dispatcher.InvokeAsync(() =>
-            {
-                size = ImageSizeCalculationHelper.GetImageSize(
-                    width: bitmapSource.PixelWidth * MonitorInfo.DpiScaling,
-                    height: bitmapSource.PixelHeight * MonitorInfo.DpiScaling,
-                    monitorWidth: MonitorInfo.WorkArea.Width * MonitorInfo.DpiScaling,
-                    monitorHeight: MonitorInfo.WorkArea.Height * MonitorInfo.DpiScaling,
-                    rotationAngle: 0,
-                    stretch: SettingsHelper.Settings.ImageScaling.StretchImage,
-                    padding: 20 * MonitorInfo.DpiScaling,
-                    dpiScaling: MonitorInfo.DpiScaling,
-                    fullscreen: SettingsHelper.Settings.ImageScaling.StretchImage,
-                    uiTopSize: 30 * MonitorInfo.DpiScaling,
-                    uiBottomSize: 25 * MonitorInfo.DpiScaling,
-                    galleryHeight: SettingsHelper.Settings.Gallery.IsBottomGalleryShown
-                        ? SettingsHelper.Settings.Gallery.BottomGalleryItemSize
-                        : 0,
-                    autoFit: SettingsHelper.Settings.WindowProperties.AutoFit,
-                    containerWidth: startupWindow.Width * MonitorInfo.DpiScaling,
-                    containerHeight: startupWindow.Height * MonitorInfo.DpiScaling,
-                    SettingsHelper.Settings.Zoom.ScrollEnabled
-                );
-
-                image.Stretch = Stretch.Fill;
-                image.Width = startupWindow.Width = size.Value.Width;
-                image.Height = startupWindow.Height = size.Value.Height;
-                ScaleImage.AspectRatio = size.Value.AspectRatio;
-
-                image.Source = bitmapSource;
-                startupWindow.TheGrid.Children.Add(image);
-
-                if (SettingsHelper.Settings.WindowProperties.AutoFit)
-                {
-                    CenterWindowOnScreen(startupWindow);
-                }
-
-                image.Source = bitmapSource;
-                mainWindow = new MainWindow();
-                ConfigureWindows.GetMainWindow = mainWindow;
-                startupWindow.TheGrid.Children.Remove(spinner);
-                mainWindow.MainImage.Source = image.Source;
-                GetSpinWaiter = spinner;
-                mainWindow.ParentContainer.Children.Add(spinner);
-            });
-
-            await QuickLoad.QuickLoadAsync(args[1], null, bitmapSource).ConfigureAwait(false);
-        }
         else
         {
-            Navigation.Pics = new List<string>();
-            await startupWindow.Dispatcher.InvokeAsync(() =>
-            {
-                mainWindow = new MainWindow();
-                ConfigureWindows.GetMainWindow = mainWindow;
-                startupWindow.TheGrid.Children.Remove(spinner);
-                GetSpinWaiter = spinner;
-                mainWindow.ParentContainer.Children.Add(spinner);
-            });
+            CenterWindowOnScreen(startupWindow);
         }
 
-        var language = SettingsHelper.Settings.UIProperties.UserLanguage;
-        await Core.Localization.TranslationHelper.LoadLanguage(language).ConfigureAwait(false);
-
-        await CustomKeybindings.LoadKeybindings().ConfigureAwait(false);
-
+        var args = Environment.GetCommandLineArgs();
+        ImageSizeCalculationHelper.ImageSize? size = null;
+        MainWindow? mainWindow = null;
         await startupWindow.Dispatcher.InvokeAsync(() =>
         {
+            mainWindow = new MainWindow();
+            ConfigureWindows.GetMainWindow = mainWindow;
+            GetSpinWaiter = GetSpinWaiter;
+
             if (SettingsHelper.Settings.WindowProperties.AutoFit == false)
             {
                 SetLastWindowSize(mainWindow);
@@ -180,13 +104,37 @@ internal static class StartLoading
                 ScaleImage.TryFitImage();
             }
 
-            startupWindow.Close();
+            startupWindow.TheGrid.Children.Remove(GetSpinWaiter);
+            mainWindow.ParentContainer.Children.Add(GetSpinWaiter);
         });
 
-        if (args.Length <= 1)
+        var language = SettingsHelper.Settings.UIProperties.UserLanguage;
+        await Core.Localization.TranslationHelper.LoadLanguage(language).ConfigureAwait(false);
+        Navigation.Pics = new List<string>();
+
+        if (args.Length > 1)
         {
-            ErrorHandling.Unload(true);
+            await QuickLoad.QuickLoadAsync(args[1]).ConfigureAwait(false);
         }
+        else
+        {
+            if (SettingsHelper.Settings.StartUp.OpenLastFile)
+            {
+                await FileHistoryNavigation.OpenLastFileAsync().ConfigureAwait(false);
+            }
+            else if (SettingsHelper.Settings.StartUp.OpenSpecificFile)
+            {
+                await LoadPic.LoadPicFromStringAsync(SettingsHelper.Settings.StartUp.OpenSpecificString).ConfigureAwait(false);
+            }
+            else
+            {
+                ErrorHandling.Unload(true);
+            }
+        }
+
+        await mainWindow.Dispatcher.InvokeAsync(startupWindow.Close);
+
+        await CustomKeybindings.LoadKeybindings().ConfigureAwait(false);
 
         ConfigColors.UpdateColor();
     }