feat: allow GUID parameter to avoid systray demotion on Windows (#21891)

* fix: systray icon demotion

Adding support for GUID parameter in Tray API.
In combination with signed binaries this allows to maintain
the position in the systray on Windows.

* unit tests

* make mac and linux compile
This commit is contained in:
bitdisaster 2020-01-30 21:37:03 -08:00 committed by GitHub
parent 2955c67c4e
commit 89eb309d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 143 additions and 15 deletions

View file

@ -11,6 +11,7 @@
#include "shell/browser/browser.h"
#include "shell/common/api/atom_api_native_image.h"
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_converters/guid_converter.h"
#include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
@ -54,8 +55,10 @@ namespace electron {
namespace api {
Tray::Tray(gin::Handle<NativeImage> image, gin_helper::Arguments* args)
: tray_icon_(TrayIcon::Create()) {
Tray::Tray(gin::Handle<NativeImage> image,
base::Optional<UUID> guid,
gin_helper::Arguments* args)
: tray_icon_(TrayIcon::Create(guid)) {
SetImage(args->isolate(), image);
tray_icon_->AddObserver(this);
@ -67,12 +70,21 @@ Tray::~Tray() = default;
// static
gin_helper::WrappableBase* Tray::New(gin_helper::ErrorThrower thrower,
gin::Handle<NativeImage> image,
base::Optional<UUID> guid,
gin_helper::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create Tray before app is ready");
return nullptr;
}
return new Tray(image, args);
#if defined(OS_WIN)
if (!guid.has_value() && args->Length() > 1) {
thrower.ThrowError("Invalid GUID format");
return nullptr;
}
#endif
return new Tray(image, guid, args);
}
void Tray::OnClicked(const gfx::Rect& bounds,

View file

@ -12,6 +12,7 @@
#include "gin/handle.h"
#include "shell/browser/ui/tray_icon.h"
#include "shell/browser/ui/tray_icon_observer.h"
#include "shell/common/gin_converters/guid_converter.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/trackable_object.h"
@ -36,13 +37,16 @@ class Tray : public gin_helper::TrackableObject<Tray>, public TrayIconObserver {
public:
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Handle<NativeImage> image,
base::Optional<UUID> guid,
gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
protected:
Tray(gin::Handle<NativeImage> image, gin_helper::Arguments* args);
Tray(gin::Handle<NativeImage> image,
base::Optional<UUID> guid,
gin_helper::Arguments* args);
~Tray() override;
// TrayIconObserver: