workspace sdk contextual actions - Genetec/DAP GitHub Wiki

About contextual actions

Contextual actions appear in right-click context menus throughout Security Desk and Config Tool. They provide context-sensitive operations for entities, tiles, maps, and other UI elements.

ContextualAction base class

Create a contextual action by inheriting from Genetec.Sdk.Workspace.ContextualAction.ContextualAction:

public class MyAction : ContextualAction
{
    public override Guid Id => new Guid("...");

    public override bool CanExecute(ContextualActionContext context) => true;

    public override bool Execute(ContextualActionContext context)
    {
        // Perform action
        return true; // Return true on success
    }
}

ContextualAction API

Member Type Description
Id Guid (abstract) Unique action identifier
Group Guid (virtual) Action group identifier
Priority int (virtual) Sort priority (lower values appear first)
Name string (virtual) Display name
Icon ImageSource (virtual) Menu icon
Workspace Workspace Workspace instance

Abstract methods

Method Returns Description
CanExecute(ContextualActionContext) bool Return true if action is available
Execute(ContextualActionContext) bool Perform the action, return true on success

Context types

The ContextualActionContext parameter indicates where the action was invoked. Check the context type to access relevant data.

ConfigurationContextualActionContext

Actions in Config Tool configuration panels.

Property Type Description
Page Page The currently active configuration page
SelectedEntities IList<Guid> GUIDs of selected entities

AddEntityContextualActionContext

Actions when adding new entities.

EntityBrowserContextualActionContext

Actions in entity browser dialogs and lists.

Property Description
SelectedEntities Currently selected entity GUIDs

MapContextualActionContext

Actions on map surfaces.

Property Type Description
MapId Guid The map GUID
Location GeoCoordinate Click location on map
SelectedEntity Guid First selected entity GUID
SelectedEntities IList<Guid> All selected entity GUIDs
EditMode bool Whether the map is in edit mode
ViewArea GeoBounds Current visible map area

TileContextualActionContext

Actions on tiles in Security Desk.

Property Type Description
State TileState The tile's current state

Access tile content through State.Content which returns a ContentGroup containing the displayed entity.

SystemStatusContextualActionContext

Actions in system status displays.

Registering actions

Register contextual actions using IContextualActionsService:

public class SampleModule : Module
{
    private MyAction m_action;

    public override void Load()
    {
        var service = Workspace.Services.Get<IContextualActionsService>();

        m_action = new MyAction();
        m_action.Initialize(Workspace);
        service.Register(m_action);
    }

    public override void Unload()
    {
        var service = Workspace.Services.Get<IContextualActionsService>();
        service.Unregister(m_action);
    }
}

Action groups

Group related actions together by creating a class that inherits from ContextualActionGroup:

public class MyActionGroup : ContextualActionGroup
{
    public override Guid Id => new Guid("...");

    public override string Name => "My Actions";

    public override int Priority => 100;
}

Register the group with the service:

var service = Workspace.Services.Get<IContextualActionsService>();
service.Register(new MyActionGroup());

You can also use predefined groups from the ContextualActionGroups static class:

  • ContextualActionGroups.Copy
  • ContextualActionGroups.Maintenance
  • ContextualActionGroups.Relation

Set your action's Group property to the group's ID:

public override Guid Group => MyActionGroupId;

Priority ordering

The Priority property controls menu ordering. Lower values appear before higher values. The default is int.MaxValue, placing actions at the end.

public override int Priority => 10; // Appears near top

Conditional availability

Use CanExecute to control when the action appears. Return false to hide the action from the menu entirely.

public override bool CanExecute(ContextualActionContext context)
{
    return context is ConfigurationContextualActionContext configContext
        && configContext.SelectedEntities.Select(Workspace.Sdk.GetEntity).All(ValidateEntity);
}

private bool ValidateEntity(Entity entity)
{
    return entity is CustomEntity customEntity && customEntity.CustomEntityType == MyCustomEntityTypeId;
}

Accessing context data

Cast the context to the appropriate type to access specific properties:

public override bool Execute(ContextualActionContext context)
{
    if (context is not ConfigurationContextualActionContext configContext)
        return false;

    IEnumerable<Entity> entities = configContext.SelectedEntities.Select(Workspace.Sdk.GetEntity).Where(ValidateEntity);

    foreach (Entity entity in entities)
    {
        // Process each selected entity
    }

    return true;
}

For a complete example, see the ContextualActionSample on GitHub.

⚠️ **GitHub.com Fallback** ⚠️