feat: allow macOS tray to maintain position (#47838)
* feat: allow macOS tray to maintain position * refactor: just use guid * test: fixup tests * docs: clarify UUID format
This commit is contained in:
parent
f49a645c06
commit
a0d983e4b5
13 changed files with 101 additions and 34 deletions
|
@ -52,10 +52,12 @@ gin::DeprecatedWrapperInfo Tray::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|||
|
||||
Tray::Tray(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> image,
|
||||
std::optional<UUID> guid)
|
||||
: tray_icon_(TrayIcon::Create(guid)) {
|
||||
std::optional<base::Uuid> guid)
|
||||
: guid_(guid), tray_icon_(TrayIcon::Create(guid)) {
|
||||
SetImage(isolate, image);
|
||||
tray_icon_->AddObserver(this);
|
||||
if (guid.has_value())
|
||||
tray_icon_->SetAutoSaveName(guid.value().AsLowercaseString());
|
||||
}
|
||||
|
||||
Tray::~Tray() = default;
|
||||
|
@ -63,19 +65,17 @@ Tray::~Tray() = default;
|
|||
// static
|
||||
gin_helper::Handle<Tray> Tray::New(gin_helper::ErrorThrower thrower,
|
||||
v8::Local<v8::Value> image,
|
||||
std::optional<UUID> guid,
|
||||
std::optional<base::Uuid> guid,
|
||||
gin::Arguments* args) {
|
||||
if (!Browser::Get()->is_ready()) {
|
||||
thrower.ThrowError("Cannot create Tray before app is ready");
|
||||
return {};
|
||||
}
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
if (!guid.has_value() && args->Length() > 1) {
|
||||
thrower.ThrowError("Invalid GUID format");
|
||||
thrower.ThrowError("Invalid GUID format - GUID must be a string");
|
||||
return {};
|
||||
}
|
||||
#endif
|
||||
|
||||
// Error thrown by us will be dropped when entering V8.
|
||||
// Make sure to abort early and propagate the error to JS.
|
||||
|
@ -392,6 +392,15 @@ gfx::Rect Tray::GetBounds() {
|
|||
return tray_icon_->GetBounds();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Tray::GetGUID() {
|
||||
if (!CheckAlive())
|
||||
return {};
|
||||
auto* isolate = JavascriptEnvironment::GetIsolate();
|
||||
if (!guid_)
|
||||
return v8::Null(isolate);
|
||||
return gin::ConvertToV8(isolate, guid_.value());
|
||||
}
|
||||
|
||||
bool Tray::CheckAlive() {
|
||||
if (!tray_icon_) {
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
|
@ -424,6 +433,7 @@ void Tray::FillObjectTemplate(v8::Isolate* isolate,
|
|||
.SetMethod("closeContextMenu", &Tray::CloseContextMenu)
|
||||
.SetMethod("setContextMenu", &Tray::SetContextMenu)
|
||||
.SetMethod("getBounds", &Tray::GetBounds)
|
||||
.SetMethod("getGUID", &Tray::GetGUID)
|
||||
.Build();
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ class Tray final : public gin_helper::DeprecatedWrappable<Tray>,
|
|||
// gin_helper::Constructible
|
||||
static gin_helper::Handle<Tray> New(gin_helper::ErrorThrower thrower,
|
||||
v8::Local<v8::Value> image,
|
||||
std::optional<UUID> guid,
|
||||
std::optional<base::Uuid> guid,
|
||||
gin::Arguments* args);
|
||||
|
||||
static void FillObjectTemplate(v8::Isolate*, v8::Local<v8::ObjectTemplate>);
|
||||
|
@ -65,7 +65,7 @@ class Tray final : public gin_helper::DeprecatedWrappable<Tray>,
|
|||
private:
|
||||
Tray(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> image,
|
||||
std::optional<UUID> guid);
|
||||
std::optional<base::Uuid> guid);
|
||||
~Tray() override;
|
||||
|
||||
// TrayIconObserver:
|
||||
|
@ -111,10 +111,12 @@ class Tray final : public gin_helper::DeprecatedWrappable<Tray>,
|
|||
void SetContextMenu(gin_helper::ErrorThrower thrower,
|
||||
v8::Local<v8::Value> arg);
|
||||
gfx::Rect GetBounds();
|
||||
v8::Local<v8::Value> GetGUID();
|
||||
|
||||
bool CheckAlive();
|
||||
|
||||
v8::Global<v8::Value> menu_;
|
||||
std::optional<base::Uuid> guid_;
|
||||
std::unique_ptr<TrayIcon> tray_icon_;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue