From 93ecc1caf1859dde85c125b37a0b35aba7d23666 Mon Sep 17 00:00:00 2001 From: Ruben Date: Fri, 8 Nov 2024 10:44:58 +0100 Subject: [PATCH] Improve resetting settings, refactor and rename. --- .../SettingsManagement/SettingsUpdater.cs | 416 +++++++++++------- src/PicView.Avalonia/StartUp/StartUpHelper.cs | 8 +- src/PicView.Avalonia/UI/FunctionsHelper.cs | 2 +- src/PicView.Avalonia/Update/UpdateManager.cs | 12 +- 4 files changed, 277 insertions(+), 161 deletions(-) diff --git a/src/PicView.Avalonia/SettingsManagement/SettingsUpdater.cs b/src/PicView.Avalonia/SettingsManagement/SettingsUpdater.cs index e2fd7c730..10c502644 100644 --- a/src/PicView.Avalonia/SettingsManagement/SettingsUpdater.cs +++ b/src/PicView.Avalonia/SettingsManagement/SettingsUpdater.cs @@ -1,214 +1,314 @@ -using Avalonia; -using Avalonia.Controls.ApplicationLifetimes; +using System.Runtime.InteropServices; +using Avalonia; using Avalonia.Controls.Primitives; using Avalonia.Media; using Avalonia.Threading; +using PicView.Avalonia.ColorManagement; using PicView.Avalonia.Navigation; using PicView.Avalonia.UI; using PicView.Avalonia.ViewModels; using PicView.Avalonia.WindowBehavior; using PicView.Core.Config; +using PicView.Core.Gallery; using PicView.Core.Localization; -using PicView.Core.ProcessHandling; namespace PicView.Avalonia.SettingsManagement; - public static class SettingsUpdater +public static class SettingsUpdater +{ + public static async Task ResetSettings(MainViewModel vm) { - public static async Task ResetSettings(MainViewModel vm) + vm.IsLoading = true; + + try { SettingsHelper.DeleteSettingFiles(); SettingsHelper.SetDefaults(); - await SettingsHelper.SaveSettingsAsync(); - string args; - if (!NavigationHelper.CanNavigate(vm)) + + ThemeManager.DetermineTheme(Application.Current, false); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - var argsList = Environment.GetCommandLineArgs(); - args = argsList.Length > 1 ? argsList[1] : string.Empty; + TurnOffUsingTouchpad(vm); } else { - args = vm.FileInfo.FullName; + TurnOffUsingTouchpad(vm); } - ProcessHelper.RestartApp(args); - - if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) + + vm.GetBottomGalleryItemHeight = GalleryDefaults.DefaultBottomGalleryHeight; + vm.GetFullGalleryItemHeight = GalleryDefaults.DefaultFullGalleryHeight; + + if (string.IsNullOrWhiteSpace(SettingsHelper.Settings.Gallery.BottomGalleryStretchMode)) { - return; + SettingsHelper.Settings.Gallery.BottomGalleryStretchMode = "UniformToFill"; } - await Dispatcher.UIThread.InvokeAsync(() => + + if (string.IsNullOrWhiteSpace(SettingsHelper.Settings.Gallery.FullGalleryStretchMode)) { - desktop.MainWindow?.Close(); - }); - } + SettingsHelper.Settings.Gallery.FullGalleryStretchMode = "UniformToFill"; + } - public static async Task ToggleUsingTouchpad(MainViewModel vm) - { - SettingsHelper.Settings.Zoom.IsUsingTouchPad = !SettingsHelper.Settings - .Zoom.IsUsingTouchPad; + await TurnOffSubdirectories(vm); - vm.GetIsUsingTouchpadTranslation = SettingsHelper.Settings - .Zoom.IsUsingTouchPad - ? TranslationHelper.Translation.UsingTouchpad - : TranslationHelper.Translation.UsingMouse; - - vm.IsUsingTouchpad = SettingsHelper.Settings.Zoom.IsUsingTouchPad; + TurnOffSideBySide(vm); + TurnOffScroll(vm); + TurnOffCtrlZoom(vm); + TurnOffLooping(vm); + await Dispatcher.UIThread.InvokeAsync(() => + { + vm.PlatformService.StopTaskbarProgress(); + if (NavigationHelper.CanNavigate(vm)) + { + vm.PlatformService.SetTaskbarProgress((ulong)vm.ImageIterator?.CurrentIndex!, + (ulong)vm.ImageIterator?.ImagePaths?.Count!); + } + WindowResizing.SetSize(vm); + }); + await SettingsHelper.SaveSettingsAsync(); } - - public static async Task ToggleSubdirectories(MainViewModel vm) + finally { - if (SettingsHelper.Settings.Sorting.IncludeSubDirectories) - { - vm.IsIncludingSubdirectories = false; - SettingsHelper.Settings.Sorting.IncludeSubDirectories = false; - } - else - { - vm.IsIncludingSubdirectories = true; - SettingsHelper.Settings.Sorting.IncludeSubDirectories = true; - } + SetTitleHelper.RefreshTitle(vm); + vm.IsLoading = false; + } + } - await vm.ImageIterator.ReloadFileList(); - SetTitleHelper.SetTitle(vm); - await SettingsHelper.SaveSettingsAsync(); + public static async Task ToggleUsingTouchpad(MainViewModel vm) + { + if (SettingsHelper.Settings.Zoom.IsUsingTouchPad) + { + TurnOffUsingTouchpad(vm); } + else + { + TurnOnUsingTouchpad(vm); + } + + await SettingsHelper.SaveSettingsAsync(); + } + + public static void TurnOffUsingTouchpad(MainViewModel vm) + { + SettingsHelper.Settings.Zoom.IsUsingTouchPad = false; + vm.GetIsUsingTouchpadTranslation = TranslationHelper.Translation.UsingMouse; + vm.IsUsingTouchpad = false; + } + + public static void TurnOnUsingTouchpad(MainViewModel vm) + { + SettingsHelper.Settings.Zoom.IsUsingTouchPad = true; + vm.GetIsUsingTouchpadTranslation = TranslationHelper.Translation.UsingTouchpad; + vm.IsUsingTouchpad = true; + } + + public static async Task ToggleSubdirectories(MainViewModel vm) + { + if (SettingsHelper.Settings.Sorting.IncludeSubDirectories) + { + await TurnOffSubdirectories(vm); + } + else + { + await TurnOnSubdirectories(vm); + } + await SettingsHelper.SaveSettingsAsync(); + } + + public static async Task TurnOffSubdirectories(MainViewModel vm) + { + vm.IsIncludingSubdirectories = false; + SettingsHelper.Settings.Sorting.IncludeSubDirectories = false; - public static async Task ToggleTaskbarProgress(MainViewModel vm) + await vm.ImageIterator.ReloadFileList(); + SetTitleHelper.SetTitle(vm); + } + + public static async Task TurnOnSubdirectories(MainViewModel vm) + { + vm.IsIncludingSubdirectories = true; + SettingsHelper.Settings.Sorting.IncludeSubDirectories = true; + + await vm.ImageIterator.ReloadFileList(); + SetTitleHelper.SetTitle(vm); + } + + public static async Task ToggleTaskbarProgress(MainViewModel vm) + { + if (SettingsHelper.Settings.UIProperties.IsTaskbarProgressEnabled) + { + SettingsHelper.Settings.UIProperties.IsTaskbarProgressEnabled = false; + await Dispatcher.UIThread.InvokeAsync(() => + { + vm.PlatformService.StopTaskbarProgress(); + }); + } + else { - if (SettingsHelper.Settings.UIProperties.IsTaskbarProgressEnabled) + SettingsHelper.Settings.UIProperties.IsTaskbarProgressEnabled = true; + if (NavigationHelper.CanNavigate(vm)) { - SettingsHelper.Settings.UIProperties.IsTaskbarProgressEnabled = false; await Dispatcher.UIThread.InvokeAsync(() => { - vm.PlatformService.StopTaskbarProgress(); + vm.PlatformService.SetTaskbarProgress((ulong)vm.ImageIterator?.CurrentIndex!, + (ulong)vm.ImageIterator?.ImagePaths?.Count!); }); } - else - { - SettingsHelper.Settings.UIProperties.IsTaskbarProgressEnabled = true; - if (NavigationHelper.CanNavigate(vm)) - { - await Dispatcher.UIThread.InvokeAsync(() => - { - vm.PlatformService.SetTaskbarProgress((ulong)vm.ImageIterator.CurrentIndex, - (ulong)vm.ImageIterator.ImagePaths.Count); - }); - } - } + } - await SettingsHelper.SaveSettingsAsync(); + await SettingsHelper.SaveSettingsAsync(); + } + + #region Image settings + + public static async Task ToggleSideBySide(MainViewModel vm) + { + if (vm is null) + { + return; } - #region Image settings + if (SettingsHelper.Settings.ImageScaling.ShowImageSideBySide) + { + TurnOffSideBySide(vm); + } + else + { + await TurnOnSideBySide(vm); + } - public static async Task ToggleSideBySide(MainViewModel vm) + await SettingsHelper.SaveSettingsAsync(); + } + + public static void TurnOffSideBySide(MainViewModel vm) + { + SettingsHelper.Settings.ImageScaling.ShowImageSideBySide = false; + vm.IsShowingSideBySide = false; + vm.SecondaryImageSource = null; + WindowResizing.SetSize(vm); + } + + public static async Task TurnOnSideBySide(MainViewModel vm) + { + SettingsHelper.Settings.ImageScaling.ShowImageSideBySide = true; + vm.IsShowingSideBySide = true; + if (NavigationHelper.CanNavigate(vm)) { - if (vm is null) - { - return; - } - - if (SettingsHelper.Settings.ImageScaling.ShowImageSideBySide) - { - SettingsHelper.Settings.ImageScaling.ShowImageSideBySide = false; - vm.IsShowingSideBySide = false; - vm.SecondaryImageSource = null; - WindowResizing.SetSize(vm); - } - else + var preloadValue = await vm.ImageIterator?.GetNextPreLoadValueAsync(); + vm.SecondaryImageSource = preloadValue?.ImageModel.Image; + await Dispatcher.UIThread.InvokeAsync(() => { - SettingsHelper.Settings.ImageScaling.ShowImageSideBySide = true; - vm.IsShowingSideBySide = true; - if (NavigationHelper.CanNavigate(vm)) - { - var preloadValue = await vm.ImageIterator?.GetNextPreLoadValueAsync(); - vm.SecondaryImageSource = preloadValue?.ImageModel.Image; - await Dispatcher.UIThread.InvokeAsync(() => - { - WindowResizing.SetSize(vm.ImageWidth, vm.ImageHeight, preloadValue.ImageModel.PixelWidth, - preloadValue.ImageModel.PixelHeight, vm.RotationAngle, vm); - }); - } - } - - await SettingsHelper.SaveSettingsAsync(); + WindowResizing.SetSize(vm.ImageWidth, vm.ImageHeight, preloadValue.ImageModel.PixelWidth, + preloadValue.ImageModel.PixelHeight, vm.RotationAngle, vm); + }); + } + } + + public static async Task ToggleScroll(MainViewModel vm) + { + if (vm is null) + { + return; } - public static async Task ToggleScroll(MainViewModel vm) + if (SettingsHelper.Settings.Zoom.ScrollEnabled) { - if (vm is null) - { - return; - } - - if (SettingsHelper.Settings.Zoom.ScrollEnabled) - { - vm.ToggleScrollBarVisibility = ScrollBarVisibility.Disabled; - vm.GetIsScrollingTranslation = TranslationHelper.Translation.ScrollingDisabled; - vm.IsScrollingEnabled = false; - SettingsHelper.Settings.Zoom.ScrollEnabled = false; - } - else - { - vm.ToggleScrollBarVisibility = ScrollBarVisibility.Visible; - vm.GetIsScrollingTranslation = TranslationHelper.Translation.ScrollingEnabled; - vm.IsScrollingEnabled = true; - SettingsHelper.Settings.Zoom.ScrollEnabled = true; - } - - WindowResizing.SetSize(vm); - - await SettingsHelper.SaveSettingsAsync(); + TurnOffScroll(vm); + } + else + { + TurnOnScroll(vm); } + + WindowResizing.SetSize(vm); - public static async Task ChangeCtrlZoom(MainViewModel vm) + await SettingsHelper.SaveSettingsAsync(); + } + + public static void TurnOffScroll(MainViewModel vm) + { + vm.ToggleScrollBarVisibility = ScrollBarVisibility.Disabled; + vm.GetIsScrollingTranslation = TranslationHelper.Translation.ScrollingDisabled; + vm.IsScrollingEnabled = false; + SettingsHelper.Settings.Zoom.ScrollEnabled = false; + } + + public static void TurnOnScroll(MainViewModel vm) + { + vm.ToggleScrollBarVisibility = ScrollBarVisibility.Visible; + vm.GetIsScrollingTranslation = TranslationHelper.Translation.ScrollingEnabled; + vm.IsScrollingEnabled = true; + SettingsHelper.Settings.Zoom.ScrollEnabled = true; + } + + public static async Task ToggleCtrlZoom(MainViewModel vm) + { + if (vm is null) { - if (vm is null) - { - return; - } - - SettingsHelper.Settings.Zoom.CtrlZoom = !SettingsHelper.Settings.Zoom.CtrlZoom; - vm.GetIsCtrlZoomTranslation = SettingsHelper.Settings.Zoom.CtrlZoom - ? TranslationHelper.Translation.CtrlToZoom - : TranslationHelper.Translation.ScrollToZoom; - - // Set source for ChangeCtrlZoomImage - if (!Application.Current.TryGetResource("ScanEyeImage", Application.Current.RequestedThemeVariant, out var scanEyeImage )) - { - return; - } - if (!Application.Current.TryGetResource("LeftRightArrowsImage", Application.Current.RequestedThemeVariant, out var leftRightArrowsImage )) - { - return; - } - var isNavigatingWithCtrl = SettingsHelper.Settings.Zoom.CtrlZoom; - vm.ChangeCtrlZoomImage = isNavigatingWithCtrl ? leftRightArrowsImage as DrawingImage : scanEyeImage as DrawingImage; - await SettingsHelper.SaveSettingsAsync().ConfigureAwait(false); + return; } - public static async Task ToggleLooping(MainViewModel vm) + SettingsHelper.Settings.Zoom.CtrlZoom = !SettingsHelper.Settings.Zoom.CtrlZoom; + vm.GetIsCtrlZoomTranslation = SettingsHelper.Settings.Zoom.CtrlZoom + ? TranslationHelper.Translation.CtrlToZoom + : TranslationHelper.Translation.ScrollToZoom; + + // Set source for ChangeCtrlZoomImage + if (!Application.Current.TryGetResource("ScanEyeImage", Application.Current.RequestedThemeVariant, out var scanEyeImage )) { - if (vm is null) - { - return; - } - - var value = !SettingsHelper.Settings.UIProperties.Looping; - SettingsHelper.Settings.UIProperties.Looping = value; - vm.GetIsLoopingTranslation = value - ? TranslationHelper.Translation.LoopingEnabled - : TranslationHelper.Translation.LoopingDisabled; - vm.IsLooping = value; - - var msg = value - ? TranslationHelper.Translation.LoopingEnabled - : TranslationHelper.Translation.LoopingDisabled; - await TooltipHelper.ShowTooltipMessageAsync(msg); - - await SettingsHelper.SaveSettingsAsync(); + return; + } + if (!Application.Current.TryGetResource("LeftRightArrowsImage", Application.Current.RequestedThemeVariant, out var leftRightArrowsImage )) + { + return; + } + var isNavigatingWithCtrl = SettingsHelper.Settings.Zoom.CtrlZoom; + vm.ChangeCtrlZoomImage = isNavigatingWithCtrl ? leftRightArrowsImage as DrawingImage : scanEyeImage as DrawingImage; + await SettingsHelper.SaveSettingsAsync().ConfigureAwait(false); + } + + public static void TurnOffCtrlZoom(MainViewModel vm) + { + SettingsHelper.Settings.Zoom.CtrlZoom = false; + vm.GetIsCtrlZoomTranslation = TranslationHelper.Translation.ScrollToZoom; + if (!Application.Current.TryGetResource("ScanEyeImage", Application.Current.RequestedThemeVariant, out var scanEyeImage )) + { + return; + } + vm.ChangeCtrlZoomImage = scanEyeImage as DrawingImage; + } + + + + public static async Task ToggleLooping(MainViewModel vm) + { + if (vm is null) + { + return; } - #endregion + var value = !SettingsHelper.Settings.UIProperties.Looping; + SettingsHelper.Settings.UIProperties.Looping = value; + vm.GetIsLoopingTranslation = value + ? TranslationHelper.Translation.LoopingEnabled + : TranslationHelper.Translation.LoopingDisabled; + vm.IsLooping = value; + + var msg = value + ? TranslationHelper.Translation.LoopingEnabled + : TranslationHelper.Translation.LoopingDisabled; + await TooltipHelper.ShowTooltipMessageAsync(msg); + + await SettingsHelper.SaveSettingsAsync(); + } + + public static void TurnOffLooping(MainViewModel vm) + { + SettingsHelper.Settings.UIProperties.Looping = false; + vm.GetIsLoopingTranslation = TranslationHelper.Translation.LoopingDisabled; + vm.IsLooping = false; } + + #endregion +} diff --git a/src/PicView.Avalonia/StartUp/StartUpHelper.cs b/src/PicView.Avalonia/StartUp/StartUpHelper.cs index ee9512ae6..9b70f0623 100644 --- a/src/PicView.Avalonia/StartUp/StartUpHelper.cs +++ b/src/PicView.Avalonia/StartUp/StartUpHelper.cs @@ -1,4 +1,5 @@ -using Avalonia; +using System.Runtime.InteropServices; +using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Primitives; @@ -210,6 +211,11 @@ private static void InitializeWindowForNoSettings(Window w, MainViewModel vm) WindowFunctions.CenterWindowOnScreen(); vm.CanResize = true; vm.IsAutoFit = false; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + SettingsHelper.Settings.Zoom.IsUsingTouchPad = true; + } } private static void HandleMultipleInstances(string[] args) diff --git a/src/PicView.Avalonia/UI/FunctionsHelper.cs b/src/PicView.Avalonia/UI/FunctionsHelper.cs index 1d76521f4..3c8be309f 100644 --- a/src/PicView.Avalonia/UI/FunctionsHelper.cs +++ b/src/PicView.Avalonia/UI/FunctionsHelper.cs @@ -344,7 +344,7 @@ public static async Task ToggleScroll() public static async Task ChangeCtrlZoom() { - await SettingsUpdater.ChangeCtrlZoom(Vm); + await SettingsUpdater.ToggleCtrlZoom(Vm); } public static async Task ToggleLooping() diff --git a/src/PicView.Avalonia/Update/UpdateManager.cs b/src/PicView.Avalonia/Update/UpdateManager.cs index 5af95d25b..2e42b40c3 100644 --- a/src/PicView.Avalonia/Update/UpdateManager.cs +++ b/src/PicView.Avalonia/Update/UpdateManager.cs @@ -49,7 +49,17 @@ public static async Task UpdateCurrentVersion(MainViewModel vm) WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory } }; - process.Start(); + try + { + process.Start(); + } + catch (Exception e) + { + #if DEBUG + Console.WriteLine(e); + #endif + return; + } Environment.Exit(0); } }