diff --git a/docs/api/base-window.md b/docs/api/base-window.md index c6cc7c878bc5..30f2db76fa9d 100644 --- a/docs/api/base-window.md +++ b/docs/api/base-window.md @@ -512,7 +512,7 @@ Sets the content view of the window. #### `win.getContentView()` -Returns [`View`](view.md) - The content view of the window. +Returns [View](view.md) - The content view of the window. #### `win.destroy()` diff --git a/shell/common/platform_util_mac.mm b/shell/common/platform_util_mac.mm index 3df0ce366f6f..6e99e336e392 100644 --- a/shell/common/platform_util_mac.mm +++ b/shell/common/platform_util_mac.mm @@ -19,6 +19,7 @@ #include "base/logging.h" #include "base/mac/scoped_aedesc.h" #include "base/strings/sys_string_conversions.h" +#include "base/task/sequenced_task_runner.h" #include "base/task/thread_pool.h" #include "content/public/browser/browser_task_traits.h" #include "electron/mas.h" @@ -147,11 +148,27 @@ void OpenExternal(const GURL& url, return; } - bool success = [[NSWorkspace sharedWorkspace] openURL:ns_url]; - if (success && options.activate) - [NSApp activateIgnoringOtherApps:YES]; + NSWorkspaceOpenConfiguration* configuration = + [NSWorkspaceOpenConfiguration configuration]; + configuration.activates = options.activate; - std::move(callback).Run(success ? "" : "Failed to open URL"); + __block OpenCallback copied_callback = std::move(callback); + scoped_refptr runner = + base::SequencedTaskRunner::GetCurrentDefault(); + + [[NSWorkspace sharedWorkspace] + openURL:ns_url + configuration:configuration + completionHandler:^(NSRunningApplication* _Nullable app, + NSError* _Nullable error) { + if (error) { + runner->PostTask(FROM_HERE, base::BindOnce(std::move(copied_callback), + "Failed to open URL")); + } else { + runner->PostTask(FROM_HERE, + base::BindOnce(std::move(copied_callback), "")); + } + }]; } bool MoveItemToTrashWithError(const base::FilePath& full_path, diff --git a/spec/api-shell-spec.ts b/spec/api-shell-spec.ts index 3883bf79ddde..4000ee106c3e 100644 --- a/spec/api-shell-spec.ts +++ b/spec/api-shell-spec.ts @@ -76,6 +76,21 @@ describe('shell module', () => { requestReceived ]); }); + + ifit(process.platform === 'darwin')('removes focus from the electron window after opening an external link', async () => { + const url = 'http://127.0.0.1'; + const w = new BrowserWindow({ show: true }); + + await once(w, 'focus'); + expect(w.isFocused()).to.be.true(); + + await Promise.all([ + shell.openExternal(url), + once(w, 'blur') as Promise + ]); + + expect(w.isFocused()).to.be.false(); + }); }); describe('shell.trashItem()', () => {