feat: add force option to app.focus() ()

This commit is contained in:
Shelley Vohr 2020-03-11 16:07:01 +00:00 committed by GitHub
parent 75cef84877
commit b724fbc0ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 6 deletions

View file

@ -554,11 +554,17 @@ Returns `Promise<void>` - fulfilled when Electron is initialized.
May be used as a convenient alternative to checking `app.isReady()` May be used as a convenient alternative to checking `app.isReady()`
and subscribing to the `ready` event if the app is not ready yet. and subscribing to the `ready` event if the app is not ready yet.
### `app.focus()` ### `app.focus([options])`
* `options` Object (optional)
* `steal` Boolean _macOS_ - Make the receiver the active app even if another app is
currently active.
On Linux, focuses on the first visible window. On macOS, makes the application On Linux, focuses on the first visible window. On macOS, makes the application
the active app. On Windows, focuses on the application's first window. the active app. On Windows, focuses on the application's first window.
You should seek to use the `steal` option as sparingly as possible.
### `app.hide()` _macOS_ ### `app.hide()` _macOS_
Hides all application windows without minimizing them. Hides all application windows without minimizing them.

View file

@ -57,7 +57,7 @@ class Browser : public WindowListObserver {
void Shutdown(); void Shutdown();
// Focus the application. // Focus the application.
void Focus(); void Focus(gin_helper::Arguments* args);
// Returns the version of the executable (or bundle). // Returns the version of the executable (or bundle).
std::string GetVersion() const; std::string GetVersion() const;

View file

@ -84,7 +84,7 @@ bool SetDefaultWebClient(const std::string& protocol) {
return ran_ok && exit_code == EXIT_SUCCESS; return ran_ok && exit_code == EXIT_SUCCESS;
} }
void Browser::Focus() { void Browser::Focus(gin_helper::Arguments* args) {
// Focus on the first visible window. // Focus on the first visible window.
for (auto* const window : WindowList::GetWindows()) { for (auto* const window : WindowList::GetWindows()) {
if (window->IsVisible()) { if (window->IsVisible()) {

View file

@ -21,6 +21,8 @@
#include "shell/browser/window_list.h" #include "shell/browser/window_list.h"
#include "shell/common/application_info.h" #include "shell/common/application_info.h"
#include "shell/common/gin_helper/arguments.h" #include "shell/common/gin_helper/arguments.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/promise.h" #include "shell/common/gin_helper/promise.h"
#include "shell/common/platform_util.h" #include "shell/common/platform_util.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
@ -32,8 +34,20 @@ void Browser::SetShutdownHandler(base::Callback<bool()> handler) {
[[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)]; [[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)];
} }
void Browser::Focus() { void Browser::Focus(gin_helper::Arguments* args) {
[[AtomApplication sharedApplication] activateIgnoringOtherApps:NO]; gin_helper::Dictionary opts;
bool steal_focus = false;
if (args->GetNext(&opts)) {
gin_helper::ErrorThrower thrower(args->isolate());
if (!opts.Get("steal", &steal_focus)) {
thrower.ThrowError(
"Expected options object to contain a 'steal' boolean property");
return;
}
}
[[AtomApplication sharedApplication] activateIgnoringOtherApps:steal_focus];
} }
void Browser::Hide() { void Browser::Hide() {

View file

@ -163,7 +163,7 @@ Browser::UserTask::UserTask() = default;
Browser::UserTask::UserTask(const UserTask&) = default; Browser::UserTask::UserTask(const UserTask&) = default;
Browser::UserTask::~UserTask() = default; Browser::UserTask::~UserTask() = default;
void Browser::Focus() { void Browser::Focus(gin_helper::Arguments* args) {
// On Windows we just focus on the first window found for this process. // On Windows we just focus on the first window found for this process.
DWORD pid = GetCurrentProcessId(); DWORD pid = GetCurrentProcessId();
EnumWindows(&WindowsEnumerationHandler, reinterpret_cast<LPARAM>(&pid)); EnumWindows(&WindowsEnumerationHandler, reinterpret_cast<LPARAM>(&pid));