Browse Source

Added Mask Example and examples caching

Krzysztof Krysiński 6 months ago
parent
commit
7e309a9a1d

BIN
src/PixiEditor/Data/BetaExampleFiles/Mask.pixi


+ 3 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -866,5 +866,7 @@
   "STRING_EDIT_IN_DEFAULT_APP": "Edit in default app",
   "STRING_OPEN_IN_FOLDER": "Open in folder",
   "DISCO_BALL_EXAMPLE": "Disco Ball",
-  "COLOR_SPACE": "Color Space"
+  "COLOR_SPACE": "Color Space",
+  "PHOTO_EXAMPLES": "Photo",
+  "MASK_EXAMPLE": "Mask"
 }

+ 3 - 3
src/PixiEditor/Views/Windows/BetaExampleButton.axaml

@@ -15,8 +15,8 @@
                 Command="{Binding OpenCommand, RelativeSource={RelativeSource AncestorType=windows1:BetaExampleButton}}"
                 x:Name="fileButton">
             <Grid Width="100" Height="100">
-                <visuals1:SurfaceControl
-                    Surface="{Binding BetaExampleFile.PreviewImage, RelativeSource={RelativeSource AncestorType=windows1:BetaExampleButton}}"
+                <visuals1:TextureControl
+                    Texture="{Binding BetaExampleFile.PreviewImage, RelativeSource={RelativeSource AncestorType=windows1:BetaExampleButton}}"
                     Margin="10"
                     Stretch="Uniform"
                     x:Name="image">
@@ -27,7 +27,7 @@
                             <Binding ElementName="image" Path="Width" />
                         </MultiBinding>
                     </ui:RenderOptionsBindable.BitmapInterpolationMode>
-                </visuals1:SurfaceControl>
+                </visuals1:TextureControl>
             </Grid>
         </Button>
 

+ 33 - 7
src/PixiEditor/Views/Windows/BetaExampleButton.axaml.cs

@@ -1,6 +1,7 @@
 using Avalonia;
 using Avalonia.Controls;
 using CommunityToolkit.Mvvm.Input;
+using Drawie.Backend.Core;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.ViewModels;
@@ -38,7 +39,7 @@ public partial class BetaExampleButton : UserControl
         get => GetValue(DisplayNameProperty);
         set => SetValue(DisplayNameProperty, value);
     }
-    
+
     public BetaExampleFile BetaExampleFile
     {
         get => GetValue(BetaExampleFileProperty);
@@ -47,33 +48,58 @@ public partial class BetaExampleButton : UserControl
 
     public AsyncRelayCommand OpenCommand { get; }
 
+    private static Dictionary<string, BetaExampleFile> exampleFilesCache = new();
+
     public BetaExampleButton()
     {
         OpenCommand = new AsyncRelayCommand(OpenExample);
-        
+
         InitializeComponent();
-        FileNameProperty.Changed.AddClassHandler((BetaExampleButton o, AvaloniaPropertyChangedEventArgs<string> args) => FileNameChanged(o, args));
+        FileNameProperty.Changed.AddClassHandler((BetaExampleButton o, AvaloniaPropertyChangedEventArgs<string> args) =>
+            FileNameChanged(o, args));
     }
 
     private static void FileNameChanged(BetaExampleButton sender, AvaloniaPropertyChangedEventArgs<string> e)
     {
+        if (e.OldValue.Value != null)
+        {
+            if (exampleFilesCache.ContainsKey(e.OldValue.Value))
+            {
+                var oldFile = exampleFilesCache[e.OldValue.Value];
+                oldFile.Dispose();
+
+                exampleFilesCache.Remove(e.OldValue.Value);
+            }
+        }
+
+        if (e.NewValue.HasValue == false || string.IsNullOrWhiteSpace(e.NewValue.Value))
+        {
+            return;
+        }
+
+        if (exampleFilesCache.ContainsKey(e.NewValue.Value))
+        {
+            sender.BetaExampleFile = exampleFilesCache[e.NewValue.Value];
+            return;
+        }
+
         sender.BetaExampleFile = new BetaExampleFile(e.NewValue.Value, sender.DisplayName);
+        exampleFilesCache.Add(e.NewValue.Value, sender.BetaExampleFile);
     }
-    
+
     private async Task OpenExample()
     {
         await using var stream = BetaExampleFile.GetStream();
-        
+
         var bytes = new byte[stream.Length];
         await stream.ReadExactlyAsync(bytes);
 
         Application.Current.ForDesktopMainWindow(mainWindow => mainWindow.Activate());
         CloseCommand.Execute(null);
-        
+
         ViewModelMain.Current.FileSubViewModel.OpenRecoveredDotPixi(null, bytes);
         ViewModelMain.Current.DocumentManagerSubViewModel.Documents[^1].Operations.UseSrgbProcessing();
         ViewModelMain.Current.DocumentManagerSubViewModel.Documents[^1].Operations.ClearUndo();
         Analytics.SendOpenExample(FileName);
     }
-
 }

+ 8 - 3
src/PixiEditor/Views/Windows/BetaExampleFile.cs

@@ -6,11 +6,11 @@ using PixiEditor.Parser;
 
 namespace PixiEditor.Views.Windows;
 
-public class BetaExampleFile
+public class BetaExampleFile : IDisposable
 {
     private readonly string resourcePath;
     
-    public Surface PreviewImage { get; }
+    public Texture PreviewImage { get; }
     
     public LocalizedString DisplayName { get; }
     
@@ -22,8 +22,13 @@ public class BetaExampleFile
         var stream = GetStream();
         var bytes = PixiParser.ReadPreview(stream);
 
-        PreviewImage = Surface.Load(bytes);
+        PreviewImage = Texture.Load(bytes);
     }
     
     public Stream GetStream() => AssetLoader.Open(new Uri(resourcePath));
+
+    public void Dispose()
+    {
+        PreviewImage.Dispose();
+    }
 }

+ 10 - 0
src/PixiEditor/Views/Windows/HelloTherePopup.axaml

@@ -374,6 +374,16 @@
                                                        CloseCommand="{Binding CloseCommand, RelativeSource={RelativeSource AncestorType=windows:HelloTherePopup}}" />
                         </StackPanel>
                     </ScrollViewer>
+
+                    <TextBlock ui:Translator.Key="PHOTO_EXAMPLES" Margin="0,8,0,2" HorizontalAlignment="Center"
+                               TextAlignment="Center" FontSize="18" FontWeight="SemiBold" />
+                    <ScrollViewer HorizontalAlignment="Center" VerticalScrollBarVisibility="Auto">
+                        <StackPanel Orientation="Horizontal">
+                            <windows:BetaExampleButton FileName="Mask.pixi" DisplayName="MASK_EXAMPLE"
+                                                       CloseCommand="{Binding CloseCommand, RelativeSource={RelativeSource AncestorType=windows:HelloTherePopup}}" />
+                        </StackPanel>
+                    </ScrollViewer>
+
                 </StackPanel>
             </ScrollViewer>