Skip to content

Commit

Permalink
Merge pull request #203 from ooni/fix/restore-mac-window
Browse files Browse the repository at this point in the history
Restore window on MacOS when activated from dock
  • Loading branch information
sarathms authored Mar 17, 2021
2 parents 7718dee + 4b1f4c4 commit 128cbf4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
79 changes: 54 additions & 25 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ log.transports.console.level = isDev ? 'debug' : 'info'
log.transports.file.level = 'debug'

const { getConfig, maybeMigrate, initConfigFile } = require('./utils/config')
const { mainWindow, openAboutWindow, windowURL } = require('./windows')
const { mainWindow, openAboutWindow } = require('./windows')
const toggleWindow = require('./windows/toggle')
const { ipcBindingsForMain } = require('./ipcBindings')
const initializeSentry = require('./utils/sentry')
const store = require('./utils/store')

log.info(`Initializing ${app.name} in ${isDev? 'development': 'production'} mode.`)

// Prevent a second instance from launching

if (!app.requestSingleInstanceLock()) {
log.info('Second instance not allowed. Quitting.')
app.quit()
}
// Get sentry up and running (if already)
initializeSentry()

Expand All @@ -41,6 +47,8 @@ autoUpdater.logger.transports.file.level = 'info'

// To prevent garbage collection of the windows
let windows = null
// Make the window instances accessible from everywhere
global.windows = windows

app.allowRendererProcessReuse = true

Expand Down Expand Up @@ -97,6 +105,8 @@ if (is.macos) {
]
}

Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate))

// This set $PATH properly based on .zsrch/.bashrc/etc.
fixPath()

Expand Down Expand Up @@ -153,6 +163,16 @@ autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall()
})

const createWindow = async (url) => {
windows = {
main: mainWindow(url)
}

windows.main.once('ready-to-show', () => {
toggleWindow(null, windows.main)
})
}

// Prepare the renderer once the app is ready
app.on('ready', async () => {

Expand All @@ -161,6 +181,7 @@ app.on('ready', async () => {
autoUpdater.checkForUpdatesAndNotify()
}

// Setup devtools in development mode
if (isDev && process.env.NODE_ENV !== 'test') {
const {
default: installExtension,
Expand All @@ -174,20 +195,13 @@ app.on('ready', async () => {
/* eslint-enable no-console */
}

// Start nextjs devServer or static file server in production
await prepareNext('./renderer')

windows = {
main: mainWindow()
}

// wire up IPC event handlers to the mainWindow
ipcBindingsForMain(ipcMain)

Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate))
// Make the window instances accessible from everywhere
global.windows = windows

const { wasOpenedAtLogin } = app.getLoginItemSettings()
try {
await maybeMigrate()
} catch (err) {
Expand All @@ -198,29 +212,44 @@ app.on('ready', async () => {
await initConfigFile()
}
const config = await getConfig()

// XXX Only allow one instance of OONI Probe running
// at the same time
const { wasOpenedAtLogin } = app.getLoginItemSettings()
if (!wasOpenedAtLogin) {

// Initiate onboarding if informed consent is not given or not available
try {
if (!config) {
throw new Error('Configuration not found')
} else if (typeof config['_informed_consent'] === 'undefined') {
throw new Error('Informed consent information unavailable')
} else if (config['_informed_consent'] !== true) {
throw new Error('Informed consent not given')
if (config !== null && config['_informed_consent'] === true) {
log.info('Informed consent found in config file.')
await createWindow()
} else {
try {
if (!config) {
throw new Error('Configuration not found')
} else if (typeof config['_informed_consent'] === 'undefined') {
throw new Error('Informed consent information unavailable')
} else if (config['_informed_consent'] !== true) {
throw new Error('Informed consent not given')
}
} catch (e) {
log.info(e.message)
await createWindow('onboard')
}
if (config['_informed_consent'] === true) {
log.info('Informed consent found in config file.')
}
} catch (e) {
log.info(e.message)
windows.main.loadURL(windowURL('onboard'))
}
}
})

windows.main.once('ready-to-show', () => {
toggleWindow(null, windows.main)
})
app.on('activate', async (event, hasVisibleWindows) => {
if (!hasVisibleWindows) {
await createWindow()
}
})

app.on('second-instance', () => {
if (windows.main) {
if (windows.main.isMinimized()) {
windows.main.restore()
}
windows.main.focus()
}
})
5 changes: 3 additions & 2 deletions main/windows/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global require, module, process */

const electron = require('electron')
const { resolve } = require('app-root-path')
const log = require('electron-log')

const isWinOS = process.platform === 'win32'

Expand Down Expand Up @@ -29,7 +30,7 @@ const mainWindow = () => {
}
})
log.info('Loading main window.')
win.loadURL(windowURL('dashboard'))
win.loadURL(windowURL(url))
return win
}

Expand Down

0 comments on commit 128cbf4

Please sign in to comment.