Skip to content
Snippets Groups Projects
Unverified Commit b56d2e3c authored by trop[bot]'s avatar trop[bot] Committed by GitHub
Browse files

fix: update shell.openExternal to correctly focus on external window (#44408)


* fix: Use openURL:configuration:completionHandler instead of openUrl

* test: add a test

* fix: add dispatch_async to replace GetUIThreadTaskRunner

* refactor: remove unused import

* fix: update to use BindPostTaskToCurrentDefault

* test: add regression test for window focus

* refactor: update to explicit task runner

Co-authored-by: default avatarAlice Zhao <alice@makenotion.com>
......@@ -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 "net/base/apple/url_conversions.h"
......@@ -146,11 +147,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;
__block OpenCallback copied_callback = std::move(callback);
scoped_refptr<base::SequencedTaskRunner> runner =
base::SequencedTaskRunner::GetCurrentDefault();
std::move(callback).Run(success ? "" : "Failed to open URL");
[[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,
......
......@@ -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<void>([
shell.openExternal(url),
once(w, 'blur') as Promise<any>
]);
expect(w.isFocused()).to.be.false();
});
});
describe('shell.trashItem()', () => {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment