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

Add menu item order control #12362

Merged
merged 16 commits into from
May 5, 2018
Merged

Add menu item order control #12362

merged 16 commits into from
May 5, 2018

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Mar 20, 2018

This PR reworks the way that we approach menu item ordering by porting an approximation of the solution devised in atom/atom with atom/atom#16661 to Electron.

Fixes #11668.

To-Do:

  • Fix two failing tests
  • Documentation

This PR would hopefully be a breaking change in 3-0-x.

@codebytere codebytere requested a review from a team March 20, 2018 05:34
@codebytere codebytere changed the title Add context menu order control Add menu item order control Mar 20, 2018
}
])
assert.equal(menu.items[0].label, '1')
assert.equal(menu.items[1].label, '2')
assert.equal(menu.items[2].label, '3')
})

it('should position after existing item', () => {
it.only('should position after existing item', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

i need to remove this i know; i'll do it with the next commit

@codebytere codebytere requested a review from a team March 25, 2018 14:29
@ckerr ckerr added semver/major incompatible API changes and removed pending-major-release labels Apr 14, 2018
@@ -0,0 +1,169 @@
function splitArray (arr, predicate) {
Copy link
Member

Choose a reason for hiding this comment

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

I really want this to be a reduce (not a fan of a forEach creating a new array)

const result = arr.reduce((multi, item) => {
  const current = multi[multi.length - 1]
  if (predicate(item)) {
    if (current.length > 0) multi.push([])
  } else {
    current.push(item)
  }
  return multi
}, [[]])
if (result[result.length - 1].length === 0) {
  return result.slice(0, result.length - 1)
}
return result

Not sure if that's ^^ the best way to do it but would love to see the forEach go away 😄

return multiArr
}

function joinArrays (arrays, joiner) {
Copy link
Member

Choose a reason for hiding this comment

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

This is better as a reduce

return arrays.reduce((joined, arr, i) => {
  if (i > 0 && arr.length > 0) {
    joined.push(joiner)
  }
  return joined.concat(arr)
}, []);

Copy link
Contributor

@zeke zeke left a comment

Choose a reason for hiding this comment

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

Left a few comments.

I find the afterGroupContaining and beforeGroupContaining descriptions difficult to understand. Can you take a shot at rewording them?

It might help to have some illustrative examples. Maybe it's high time we had a Menu tutorial 🤔

function joinArrays (arrays, joiner) {
const joinedArr = []
arrays.forEach((arr, i) => {
if (i > 0 && arr.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

0 is falsy, so you could just do && arr.length

Copy link
Contributor

Choose a reason for hiding this comment

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

But this is more explicit. I don't care! Just a note.

docs/api/menu.md Outdated
@@ -302,27 +302,20 @@ browser windows.

## Menu Item Position

You can make use of `position` and `id` to control how the item will be placed
when building a menu with `Menu.buildFromTemplate`.
You can make use of `before/after`, `beforeGroupContaining/afterGroupContaining` and `id` to control how the item will be placed when building a menu with `Menu.buildFromTemplate`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would break out before/after into before, after

docs/api/menu.md Outdated
an existing item in the menu:

* `before` - Inserts this item before the id referenced item. If the
* `before` - Inserts this item before the label referenced item. If the
Copy link
Contributor

Choose a reason for hiding this comment

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

"before the label referenced item"? Not sure I understand this.

Copy link
Member Author

Choose a reason for hiding this comment

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

revised to after the item with the specified label

docs/api/menu.md Outdated
the first item.
the menu. Also implies that the menu item in question should be placed in the same “group” as the item
* `beforeGroupContaining` - Provides a means for a single context menu to declare
the placement of their containing group after to the containing group of the label specified item.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: "after to the containing"

docs/api/menu.md Outdated
* `afterGroupContaining` Provides a means for a single context menu to declare
the placement of their containing group before the containing group of the label specified item.

By default, items will be inserted in the order they exist in the template unless one of the specified positioning keywords is utilized.
Copy link
Contributor

Choose a reason for hiding this comment

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

utilized => used

docs/api/menu.md Outdated
an existing item in the menu:

* `before` - Inserts this item before the id referenced item. If the
* `before` - Inserts this item before the item with the specified label.. If the
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: ..

docs/api/menu.md Outdated
the first item.
the menu. Also implies that the menu item in question should be placed in the same “group” as the item.
* `beforeGroupContaining` - Provides a means for a single context menu to declare
the placement of their containing group after the containing group of the item with the specified label.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/after/before/

}

function attemptToMergeAGroup (groups) {
for (let i = 0; i < groups.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

i can be a const here

Copy link
Member Author

Choose a reason for hiding this comment

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

won't that not let me increment i?

Copy link
Contributor

Choose a reason for hiding this comment

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

derp sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/major incompatible API changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants