CommandsSampleExtension.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using PixiEditor.Extensions.CommonApi.Commands;
  2. using PixiEditor.Extensions.Sdk;
  3. namespace Sample8_Menu;
  4. public class CommandsSampleExtension : PixiEditorExtension
  5. {
  6. /// <summary>
  7. /// This method is called when extension is loaded.
  8. /// All extensions are first loaded and then initialized. This method is called before <see cref="OnInitialized"/>.
  9. /// </summary>
  10. public override void OnLoaded()
  11. {
  12. }
  13. /// <summary>
  14. /// This method is called when extension is initialized. After this method is called, you can use Api property to access PixiEditor API.
  15. /// </summary>
  16. public override void OnInitialized()
  17. {
  18. // A good practice is to use localization keys instead of hardcoded strings.
  19. // And add them to the localization file. Check Sample2_LocalizationSample for more information.
  20. CommandMetadata firstCommand = new CommandMetadata("Loggers.WriteHello");
  21. firstCommand.DisplayName = "Write Hello"; // can be localized
  22. firstCommand.Description = "Writes Hello to the log"; // can be localized
  23. // Either an icon key (https://github.com/PixiEditor/PixiEditor/blob/master/src/PixiEditor.UI.Common/Fonts/PixiPerfectIcons.axaml)
  24. // or unicode character
  25. firstCommand.Icon = "icon-terminal";
  26. firstCommand.MenuItemPath = "AWESOME_LOGGER/Write Hello"; // AWESOME_LOGGER is taken from localization, same can be done for the rest
  27. firstCommand.Shortcut = new Shortcut(Key.H, KeyModifiers.Control | KeyModifiers.Alt);
  28. Api.Commands.RegisterCommand(firstCommand, () => { Api.Logger.Log("Hello from the command!"); });
  29. int clickedCount = 0;
  30. CommandMetadata secondCommand = new CommandMetadata("Loggers.WriteClickedCount");
  31. secondCommand.DisplayName = "Write Clicked Count";
  32. secondCommand.Description = "Writes clicked count to the log";
  33. secondCommand.Icon = "icon-terminal";
  34. secondCommand.MenuItemPath = "EDIT/Write Clicked Count"; // append to EDIT menu
  35. secondCommand.Order = 1000; // Last
  36. secondCommand.Shortcut = new Shortcut(Key.C, KeyModifiers.Control | KeyModifiers.Alt);
  37. Api.Commands.RegisterCommand(secondCommand, () =>
  38. {
  39. clickedCount++;
  40. Api.Logger.Log($"Clicked {clickedCount} times");
  41. });
  42. Api.Commands.InvokeCommand("PixiEditor.File.New");
  43. if (Api.Commands.CommandExists("yourCompany.Samples.CommandLibrary:PrintHelloWorld"))
  44. {
  45. Api.Commands.InvokeCommand("yourCompany.Samples.CommandLibrary:PrintHelloWorld");
  46. }
  47. if (Api.Commands.CommandExists("yourCompany.Samples.CommandLibrary:PrintHelloWorldFamily"))
  48. {
  49. Api.Commands.InvokeCommand("yourCompany.Samples.CommandLibrary:PrintHelloWorldFamily");
  50. }
  51. if (Api.Commands.CommandExists("yourCompany.Samples.CommandLibrary:PrintHelloWorldPrivate"))
  52. {
  53. // This will log an error.
  54. Api.Commands.InvokeCommand("yourCompany.Samples.CommandLibrary:PrintHelloWorldPrivate");
  55. }
  56. if (Api.Commands.CommandExists("yourCompany.Samples.CommandLibrary:PrintHelloWorldExplicit"))
  57. {
  58. Api.Commands.InvokeCommand("yourCompany.Samples.CommandLibrary:PrintHelloWorldExplicit");
  59. }
  60. }
  61. }