2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-05-30 15:57:54 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/electron_api_tray.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
|
2014-06-01 02:20:06 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-06-22 12:33:44 +00:00
|
|
|
#include "base/containers/fixed_flat_map.h"
|
2020-03-30 01:32:02 +00:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/electron_api_menu.h"
|
2020-03-30 01:32:02 +00:00
|
|
|
#include "shell/browser/api/ui_event.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
2020-06-22 16:35:24 +00:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/api/electron_api_native_image.h"
|
2021-01-04 20:58:31 +00:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_converters/gfx_converter.h"
|
2020-01-31 05:37:03 +00:00
|
|
|
#include "shell/common/gin_converters/guid_converter.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_converters/image_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2020-03-30 01:32:02 +00:00
|
|
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2015-01-03 02:43:56 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2014-05-30 15:57:54 +00:00
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
namespace gin {
|
2019-08-08 21:43:33 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<electron::TrayIcon::IconType> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
electron::TrayIcon::IconType* out) {
|
2023-06-22 12:33:44 +00:00
|
|
|
using Val = electron::TrayIcon::IconType;
|
|
|
|
static constexpr auto Lookup =
|
|
|
|
base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
|
|
|
|
{"custom", Val::kCustom},
|
|
|
|
{"error", Val::kError},
|
|
|
|
{"info", Val::kInfo},
|
|
|
|
{"none", Val::kNone},
|
|
|
|
{"warning", Val::kWarning},
|
|
|
|
});
|
|
|
|
return FromV8WithLookup(isolate, val, Lookup, out);
|
2019-08-08 21:43:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
} // namespace gin
|
2019-08-08 21:43:33 +00:00
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2014-05-30 15:57:54 +00:00
|
|
|
|
2020-03-30 01:32:02 +00:00
|
|
|
gin::WrapperInfo Tray::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
Tray::Tray(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> image,
|
2021-06-03 08:05:04 +00:00
|
|
|
absl::optional<UUID> guid)
|
2020-01-31 05:37:03 +00:00
|
|
|
: tray_icon_(TrayIcon::Create(guid)) {
|
2021-01-04 20:58:31 +00:00
|
|
|
SetImage(isolate, image);
|
2014-06-02 03:28:23 +00:00
|
|
|
tray_icon_->AddObserver(this);
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 22:40:30 +00:00
|
|
|
Tray::~Tray() = default;
|
2014-05-30 15:57:54 +00:00
|
|
|
|
|
|
|
// static
|
2020-03-30 01:32:02 +00:00
|
|
|
gin::Handle<Tray> Tray::New(gin_helper::ErrorThrower thrower,
|
2021-01-04 20:58:31 +00:00
|
|
|
v8::Local<v8::Value> image,
|
2021-06-03 08:05:04 +00:00
|
|
|
absl::optional<UUID> guid,
|
2020-03-30 01:32:02 +00:00
|
|
|
gin::Arguments* args) {
|
2015-03-25 14:40:01 +00:00
|
|
|
if (!Browser::Get()->is_ready()) {
|
2019-10-15 01:15:23 +00:00
|
|
|
thrower.ThrowError("Cannot create Tray before app is ready");
|
2020-03-30 01:32:02 +00:00
|
|
|
return gin::Handle<Tray>();
|
2015-03-25 14:40:01 +00:00
|
|
|
}
|
2020-01-31 05:37:03 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-01-31 05:37:03 +00:00
|
|
|
if (!guid.has_value() && args->Length() > 1) {
|
|
|
|
thrower.ThrowError("Invalid GUID format");
|
2020-03-30 01:32:02 +00:00
|
|
|
return gin::Handle<Tray>();
|
2020-01-31 05:37:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-02-24 19:03:59 +00:00
|
|
|
auto handle = gin::CreateHandle(args->isolate(),
|
|
|
|
new Tray(args->isolate(), image, guid));
|
|
|
|
handle->Pin(args->isolate());
|
|
|
|
return handle;
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 02:49:26 +00:00
|
|
|
void Tray::OnClicked(const gfx::Rect& bounds,
|
|
|
|
const gfx::Point& location,
|
|
|
|
int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("click", CreateEventFromFlags(modifiers), bounds, location);
|
2014-06-02 03:08:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 10:15:51 +00:00
|
|
|
void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("double-click", CreateEventFromFlags(modifiers), bounds);
|
2015-07-29 06:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("right-click", CreateEventFromFlags(modifiers), bounds);
|
2014-09-09 11:45:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 11:42:57 +00:00
|
|
|
void Tray::OnBalloonShow() {
|
|
|
|
Emit("balloon-show");
|
|
|
|
}
|
|
|
|
|
2014-11-28 10:50:31 +00:00
|
|
|
void Tray::OnBalloonClicked() {
|
2015-11-13 08:41:33 +00:00
|
|
|
Emit("balloon-click");
|
2014-11-28 10:50:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 11:42:57 +00:00
|
|
|
void Tray::OnBalloonClosed() {
|
|
|
|
Emit("balloon-closed");
|
|
|
|
}
|
|
|
|
|
2015-11-10 16:02:50 +00:00
|
|
|
void Tray::OnDrop() {
|
|
|
|
Emit("drop");
|
|
|
|
}
|
|
|
|
|
2015-07-19 04:12:28 +00:00
|
|
|
void Tray::OnDropFiles(const std::vector<std::string>& files) {
|
|
|
|
Emit("drop-files", files);
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:21:39 +00:00
|
|
|
void Tray::OnDropText(const std::string& text) {
|
|
|
|
Emit("drop-text", text);
|
|
|
|
}
|
|
|
|
|
2017-06-28 19:09:12 +00:00
|
|
|
void Tray::OnMouseEntered(const gfx::Point& location, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("mouse-enter", CreateEventFromFlags(modifiers), location);
|
2017-06-14 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 19:09:12 +00:00
|
|
|
void Tray::OnMouseExited(const gfx::Point& location, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("mouse-leave", CreateEventFromFlags(modifiers), location);
|
2017-06-14 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 18:04:58 +00:00
|
|
|
void Tray::OnMouseMoved(const gfx::Point& location, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("mouse-move", CreateEventFromFlags(modifiers), location);
|
2017-08-26 18:04:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:28:34 +00:00
|
|
|
void Tray::OnMouseUp(const gfx::Point& location, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("mouse-up", CreateEventFromFlags(modifiers), location);
|
2020-01-17 16:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::OnMouseDown(const gfx::Point& location, int modifiers) {
|
2020-04-27 18:38:43 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-02-13 21:39:18 +00:00
|
|
|
EmitWithoutEvent("mouse-down", CreateEventFromFlags(modifiers), location);
|
2020-01-17 16:28:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 00:45:43 +00:00
|
|
|
void Tray::OnDragEntered() {
|
2015-11-10 15:27:39 +00:00
|
|
|
Emit("drag-enter");
|
2015-11-06 00:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::OnDragExited() {
|
2015-11-10 15:27:39 +00:00
|
|
|
Emit("drag-leave");
|
2015-11-06 00:45:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 16:02:50 +00:00
|
|
|
void Tray::OnDragEnded() {
|
|
|
|
Emit("drag-end");
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:32:02 +00:00
|
|
|
void Tray::Destroy() {
|
2022-02-24 19:03:59 +00:00
|
|
|
Unpin();
|
2020-03-30 01:32:02 +00:00
|
|
|
menu_.Reset();
|
|
|
|
tray_icon_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tray::IsDestroyed() {
|
|
|
|
return !tray_icon_;
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
void Tray::SetImage(v8::Isolate* isolate, v8::Local<v8::Value> image) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2021-01-04 20:58:31 +00:00
|
|
|
|
|
|
|
NativeImage* native_image = nullptr;
|
|
|
|
if (!NativeImage::TryConvertNativeImage(isolate, image, &native_image))
|
|
|
|
return;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-01-04 20:58:31 +00:00
|
|
|
tray_icon_->SetImage(native_image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
2016-05-20 07:55:22 +00:00
|
|
|
#else
|
2021-01-04 20:58:31 +00:00
|
|
|
tray_icon_->SetImage(native_image->image());
|
2016-05-20 07:55:22 +00:00
|
|
|
#endif
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
void Tray::SetPressedImage(v8::Isolate* isolate, v8::Local<v8::Value> image) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2021-01-04 20:58:31 +00:00
|
|
|
|
|
|
|
NativeImage* native_image = nullptr;
|
|
|
|
if (!NativeImage::TryConvertNativeImage(isolate, image, &native_image))
|
|
|
|
return;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-01-04 20:58:31 +00:00
|
|
|
tray_icon_->SetPressedImage(
|
|
|
|
native_image->GetHICON(GetSystemMetrics(SM_CXSMICON)));
|
2016-05-20 07:55:22 +00:00
|
|
|
#else
|
2021-01-04 20:58:31 +00:00
|
|
|
tray_icon_->SetPressedImage(native_image->image());
|
2016-05-20 07:55:22 +00:00
|
|
|
#endif
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 07:14:40 +00:00
|
|
|
void Tray::SetToolTip(const std::string& tool_tip) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2014-05-30 15:57:54 +00:00
|
|
|
tray_icon_->SetToolTip(tool_tip);
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:39:29 +00:00
|
|
|
void Tray::SetTitle(const std::string& title,
|
2021-06-03 08:05:04 +00:00
|
|
|
const absl::optional<gin_helper::Dictionary>& options,
|
2020-08-23 21:39:29 +00:00
|
|
|
gin::Arguments* args) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-08-23 21:39:29 +00:00
|
|
|
TrayIcon::TitleOptions title_options;
|
|
|
|
if (options) {
|
|
|
|
if (options->Get("fontType", &title_options.font_type)) {
|
|
|
|
// Validate the font type if it's passed in
|
|
|
|
if (title_options.font_type != "monospaced" &&
|
|
|
|
title_options.font_type != "monospacedDigit") {
|
|
|
|
args->ThrowTypeError(
|
|
|
|
"fontType must be one of 'monospaced' or 'monospacedDigit'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (options->Has("fontType")) {
|
|
|
|
args->ThrowTypeError(
|
|
|
|
"fontType must be one of 'monospaced' or 'monospacedDigit'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (args->Length() >= 2) {
|
|
|
|
args->ThrowTypeError("setTitle options must be an object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tray_icon_->SetTitle(title, title_options);
|
2019-03-18 19:40:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Tray::GetTitle() {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return std::string();
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-03-18 19:40:34 +00:00
|
|
|
return tray_icon_->GetTitle();
|
|
|
|
#else
|
|
|
|
return "";
|
|
|
|
#endif
|
2014-09-09 11:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 01:07:26 +00:00
|
|
|
void Tray::SetIgnoreDoubleClickEvents(bool ignore) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2018-04-02 01:07:26 +00:00
|
|
|
tray_icon_->SetIgnoreDoubleClickEvents(ignore);
|
2018-04-29 03:07:32 +00:00
|
|
|
#endif
|
2018-04-02 01:07:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 07:43:46 +00:00
|
|
|
bool Tray::GetIgnoreDoubleClickEvents() {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return false;
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2018-05-03 07:43:46 +00:00
|
|
|
return tray_icon_->GetIgnoreDoubleClickEvents();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
void Tray::DisplayBalloon(gin_helper::ErrorThrower thrower,
|
|
|
|
const gin_helper::Dictionary& options) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2019-08-08 21:43:33 +00:00
|
|
|
TrayIcon::BalloonOptions balloon_options;
|
|
|
|
|
|
|
|
if (!options.Get("title", &balloon_options.title) ||
|
|
|
|
!options.Get("content", &balloon_options.content)) {
|
2019-10-21 07:05:40 +00:00
|
|
|
thrower.ThrowError("'title' and 'content' must be defined");
|
2014-11-28 10:39:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
v8::Local<v8::Value> icon_value;
|
|
|
|
NativeImage* icon = nullptr;
|
|
|
|
if (options.Get("icon", &icon_value) &&
|
|
|
|
!NativeImage::TryConvertNativeImage(thrower.isolate(), icon_value,
|
|
|
|
&icon)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-08 21:43:33 +00:00
|
|
|
options.Get("iconType", &balloon_options.icon_type);
|
|
|
|
options.Get("largeIcon", &balloon_options.large_icon);
|
|
|
|
options.Get("noSound", &balloon_options.no_sound);
|
|
|
|
options.Get("respectQuietTime", &balloon_options.respect_quiet_time);
|
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
if (icon) {
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2019-08-08 21:43:33 +00:00
|
|
|
balloon_options.icon = icon->GetHICON(
|
|
|
|
GetSystemMetrics(balloon_options.large_icon ? SM_CXICON : SM_CXSMICON));
|
2016-05-20 07:55:22 +00:00
|
|
|
#else
|
2019-08-08 21:43:33 +00:00
|
|
|
balloon_options.icon = icon->image();
|
2016-05-20 07:55:22 +00:00
|
|
|
#endif
|
2019-08-08 21:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tray_icon_->DisplayBalloon(balloon_options);
|
2014-11-28 10:39:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 15:52:47 +00:00
|
|
|
void Tray::RemoveBalloon() {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2019-08-05 15:52:47 +00:00
|
|
|
tray_icon_->RemoveBalloon();
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:43:48 +00:00
|
|
|
void Tray::Focus() {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2019-08-09 14:43:48 +00:00
|
|
|
tray_icon_->Focus();
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:32:02 +00:00
|
|
|
void Tray::PopUpContextMenu(gin::Arguments* args) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2019-10-21 07:05:40 +00:00
|
|
|
gin::Handle<Menu> menu;
|
2015-07-16 03:39:49 +00:00
|
|
|
gfx::Point pos;
|
2020-03-30 01:32:02 +00:00
|
|
|
|
|
|
|
v8::Local<v8::Value> first_arg;
|
|
|
|
if (args->GetNext(&first_arg)) {
|
|
|
|
if (!gin::ConvertFromV8(args->isolate(), first_arg, &menu)) {
|
|
|
|
if (!gin::ConvertFromV8(args->isolate(), first_arg, &pos)) {
|
|
|
|
args->ThrowError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (args->Length() >= 2) {
|
|
|
|
if (!args->GetNext(&pos)) {
|
|
|
|
args->ThrowError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 11:05:22 +00:00
|
|
|
tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
|
2015-07-16 02:50:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 23:25:17 +00:00
|
|
|
void Tray::CloseContextMenu() {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2020-01-22 23:25:17 +00:00
|
|
|
tray_icon_->CloseContextMenu();
|
|
|
|
}
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
void Tray::SetContextMenu(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> arg) {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return;
|
2019-10-21 07:05:40 +00:00
|
|
|
gin::Handle<Menu> menu;
|
|
|
|
if (arg->IsNull()) {
|
|
|
|
menu_.Reset();
|
|
|
|
tray_icon_->SetContextMenu(nullptr);
|
|
|
|
} else if (gin::ConvertFromV8(thrower.isolate(), arg, &menu)) {
|
|
|
|
menu_.Reset(thrower.isolate(), menu.ToV8());
|
|
|
|
tray_icon_->SetContextMenu(menu->model());
|
|
|
|
} else {
|
|
|
|
thrower.ThrowTypeError("Must pass Menu or null");
|
|
|
|
}
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 06:40:30 +00:00
|
|
|
gfx::Rect Tray::GetBounds() {
|
2020-04-03 00:22:46 +00:00
|
|
|
if (!CheckAlive())
|
2020-03-30 01:32:02 +00:00
|
|
|
return gfx::Rect();
|
2016-06-21 06:40:30 +00:00
|
|
|
return tray_icon_->GetBounds();
|
|
|
|
}
|
|
|
|
|
2020-04-03 00:22:46 +00:00
|
|
|
bool Tray::CheckAlive() {
|
2020-03-30 01:32:02 +00:00
|
|
|
if (!tray_icon_) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-03-30 01:32:02 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
gin_helper::ErrorThrower(isolate).ThrowError("Tray is destroyed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
// static
|
2023-02-06 20:59:49 +00:00
|
|
|
void Tray::FillObjectTemplate(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> templ) {
|
|
|
|
gin::ObjectTemplateBuilder(isolate, "Tray", templ)
|
2020-03-30 01:32:02 +00:00
|
|
|
.SetMethod("destroy", &Tray::Destroy)
|
|
|
|
.SetMethod("isDestroyed", &Tray::IsDestroyed)
|
2014-05-30 15:57:54 +00:00
|
|
|
.SetMethod("setImage", &Tray::SetImage)
|
|
|
|
.SetMethod("setPressedImage", &Tray::SetPressedImage)
|
|
|
|
.SetMethod("setToolTip", &Tray::SetToolTip)
|
2014-09-09 11:33:58 +00:00
|
|
|
.SetMethod("setTitle", &Tray::SetTitle)
|
2019-03-18 19:40:34 +00:00
|
|
|
.SetMethod("getTitle", &Tray::GetTitle)
|
2018-04-02 01:07:26 +00:00
|
|
|
.SetMethod("setIgnoreDoubleClickEvents",
|
|
|
|
&Tray::SetIgnoreDoubleClickEvents)
|
2018-05-03 07:43:46 +00:00
|
|
|
.SetMethod("getIgnoreDoubleClickEvents",
|
|
|
|
&Tray::GetIgnoreDoubleClickEvents)
|
2014-11-28 10:39:30 +00:00
|
|
|
.SetMethod("displayBalloon", &Tray::DisplayBalloon)
|
2019-08-05 15:52:47 +00:00
|
|
|
.SetMethod("removeBalloon", &Tray::RemoveBalloon)
|
2019-08-09 14:43:48 +00:00
|
|
|
.SetMethod("focus", &Tray::Focus)
|
2015-08-10 05:00:15 +00:00
|
|
|
.SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
|
2020-01-22 23:25:17 +00:00
|
|
|
.SetMethod("closeContextMenu", &Tray::CloseContextMenu)
|
2016-06-21 06:40:30 +00:00
|
|
|
.SetMethod("setContextMenu", &Tray::SetContextMenu)
|
2020-03-30 01:32:02 +00:00
|
|
|
.SetMethod("getBounds", &Tray::GetBounds)
|
|
|
|
.Build();
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|
2014-05-30 15:57:54 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-08-02 08:02:04 +00:00
|
|
|
using electron::api::Tray;
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2016-08-02 08:02:04 +00:00
|
|
|
|
2020-03-30 01:32:02 +00:00
|
|
|
gin::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("Tray", Tray::GetConstructor(context));
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_tray, Initialize)
|