Skip to content
Alen Pelin edited this page Jul 2, 2015 · 1 revision

← Developer Center

Plugin Engine

Description

Since 1.2 release SIM has plugin engine that can be utilized for extending functionality in a good manner. Detailed documentation is in progress, but by a meanwhile you can use Plugin Template Project.

Example

Plugin.xml
<plugin version="1.3">
  <init>
    <processor type="SIM.Tool.Plugins.Demo.AppInitialize, SIM.Tool.Plugins.Demo" mode="sync" />
    <processor type="SIM.Tool.Plugins.Demo.AppInitializeAsync, SIM.Tool.Plugins.Demo" mode="async" />
  </init>
  <mainWindow>
    <loaded>
      <processor type="SIM.Tool.Plugins.Demo.MainWindowLoaded, SIM.Tool.Plugins.Demo" />
    </loaded>
    <ribbon>
      <tab name="Tools">
        <group name="Plugins">
          <button label="Demo Button 1" largeImage="/Images/demo.png, SIM.Tool.Plugins.Demo" type="SIM.Tool.Plugins.Demo.DemoButton, SIM.Tool.Plugins.Demo" />
          <button label="Demo Button 2" largeImage="/Images/demo.png, SIM.Tool.Plugins.Demo" type="SIM.Tool.Plugins.Demo.DemoButton, SIM.Tool.Plugins.Demo" width="80" />
        </group>
      </tab>
    </ribbon>
    <contextMenu>
      <item header="Demo Menu Item" image="/Images/demo.png, SIM.Tool.Plugins.Demo" type="SIM.Tool.Plugins.Demo.DemoButton, SIM.Tool.Plugins.Demo" />
    </contextMenu>
  </mainWindow>
</plugin>
Classes.cs
namespace SIM.Tool.Plugins.Demo
{
  public class AppInitialize : SIM.Tool.Base.Plugins.IInitProcessor
  {
    public void Process()
    {
      MessageBox.Show("This processor is being invoked before MainWindow is shown. This one behaves sync. After clicking OK it will do Thread.Sleep(5000) that would leed to app hang this time.");
      Thread.Sleep(5000);
      MessageBox.Show("Synchronous Thread.Sleep(5000) finished.");
    }
  }

  public class AppInitializeAsync : SIM.Tool.Base.Plugins.IInitProcessor
  {
    public void Process()
    {
      MessageBox.Show("This processor is being invoked before MainWindow is shown. This one behaves asynchronously. After clicking OK it will do Thread.Sleep(10000), but the app will behave normally.");
      Thread.Sleep(10000);
      MessageBox.Show("Asynchronous Thread.Sleep(10 000) finished.");
    }
  }

  public class MainWindowLoaded : SIM.Tool.Base.Plugins.IMainWindowLoadedProcessor
  {
    public void Process(Window mainWindow)
    {
      // The window already loaded and ready for modifications if require
      mainWindow.Dispatcher.Invoke(new Action(() =>
      {
        mainWindow.MinWidth = 500;
        mainWindow.MaxWidth = 800;        
        mainWindow.Width = 500;
      }));
    }
  }

  public class DemoButton : SIM.Tool.Base.Plugins.IMainWindowButton
  {
    public bool IsEnabled(Window mainWindow, Instance instance)
    {
      // You have an access to MainWindow and selected Sitecore Instance (may be null if nothing is selected) here to evaluate some condition,
      // but keep in mind that this code must perform very fast to prevent UI from lagging - it is synchronous.
      return true;
    }

    public void OnClick(Window mainWindow, Instance instance)
    {
      // You have an access to MainWindow and selected Sitecore Instance (may be null if nothing is selected) here to evaluate some condition,
      // but keep in mind that this code must perform very fast to prevent UI from lagging - it is synchronous.
      // So if you need to do something long-running, please use WindowHelper.LongRunningTask.

      var message = "Doing nothing for 10 seconds" + (instance != null ? "for {0} instance".FormatWith(instance.Name) : string.Empty);
      WindowHelper.LongRunningTask(() => Thread.Sleep(10000), "Dumb waiting", mainWindow, message);
    }
  }
}
Clone this wiki locally