brrd / abricotine

Markdown editor with inline preview
GNU General Public License v3.0
2.63k stars 159 forks source link

Two instances are opened when I try to open a single file #258

Closed rudolfvesely closed 4 years ago

rudolfvesely commented 5 years ago

Hi all,

Fantastic Markdown editor. Thank you for the great work.

The only issue I have is that when I click on another (second, third...) markdown file the editor is opened twice. I recorded a short GIF to explain:

vokoscreen-2019-04-23_09-37-36

My environment:

Abricotine: 7.0.0 Linux with KDE Plasma 5.14 desktop

Kind regards, Rudolf

GitSquared commented 4 years ago

This might be related?

https://github.com/brrd/Abricotine/blob/63de38d54eb30d6f893f045dc698423397917556/app/index.js#L17-L28

brrd commented 4 years ago

This might be related?

Probably yes. Obviously the document is redirected to the first instance as expected, but it is still opened in the new instance, which makes a duplicate.

GitSquared commented 4 years ago

@brrd It seems that the second instance continue its startup process even though it failed to acquire the lock. From the docs, you should add this:

const { app } = require('electron')
let myWindow = null

const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // Someone tried to run a second instance, we should focus our window.
    if (myWindow) {
      if (myWindow.isMinimized()) myWindow.restore()
      myWindow.focus()
    }
  })

  // Create myWindow, load the rest of the app, etc...
  app.on('ready', () => {
  })
}

Or you could simply do this at line 18:

app.requestSingleInstanceLock() || app.quit();
brrd commented 4 years ago

@GitSquared Yes, you're right. It should use app.requestSingleInstanceLock() return value. I obviously forgot this point when I moved from the previous API.

GitSquared commented 4 years ago

@brrd I suppose you won't need a PR for this, but ping me if needed.

tsonne commented 4 years ago

Sorry if I'm polluting your Issues board here with my fork commit. I just wanted to fix it for myself. One thing: I couldn't build Abricotine with the latest Node.js 13.8.0 due to the spellchecker module, but it works fine with node version 12.14.1