xAlien95 xAlien95 - 3 months ago 76
AngularJS Question

New electron BrowserWindow starting at specified ui-router state

Is it possible to create a new

BrowserWindow
at a specified
ui-router
state? Here's my
main.js
:

const {app, BrowserWindow, Menu} = require('electron')

let win

function createWindow() {
win = new BrowserWindow({ width: 950, height: 660 })
win.loadURL(`file://${__dirname}/index.html`)

// Emitted when the window is closed.
win.on('closed', () => { win = null })

app.dock.setMenu(Menu.buildFromTemplate([
{ label: 'Open at page', click: () => { goToState('page') } }
]))
}

let goToState = (state) => {
if (!win) {
createWindow()
// when win is ready, call win.webContents.send('sref', state)
} else {
win.webContents.send('sref', state)
}
}

app.on('ready', createWindow)

app.on('activate', () => {
if (win === null) {
createWindow()
}
})


The
'sref'
event performs a
$state.go(state)
in the rendered process.

What I already tried:


  • Pass the function as callback in
    createWindow()


    function createWindow (callback) {

    ...

    if (callback) callback()
    }

    let goToState = (state) => {
    if (!win) {
    createWindow(() => {
    win.webContents.send('sref', state)
    })
    } else {
    win.webContents.send('sref', state)
    }
    }

  • Use
    app.on('web-contents-created')


    let goToState = (state) => {
    if (!win) {
    createWindow()
    app.on('web-content-created', () => {
    win.webContents.send('sref', state)
    })
    } else {
    win.webContents.send('sref', state)
    }
    }



Thanks for reading.

Answer

Did you try just setting the url with the state after the hash:

win.loadURL(`file://${__dirname}/index.html#/page`)

assuming you have a ui-router state configured with the url:

"url":"/page"