Skip to content

Commit

Permalink
fix: Setup Desktop before opening notes on macOS (#2369)
Browse files Browse the repository at this point in the history
Desktop registers with the OS as being able to open Cozy Note
documents. Thus, when a user opens a local `.cozy-note` file Desktop
opens the associated note in the default browser.

On Linux and Windows we receive one of two events depending on the
state of the application when the file is opened:
- `second-instance` when Desktop was already running
- `ready` when Desktop was not running

In both cases the file path is found in the execution arguments.

On macOS however, the file path will be made available via a dedicated
`open-file` event in both situations.
When the application is not running though, we'll also receive a
`ready` event **after** the `open-file` event.

This is where we encountered a deadlock situation:
- when receiving the `ready` event, we setup the application and start
  the synchronization unless asked not to (e.g. when opening a note
  while Desktop was not already running)
- when receiving the `open-file` event, we request not to start the
  synchronization if the application was not already running but need
  to wait for the setup to complete to open the note

The setup process was only run if the synchronization should be
started or a note file path was passed as argument which only occurs
on Linux and Windows.
Therefore, the app was stuck, ever waiting for the setup process to
complete.

We now make sure the setup process is run as long as we have a valid
config.
  • Loading branch information
taratatach authored Jan 7, 2025
2 parents 69d3268 + 8fc560b commit c9b53ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
49 changes: 23 additions & 26 deletions gui/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,12 @@ app.on('second-instance', async (event, argv) => {
log.info('second instance invoked with arguments', { filePath })

// If we found a note to open, stop here. Otherwise, show main window.
if (
filePath.endsWith('.cozy-note') &&
(await openNote(filePath, { desktop }))
)
if (filePath.endsWith('.cozy-note')) {
await openNote(filePath, { desktop })
return
} else {
log.warn('file path argument does not have valid Cozy note extension')
}
}

// Make sure the main window exists before trying to show it
Expand Down Expand Up @@ -558,7 +559,7 @@ app.on('open-file', async (event, filePath) => {
// `ready`. This means the app is not ready at this time.
// Since we just want to open a note, not start the Sync, we'll want to quit
// the app when all opened notes will be closed.
const noSync = openedNotes.length === 0 && !app.isReady()
const noSync = !app.isReady()
if (noSync) preventSyncStart()

log.info('open-file invoked', { filePath })
Expand Down Expand Up @@ -622,29 +623,29 @@ app.on('ready', async () => {
log.info('Loading CLI...')
i18n.init(app)

// We need a valid config to start the App and open the requested note.
// We assume users won't have notes they want to open without a connected
// client.
if (argv && argv.length > 2) {
if (!desktop.config.syncPath) {
await exit(0)
return
}

// TODO: don't run migrations here?
if (desktop.config.syncPath) {
await setupDesktop()
}

if (process.platform !== 'darwin' && argv && argv.length > 2) {
const filePath = argv[argv.length - 1]
log.info('main instance invoked with arguments', { filePath, argv })

// If we found a note to open, stop here. Otherwise, start sync app.
if (
filePath.endsWith('.cozy-note') &&
(await openNote(filePath, { desktop }))
) {
await exit(0)
return
// We need a valid config to start the App and open the requested note.
// We assume users won't have notes they want to open without a connected
// client.
if (desktop.config.syncPath) {
if (filePath.endsWith('.cozy-note')) {
await openNote(filePath, { desktop })
} else {
log.warn('file path argument does not have valid Cozy note extension')
}
} else {
log.warn('no valid config')
}

await exit(0)
return
}

if (shouldStartSync) {
Expand All @@ -670,10 +671,6 @@ app.on('ready', async () => {
// dock icon is clicked and there are no other windows open.
app.on('activate', showWindow)

if (desktop.config.syncPath) {
await setupDesktop()
}

if (app.isPackaged) {
log.trace('Setting up updater WM...')
updaterWindow = new UpdaterWM(app, desktop)
Expand Down
10 changes: 5 additions & 5 deletions gui/utils/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ const openMarkdownViewer = async (
}
}

const showGenericError = (filePath, err) => {
const showGenericError = async (filePath, err) => {
log.error('Could not display markdown content of note', {
err,
path: filePath,
filePath,
sentry: true
})

dialog.showMessageBoxSync(null, {
await dialog.showMessageBox(null, {
type: 'error',
message: translate('Error Unexpected error'),
details: `${err.name}: ${err.message}`,
detail: `${err.name}: ${err.message}`,
buttons: [translate('AppMenu Close')]
})
}
Expand Down Expand Up @@ -119,11 +119,11 @@ const openNote = async (
)
return true
} catch (err) {
showGenericError(filePath, err)
await showGenericError(filePath, err)
return false
}
} else {
showGenericError(filePath, err)
await showGenericError(filePath, err)
return false
}
}
Expand Down

0 comments on commit c9b53ce

Please sign in to comment.