asticode / go-astilectron

Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
MIT License
4.9k stars 344 forks source link

how to use dialog? #314

Open cosse opened 3 years ago

cosse commented 3 years ago

I see a lot of issues solved with Dialog, But when I try const {dialog} = require('electron').remote; in main.js , It will prompt me TypeError: fs.existsSync is not a function,
Where is the correct quote. Thanks.

asticode commented 3 years ago

It seems like remote module has been removed from newer version of Electron therefore it's not available anymore in JS scripts. And therefore dialog is not available using this method anymore.

I won't have time to work on this change anytime soon, so if somebody feels like contributing, I'm welcoming PRs.

Yukaru-san commented 3 years ago

Fixing this problem should be as easy as adding require('@electron/remote/main').initialize() to the underlying index.js from astilectron.

This is caused by a breaking change from electron.

By the way: const { BrowserWindow } = require('electron').remote became something like const { BrowserWindow } = require('@electron/remote')

I did test this but since I don't know how you handle external libraries I didn't create a pull request. I hope this makes it easier to implement a fix for this problem though.

For future references: Here you can find the The remote's Github repository which needs to be implemented and electron's list of breaking changes

OneManMonkeySquad commented 2 years ago

npm install --save @electron/remote

vendor/astilectron/index.js

// After the last require (line 10 or so):
const remoteMain = require('@electron/remote/main')
remoteMain.initialize();

...

// Need to change window creation (search for windowCreate):
function windowCreate(json) {
    if (!json.windowOptions.webPreferences) {
        json.windowOptions.webPreferences = {}
    }
    json.windowOptions.webPreferences.contextIsolation = false
    json.windowOptions.webPreferences.nodeIntegration = true
    let window = new BrowserWindow(json.windowOptions)
    elements[json.targetID] = window
    windowOptions[json.targetID] = json.windowOptions

    remoteMain.enable(window.webContents); // <------- this is the relevant change; note you need to change the let window = also

yourcode.js

const { dialog } = require('@electron/remote')

function PickInstallationFolder() {
            dialog.showOpenDialog({
                title: "Installation Path",
                properties: ['openDirectory']
            });
        }
ThePutzy commented 2 years ago

Hi, i tried many ways, but it is not possible to get "dialog" running.

i'm using

VersionAstilectron = "0.56.0"
VersionElectron    = "11.4.3"

Is there currently a possibility to use dialog?

thomas-lud commented 2 years ago

Hi @ThePutzy I currently use the file dialog with the following settings:

VersionElectron:    "11.1.0",
VersionAstilectron: "0.47.0",

and in the JS I have const { dialog } = require('electron').remote

wlwatkins commented 2 years ago

@thomas-lud I tried with these versions declared in astilectron.Now but I still get Cannot read property 'dialog' of undefined at index.dev.js:3 Has this been fixed?

wlwatkins commented 2 years ago

I think i have a fix for the current versions

Astilectron has already been provisioned to version 0.55.0
Electron has already been provisioned to version 11.4.3

as describe here https://stackoverflow.com/a/37893140/8571243 one must set enableRemoteModule: true in electronJS. Thankfully, @asticode has implemented this possibility, hence


w, err = a.NewWindow("dist/index.html", &astilectron.WindowOptions{
                //......
        Width:          astikit.IntPtr(1500),
        Height:         astikit.IntPtr(800),
        WebPreferences: &astilectron.WebPreferences{EnableRemoteModule: astikit.BoolPtr(true)},
    })
```  and now this works for me