简体   繁体   中英

How to add toggle menu to 'view/title' with VSCode extension?

How to add toggle menu to 'view/title' with VSCode extension?

Like "✓ Open Editors".

I'd really appreciate it if you could show me the sample code or an example.

在此处输入图像描述

在此处输入图像描述

I want to change 'Minify' to '✓ Minify'!

We have no control of the space in front of the menu item. And we can't use theme icons in the command title. The icon of a command is used when the menu entry is used in the group navigation and may another.

Simulate a checkmark with a Unicode character.

Create 2 menu entries, one with and one without the checkmark and use a when with a context variable to show one of the menu entries.

    "activationEvents": [
        "onCommand:myext.minify",
        "onCommand:myext.restore"
    ],
    "contributes": {
        "commands": [
          {
            "command": "myext.minify",
            "title": "  Minify"
          },
          {
            "command": "myext.restore",
            "title": "✓ Minify"
          }
        ],
        "menus": {
          "view/title": [
            {
              "command": "myext.minify",
              "when": "view == krews && !myext:isMinified"
            },
            {
              "command": "myext.restore",
              "when": "view == krews && myext:isMinified"
            }
          ]
        }
    }

Set a context variable with the state of the checkmark.

    const setMinifyContext = (isMinified) => {
      vscode.commands.executeCommand('setContext', 'myext:isMinified', isMinified);
    };
    context.subscriptions.push(
      vscode.commands.registerCommand('myext.minify', () => {
        setMinifyContext(true);
      })
    );
    context.subscriptions.push(
      vscode.commands.registerCommand('myext.restore', () => {
        setMinifyContext(false);
      })
    );
    setMinifyContext(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM