tiled icon indicating copy to clipboard operation
tiled copied to clipboard

Scripting: Allow scripts to create and use submenus

Open eishiya opened this issue 2 years ago • 0 comments

For those of us using many scripts, they often end up making our menus rather long, and some scripts add multiple related actions. It would make things tidier if scripts could put their actions in a submenu. For example, here's my Tileset menu now: image And here's what it could look like with submenus: image

Here's what the code for this could perhaps look like:

tiled.extendMenu("Tileset", [
   {submenu: "Copy/Paste Terrains", before: "TilesetProperties"}
]);
tiled.extendMenu("Copy/Paste Terrains", [
   {action: "copyTerrains"},
   {action: "pasteTerrains"},
   {action: "copyTerrainArrangements"},
   {action: "pasteTerrainArrangements"}
]);

This would require submenu names to be unique, however, which might be a downside, and would get messy for actions that want to support multiple languages. Perhaps instead menus should be registered similarly to actions, with separate display names and string IDs:

let submenu = tiled.registerSubmenu("copypasteTerrains", "Display Text"); //new tiled method specifically for creating submenus
submenu.text = "Copy/Paste Terrains"; //submenu text can be edited later, too, just like action text
tiled.extendMenu("Tileset", [
   {submenu: "copypasteTerrains", before: "TilesetProperties"}
]);
tiled.extendSubmenu("copypasteTerrains", [ //new tiled method specifically for adding to submenus
   {action: "copyTerrains"},
   {action: "pasteTerrains"},
   {action: "copyTerrainArrangements"},
   {action: "pasteTerrainArrangements"}
]);

It would be cool if the existing View submenus (Snapping, Views and Toolbars) had IDs that extendSubmenu could address, allowing script writers to put some actions there. For example, the Views and Toolbars submenu would be a good place to put an action to show/hide a Dialog that displays a to-do list, and Snapping could be a place to add a toggle to control the snapping behaviour of some custom tool. The File submenus should not be accessible to scripts. Attempting to modify the text of native menus, or to do anything with them beyond extending them, should throw an error.

There needs to be some mechanism for multiple scripts to use the same menu. Perhaps registerSubmenu should just return the existing menu if an existing ID is used? That way, scripts wouldn't need to somehow check if the menu they want already exists.

eishiya avatar Jul 02 '22 14:07 eishiya