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

Access electron utilities using JavaScript FFI #200

Closed
wants to merge 9 commits into from

Conversation

bradrn
Copy link
Contributor

@bradrn bradrn commented Sep 11, 2017

Following on from #111 and #169, I have discovered that by adding a custom index.html which loads jQuery, the nodeIntegration parameter in electron.js can be set to true, allowing the electron utilities to be accessed from the JavaScript FFI. For instance:

button <- UI.button # set UI.text "Click me!"
on UI.click button $ const $ runFunction $ ffi "require('electron').dialog.showOpenDialog({})"

I have been working on a library which supplies Haskell function for creating menus, dialog boxes etc. Hopefully I will be able to upload it to Hackage some time in the future.

(Side note: commit a6853b3 isn't related to the rest of the pull request, but I'm including it because I couldn't figure out how to separate it into its own PR.)

In order to set nodeIntegration=true in electron.js, the index.html file
needs to load jQuery.
As commit fad91bf allowed the index.html page to load jQuery, we can now set nodeIntegration=true to allow the threepenny-gui process create menus, dialogs etc. through the JavaScript FFI.
@HeinrichApfelmus
Copy link
Owner

That looks interesting!

Ideally, we wouldn't need a custom index.html for this use case, though. The haskell.js script includes jQuery (version 3.2.1 at the moment). Can't we just add the module stuff?

I definitely want to avoid querying google for jQuery on a local app. (As said, jQuery is bundled with Threepenny, and I'm happy to make it available under different URIs.)

I don't understand: Why do we need to load jQuery in order to activate node integration?

(Re sidenote: A pull request adds all commits in a branch. Make a new branch and cherry pick commits that you want to be pulled.)

@bradrn
Copy link
Contributor Author

bradrn commented Sep 12, 2017

That looks interesting!

Ideally, we wouldn't need a custom index.html for this use case, though. The haskell.js script includes jQuery (version 3.2.1 at the moment). Can't we just add the module stuff?

Unfortunately, for some reason electron can't just use jQuery; it has to be loaded in a different way (see https://stackoverflow.com/a/37480521 and electron/electron#254 (comment) for more details).

I definitely want to avoid querying google for jQuery on a local app. (As said, jQuery is bundled with Threepenny, and I'm happy to make it available under different URIs.)

You're right; it should be possible to figure out how to use the jQuery bundled with threepenny.

@bradrn
Copy link
Contributor Author

bradrn commented Sep 14, 2017

I've figured out how to remove the reference to the Google CDN. It turned out that if I moved the reference to Haskell.js to where the Google CDN was, then jQuery would still work.

@HeinrichApfelmus
Copy link
Owner

It turned out that if I moved the reference to Haskell.js to where the Google CDN was, then jQuery would still work.

Great! I think then there is no need for a custom "index.html" anymore, we can integrate this into the default "index.html", right?

@bradrn
Copy link
Contributor Author

bradrn commented Sep 16, 2017

Great! I think then there is no need for a custom "index.html" anymore, we can integrate this into the default "index.html", right?

Yes: according to this Stack Overflow question, the extra bits surrounding the call to Haskell.js won't have any effect outside Electron.

This reduces setup time. The modified index.html (previously in
docs/electron) allows easier Electron integration, but has no effect on
normal web pages.
@bradrn
Copy link
Contributor Author

bradrn commented Sep 16, 2017

I have tested the custom index.html with the CRUD example and it didn't break anything, so I've moved it.

HeinrichApfelmus pushed a commit that referenced this pull request Sep 19, 2017
The file `index.html` now loads jQuery correctly
if used in the context of an Electron `BrowserWindow`.
This allows us to set `nodeIntegration=true` in
`electron.js` and gives access to Electron’s
main process from the Browser Window.
@HeinrichApfelmus
Copy link
Owner

Thanks a lot for your contribution! The pull request is a bit of a mess, though, I have squashed everything into a single commit (3d0f808) and commited it to the master branch. I hope this works for you!

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.

2 participants