Browse Source

Properly handle activations

Krzysztof Krysiński 2 months ago
parent
commit
724f45b209

+ 3 - 40
src/PixiEditor.MacOs/MacOperatingSystem.cs

@@ -10,11 +10,10 @@ public sealed class MacOperatingSystem : IOperatingSystem
     public string Name { get; } = "MacOS";
 
     public string AnalyticsId => "macOS";
-    
+
     public IInputKeys InputKeys { get; } = new MacOsInputKeys();
     public IProcessUtility ProcessUtility { get; } = new MacOsProcessUtility();
 
-    private List<Uri> activationUris;
 
     public string ExecutableExtension { get; } = string.Empty;
 
@@ -28,45 +27,9 @@ public sealed class MacOperatingSystem : IOperatingSystem
         ProcessUtility.ShellExecute(Path.GetDirectoryName(path));
     }
 
-    public bool HandleNewInstance(Dispatcher? dispatcher, Action<string, bool> openInExistingAction, IApplicationLifetime lifetime)
+    public bool HandleNewInstance(Dispatcher? dispatcher, Action<string, bool> openInExistingAction,
+        IApplicationLifetime lifetime)
     {
-        StringBuilder args = new StringBuilder();
-        
-        if(activationUris != null)
-        {
-            foreach (var uri in activationUris)
-            {
-                args.Append('"');
-                args.Append(uri.AbsolutePath);
-                args.Append('"');
-                args.Append(' ');
-            }
-        }
-        
-        dispatcher.Invoke(() => openInExistingAction(args.ToString(), true));
         return true;
     }
-
-    public void HandleActivatedWithFile(FileActivatedEventArgs fileActivatedEventArgs)
-    {
-        if(activationUris == null)
-        {
-            activationUris = [];
-        }
-        
-        foreach (var file in fileActivatedEventArgs.Files)
-        {
-           activationUris.Add(file.Path);
-        }
-    }
-
-    public void HandleActivatedWithUri(ProtocolActivatedEventArgs openUriEventArgs)
-    {
-        if(activationUris == null)
-        {
-            activationUris = [];
-        }
-        
-        activationUris.Add(openUriEventArgs.Uri);
-    }
 }

+ 0 - 2
src/PixiEditor.OperatingSystem/IOperatingSystem.cs

@@ -33,6 +33,4 @@ public interface IOperatingSystem
     public void OpenUri(string uri);
     public void OpenFolder(string path);
     public bool HandleNewInstance(Dispatcher? dispatcher, Action<string, bool> openInExistingAction, IApplicationLifetime lifetime);
-    public void HandleActivatedWithFile(FileActivatedEventArgs fileActivatedEventArgs);
-    public void HandleActivatedWithUri(ProtocolActivatedEventArgs openUriEventArgs);
 }

+ 19 - 4
src/PixiEditor/Initialization/ClassicDesktopEntry.cs

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Platform.Storage;
 using Avalonia.Threading;
 using Avalonia.Xaml.Interactivity;
 using PixiEditor.Extensions.Runtime;
@@ -48,13 +49,26 @@ internal class ClassicDesktopEntry
 
     private void ActivableOnActivated(object? sender, ActivatedEventArgs e)
     {
+        // TODO: Handle activation more generically. This only is handled by macos btw.
+        if (desktop.MainWindow is not MainWindow mainWindow) return;
         if (e.Kind == ActivationKind.File && e is FileActivatedEventArgs fileActivatedEventArgs)
         {
-            IOperatingSystem.Current.HandleActivatedWithFile(fileActivatedEventArgs);
+            foreach (var storageItem in fileActivatedEventArgs.Files)
+            {
+                string? file = storageItem.TryGetLocalPath();
+                if (file != null && File.Exists(file))
+                {
+                    mainWindow.DataContext.FileSubViewModel.OpenFromPath(file);
+                }
+            }
         }
         else if (e.Kind == ActivationKind.OpenUri && e is ProtocolActivatedEventArgs openUriEventArgs)
         {
-            IOperatingSystem.Current.HandleActivatedWithUri(openUriEventArgs);
+            var uri = openUriEventArgs.Uri;
+            if (uri.AbsolutePath.StartsWith("lospec-palette://"))
+            {
+                Dispatcher.UIThread.InvokeAsync(async () => await mainWindow.DataContext.ColorsSubViewModel.ImportLospecPalette(uri.AbsoluteUri));
+            }
         }
     }
 
@@ -90,7 +104,7 @@ internal class ClassicDesktopEntry
 
             return;
         }
-        
+
 #if !STEAM && !DEBUG
         if (!HandleNewInstance(Dispatcher.UIThread))
         {
@@ -119,7 +133,8 @@ internal class ClassicDesktopEntry
 
         NumberInput.AttachGlobalBehaviors += AttachGlobalShortcutBehavior;
 
-        ExtensionLoader extensionLoader = new ExtensionLoader([Paths.InstallDirExtensionPackagesPath, Paths.LocalExtensionPackagesPath], Paths.UserExtensionsPath);
+        ExtensionLoader extensionLoader = new ExtensionLoader(
+            [Paths.InstallDirExtensionPackagesPath, Paths.LocalExtensionPackagesPath], Paths.UserExtensionsPath);
         //TODO: fetch from extension store
         extensionLoader.AddOfficialExtension("pixieditor.founderspack",
             new OfficialExtensionData("supporter-pack.snk", AdditionalContentProduct.SupporterPack));

+ 6 - 7
src/PixiEditor/ViewModels/SubViewModels/ColorsViewModel.cs

@@ -166,7 +166,9 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>, IColorsHandler
 
     private async void OwnerOnStartupEvent()
     {
-        await ImportLospecPalette();
+        var args = StartupArgs.Args;
+        var lospecPaletteArg = args.FirstOrDefault(x => x.StartsWith("lospec-palette://"));
+        await ImportLospecPalette(lospecPaletteArg);
     }
 
     [Commands_Command.Basic("PixiEditor.Colors.OpenPaletteBrowser", "OPEN_PALETTE_BROWSER", "OPEN_PALETTE_BROWSER",
@@ -177,17 +179,14 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>, IColorsHandler
         PalettesBrowser.Open();
     }
 
-    private async Task ImportLospecPalette()
+    public async Task ImportLospecPalette(string? uri)
     {
-        var args = StartupArgs.Args;
-        var lospecPaletteArg = args.FirstOrDefault(x => x.StartsWith("lospec-palette://"));
-
-        if (lospecPaletteArg != null)
+        if (uri != null)
         {
             var browser = PalettesBrowser.Open();
 
             browser.IsFetching = true;
-            var palette = await LospecPaletteFetcher.FetchPalette(lospecPaletteArg.Split(@"://")[1].Replace("/", ""));
+            var palette = await LospecPaletteFetcher.FetchPalette(uri.Split(@"://")[1].Replace("/", ""));
             if (palette != null)
             {
                 if (LocalPalettesFetcher.PaletteExists(palette.Name))