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

proof of concept: close Web UI window instead of hiding #1929

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/second-instance.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: analyticsKeys.FN_LAUNCH_WEB_UI })
} else {
logger.info(`[web ui] navigate to ${path}`, { withAnalytics: analyticsKeys.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,18 +218,9 @@ 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('/')
}
}
}