Explorar o código

[Avalonia] Add recent files to the contextmenu

Ruben hai 1 ano
pai
achega
7f8d07c240

+ 10 - 0
src/PicView.Avalonia/Navigation/FileHistoryNavigation.cs

@@ -38,6 +38,11 @@ public static class FileHistoryNavigation
         return _fileHistory.GetLastFile() ?? string.Empty;
     }
 
+    public static int GetCount()
+    {
+        return _fileHistory?.GetCount() ?? 0;
+    }
+
     internal static async Task OpenLastFileAsync(MainViewModel vm)
     {
         _fileHistory ??= new FileHistory();
@@ -149,4 +154,9 @@ public static class FileHistoryNavigation
         _fileHistory?.WriteToFile();
     }
 
+    public static string GetFileLocation(int i)
+    {
+        _fileHistory ??= new FileHistory();
+        return _fileHistory.GetEntryAt(i) ?? string.Empty;
+    }
 }

+ 2 - 2
src/PicView.Avalonia/Views/MainView.axaml

@@ -23,7 +23,7 @@
         <converters:SortFilesByToBoolConverter x:Key="EnumToBoolConverter" />
     </UserControl.Resources>
     <UserControl.ContextMenu>
-        <ContextMenu>
+        <ContextMenu x:Name="MainContextMenu">
             <MenuItem Command="{CompiledBinding OpenFileCommand}" Header="{CompiledBinding Open, Mode=OneWay}">
                 <MenuItem.Icon>
                     <Path
@@ -169,7 +169,7 @@
                                              Mode=OneWay}"
                     ToggleType="Radio" />
             </MenuItem>
-            <MenuItem Header="{Binding RecentFiles, Mode=OneWay}">
+            <MenuItem Header="{Binding RecentFiles, Mode=OneWay}" x:Name="RecentFilesCM">
                 <MenuItem.Icon>
                     <Path
                         Data="M504 255.531c.253 136.64-111.18 248.372-247.82 248.468-59.015.042-113.223-20.53-155.822-54.911-11.077-8.94-11.905-25.541-1.839-35.607l11.267-11.267c8.609-8.609 22.353-9.551 31.891-1.984C173.062 425.135 212.781 440 256 440c101.705 0 184-82.311 184-184 0-101.705-82.311-184-184-184-48.814 0-93.149 18.969-126.068 49.932l50.754 50.754c10.08 10.08 2.941 27.314-11.313 27.314H24c-8.837 0-16-7.163-16-16V38.627c0-14.254 17.234-21.393 27.314-11.314l49.372 49.372C129.209 34.136 189.552 8 256 8c136.81 0 247.747 110.78 248 247.531zm-180.912 78.784l9.823-12.63c8.138-10.463 6.253-25.542-4.21-33.679L288 256.349V152c0-13.255-10.745-24-24-24h-16c-13.255 0-24 10.745-24 24v135.651l65.409 50.874c10.463 8.137 25.541 6.253 33.679-4.21z"

+ 75 - 11
src/PicView.Avalonia/Views/MainView.axaml.cs

@@ -1,13 +1,14 @@
 using System.Runtime.InteropServices;
 using Avalonia;
 using Avalonia.Controls;
-using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Input;
+using Avalonia.Media;
 using Avalonia.Platform.Storage;
+using Avalonia.Styling;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
-using ReactiveUI;
+using PicView.Core.Extensions;
 
 namespace PicView.Avalonia.Views;
 
@@ -19,22 +20,85 @@ public partial class MainView : UserControl
         // TODO add visual feedback for drag and drop
         //AddHandler(DragDrop.DragOverEvent, DragOver);
         AddHandler(DragDrop.DropEvent, Drop);
-        GotFocus += delegate
+        
+        GotFocus += CloseTitlebarIfOpen;
+        PointerPressed += CloseTitlebarIfOpen;
+        
+        MainContextMenu.Opened += OnMainContextMenuOpened;
+    }
+    
+    private void CloseTitlebarIfOpen(object? sender, EventArgs e)
+    {
+        if (UIHelper.GetEditableTitlebar.IsOpen)
+        {
+            UIHelper.GetEditableTitlebar.CloseTitlebar();
+        }
+    }
+
+    private void OnMainContextMenuOpened(object? sender, EventArgs e)
+    {
+        if (DataContext is not MainViewModel vm)
         {
-            if (UIHelper.GetEditableTitlebar.IsOpen)
+            return;
+        }
+
+        var count = FileHistoryNavigation.GetCount();
+        if (RecentFilesCM.Items.Count < count)
+        {
+            for (var i = RecentFilesCM.Items.Count; i < count; i++)
             {
-                UIHelper.GetEditableTitlebar.CloseTitlebar();
+                AddOrReplaceMenuItem(i, vm, isReplace: false);
             }
-        };
-        PointerPressed += delegate
+        }
+        else
         {
-            if (UIHelper.GetEditableTitlebar.IsOpen)
+            for (var i = 0; i < count; i++)
             {
-                UIHelper.GetEditableTitlebar.CloseTitlebar();
+                AddOrReplaceMenuItem(i, vm, isReplace: true);
             }
+        }
+    }
+
+    private void AddOrReplaceMenuItem(int index, MainViewModel vm, bool isReplace)
+    {
+        if (!Application.Current.TryGetResource("LogoAccentColor", ThemeVariant.Default, out var secondaryAccentColor))
+        {
+            return;
+        }
+
+        var secondaryAccentBrush = new SolidColorBrush((Color)(secondaryAccentColor ?? Brushes.Yellow));
+        var fileLocation = FileHistoryNavigation.GetFileLocation(index);
+        var selected = vm.ImageIterator?.CurrentIndex == vm.ImageIterator?.ImagePaths.IndexOf(fileLocation);
+        var header = Path.GetFileNameWithoutExtension(fileLocation);
+        header = header.Length > 60 ? header.Shorten(60) : header;
+        
+        var item = new MenuItem
+        {
+            Header = header,
+        };
+
+        if (selected)
+        {
+            item.Foreground = secondaryAccentBrush;
+        }
+        
+        item.Click += async delegate
+        {
+            await NavigationHelper.LoadPicFromStringAsync(fileLocation, vm).ConfigureAwait(false);
         };
+        
+        ToolTip.SetTip(item, fileLocation);
+
+        if (isReplace)
+        {
+            RecentFilesCM.Items[index] = item;
+        }
+        else
+        {
+            RecentFilesCM.Items.Insert(index, item);
+        }
     }
-    
+
     private async Task Drop(object? sender, DragEventArgs e)
     {
         if (DataContext is not MainViewModel vm)
@@ -56,4 +120,4 @@ public partial class MainView : UserControl
             // TODO Open each file in a new window if the setting to open in the same window is false
         }
     }
-}
+}