Browse Source

Implemented create node, create keyframe, close document, use tool and switch tool events

CPKreuz 1 year ago
parent
commit
2de0a4ffa7

+ 26 - 1
src/PixiEditor/Models/AnalyticsAPI/Analytics.cs

@@ -1,4 +1,7 @@
-using PixiEditor.Models.Files;
+using System.Reflection;
+using PixiEditor.ChangeableDocument.Changeables.Graph;
+using PixiEditor.Models.Files;
+using PixiEditor.Models.Handlers;
 using PixiEditor.Numerics;
 
 namespace PixiEditor.Models.AnalyticsAPI;
@@ -13,6 +16,28 @@ public static class Analytics
 
     internal static AnalyticEvent SendOpenFile(IoFileType fileType, long fileSize, VecI size) =>
         SendEvent(AnalyticEventTypes.OpenFile, ("FileType", fileType.PrimaryExtension), ("FileSize", fileSize), ("Width", size.X), ("Height", size.Y));
+
+    internal static AnalyticEvent SendCreateNode(Type nodeType) =>
+        SendEvent(AnalyticEventTypes.CreateNode, ("NodeType", nodeType.GetCustomAttribute<NodeInfoAttribute>()?.UniqueName));
+
+    internal static AnalyticEvent SendCreateKeyframe(int position, string type, int fps, int duration, int totalKeyframes) =>
+        SendEvent(
+            AnalyticEventTypes.CreateKeyframe,
+            ("Position", position),
+            ("Type", type),
+            ("FPS", fps),
+            ("Duration", duration),
+            ("TotalKeyframes", totalKeyframes));
+
+    internal static AnalyticEvent SendCloseDocument() => SendEvent(AnalyticEventTypes.CloseDocument);
+    
+    internal static AnalyticEvent SendOpenExample(string fileName) => SendEvent(AnalyticEventTypes.OpenExample, ("FileName", fileName));
+
+    internal static AnalyticEvent SendUseTool(IToolHandler? tool, VecD positionOnCanvas, VecD documentSize) =>
+        SendEvent(AnalyticEventTypes.UseTool, ("Tool", tool?.ToolName), ("Position", new VecD(documentSize.X / positionOnCanvas.X, documentSize.Y / positionOnCanvas.Y)));
+
+    internal static AnalyticEvent SendSwitchToTool(IToolHandler? newTool, IToolHandler? oldTool) =>
+        SendEvent(AnalyticEventTypes.SwitchTool, ("NewTool", newTool?.ToolName), ("OldTool", oldTool?.ToolName));
     
     private static AnalyticEvent SendEvent(string name, params (string, object)[] data) =>
         SendEvent(name, data.ToDictionary());

+ 12 - 9
src/PixiEditor/Models/AnalyticsAPI/AnalyticsPeriodicReporter.cs

@@ -42,16 +42,19 @@ public class AnalyticsPeriodicReporter
 
     public void AddEvent(AnalyticEvent value)
     {
-        _semaphore.Wait();
-
-        try
+        Task.Run(() =>
         {
-            _backlog.Add(value);
-        }
-        finally
-        {
-            _semaphore.Release();
-        }
+            _semaphore.Wait();
+
+            try
+            {
+                _backlog.Add(value);
+            }
+            finally
+            {
+                _semaphore.Release();
+            }
+        });
     }
 
     private async Task RunAsync()

+ 1 - 1
src/PixiEditor/Models/DocumentModels/DocumentUpdater.cs

@@ -504,7 +504,7 @@ internal class DocumentUpdater
             Document = (DocumentViewModel)doc,
             Internals = helper
         };
-        
+
         node.SetName(info.NodeName);
         node.SetPosition(info.Position);
         

+ 8 - 0
src/PixiEditor/ViewModels/SubViewModels/AnimationsViewModel.cs

@@ -1,6 +1,7 @@
 using Avalonia.Input;
 using ChunkyImageLib;
 using PixiEditor.AnimationRenderer.Core;
+using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.Commands.Attributes.Commands;
 using PixiEditor.Numerics;
@@ -38,6 +39,13 @@ internal class AnimationsViewModel : SubViewModel<ViewModelMain>
             frameToCopyFrom);
         
         activeDocument.Operations.SetActiveFrame(newFrame);
+
+        Analytics.SendCreateKeyframe(
+            newFrame,
+            "Raster",
+            activeDocument.AnimationDataViewModel.FrameRateBindable,
+            activeDocument.AnimationDataViewModel.FramesCount,
+            activeDocument.AnimationDataViewModel.AllKeyFrames.Count);
     }
     
     [Command.Basic("PixiEditor.Animation.DeleteKeyFrames", "DELETE_KEY_FRAMES", "DELETE_KEY_FRAMES_DESCRIPTIVE",

+ 3 - 0
src/PixiEditor/ViewModels/SubViewModels/IoViewModel.cs

@@ -6,6 +6,7 @@ using CommunityToolkit.Mvvm.Input;
 using PixiEditor.Models.Preferences;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.Extensions.CommonApi.UserPreferences.Settings.PixiEditor;
+using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.Models.Commands;
 using PixiEditor.Models.Commands.Commands;
 using PixiEditor.Models.Controllers;
@@ -162,6 +163,8 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
         drawingWithRight = args.Button == MouseButton.Right;
         Owner.ToolsSubViewModel.UseToolEventInlet(args.PositionOnCanvas, args.Button);
         activeDocument.EventInlet.OnCanvasLeftMouseButtonDown(args.PositionOnCanvas);
+
+        Analytics.SendUseTool(Owner.ToolsSubViewModel.ActiveTool, args.PositionOnCanvas, activeDocument.SizeBindable);
     }
 
     private bool HandleRightMouseDown()

+ 2 - 0
src/PixiEditor/ViewModels/SubViewModels/NodeGraphManagerViewModel.cs

@@ -1,4 +1,5 @@
 using Avalonia.Input;
+using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.ViewModels.Nodes;
 using PixiEditor.Models.Commands.Attributes.Commands;
 using PixiEditor.Models.Handlers;
@@ -36,6 +37,7 @@ internal class NodeGraphManagerViewModel : SubViewModel<ViewModelMain>
     public void CreateNode((Type nodeType, VecD pos) data)
     {
         Owner.DocumentManagerSubViewModel.ActiveDocument?.NodeGraph.CreateNode(data.nodeType, data.pos);
+        Analytics.SendCreateNode(data.nodeType);
     }
     
     [Command.Internal("PixiEditor.NodeGraph.ConnectProperties")]

+ 6 - 0
src/PixiEditor/ViewModels/SubViewModels/ToolsViewModel.cs

@@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Models.Preferences;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.Extensions.CommonApi.UserPreferences.Settings.PixiEditor;
+using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.Models.Commands.Attributes.Commands;
 using PixiEditor.Models.Commands.Attributes.Evaluators;
 using PixiEditor.Models.Controllers;
@@ -183,6 +184,11 @@ internal class ToolsViewModel : SubViewModel<ViewModelMain>, IToolsHandler
         {
             Owner.StylusSubViewModel.ToolSetByStylus = false;
         }
+
+        if (ActiveTool != null || LastActionTool != null)
+        {
+            Analytics.SendSwitchToTool(tool, LastActionTool);
+        }
     }
 
     public void SetTool(object parameter)

+ 2 - 0
src/PixiEditor/ViewModels/SubViewModels/WindowViewModel.cs

@@ -5,6 +5,7 @@ using System.Threading.Tasks;
 using Avalonia.Input;
 using CommunityToolkit.Mvvm.Input;
 using PixiDocks.Core.Docking;
+using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.Models.Commands;
 using PixiEditor.UI.Common.Fonts;
 using PixiEditor.ViewModels.Document;
@@ -129,6 +130,7 @@ internal class WindowViewModel : SubViewModel<ViewModelMain>
         var viewports = Viewports.Where(vp => vp.Document == viewport.Document).ToArray();
         if (viewports.Length == 1)
         {
+            Analytics.SendCloseDocument();
             return await Owner.DisposeDocumentWithSaveConfirmation(viewport.Document);
         }
 

+ 2 - 0
src/PixiEditor/Views/Windows/BetaExampleButton.axaml.cs

@@ -2,6 +2,7 @@
 using Avalonia.Controls;
 using CommunityToolkit.Mvvm.Input;
 using PixiEditor.Helpers.Extensions;
+using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.ViewModels;
 
 namespace PixiEditor.Views.Windows;
@@ -70,6 +71,7 @@ public partial class BetaExampleButton : UserControl
         CloseCommand.Execute(null);
         
         ViewModelMain.Current.FileSubViewModel.OpenRecoveredDotPixi(null, bytes);
+        Analytics.SendOpenExample(FileName);
     }
 
 }