Skip to content

Commit

Permalink
PoC of actually closing window
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
hacdias committed May 27, 2022
1 parent e77083e commit 82daae3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/second-instance.js
Expand Up @@ -12,6 +12,6 @@ module.exports = async function (ctx) {
return
}

ctx.launchWebUI()
await ctx.launchWebUI()
})
}
6 changes: 4 additions & 2 deletions src/take-screenshot.js
Expand Up @@ -99,9 +99,11 @@ function handleScreenshot (ctx) {
}
}

function takeScreenshot (ctx) {
const { webui } = ctx
async function takeScreenshot (ctx) {
const { getWebUI } = ctx
logger.info('[screenshot] taking screenshot')

const webui = await getWebUI()
webui.webContents.send('screenshot')
}

Expand Down
91 changes: 58 additions & 33 deletions src/webui/index.js
Expand Up @@ -96,13 +96,6 @@ const createWindow = () => {
store.set('window.height', dim[1])
})

window.on('close', (event) => {
event.preventDefault()
window.hide()
dock.hide()
logger.info('[web ui] window hidden')
})

app.on('before-quit', () => {
// Makes sure the app quits even though we prevent
// the closing of this window.
Expand All @@ -129,33 +122,72 @@ module.exports = async function (ctx) {

openExternal()

const window = createWindow(ctx)
let apiAddress = null

ctx.webui = window

const url = new URL('/', 'webui://-')
url.hash = '/blank'
url.searchParams.set('deviceId', ctx.countlyDeviceId)

ctx.launchWebUI = (path, { focus = true, forceRefresh = false } = {}) => {
if (forceRefresh) window.webContents.reload()
let window = null
let apiAddress = null

const getWindow = async () => {
if (window !== null) {
return window
}

window = createWindow(ctx)

window.once('close', () => {
const myself = window
window = null
dock.hide() // NOTE: not working?
myself.close()
})

await new Promise(resolve => {
window.once('ready-to-show', () => {
logger.info('[web ui] window ready')
launchWindow('/').then(resolve)
})

updateLanguage()
window.loadURL(url.toString())
})

return window
}

const launchWindow = async (path, { focus = true, forceRefresh = false } = {}) => {
const window = await getWindow()

if (forceRefresh) {
window.webContents.reload()
}

if (!path) {
logger.info('[web ui] launching web ui', { withAnalytics: 'FN_LAUNCH_WEB_UI_FOO' })
} else {
logger.info(`[web ui] navigate to ${path}`, { withAnalytics: 'FN_LAUNCH_WEB_UI_WITH_PATH' })
url.hash = path
window.webContents.loadURL(url.toString())
}

if (focus) {
window.show()
window.focus()
dock.show()
}

// load again: minimize visual jitter on windows
if (path) window.webContents.loadURL(url.toString())
}

ctx.launchWebUI = launchWindow
ctx.getWebUI = getWindow

app.on('window-all-closed', () => {
// Do not quit the app just because there's no windows.
})

function updateLanguage () {
url.searchParams.set('lng', store.get('language'))
}
Expand All @@ -167,15 +199,17 @@ module.exports = async function (ctx) {
apiAddress = ipfsd.apiAddr
url.searchParams.set('api', apiAddress.toString())
updateLanguage()
window.loadURL(url.toString())
if (window) window.loadURL(url.toString())
}
})

ipcMain.on('config.get', () => {
window.webContents.send('config.changed', {
platform: os.platform(),
config: store.store
})
if (window) {
window.webContents.send('config.changed', {
platform: os.platform(),
config: store.store
})
}
})

// Set user agent
Expand All @@ -184,20 +218,11 @@ module.exports = async function (ctx) {
callback({ cancel: false, requestHeaders: details.requestHeaders }) // eslint-disable-line
})

return new Promise(resolve => {
window.once('ready-to-show', () => {
logger.info('[web ui] window ready')

if (store.get(CONFIG_KEY)) {
ctx.launchWebUI('/')
}

resolve()
})

updateLanguage()
window.loadURL(url.toString())
})
return async () => {
if (store.get(CONFIG_KEY)) {
await launchWindow('/')
}
}
}

module.exports.CONFIG_KEY = CONFIG_KEY

0 comments on commit 82daae3

Please sign in to comment.