Skip to main content

Lib: UI Extender




A FoundryVTT module library that adds easy ways to extend the base Foundry UI.

Overview​

Lib: UI Extender lets a module add HUD buttons, scene controls, scene control tools, sidebar directories, and settings buttons by registering them, rather than by wrapping core methods itself. Registrations all go through the library, so several modules can extend the same part of the interface without conflicting.

This is a library. Install it alongside a module that requires it; on its own it does nothing.

Features​

  • Easily add new HUD controls to tokens, tiles, or drawings
  • Easily add new scene controls, each with its own set of tools
  • Easily add new scene control tools to any layer controls (token, tiles, drawings, walls, etc.)
  • Easily add new sidebar directories
  • Easily add a button to the Settings sidebar that opens your module's settings
  • Fully typed library included in repository for Typescript projects

Settings​

MenuDescription
RegistrationView details of the HUD buttons, scene controls, directories, and settings buttons that modules have registered.
info

This menu is restricted to Game Masters. It is read-only and exists for debugging - nothing in it changes how the library behaves.

The Registration Config window

Usage​

To use this in your own module, register your UI elements inside the uiExtender.init hook, using the uiExtender instance it provides:

Hooks.once("uiExtender.init", (uiExtender) => {
uiExtender.registerSceneControlTool({ ... });
});
warning

Register everything inside the uiExtender.init hook. UI Extender creates the registered elements during Foundry's setup hook, so anything registered after that, such as in a setup or ready hook, is ignored.

info

Everything will be ready to render when the Foundry setup hook is complete.

tip

Check the Migration Guides for details on updating a module between Foundry Versions.

Hooks​

Lib: DFreds UI Extender provides a few hooks.

  • uiExtender.init - This is called once on Foundry's init hook. Register all of your controls here, using the provided instance of uiExtender. Registrations made after this hook are ignored.
  • uiExtender.setup - This is called once on Foundry's setup hook. At this point, all controls will be created and available when they render.

API Methods​

Currently, these are the supported API methods:

Register Scene Control​

A scene control is a button in the scene controls with its own set of tools, such as the Tokens or Walls controls. Under the hood, this uses the getSceneControlButtons hook.

registerSceneControl(input: SceneControlInput)

For the type information for SceneControlInput, look at the types defined in the repository here.

  • before and after place the scene control next to another one by name, such as "walls". before takes precedence over after, which takes precedence over control.order. If the named scene control does not exist, the next option is used, then the end of the scene controls.
  • gmOnly shows the scene control only to GM users.
  • control.layer is the canvas layer that is activated with the scene control.
  • control.tools is a list. A tool without an order is placed by its position in the list.

Tools can be added to a registered scene control with registerSceneControlTool, using the scene control's name.

An example:

Hooks.once("uiExtender.init", (uiExtender) => {
uiExtender.registerSceneControl({
moduleId: MODULE_ID,
after: "walls",
gmOnly: true,
control: {
name: "myControl",
title: "My Control",
icon: "fas fa-robot",
layer: "walls",
tools: [
{ name: "mySelect", title: "Select", icon: "fas fa-hand-pointer" },
{
name: "myButton",
title: "Say Hello",
icon: "fas fa-bell",
button: true,
onChange: () => ui.notifications.info("Hello"),
},
],
},
});
});

Register Scene Control Tool​

A scene control tool is a button that is located on a specific layer. Under the hood, this uses the getSceneControlButtons hook.

A scene control tool added by DFreds Clipboard

registerSceneControlTool(input: SceneControlToolInput)

For the type information for SceneControlToolInput, look at the types defined in the repository here.

An example:

export function mySampleModule() {
Hooks.once("uiExtender.init", (uiExtender) => {
uiExtender.registerSceneControlTool({
moduleId: MODULE_ID,
name: "tokens",
tool: {
name: "testing-button",
title: "DFreds Test Button",
icon: "fas fa-robot",
button: true,
order: 2,
onChange: (event, active) => {
console.log("onChange", event, active);
ui.notifications.info(`You clicked me! Active: ${active}`);
},
},
});
}
warning

Passing a tool to registerSceneControl is deprecated since v2.7.0 and will stop working in v3.0.0. It still adds the tool, but logs a compatibility warning. Use registerSceneControlTool instead.

Register HUD Button​

A HUD button is a button located on a specific item on the canvas when you right click. Under the hood, this uses the render${type}HUD hook.

A token HUD button added by DFreds Status Effects

registerHudButton(input: HudButtonInput)

For the type information for HudButtonInput, look at the types defined in the repository here.

Set nativeDom: true so the handlers get native DOM elements and events. Without it, the handlers get jQuery objects, which are deprecated and will be removed in v3.0.0. See the Migration Guides for details.

Some examples:

export function mySampleModule() {
Hooks.once("uiExtender.init", (uiExtender) => {
uiExtender.registerHudButton({
moduleId: MODULE_ID,
hudType: "token",
nativeDom: true,
tooltip: "Show Art Button",
icon: `<i class="fas fa-image fa-fw"></i>`,
location: "div.left",
onClick: (
_event: MouseEvent,
_button: HTMLButtonElement,
_token: any,
) => {
console.log("clicked");
},
onRightClick: (
_event: MouseEvent,
_button: HTMLButtonElement,
_token: any,
) => {
console.log("right clicked");
},
});

uiExtender.registerHudButton({
moduleId: MODULE_ID,
hudType: "tile",
nativeDom: true,
tooltip: "Show Art Button",
icon: `<i class="fas fa-image fa-fw"></i>`,
location: "div.left",
onClick: (
_event: MouseEvent,
_button: HTMLButtonElement,
tile: any,
) => {
new ImagePopout(tile.texture, {
title: "Tile Image",
shareable: true,
}).render(true);
},
onRightClick: (
_event: MouseEvent,
_button: HTMLButtonElement,
tile: any,
) => {
new ImagePopout(tile.texture, {
title: "Tile Image",
shareable: true,
}).render(true);
},
});

uiExtender.registerHudButton({
moduleId: MODULE_ID,
hudType: "drawing",
nativeDom: true,
tooltip: "Say Hi",
icon: `<i class="fas fa-robot fa-fw"></i>`,
location: "div.right",
onClick: (
_event: MouseEvent,
_button: HTMLButtonElement,
drawing: any,
) => {
ui.notifications.info(
`Hello from drawing ${drawing.fillColor}`,
);
},
});
})
}

Register Sidebar Directory​

A sidebar directory is a button located on the sidebar.

A sidebar directory added by DFreds Convenient Effects

registerDirectory(input: DirectoryInput)

For the type information for DirectoryInput, look at the types defined in the repository here.

Some examples:

const { AbstractSidebarTab } = foundry.applications.sidebar;
const { HandlebarsApplicationMixin } = foundry.applications.api;

// An application must extend AbstractSidebarTab
class SampleApplication extends HandlebarsApplicationMixin(AbstractSidebarTab) {
static DEFAULT_OPTIONS = {
window: {
title: "Sample",
},
};

static PARTS = {
sample: {
template: `modules/${MODULE_ID}/templates/sample.hbs`,
},
};

static tabName = "sample";
}

export function mySampleModule() {
Hooks.once("uiExtender.init", (uiExtender) => {
uiExtender.registerDirectory({
moduleId: MODULE_ID,
id: "sample",
tooltip: "Sample",
icon: "fas fa-robot",
before: "settings", // directly above the settings directory - takes precedence over `after` and `order`
after: "items", // directly below the items directory - takes precedence over `order`
order: 1, // positional index, resolved after other modules have inserted theirs

applicationClass: SampleApplication,
});
})
}

Register Settings Button​

A settings button is a button in the Settings sidebar tab that opens one of your module's applications. All registered buttons appear together in a Module Settings section, which sits between the "Settings and Configuration" and "Help and Documentation" sections. The section only shows up if at least one module has registered a button.

Buttons are listed alphabetically by name, so the order does not change as players enable and disable modules.

registerSettingsButton(input: SettingsButtonInput)

For the type information for SettingsButtonInput, look at the types defined in the repository here.

An example:

const { ApplicationV2, HandlebarsApplicationMixin } = foundry.applications.api;

class MySettingsApplication extends HandlebarsApplicationMixin(ApplicationV2) {
static DEFAULT_OPTIONS = {
window: {
title: "My Module Settings",
},
};

static PARTS = {
settings: {
template: `modules/${MODULE_ID}/templates/settings.hbs`,
},
};
}

export function mySampleModule() {
Hooks.once("uiExtender.init", (uiExtender) => {
uiExtender.registerSettingsButton({
moduleId: MODULE_ID,
name: "My Module Settings", // can also be a localization key
icon: "fa-solid fa-robot",
gmOnly: true, // optional - only show the button to Game Masters
applicationClass: MySettingsApplication,
});
})
}

The application is constructed with no arguments and rendered each time the button is clicked.

Handlebar Helpers​

A few handlebar helpers have also been registered to make common use cases easier.

Compare​

This will help in conditionally rendering some HTML depending on two values and the operator provided.

Operators include:

  • ==
  • ===
  • !=
  • !==
  • <
  • >
  • <=
  • >=
  • typeof
{{#compare myDataString.length ">" "the string I want"}}
<p>myDataString is longer than the string I want!</p>
{{else}}
<p>myDataString is equal to or shorter than the string I want!</p>
{{/compare}}
{{#compare "Test" "Test"}}
Default comparison of "==="
{{/compare}}

Is GM​

This will help in conditionally rendering some HTML depending on if the user is a GM or not.

{{#if (isGm)}}
<button class="view-backups">
<i class="fas fa-arrow-rotate-left"></i>
My GM Button
</button>
{{/if}}

Strip HTML​

This will remove any HTML elements from a provided string.

<h4><a title="{{stripHtml myHtmlString}}">Some link</a></h4>