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

* Revert "docs: fix `win.getContentView(`) return type (#44464)"

This reverts commit b11c6cf9bf.

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

Co-authored-by: alice <alice@makenotion.com>

* test: add a test

Co-authored-by: alice <alice@makenotion.com>

* fix: add dispatch_async to replace GetUIThreadTaskRunner

Co-authored-by: alice <alice@makenotion.com>

* refactor: remove unused import

Co-authored-by: alice <alice@makenotion.com>

* fix: update to use BindPostTaskToCurrentDefault

Co-authored-by: alice <alice@makenotion.com>

* test: add regression test for window focus

Co-authored-by: alice <alice@makenotion.com>

* refactor: update to explicit task runner

Co-authored-by: alice <alice@makenotion.com>

---------

Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: alice <alice@makenotion.com>
This commit is contained in:
trop[bot] 2024-10-31 12:05:44 +01:00 committed by GitHub
parent 13dacf6a32
commit 5d5c18b7a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 5 deletions

View file

@ -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()`

View file

@ -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<base::SequencedTaskRunner> 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,

View file

@ -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()', () => {