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 the ability to disable/enable the context menu #93

Merged
merged 26 commits into from Apr 27, 2020

Conversation

loopmode
Copy link
Contributor

@loopmode loopmode commented Feb 1, 2020

This PR adds a dispose function and fixes #82 and fixes #84.

The implementation is such that the contextMenu() return value is a function that will remove the registered event listeners.

@loopmode
Copy link
Contributor Author

loopmode commented Feb 1, 2020

@sindresorhus I also have updated the fixture.js and html files, but I'm not sure whether I did it "correctly", so it's parked on a separate branch. See loopmode@26f36c4

It adds a checkbox to enable or disable the context menu, but for that to work while keeping it simple, I moved the entire logic our of the js and into the html file.

I can provide another PR for that.

index.js Outdated Show resolved Hide resolved

const dispose = () => {
disposables.forEach(dispose => dispose());
isDisposed = true;
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of a boolean, just empty the array and check the length. You should empty the array regardless as you don't want to hold onto those forever.

Copy link
Contributor Author

@loopmode loopmode Apr 15, 2020

Choose a reason for hiding this comment

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

Hmm. I believe it would cause a chicken/egg problem and we'd never create the menu in the first place in https://github.com/loopmode/electron-context-menu/blob/feature/dispose-function/index.js#L258-L265

Looking into it.

Copy link
Contributor Author

@loopmode loopmode Apr 15, 2020

Choose a reason for hiding this comment

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

Indeed, it's not that easy.
Avoiding the flag requires either a completely different approach, or several more changes that seem unnecessarily complex.
It's really about bailing out in certain deferred edge cases, and the significant piece of information of the flag is: "No need to create the menu anymore, the callsite already disposed it".

I already tried a couple variants, and I still believe the flag is the best solution.

Should let go of the references despite of that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

array length reset

@sindresorhus
Copy link
Owner

This looks great :)

You also need to add it to https://github.com/sindresorhus/electron-context-menu/blob/master/index.d.ts

@sindresorhus
Copy link
Owner

It adds a checkbox to enable or disable the context menu, but for that to work while keeping it simple, I moved the entire logic our of the js and into the html file.

I think you should create a new fixture just for this. It's totally fine that it's in the HTML file. It can be as just contextMenu(), with no options. It should be part of this PR.

@loopmode
Copy link
Contributor Author

Great, will do that!

@sindresorhus
Copy link
Owner

Bump :)

@loopmode
Copy link
Contributor Author

loopmode commented Apr 15, 2020

Hey @sindresorhus

So.. I did quite some things extra here, hope it doesn't make accepting the PR too much of a problem?

  • I merged your 1.0 from master
  • I added a .prettierrc which basically resembles the existing code style, at least I was able to format on save without causing extra changes
  • I added a fixture for creating and disposing the menu, it can be used via yarn start-fixture3. I suggest you/we rename the fixture scripts :)
  • Because the fixture files started to dominate the project root, i moved all of them into a fixtures/ subfolder and adjusted the package scripts accordingly
  • The array of disposable functions does get emptied on dispose now, but I still did leave the isDisposed flag. As mentioned previously, it's currently essential. Maybe it's my lack of imagination, but I don't see another simple or elegant approach. Emptying the array and checking for length === 0 is unfortunately definitely not the way to go, as it cannot distinguish between "already disposed" and "not initialized yet". I'd be happy to learn something here, but really I think the boolean flag is the way to go

Ah, and I added couple of cleanup commits to pass through your travis pipeline.

@sindresorhus
Copy link
Owner

I added a .prettierrc which basically resembles the existing code style, at least I was able to format on save without causing extra changes

I'm not interested in having this.

@sindresorhus
Copy link
Owner

I added a fixture for creating and disposing the menu, it can be used via yarn start-fixture3. I suggest you/we rename the fixture scripts :)
Because the fixture files started to dominate the project root, i moved all of them into a fixtures/ subfolder and adjusted the package scripts accordingly
The array of disposable functions does get emptied on dispose now, but I still did leave the isDisposed flag. As mentioned previously, it's currently essential. Maybe it's my lack of imagination, but I don't see another simple or elegant approach. Emptying the array and checking for length === 0 is unfortunately definitely not the way to go, as it cannot distinguish between "already disposed" and "not initialized yet". I'd be happy to learn something here, but really I think the boolean flag is the way to go

👍

readme.md Outdated
@@ -52,10 +52,21 @@ let mainWindow;
})();
```

Note that the return value of `contextMenu()` is a dispose function:
Copy link
Owner

Choose a reason for hiding this comment

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

The readme and index.d.ts should be in sync documentation-wise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a couple of differences, as I didn't know about the sync aspect. Will adjust.
Actually, there's even a react hook example in index.d.ts that is not in readme.
I will remove that altogether as it feels out of scope here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The readme and index.d.ts should be in sync documentation-wise.

Synced

index.js Outdated
label: 'No Guesses Found',
visible: hasText && props.misspelledWord,
enabled: false
});
Copy link
Owner

Choose a reason for hiding this comment

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

Don't do unrelated changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, sorry. Related to prettierrc - will disable auto-formatting in my editor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't do unrelated changes.

Reverted

@loopmode
Copy link
Contributor Author

loopmode commented Apr 23, 2020

I added a .prettierrc which basically resembles the existing code style, at least I was able to format on save without causing extra changes

I'm not interested in having this.

removed

The readme and index.d.ts should be in sync documentation-wise.

Synced

Don't do unrelated changes.

Reverted

package.json Outdated
@@ -10,10 +10,12 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"contributors": ["Jovica Aleksic <jovica.aleksic@loopmode.de>"],
Copy link
Owner

Choose a reason for hiding this comment

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

I don't use this field. It's not actually used anywhere and it's creates inequality with contributors that doesn't add themselves there. I prefer to keep it simple and just not use it. Hope you understand :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely no problem! :)

@sindresorhus sindresorhus changed the title Return a dispose function Add the ability to disable/enable the context menu Apr 27, 2020
@sindresorhus sindresorhus merged commit 2547936 into sindresorhus:master Apr 27, 2020
@sindresorhus
Copy link
Owner

Thanks :)

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.

Multiple invocations, destroy function? Returns dispose function
3 participants