Remove extra menu separators by codebytere · Pull Request #11827 · electron/electron · GitHub
Skip to content

Remove extra menu separators#11827

Merged
ckerr merged 4 commits into
masterfrom
remove-leading-trailing-seps
Feb 5, 2018
Merged

Remove extra menu separators#11827
ckerr merged 4 commits into
masterfrom
remove-leading-trailing-seps

Conversation

@codebytere

@codebytere codebytere commented Feb 5, 2018

Copy link
Copy Markdown
Member

Removes leading and trailing menu separators and resolves #5869.

Tested manually on MacOS, Windows, and Linux

To-Do:

  • Add specs

@codebytere codebytere requested a review from a team February 5, 2018 04:41
Comment thread lib/browser/api/menu.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a little hard to scan. I think that a reduce would work wonders here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with felix this is a little hard to scan. I think the reasons are:

  • meetsDeleteConditions and toDelete are confusingly named, e.g. how can you meet delete conditions if type isn't separator?
  • visibleItems requires a little thought before you can tell how the loop will behave in edge cases

I'm not thinking functionally enough to see how a reduce would work cleanly here and would love enlightenment on this, no snark intended 😄

After poking around with a few approaches, the clearest I came up with was (untested):

function removeExtraSeparators(items) {
  // remove invisible items 
  items = items.filter(e => e.visible !== false)
  // fold adjacent separators together
  items = items.filter((e, idx, arr) => e.type !== 'separator' || idx==0 || arr[idx-1].type!=='separator')
  // remove edge separators
  return items.filter((e, idx, arr) => e.type !== 'separator' || (idx!=0 && idx!=arr.length-1))
}

Marking as Request Changes for cleanup to this section, but the above code is just a suggestion, not a requirement

Comment thread lib/browser/api/menu.js Outdated

return visibleItems.filter((el, idx, array) => {
const meetsDeleteConditions = !visiblePrev || idx === array.length - 1 || array[idx + 1].type === 'separator'
const toDelete = el.type === 'separator' && meetsDeleteConditions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with felix this is a little hard to scan. I think the reasons are:

  • meetsDeleteConditions and toDelete are confusingly named, e.g. how can you meet delete conditions if type isn't separator?
  • visibleItems requires a little thought before you can tell how the loop will behave in edge cases

I'm not thinking functionally enough to see how a reduce would work cleanly here and would love enlightenment on this, no snark intended 😄

After poking around with a few approaches, the clearest I came up with was (untested):

function removeExtraSeparators(items) {
  // remove invisible items 
  items = items.filter(e => e.visible !== false)
  // fold adjacent separators together
  items = items.filter((e, idx, arr) => e.type !== 'separator' || idx==0 || arr[idx-1].type!=='separator')
  // remove edge separators
  return items.filter((e, idx, arr) => e.type !== 'separator' || (idx!=0 && idx!=arr.length-1))
}

Marking as Request Changes for cleanup to this section, but the above code is just a suggestion, not a requirement

Comment thread spec/api-menu-spec.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another good edge case to test for would be [ sep, sep, a, b, c, sep, sep ] -> [ a, b, c ]

@ckerr ckerr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🎉

@ckerr ckerr merged commit 5240352 into master Feb 5, 2018
@codebytere codebytere deleted the remove-leading-trailing-seps branch February 5, 2018 19:54
sethlu pushed a commit to sethlu/electron that referenced this pull request May 3, 2018
* add function to remove leading/trailing separators

* change const  name for clarity

* add spec to check filtered separators

* clean method and add edge case spec per review
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

Successfully merging this pull request may close these issues.

Remove leading and trailing Menu separators

3 participants