From 87a4213b6a1e288056eb7f0c9f4f3f641c777381 Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Wed, 17 Apr 2019 15:56:49 -0400 Subject: [PATCH] Rewrite AtomApplication::openPaths() logic to find existing windows --- .../main-process/atom-application.new.test.js | 6 --- src/main-process/atom-application.js | 43 +++++++++++++------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/spec/main-process/atom-application.new.test.js b/spec/main-process/atom-application.new.test.js index 2bb5c5f86..a7b1b332d 100644 --- a/spec/main-process/atom-application.new.test.js +++ b/spec/main-process/atom-application.new.test.js @@ -66,14 +66,12 @@ describe('AtomApplication', function () { }) // This is also the case when a user clicks on a file in their file manager - // FIXME it('opens a file', async function () { await scenario.open(parseCommandLine(['a/1.md'])) await scenario.assert('[_ 1.md]') }) // This is also the case when a user clicks on a folder in their file manager - // FIXME it('opens a directory', async function () { await scenario.open(parseCommandLine(['a'])) await scenario.assert('[a _]') @@ -118,7 +116,6 @@ describe('AtomApplication', function () { }) // This is also the case when a user clicks on a file outside the project root in their file manager - // FIXME it('opens a file outside the project root', async function () { await scenario.open(parseCommandLine(['b/2.md'])) await scenario.assert('[a 2.md]') @@ -189,7 +186,6 @@ describe('AtomApplication', function () { }) // This is also the case when a user clicks on a file outside the project root in their file manager - // FIXME it('opens a file outside the project root', async function () { await scenario.open(parseCommandLine(['b/2.md'])) await scenario.assert('[a _] [_ 2.md]') @@ -260,14 +256,12 @@ describe('AtomApplication', function () { }) // This is also the case when a user clicks on a file outside the project root in their file manager - // FIXME it('opens a file outside the project root', async function () { await scenario.open(parseCommandLine(['b/2.md'])) await scenario.assert('[_ 2.md] [a _]') }) // This is also the case when a user clicks on a new folder in their file manager - // FIXME it('opens a directory other than the project root', async function () { await scenario.open(parseCommandLine(['b'])) await scenario.assert('[b _] [a _]') diff --git a/src/main-process/atom-application.js b/src/main-process/atom-application.js index 2f9ae9ad0..cb5c77e1d 100644 --- a/src/main-process/atom-application.js +++ b/src/main-process/atom-application.js @@ -973,23 +973,40 @@ class AtomApplication extends EventEmitter { let existingWindow - // Explicitly provided AtomWindow has precedence unless a new window is forced. if (!newWindow) { + // An explicitly provided AtomWindow has precedence. existingWindow = window - } - // If no window is specified, a new window is not forced, and at least one path is provided, locate - // an existing window that contains all paths. - if (!existingWindow && !newWindow && normalizedPathsToOpen.length > 0) { - existingWindow = this.windowForPaths(normalizedPathsToOpen, devMode) - } + // If no window is specified and at least one path is provided, locate an existing window that contains all + // provided paths. + if (!existingWindow && normalizedPathsToOpen.length > 0) { + existingWindow = this.windowForPaths(normalizedPathsToOpen, devMode) + } - // No window specified, new window not forced, no existing window found, and addition to the last window - // requested. Find the last focused window. - if (!existingWindow && !newWindow && addToLastWindow) { - let lastWindow = window || this.getLastFocusedWindow() - if (lastWindow && lastWindow.devMode === devMode) { - existingWindow = lastWindow + // No window specified, no existing window found, and addition to the last window requested. Find the last + // focused window that matches the requested dev and safe modes. + if (!existingWindow && addToLastWindow) { + existingWindow = this.getLastFocusedWindow(win => { + return win.devMode === devMode && win.safeMode === safeMode + }) + } + + // Fall back to the last focused window that has no project roots. + if (!existingWindow) { + existingWindow = this.getLastFocusedWindow(win => !win.hasProjectPath()) + } + + // One last case: if *no* paths are directories, add to the last focused window. + if (!existingWindow) { + const noDirectories = + locationsToOpen.every(location => !location.mustBeDirectory) && + normalizedPathsToOpen.every(pathToOpen => !fs.isDirectorySync(pathToOpen)) + + if (noDirectories) { + existingWindow = this.getLastFocusedWindow(win => { + return win.devMode === devMode && win.safeMode === safeMode + }) + } } }