Browse Source

Add functionality for opening keybindings file as well as user settings file from the settings window menu

Ruben 7 months ago
parent
commit
7be2705aee

+ 2 - 38
src/PicView.Avalonia.Win32/Views/SettingsWindow.axaml

@@ -9,6 +9,7 @@
     x:Class="PicView.Avalonia.Win32.Views.SettingsWindow"
     x:DataType="viewModels:MainViewModel"
     xmlns="https://github.com/avaloniaui"
+    xmlns:buttons="clr-namespace:PicView.Avalonia.Views.UC.Buttons;assembly=PicView.Avalonia"
     xmlns:customControls="clr-namespace:PicView.Avalonia.CustomControls;assembly=PicView.Avalonia"
     xmlns:viewModels="clr-namespace:PicView.Avalonia.ViewModels;assembly=PicView.Avalonia"
     xmlns:views="clr-namespace:PicView.Avalonia.Views;assembly=PicView.Avalonia"
@@ -69,44 +70,7 @@
                         Width="30"
                         x:Name="MinimizeButton" />
 
-                    <customControls:IconButton
-                        Background="{StaticResource WindowSecondaryBackgroundColor}"
-                        BorderBrush="{DynamicResource MainBorderColor}"
-                        BorderThickness="1,0,0,0"
-                        Classes="hover"
-                        ClickMode="Press"
-                        DockPanel.Dock="Right"
-                        Foreground="{DynamicResource MainTextColor}"
-                        Icon="{StaticResource MenuImage}"
-                        IconHeight="12"
-                        IconWidth="12"
-                        Width="30"
-                        x:Name="SettingsButton">
-                        <customControls:IconButton.Flyout>
-                            <MenuFlyout FlyoutPresenterClasses="noCornerRadius" Placement="Bottom">
-                                <MenuItem Foreground="{DynamicResource MainTextColor}" Header="UserSettings.json">
-                                    <MenuItem.Icon>
-                                        <Image
-                                            Height="12"
-                                            Source="{StaticResource BracesImage}"
-                                            Width="12" />
-                                    </MenuItem.Icon>
-                                </MenuItem>
-                                <MenuItem
-                                    Command="{CompiledBinding ResetSettingsCommand}"
-                                    Foreground="{DynamicResource MainTextColor}"
-                                    Header="{CompiledBinding Translation.ResetButtonText,
-                                                             Mode=OneWay}">
-                                    <MenuItem.Icon>
-                                        <Image
-                                            Height="12"
-                                            Source="{StaticResource ResetImage}"
-                                            Width="12" />
-                                    </MenuItem.Icon>
-                                </MenuItem>
-                            </MenuFlyout>
-                        </customControls:IconButton.Flyout>
-                    </customControls:IconButton>
+                    <buttons:SettingsMenuButton x:Name="SettingsButton" />
 
                     <TextBlock
                         Background="{StaticResource WindowSecondaryBackgroundColor}"

+ 2 - 7
src/PicView.Avalonia/Input/KeybindingManager.cs

@@ -103,11 +103,6 @@ public static class KeybindingManager
         await Loop(keyValues).ConfigureAwait(false);
     }
     
-    private static string? GetFunctionNameByFunction(Func<Task> function)
-    {
-        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
-        if (function == null)
-            return "";
-        return CustomShortcuts.FirstOrDefault(x => x.Value == function).Value.Method.Name ?? "";
-    }
+    private static string GetFunctionNameByFunction(Func<Task> function) =>
+        function == null ? "" : CustomShortcuts.FirstOrDefault(x => x.Value == function).Value.Method.Name;
 }

+ 9 - 0
src/PicView.Avalonia/UI/FunctionsMapper.cs

@@ -14,6 +14,7 @@ using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.WindowBehavior;
 using PicView.Core.FileHandling;
 using PicView.Core.ImageDecoding;
+using PicView.Core.Keybindings;
 using PicView.Core.Navigation;
 using PicView.Core.ProcessHandling;
 
@@ -114,6 +115,8 @@ public static class FunctionsMapper
             "DeleteFilePermanently" => DeleteFilePermanently,
             "Rename" => Rename,
             "ShowFileProperties" => ShowFileProperties,
+            "ShowSettingsFile" => ShowSettingsFile,
+            "ShowKeybindingsFile" => ShowKeybindingsFile,
 
             // Image functions
             "ResizeImage" => ResizeImage,
@@ -855,6 +858,12 @@ public static class FunctionsMapper
         });
     }
     
+    public static async Task ShowSettingsFile() =>
+        await Task.Run(() => Vm?.PlatformService?.OpenWith(CurrentSettingsPath)).ConfigureAwait(false);
+    
+    public static async Task ShowKeybindingsFile() =>
+        await Task.Run(() => Vm?.PlatformService?.OpenWith(KeybindingFunctions.CurrentKeybindingsPath)).ConfigureAwait(false);
+    
     /// <inheritdoc cref="SettingsUpdater.ToggleUsingTouchpad(MainViewModel)" />
     public static async Task ToggleUsingTouchpad() =>
         await SettingsUpdater.ToggleUsingTouchpad(Vm).ConfigureAwait(false);

+ 8 - 0
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -284,6 +284,10 @@ public class MainViewModel : ReactiveObject
         RestartCommand = ReactiveCommand.CreateFromTask(FunctionsMapper.Restart);
 
         ToggleUsingTouchpadCommand = ReactiveCommand.CreateFromTask(FunctionsMapper.ToggleUsingTouchpad);
+        
+        ShowSettingsFileCommand = ReactiveCommand.CreateFromTask(FunctionsMapper.ShowSettingsFile);
+        
+        ShowKeybindingsFileCommand = ReactiveCommand.CreateFromTask(FunctionsMapper.ShowKeybindingsFile);
 
         #endregion Settings commands
     }
@@ -667,6 +671,10 @@ public class MainViewModel : ReactiveObject
     public ReactiveCommand<Unit, Unit>? ShowSideBySideCommand { get; }
 
     public ReactiveCommand<Unit, Unit>? RestartCommand { get; }
+    
+    public ReactiveCommand<Unit, Unit>? ShowSettingsFileCommand { get; }
+    
+    public ReactiveCommand<Unit, Unit>? ShowKeybindingsFileCommand { get; }
 
     #endregion Commands
 

+ 65 - 0
src/PicView.Avalonia/Views/UC/Buttons/SettingsMenuButton.axaml

@@ -0,0 +1,65 @@
+<UserControl
+    DockPanel.Dock="Right"
+    d:DesignHeight="450"
+    d:DesignWidth="800"
+    mc:Ignorable="d"
+    x:Class="PicView.Avalonia.Views.UC.Buttons.SettingsMenuButton"
+    x:DataType="viewModels:MainViewModel"
+    xmlns="https://github.com/avaloniaui"
+    xmlns:customControls="clr-namespace:PicView.Avalonia.CustomControls"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:viewModels="clr-namespace:PicView.Avalonia.ViewModels"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
+    <customControls:IconButton
+        Background="{StaticResource WindowSecondaryBackgroundColor}"
+        BorderBrush="{DynamicResource MainBorderColor}"
+        BorderThickness="1,0,0,0"
+        Classes="hover"
+        ClickMode="Press"
+        Foreground="{DynamicResource MainTextColor}"
+        Icon="{StaticResource MenuImage}"
+        IconHeight="12"
+        IconWidth="12"
+        Width="30"
+        x:Name="SettingsButton">
+        <customControls:IconButton.Flyout>
+            <MenuFlyout FlyoutPresenterClasses="noCornerRadius" Placement="Bottom">
+                <MenuItem
+                    Command="{CompiledBinding ShowSettingsFileCommand}"
+                    Foreground="{DynamicResource MainTextColor}"
+                    Header="UserSettings.json">
+                    <MenuItem.Icon>
+                        <Image
+                            Height="12"
+                            Source="{StaticResource BracesImage}"
+                            Width="12" />
+                    </MenuItem.Icon>
+                </MenuItem>
+                <MenuItem
+                    Command="{CompiledBinding ShowKeybindingsFileCommand}"
+                    Foreground="{DynamicResource MainTextColor}"
+                    Header="keybindings.json">
+                    <MenuItem.Icon>
+                        <Image
+                            Height="12"
+                            Source="{StaticResource BracesImage}"
+                            Width="12" />
+                    </MenuItem.Icon>
+                </MenuItem>
+                <MenuItem
+                    Command="{CompiledBinding ResetSettingsCommand}"
+                    Foreground="{DynamicResource MainTextColor}"
+                    Header="{CompiledBinding Translation.ResetButtonText,
+                                             Mode=OneWay}">
+                    <MenuItem.Icon>
+                        <Image
+                            Height="12"
+                            Source="{StaticResource ResetImage}"
+                            Width="12" />
+                    </MenuItem.Icon>
+                </MenuItem>
+            </MenuFlyout>
+        </customControls:IconButton.Flyout>
+    </customControls:IconButton>
+</UserControl>

+ 12 - 0
src/PicView.Avalonia/Views/UC/Buttons/SettingsMenuButton.axaml.cs

@@ -0,0 +1,12 @@
+using Avalonia.Controls;
+
+namespace PicView.Avalonia.Views.UC.Buttons
+{
+    public partial class SettingsMenuButton : UserControl
+    {
+        public SettingsMenuButton()
+        {
+            InitializeComponent();
+        }
+    }
+}

+ 5 - 14
src/PicView.Core/Config/SettingsManager.cs

@@ -17,7 +17,7 @@ public static class SettingsManager
     private const string RoamingConfigFolder = "Ruben2776/PicView/Config";
     private const string RoamingConfigPath = RoamingConfigFolder + "/" + ConfigFileName;
     
-    private static bool _isSavingFromRoamingPath;
+    public static string? CurrentSettingsPath { get; private set; }
 
     public static AppSettings? Settings { get; private set; }
 
@@ -134,7 +134,6 @@ public static class SettingsManager
         try
         {
             await ReadSettingsFromPathAsync(path).ConfigureAwait(false);
-            _isSavingFromRoamingPath = true;
             return true;
         }
         catch (Exception)
@@ -179,6 +178,7 @@ public static class SettingsManager
         }
 
         Settings = await UpgradeSettingsIfNeededAsync(settings).ConfigureAwait(false);
+        CurrentSettingsPath = path.Replace("/", "\\");
     }
 
     /// <summary>
@@ -193,15 +193,7 @@ public static class SettingsManager
 
         try
         {
-            if (_isSavingFromRoamingPath)
-            {
-                await SaveSettingsToPathAsync(GetRoamingSettingsPath()).ConfigureAwait(false);
-            }
-            else
-            {
-                var localPath = GetLocalSettingsPath();
-                await SaveSettingsToPathAsync(localPath).ConfigureAwait(false);
-            }
+            await SaveSettingsToPathAsync(CurrentSettingsPath).ConfigureAwait(false);
             return true;
         }
         catch (UnauthorizedAccessException)
@@ -283,10 +275,9 @@ public static class SettingsManager
             }
 
             var jsonString = await File.ReadAllTextAsync(localPath).ConfigureAwait(false);
-            var existingSettings = JsonSerializer.Deserialize(
-                jsonString, typeof(AppSettings), SourceGenerationContext.Default) as AppSettings;
 
-            if (existingSettings == null)
+            if (JsonSerializer.Deserialize(
+                    jsonString, typeof(AppSettings), SourceGenerationContext.Default) is not AppSettings existingSettings)
             {
                 return;
             }

+ 5 - 0
src/PicView.Core/Keybindings/KeybindingFunctions.cs

@@ -2,6 +2,7 @@
 
 public static class KeybindingFunctions
 {
+    public static string? CurrentKeybindingsPath { get; private set; }
     public static async Task SaveKeyBindingsFile(string json)
     {
         try
@@ -9,6 +10,7 @@ public static class KeybindingFunctions
             var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/keybindings.json");
             await using var writer = new StreamWriter(path);
             await writer.WriteAsync(json).ConfigureAwait(false);
+            CurrentKeybindingsPath = path.Replace("/", "\\");;
         }
         catch (Exception)
         {
@@ -20,6 +22,7 @@ public static class KeybindingFunctions
             }
             await using var newWriter = new StreamWriter(newPath);
             await newWriter.WriteAsync(json).ConfigureAwait(false);
+            CurrentKeybindingsPath = newPath.Replace("/", "\\");;
         }
     }
 
@@ -29,6 +32,7 @@ public static class KeybindingFunctions
         if (File.Exists(path))
         {
             var text = await File.ReadAllTextAsync(path).ConfigureAwait(false);
+            CurrentKeybindingsPath = path.Replace("/", "\\");;
             return text;
         }
 
@@ -36,6 +40,7 @@ public static class KeybindingFunctions
         if (File.Exists(newPath))
         {
             var text = await File.ReadAllTextAsync(newPath).ConfigureAwait(false);
+            CurrentKeybindingsPath = path.Replace("/", "\\");;
             return text;
         }