asticode / astilectron

Electron app that provides an API over a TCP socket that allows executing Electron's method as well as capturing Electron's events
MIT License
285 stars 67 forks source link

Exposing Dialog #64

Closed rkrasnan closed 1 year ago

rkrasnan commented 1 year ago

I have edited my vendor/astilectron/index.js code to expose the dialog via the astilectron object.

Starting on line 536

  `const {ipcRenderer} = require('electron');
  const {dialog} = require('electron').remote;
  var astilectron = {
    dialog: dialog,
    onMessageOnce: false,
    ...
  };
  ...

This made it so I could extend vue/src/plugins/astor.js and further expose the dialog to the rest of my app.

Is this a good way to get access to the dialog object?

asticode commented 1 year ago

That could be one way to do it. Are dialogs working properly?

rkrasnan commented 1 year ago

Yes I am able to access the dialog inside of my vue app and it works properly.

I also have the EnabledRemoteModule flagged as true from here https://github.com/asticode/go-astilectron/issues/314#issuecomment-1186537805

Adding const {dialog} = require('electron').remote; in my vue app wouldn't compile since electron isn't part of that project.

But index.js was created by the bundler, so this code change will be overwritten every time I build. Should I open a PR for this change or am I way off base?

rkrasnan commented 1 year ago

I was able to achieve this without needing to edit the index.js file using the custom.script

in main.go, in the astilectron.WindowOptions

...
Options: &astilectron.WindowOptions{
  ...
  WebPreferences: &astilectron.WebPreferences{EnableRemoteModule: astikit.BoolPtr(true)},
  Custom: &astilectron.WindowCustomOptions{Script: `const {dialog} = require("electron").remote; astilectron.dialog = dialog;`},
},
...
asticode commented 1 year ago

I was able to achieve this without needing to edit the index.js file using the custom.script

I love this idea!

Did you really need the astilectron.dialog = dialog; or could you remove it and call dialog.[...] in your javascript instead?

rkrasnan commented 1 year ago

You are right, I didn't need that part and now I have access to dialog in my vue components.

eslint doesn't like it but the vue app compiles and the dialogs work properly.

asticode commented 1 year ago

Thanks a lot for sharing! ❤️

rkrasnan commented 1 year ago

Thank you for the guidance!