Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request: Auto-generate Create buttons for added variable types #2256

Closed
seldomU opened this issue Feb 4, 2019 · 2 comments
Closed

request: Auto-generate Create buttons for added variable types #2256

seldomU opened this issue Feb 4, 2019 · 2 comments

Comments

@seldomU
Copy link
Contributor

seldomU commented Feb 4, 2019

I'd like to have a method workspace.registerType(typeName, typeNameMsgKey) to announce my custom types. The typed variable category would then generate a "Create variable of type X" button for each registered type.

Motivation
Since the variable category lives in blockly core, I can't add create-buttons to it. If I understand the docs correctly, it's suggested to put create-buttons for my variable types in other categories, but I'd prefer to have all things variables in one place. In a scenario where the user is allowed to add extension-categories to her toolbox, the problem is amplified: if two extensions add the same type, they would both have to include a create-button for it.

With type-registration, the variable flyout would get populated from registered types. Something like this:

Blockly.VariablesDynamic.flyoutCategory = function(workspace) {
  
  var xmlList = [];

  for(let variableType of workspace.getVariableTypes()){

    // variableTypes would look something like this by default
    // [
    //   { name: 'String', msgKey: TYPENAME_STRING},
    //   { name: 'Number', msgKey: TYPENAME_NUMBER},
    //   { name: 'Colour', msgKey: TYPENAME_COLOUR},
    //   { name: 'Bool', msgKey: TYPENAME_BOOL}
    // ];
    
    let callbackKey = 'CREATE_VARIABLE_' + variableType.name;
    let buttonLabel = Blockly.Msg.NEW_VARIABLE_OF_TYPE + " " + Blockly.Msg[variableType.msgKey];
    
    // add a button for this type
    var button = goog.dom.createDom('button');
    button.setAttribute('text', buttonLabel);
    button.setAttribute('callbackKey', callbackKey);
    xmlList.push(button);

    // register a callback for this type's button
    workspace.registerButtonCallback(callbackKey, (button) => {
        Blockly.Variables.createVariableButtonHandler(button.getTargetWorkspace(), null, variableType.name)
      } 
    );
  }

  var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
  xmlList = xmlList.concat(blockList);
  return xmlList;
};

Does that make sense?

@RoboErikG
Copy link
Contributor

The recommendation is actually to replace the variables flyout with a custom one. This doesn't require modifying core as there are APIs for adding your own dynamic flyouts.

@RoboErikG
Copy link
Contributor

One more note: #1606 is what we think should actually be used for more than 3 variable types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants