2

I'm writing a package for a custom component. This package allows the user/developer to either include numerous pre-written units in their project which are explicitly designed to work with this custom control, or write their own to do the same as one of the existing ones. Think of it like a "plugin" where code is implemented in its own unit, which inherits a particular object and does custom implementation of that common object.

Each "plugin" unit has an initialization clause at the bottom which registers this class with a global list. So long as that unit is "used" anywhere in the project, it gets registered into a global list, which this custom control makes use of to populate a list of all possible "plugin" units.

For example, this custom control has a property:

property PluginIndex: Integer read FPluginIndex write SetPluginIndex;

Internally, this index corresponds with one of the "plugins" which was registered from within the initialization section of its corresponding unit.

These "plugins" are registered via a global object which is instantiated like this upon first use:

function Plugins: TMyPluginList;

implementation

var
  _Plugins: TMyPluginList;

function Plugins: TMyPluginList;
begin
  if _Plugins= nil then
    _Plugins:= TMyPluginList.Create;
  Result:= _Plugins;
end;

The difficulty is that, since the control is implemented within a package (and installed into IDE), yet the "plugin" units are "used" from within another project, the package doesn't pick up the presence of these units, and therefore this custom control is also unable to access the global list, since it's in an entirely different context.

When interacting with this control on the form in design-time, if I set this PluginIndex in design-time, it fails because the control hasn't had any "plugins" register themselves in the global list. But, in run-time, assigning that same property works just fine.

How do I do this in a way that my application which uses this custom control (which relies on client-uses of each "plugin" unit) is able to actually detect these units used?


EDIT

The end goal of what I'm doing is to allow this single custom control to be simply dropped on the form, then have specific units "used" in that same form, and finally on this single custom control instance, choose the index of which of those "plugins" to actually use at any given time. I'm trying to encapsulate and automate as much of the process as possible, making it easier for a developer to simply drop this custom-control and not have to write a bunch of other code to make it work.


EDIT

I realize that this may be interpreted as these "plugin" units being a part of the package. That is not the case. Although currently I have no choice but to add these "plugin" units to the package in order to make design-time work right, I don't want it to be a part of the package, and rather up to the developer using this custom control. They can add a combination of pre-made "plugin" units as well as custom-made ones, simply by "using" them in their application.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • Probably they'd use a descendant of your control in their own package. – Sertac Akyuz Sep 27 '19 at 03:05
  • I think that only option for the IDE to be avare of when these plugins of yours are being used in a project is to wrap them into a nonvisual component which is then added to some list that is also accessible from the IDE through published property. – SilverWarior Sep 27 '19 at 07:18
  • @SilverWarior That's exactly what I'm doing, each plugin is a non-visual object which is inherited and implemented in various units - which then get "used" from within an application. – Jerry Dodge Sep 27 '19 at 13:08
  • Yeah but you have declared your `TMyPluginList` just as global variable. IDE Design-time interface doesn't have access to global variables as they exist only at runtime. You need to make your list accessible though a published property of some form or component or perhaps even better of a custom made data module. – SilverWarior Sep 27 '19 at 19:07
  • The actual project if anyone wants to see exactly how I'm going about this: https://github.com/djjd47130/JDVisuals – Jerry Dodge Sep 27 '19 at 20:33
  • As said by SilverWarior, one of the drawbacks of the Delphi IDE is the fact that it requires packages (precompiled code) to let things (like published properties) work inside the IDE. But you could try to create a component, which has a custom property editor, which can parse your projects code, so it allows you to select specific objct specifications inside your projects code... – R. Hoek Sep 29 '19 at 20:18

0 Answers0