question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add context menu support

See original GitHub issue

We’ve been working on a context menu support for GLSP and thought it might be useful to push this to Sprotty instead. What do you think?

The idea is that there is a generic IContextMenuService in Sprotty and depending on the context in which it is used (inside Theia, plain react app, or even Eclipse RCP) we bind a different implementation to this service that passes on the job of rendering the context menu to the “native” context menu implementation.

export interface IContextMenuService {
    show(items: MenuItem[], anchor: Anchor, onHide?: () => void): void;
}

The context menu providers in Sprotty could look like below (so they specify all details about a menu item and define which Sprotty actions are going to be executed on click). To decide whether an item is enabled or not, it can use the state of the model (e.g. selection as below):

@injectable()
export class DeleteContextMenuProvider implements IContextMenuProvider {
    getActions(root: Readonly<SModelRoot>, lastMousePosition?: Point): Promise<MenuItem[]> {
        const selectedElements = Array.from(root.index.all().filter(isSelected).filter(isDeletable));
        return Promise.resolve([
            {
                id: "delete",
                label: "Delete",
                sortString: "t",
                group: "a",
                actions: [new DeleteElementAction(selectedElements.map(e => e.id))],
                isEnabled: () => selectedElements.length > 0,
                isVisible: () => true,
                isToggled: () => false
            }
        ]);
    }
}

So far we’ve created an implementation for Theia based on Theia’s context menu support (see our work in progress implementation). Essentially it registers the commands, menu actions, and submenus in Theia as well as a special command handler that passes on the Sprotty actions to the action dispatcher. It currently supports grouping, submenus, icons, sorting, enablement, toggle, as well as putting menu items into a parent that some other provider defines.

This is how it looks like in action: Peek 2019-10-31 18-16

Before finalizing this change, I was wondering whether I should rather put it into Sprotty first and then do the GLSP integration (so that menu items can be provided also from a server). The work in progress implementation in GLSP is here: https://github.com/eclipsesource/graphical-lsp/commit/fa14b76a8d6c14d7f73f08169297bb942bec0b0f

Please let me know what you think! I’m happy to add this to Sprotty instead of GLSP only.

Happy Halloween! 😉

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
spoenemanncommented, Dec 3, 2019

Ok, let’s include native support for context menus. We should have proper documentation of all first-class features at some point…

0reactions
plangercommented, Nov 6, 2019

Hi Miro,

thanks for the feedback! I think it is good to raise the question; it has a point!

You are certainly right regarding the selection, the viewpoint, and the action dispatching. This could all easily be done outside of Sprotty.

However, I guess that context menu item providers might benefit from the internal model in some cases, e.g. to test against features of model elements (e.g. deletable). Theoretically, this could also be queried via request/response actions that are sent from outside though. I’m not sure whether it would make things simpler, but it would be doable.

The mouse listener should probably also still be registered as a Sprotty mouse listener, right? The code of the mouse listener doesn’t necessarily have to live in Sprotty, though.

Are you suggesting that we rather put the entire code into the sprotty-theia integration or keep it in the applications only?

In the end, I think it also depends on how relevant the cross-platform use case for Sprotty diagrams is (that is, if we want to be able to define a Sprotty diagram once, and use it with minimal effort also in different platforms, such as Theia, Eclipse RCP, VSCode). Because if this use case is very relevant, then it imho makes sense to have a common way of defining context menus inside Sprotty and have integration code (such as sprotty-theia) that is translates those menus into the native way of rendering menu items in different platforms. If this use case is not relevant, then I guess it should be fine to have the context menu as part of the integration code to provide a framework for defining those.

Leaving context menu support entirely to the application (as opposed to the platform) might lead to code duplication though, as the code for defining menu items, rendering them on a platform, and sending actions back to the action dispatcher on-click would probably always be the same for each platform (Theia, etc.).

I think I would tend to having “Sprotty-native support” for menu items to be able to specify them as part of the Sprotty diagram; but I do get your point to avoid having to cope with menu items, registries and the like in Sprotty. So I’d understand, if you prefer to keep it out and keep menu item support outside of Sprotty (in sprotty-theia or even GLSP only).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How add context menu item in Windows 11 - Microsoft Q&A
Hi How add my app (a MFC/VC++) menu item to Windows 11 modern context menu (not second menu displays by show more...).
Read more >
Context menus - Components - Human Interface Guidelines
A context menu provides access to functionality that's directly related to an onscreen item, without cluttering the interface.
Read more >
Context menu items - Mozilla - MDN Web Docs
You can add plain menu items, checkbox items, radio button groups, and separators to menus. Once a context menu item has been added...
Read more >
Extending the Context Menu and Share Dialog in Windows 11
Windows 11 brings refinement to contextual file operations in the right-click context menu and share dialog. These are extensible by apps, ...
Read more >
chrome.contextMenus - Chrome Developers
Use the chrome.contextMenus API to add items to Google Chrome's context menu. You can choose what types of objects your context menu additions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found