Skip to content

How to create an addin

DamianSuess edited this page Jan 1, 2019 · 4 revisions

In this example, we'll focus on defining our add-in via assembly versus an XML-manifest.

We'll be taking apart a basic Pomodoro Timer project extends the SystemTray and PropertiesPage extension points.

Table of Contents

  1. Getting Started
  2. Defining your Add-in
    1. Mono.Addins.Addin
    2. Mono.Addins.AddinDependency
  3. SystemTray ExtensionPoint
  4. Preferences ExtensionPoint
    1. Preference Form

Getting Started

  1. Check-out the ToolsHub project and compile the code
  2. In the Addons folder, create a new Class Library (.NET Framework). Here we'll be naming ours PomodoroAddin.
  3. Set the Build's Output Path to the same as ToolsHub's (..\..\..\output\)
    • Since our project is immature, you don't get a fancy SDK yet

Defining your Add-in

Let's start by creating a class called, AddinAssembly.cs

// Define our add-in
[assembly: Mono.Addins.Addin(
  Namespace = "",
  Id = "PomodoroAddin",
  Category = "Tools",
  CompatVersion = "0.3",
  EnabledByDefault = true,
  Version = "0.3",
  Flags = Mono.Addins.Description.AddinFlags.None,
  Url = "https://github.com/xenoinc/ToolsHub")]

// Attach to add-in host
[assembly: Mono.Addins.AddinDependency("XenoInnovations.ToolsHub", "0.3")]

Mono.Addins.Addin

You only need to modify:

  • Id
  • Version
  • EnabledByDefault

Optional arguments:

  • Flags
  • Url

Required:

  • CompatVersion - Current version is, 0.3

Mono.Addins.AddinDependency

The only time you'll modify this is if the base version of ToolsHub changes and your add-in needs to support a higher framework version. Currently, we're using 0.3

SystemTray ExtensionPoint

Create another class called, SysTrayHandler.cs

namespace PomodoroAddin.Handlers
{
  using System.Collections.Generic;
  using System.Windows.Forms;
  using Xeno.ToolsHub.ExtensionModel;

  [Mono.Addins.Extension(
    NodeName = ExtensionName.SysTrayAddin,
    Path = ExtensionPath.SystemTray)]
  public class SysTrayHandler : Xeno.ToolsHub.ExtensionModel.SystemTray.SysTrayAddin
  {
    private PomodoroManager _pomodoro;

    public SysTrayHandler()
    {
      _pomodoro = new PomodoroManager();
      this.IsInitialized = true;
    }

    public override bool IsInitialized { get; }

    public override void Initialize()
    {
    }

    public override List<MenuItem> MenuItems()
    {
      return _pomodoro.MenuItems;
    }
  }
}

Preferences ExtensionPoint

Create a class called, PreferenceHandler

namespace PomodoroAddin.Handlers
{
  using System.Windows.Forms;
  using PomodoroAddin.Views;
  using Xeno.ToolsHub.ExtensionModel;

  [Mono.Addins.Extension(
    NodeName = ExtensionName.PreferencePageAddin,
    Path = ExtensionPath.PreferencePage)]
  public class PreferenceHandler : IPreferencePageExtension
  {
    private PreferencePage _page;

    public string Id { get; set; }

    public bool IsModified => _page.IsModified;

    public Form Page => _page;

    public string Title { get { return "Pomodoro"; } }

    public void InitializePage()
    {
      _page = new PreferencePage();
    }

    public void OnSave()
    {
      _page.OnSave();
    }
  }
}

Preference Form

Create a Windows Form called, PreferencePage which uses the interface, IPreferencePageForm

The IPreferencePageForm interface - though not required - makes sure that you don't forget anything when starting off.

namespace PomodoroAddin.Views
{
  using System;
  using System.Windows.Forms;
  using Xeno.ToolsHub.ExtensionModel;
  using Xeno.ToolsHub.Services;

  public partial class PreferencePage : Form, IPreferencePageForm
  {
    private const string AddinId = "PomodoroTimer";
    private const string KeyDuration = "Duration";
    private const string KeyShortBreak = "BreakShort";
    private const string KeyLongBreak = "BreakLong";

    public PreferencePage()
    {
      InitializeComponent();

      TxtDuration.Text = SettingsService.GetInt(AddinId, KeyDuration, 25).ToString();
      TxtBreakShort.Text = SettingsService.GetInt(AddinId, KeyShortBreak, 5).ToString();
      TxtBreakLong.Text = SettingsService.GetInt(AddinId, KeyLongBreak, 10).ToString();

      IsModified = false;
    }

    public bool IsModified { get; set; }

    public bool OnSave()
    {
      SettingsService.SetValue(AddinId, KeyDuration, TxtDuration.Text);
      SettingsService.SetValue(AddinId, KeyShortBreak, TxtBreakShort.Text);
      SettingsService.SetValue(AddinId, KeyLongBreak, TxtBreakLong.Text);

      return true;
    }

    private void PreferencePage_Load(object sender, EventArgs e)
    {
    }

    private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (!char.IsControl(e.KeyChar) &&
          !char.IsDigit(e.KeyChar) &&
          (e.KeyChar != '.'))
        e.Handled = true;
      else
        IsModified = true;

      // Only allow one decimal point
      if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
        e.Handled = true;
    }
  }
}