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

Simplify Closure-sourced code for menus #3880

Merged
merged 30 commits into from May 7, 2020
Merged

Simplify Closure-sourced code for menus #3880

merged 30 commits into from May 7, 2020

Conversation

NeilFraser
Copy link
Member

@NeilFraser NeilFraser commented May 3, 2020

Bunch of changes relating to menus. Highlights:

  • Deleted a bunch of unused CSS rules and classes.
  • Put the menu code on a diet, so it only supports features that we actually use.
  • Improved menu UX with support for more keyboard navigation.
  • Remove Component as a dependency of Menu/MenuItem.

The `goog-menuitem-icon` and `goog-menuitem-noicon` classes are not present in Blockly.  Blockly doesn’t support the CSS compiler, so #noflip has no effect.  Shorten uncompressible warning string.
Also remove the “Copied from Closure” notes.  These were intended so that the CSS could be easily updated as the Closure Library evolved.  We are no longer linked to the Closure Library.
Blockly never uses the lazy-creation feature.  Remove for simplicity.
Previously, open playground.  Right-click on workspace.  Mouse-over “Add comment” (it highlights).  Mouse over “Download screenshot” (disabled option).  Mouse over “Add comment” (highlighting is lost).

Also remove `canHighlightItem` helper function.  In theory this helps abstract the concept of non-highlightable options.  But in practice it was only called in one of the several places that it should have been.  This was a false abstraction.
The JSDoc for `setHighlightedIndex` specifically states, “If another item was previously highlighted, it is un-highlighted.”  This is not what was implemented, but it should be.  This commit adds the un-highlighting, and removes all the calls previously required to correct this bug.
@NeilFraser NeilFraser changed the title Remove cargo-culted bloat from CSS Simplify Closure-sourced code for menus May 3, 2020
Real OS menus don’t wrap when one cursors off the top or bottom.

Also, replace the overly complicated helper function with a simple 1/-1 step value.
Remove unneeded sets to RTL on Menu (only MenuItem cares).
@samelhusseini samelhusseini self-assigned this May 4, 2020
Context menus only disposed properly when an option was clicked.  If they were dismissed by clicking outside the menu there was no disposal.  This might result in a memory leak.
Also un-extract (inject?) several now trivial functions.
Component is now only used by the category tree.
These were used by Menu/MenuItem.
@samelhusseini
Copy link
Contributor

@NeilFraser, looks like you're still working on this, let me know when it's ready.

@blockly
Copy link

blockly commented May 6, 2020 via email

element.id = this.getId();
this.setElementInternal(element);
element.id = Blockly.utils.IdGenerator.getNextUniqueId();
element.blocklyMenuItem = this; // Link DOM back to this data structure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly, I'm not a huge fan of this pattern, as it shortcuts typing. There is precedence for this pattern in Blockly (eg: tooltips), so not saying it has to be changed. But here are my thoughts on the pattern:

  • It makes it harder to debug for a someone new to the team / area, as there's hidden context (traditionally a DOM element doesn't have additional properties that are not DOM properties)
  • The closure typechecker is not complaining (currently) because strictTypeChecks are not on, but when we do eventually get there, this will introduce issues. There are ways around that, ie: Create a new type that subclasses HTMLElement, and adds a blocklyMenuItem of type MenuItem as a property, but its clunky.

That being said, if the perf benefit is worth it for you, then by all means keep as is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original Closure code maintained a database for efficient look-ups. But it was a lot of code and complexity undeserving for this task. The DOM-to-JS links were simple and efficient, but have the problems you cite. I've replaced this with an O(n) search of the menu's options. Shouldn't be a problem given that our menus/dropdowns aren't intended to have hundreds or thousands of items.

@samelhusseini samelhusseini added the breaking change Used to mark a PR or issue that changes our public APIs. label May 6, 2020
@samelhusseini
Copy link
Contributor

Adding api_change label to mention the deprecation of goog-menu CSS in the upcoming release.

The previous tags were inherited from Closure and don’t reflect current usage in the Blockly codebase.

The core/components/tree files are non-compliant in this regard, but I’m not going to update them since they need to be replaced and there’s no need to create an interim API change.
Copy link
Contributor

@samelhusseini samelhusseini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once travis / closure is happy

Performance is slower (O(n) rather than (O(1)), but ’n’ is the number of entries on the menu, so shouldn’t be more than a dozen or so.
Fixes a compile error (node != element).
Usually we avoid parentElement in Blockly.  That’s because it has very spotty behaviour with SVG.  But in this case we are in pure HTML.
@NeilFraser
Copy link
Member Author

Finally got the compiler happy. Nodes vs Elements are annoying.
Assuming you are ok with this PR as it is, do you prefer the 30 commits to be rebased or squashed?

@samelhusseini
Copy link
Contributor

samelhusseini commented May 7, 2020

Awesome, thanks! Lgtm.
Squashed please.

@NeilFraser NeilFraser merged commit a65afdc into develop May 7, 2020
@NeilFraser NeilFraser deleted the fraser-css branch May 7, 2020 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Used to mark a PR or issue that changes our public APIs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants