Extend the custom Jump List API

Add `app.getJumpListSettings()` and `app.setJumpList(callback)` that
make it possible to fully customize the Jump List of an Electron app.
It is now possible to:
- Add tasks to the standard `Tasks` category.
- Add separators to the standard `Tasks` category.
- Add custom categories containing tasks and file links.
- Add system managed Recent/Frequent categories.
- Remove the custom Jump List.
This commit is contained in:
Vadim Macagon 2016-08-12 16:31:17 +07:00
parent b5dec9990e
commit c64294cf60
7 changed files with 897 additions and 41 deletions

View file

@ -44,6 +44,7 @@
#include "ui/gfx/image/image.h"
#if defined(OS_WIN)
#include "atom/browser/ui/win/jump_list.h"
#include "base/strings/utf_string_conversions.h"
#endif
@ -70,6 +71,220 @@ struct Converter<Browser::UserTask> {
return true;
}
};
using atom::JumpListItem;
using atom::JumpListCategory;
using atom::JumpListResult;
template<>
struct Converter<JumpListItem::Type> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
JumpListItem::Type* out) {
std::string item_type;
if (!ConvertFromV8(isolate, val, &item_type))
return false;
if (item_type == "task")
*out = JumpListItem::Type::TASK;
else if (item_type == "separator")
*out = JumpListItem::Type::SEPARATOR;
else if (item_type == "file")
*out = JumpListItem::Type::FILE;
else
return false;
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
JumpListItem::Type val) {
std::string item_type;
switch (val) {
case JumpListItem::Type::TASK:
item_type = "task";
break;
case JumpListItem::Type::SEPARATOR:
item_type = "separator";
break;
case JumpListItem::Type::FILE:
item_type = "file";
break;
}
return mate::ConvertToV8(isolate, item_type);
}
};
template<>
struct Converter<JumpListItem> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
JumpListItem* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
if (!dict.Get("type", &(out->type)))
return false;
switch (out->type) {
case JumpListItem::Type::TASK:
if (!dict.Get("program", &(out->path)) ||
!dict.Get("title", &(out->title)))
return false;
if (dict.Get("iconPath", &(out->icon_path)) &&
!dict.Get("iconIndex", &(out->icon_index)))
return false;
dict.Get("args", &(out->arguments));
dict.Get("description", &(out->description));
return true;
case JumpListItem::Type::SEPARATOR:
return true;
case JumpListItem::Type::FILE:
return dict.Get("path", &(out->path));
}
assert(false);
return false;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const JumpListItem& val) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("type", val.type);
switch (val.type) {
case JumpListItem::Type::TASK:
dict.Set("program", val.path);
dict.Set("args", val.arguments);
dict.Set("title", val.title);
dict.Set("iconPath", val.icon_path);
dict.Set("iconIndex", val.icon_index);
dict.Set("description", val.description);
break;
case JumpListItem::Type::SEPARATOR:
break;
case JumpListItem::Type::FILE:
dict.Set("path", val.path);
break;
}
return dict.GetHandle();
}
};
template<>
struct Converter<JumpListCategory::Type> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
JumpListCategory::Type* out) {
std::string category_type;
if (!ConvertFromV8(isolate, val, &category_type))
return false;
if (category_type == "tasks")
*out = JumpListCategory::Type::TASKS;
else if (category_type == "frequent")
*out = JumpListCategory::Type::FREQUENT;
else if (category_type == "recent")
*out = JumpListCategory::Type::RECENT;
else if (category_type == "custom")
*out = JumpListCategory::Type::CUSTOM;
else
return false;
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
JumpListCategory::Type val) {
std::string category_type;
switch (val) {
case JumpListCategory::Type::TASKS:
category_type = "tasks";
break;
case JumpListCategory::Type::FREQUENT:
category_type = "frequent";
break;
case JumpListCategory::Type::RECENT:
category_type = "recent";
break;
case JumpListCategory::Type::CUSTOM:
category_type = "custom";
break;
}
return mate::ConvertToV8(isolate, category_type);
}
};
template<>
struct Converter<JumpListCategory> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
JumpListCategory* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
if (dict.Get("name", &(out->name)) && out->name.empty())
return false;
if (!dict.Get("type", &(out->type))) {
if (out->name.empty())
out->type = JumpListCategory::Type::TASKS;
else
out->type = JumpListCategory::Type::CUSTOM;
}
if ((out->type == JumpListCategory::Type::TASKS) ||
(out->type == JumpListCategory::Type::CUSTOM)) {
if (!dict.Get("items", &(out->items)))
return false;
}
return true;
}
};
// static
template<>
struct Converter<JumpListResult> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, JumpListResult val) {
std::string result_code;
switch (val) {
case JumpListResult::SUCCESS:
result_code = "ok";
break;
case JumpListResult::ARGUMENT_ERROR:
result_code = "argumentError";
break;
case JumpListResult::GENERIC_ERROR:
result_code = "error";
break;
case JumpListResult::CUSTOM_CATEGORY_SEPARATOR_ERROR:
result_code = "invalidSeparatorError";
break;
case JumpListResult::MISSING_FILE_TYPE_REGISTRATION_ERROR:
result_code = "fileTypeRegistrationError";
break;
case JumpListResult::CUSTOM_CATEGORY_ACCESS_DENIED_ERROR:
result_code = "customCategoryAccessDeniedError";
break;
}
return ConvertToV8(isolate, result_code);
}
};
#endif
template<>
@ -523,6 +738,63 @@ void App::OnCertificateManagerModelCreated(
}
#endif
#if defined(OS_WIN)
v8::Local<v8::Value> App::GetJumpListSettings() {
JumpList jump_list(Browser::Get()->GetAppUserModelID());
int min_items = 10;
std::vector<JumpListItem> removed_items;
if (jump_list.Begin(&min_items, &removed_items)) {
// We don't actually want to change anything, so abort the transaction.
jump_list.Abort();
} else {
LOG(ERROR) << "Failed to begin Jump List transaction.";
}
auto dict = mate::Dictionary::CreateEmpty(isolate());
dict.Set("minItems", min_items);
dict.Set("removedItems", mate::ConvertToV8(isolate(), removed_items));
return dict.GetHandle();
}
JumpListResult App::SetJumpList(v8::Local<v8::Value> val,
mate::Arguments* args) {
std::vector<JumpListCategory> categories;
bool delete_jump_list = val->IsNull();
if (!delete_jump_list &&
!mate::ConvertFromV8(args->isolate(), val, &categories)) {
args->ThrowError("Argument must be null or an array of categories");
return JumpListResult::ARGUMENT_ERROR;
}
JumpList jump_list(Browser::Get()->GetAppUserModelID());
if (delete_jump_list) {
return jump_list.Delete()
? JumpListResult::SUCCESS
: JumpListResult::GENERIC_ERROR;
}
// Start a transaction that updates the JumpList of this application.
if (!jump_list.Begin())
return JumpListResult::GENERIC_ERROR;
JumpListResult result = jump_list.AppendCategories(categories);
// AppendCategories may have failed to add some categories, but it's better
// to have something than nothing so try to commit the changes anyway.
if (!jump_list.Commit()) {
LOG(ERROR) << "Failed to commit changes to custom Jump List.";
// It's more useful to return the earlier error code that might give
// some indication as to why the transaction actually failed, so don't
// overwrite it with a "generic error" code here.
if (result == JumpListResult::SUCCESS)
result = JumpListResult::GENERIC_ERROR;
}
return result;
}
#endif // defined(OS_WIN)
// static
mate::Handle<App> App::Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new App(isolate));
@ -570,6 +842,8 @@ void App::BuildPrototype(
#endif
#if defined(OS_WIN)
.SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser))
.SetMethod("getJumpListSettings", &App::GetJumpListSettings)
.SetMethod("setJumpList", &App::SetJumpList)
#endif
#if defined(OS_LINUX)
.SetMethod("isUnityRunning",