Ham Cockpit   

    Show / Hide Table of Contents

    Display Panel Demo Plugin

    The source code of this plugin is available here GitHub.

    This plugin allows the user to create an unlimited number of visual panels. Each panel has three buttons, R, G and B, that collectively set the background color of the panel.

    The design of this plugin is very similar to the one described in Creating Your First Plugin. One important difference is that the Settings object now has to store the settings of a variable number of panels.

    The settings of one panel are stored in an instance of the DisplayPanelSettings class:

    public class DisplayPanelSettings
      {
        public bool Red { get; set; }
        public bool Green { get; set; }
        public bool Blue { get; set; }
    
        public DisplayPanelSettings(bool red, bool green, bool blue)
        {
          Red = red;
          Green = green;
          Blue = blue;
        }
      }
    

    The Settings object, exposed via IPlugin.Settings as follows

    public object Settings { get => BuildSettings(); set => settings = value as Settings; }
    

    contains a list of DisplayPanelSettings objects, one per panel:

      public class Settings
      {
        public List<DisplayPanelSettings> panels = new List<DisplayPanelSettings>();
      }
    

    Note that panels in Settings is not user-editable because it is a field, not a property.

    The host assigns a value to IPlugin.Settings in two cases:

    • when it reads the saved settings on program startup;
    • when it saves the settings edited by the user.

    Since there are no user-editable properties in Settings, the setter of Settings is called only once, on program startup, and our plugin just stores the settings object for future use.

    After reading the Settings properties of all plugins, the host application re-creates its layout, with all docked and floating panels that were open in the last session. It calls IPlugin.CreatePanel for every panel that needs to be present in the layout. This is where we use the saved settings of the panels:

      public UserControl CreatePanel()
        {
          var new_panel = new DisplayPanel();
    
          if (settings.panels.Count > 0)
          {
            new_panel.Settings = settings.panels[0];
            settings.panels.RemoveAt(0);
          }
    
          panels.Add(new_panel);
          return new_panel;
        }
    

    The used settings object is deleted from the list, so that each re-created panel gets its own settings.

    After all settings are consumed, new panels are created with default settings.

    Back to top Generated by DocFX