Translate menu template directly in C++
This commit is contained in:
parent
56e6b28370
commit
9236adfbf5
5 changed files with 100 additions and 98 deletions
|
@ -47,6 +47,7 @@
|
||||||
#include "content/public/browser/storage_partition.h"
|
#include "content/public/browser/storage_partition.h"
|
||||||
#include "content/public/browser/site_instance.h"
|
#include "content/public/browser/site_instance.h"
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
|
#include "content/public/common/context_menu_params.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
#include "net/http/http_response_headers.h"
|
#include "net/http/http_response_headers.h"
|
||||||
|
@ -407,8 +408,11 @@ void WebContents::RendererResponsive(content::WebContents* source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {
|
bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {
|
||||||
context_menu_context_ = params.custom_context;
|
if (!params.custom_context.is_pepper_menu)
|
||||||
Emit("-context-menu", params);
|
return false;
|
||||||
|
|
||||||
|
Emit("pepper-context-menu", std::make_pair(params, web_contents()));
|
||||||
|
web_contents()->NotifyContextMenuClosed(params.custom_context);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -848,15 +852,6 @@ void WebContents::RemoveWorkSpace(mate::Arguments* args,
|
||||||
DevToolsRemoveFileSystem(path);
|
DevToolsRemoveFileSystem(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::ExecuteContextMenuCommand(int action) {
|
|
||||||
web_contents()->ExecuteCustomContextMenuCommand(action,
|
|
||||||
context_menu_context_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::NotifyContextMenuClosed() {
|
|
||||||
web_contents()->NotifyContextMenuClosed(context_menu_context_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebContents::Undo() {
|
void WebContents::Undo() {
|
||||||
web_contents()->Undo();
|
web_contents()->Undo();
|
||||||
}
|
}
|
||||||
|
@ -1068,10 +1063,6 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
|
||||||
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
|
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
|
||||||
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
|
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
|
||||||
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
|
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
|
||||||
.SetMethod("_executeContextMenuCommand",
|
|
||||||
&WebContents::ExecuteContextMenuCommand)
|
|
||||||
.SetMethod("_notifyContextMenuClosed",
|
|
||||||
&WebContents::NotifyContextMenuClosed)
|
|
||||||
.SetProperty("session", &WebContents::Session, true)
|
.SetProperty("session", &WebContents::Session, true)
|
||||||
.SetProperty("devToolsWebContents",
|
.SetProperty("devToolsWebContents",
|
||||||
&WebContents::DevToolsWebContents, true)
|
&WebContents::DevToolsWebContents, true)
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "atom/browser/common_web_contents_delegate.h"
|
#include "atom/browser/common_web_contents_delegate.h"
|
||||||
#include "content/public/browser/web_contents_observer.h"
|
#include "content/public/browser/web_contents_observer.h"
|
||||||
#include "content/public/common/context_menu_params.h"
|
|
||||||
#include "content/public/common/favicon_url.h"
|
#include "content/public/common/favicon_url.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
#include "ui/gfx/image/image.h"
|
#include "ui/gfx/image/image.h"
|
||||||
|
@ -93,8 +92,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
void SetAudioMuted(bool muted);
|
void SetAudioMuted(bool muted);
|
||||||
bool IsAudioMuted();
|
bool IsAudioMuted();
|
||||||
void Print(mate::Arguments* args);
|
void Print(mate::Arguments* args);
|
||||||
void ExecuteContextMenuCommand(int action);
|
|
||||||
void NotifyContextMenuClosed();
|
|
||||||
|
|
||||||
// Print current page as PDF.
|
// Print current page as PDF.
|
||||||
void PrintToPDF(const base::DictionaryValue& setting,
|
void PrintToPDF(const base::DictionaryValue& setting,
|
||||||
|
@ -259,9 +256,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
// embedders' zoom level change.
|
// embedders' zoom level change.
|
||||||
void OnZoomLevelChanged(double level);
|
void OnZoomLevelChanged(double level);
|
||||||
|
|
||||||
// Recent unhandled context menu context.
|
|
||||||
content::CustomContextMenuContext context_menu_context_;
|
|
||||||
|
|
||||||
v8::Global<v8::Value> session_;
|
v8::Global<v8::Value> session_;
|
||||||
v8::Global<v8::Value> devtools_web_contents_;
|
v8::Global<v8::Value> devtools_web_contents_;
|
||||||
|
|
||||||
|
|
|
@ -35,36 +35,6 @@ PDFPageSize =
|
||||||
width_microns: 279400
|
width_microns: 279400
|
||||||
custom_display_name: "Tabloid"
|
custom_display_name: "Tabloid"
|
||||||
|
|
||||||
clickHandler = (action) ->
|
|
||||||
@_executeContextMenuCommand action
|
|
||||||
|
|
||||||
convertToMenuTemplate = (items, handler) ->
|
|
||||||
template = []
|
|
||||||
for item in items
|
|
||||||
do (item) ->
|
|
||||||
transformed =
|
|
||||||
if item.type is 'submenu'
|
|
||||||
type: 'submenu'
|
|
||||||
label: item.label
|
|
||||||
enabled: item.enabled
|
|
||||||
submenu: convertToMenuTemplate item.subItems, handler
|
|
||||||
else if item.type is 'separator'
|
|
||||||
type: 'separator'
|
|
||||||
else if item.type is 'checkbox'
|
|
||||||
type: 'checkbox'
|
|
||||||
label: item.label
|
|
||||||
enabled: item.enabled
|
|
||||||
checked: item.checked
|
|
||||||
else
|
|
||||||
type: 'normal'
|
|
||||||
label: item.label
|
|
||||||
enabled: item.enabled
|
|
||||||
if item.id?
|
|
||||||
transformed.click = ->
|
|
||||||
handler item.id
|
|
||||||
template.push transformed
|
|
||||||
template
|
|
||||||
|
|
||||||
wrapWebContents = (webContents) ->
|
wrapWebContents = (webContents) ->
|
||||||
# webContents is an EventEmitter.
|
# webContents is an EventEmitter.
|
||||||
webContents.__proto__ = EventEmitter.prototype
|
webContents.__proto__ = EventEmitter.prototype
|
||||||
|
@ -96,15 +66,10 @@ wrapWebContents = (webContents) ->
|
||||||
Object.defineProperty event, 'returnValue', set: (value) -> event.sendReply JSON.stringify(value)
|
Object.defineProperty event, 'returnValue', set: (value) -> event.sendReply JSON.stringify(value)
|
||||||
ipc.emit channel, event, args...
|
ipc.emit channel, event, args...
|
||||||
|
|
||||||
# Handle context menu action request from renderer widget.
|
# Handle context menu action request from pepper plugin.
|
||||||
webContents.on '-context-menu', (event, params) ->
|
webContents.on 'pepper-context-menu', (event, params) ->
|
||||||
if params.isPepperMenu
|
menu = Menu.buildFromTemplate params.menu
|
||||||
template = convertToMenuTemplate(params.menuItems, clickHandler.bind(webContents))
|
menu.popup params.x, params.y
|
||||||
menu = Menu.buildFromTemplate template
|
|
||||||
# The menu is expected to show asynchronously.
|
|
||||||
setImmediate ->
|
|
||||||
menu.popup params.x, params.y
|
|
||||||
webContents._notifyContextMenuClosed()
|
|
||||||
|
|
||||||
webContents.printToPDF = (options, callback) ->
|
webContents.printToPDF = (options, callback) ->
|
||||||
printingSetting =
|
printingSetting =
|
||||||
|
|
|
@ -4,50 +4,96 @@
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/content_converter.h"
|
#include "atom/common/native_mate_converters/content_converter.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
|
#include "content/public/browser/web_contents.h"
|
||||||
#include "content/public/common/context_menu_params.h"
|
#include "content/public/common/context_menu_params.h"
|
||||||
#include "content/public/common/menu_item.h"
|
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void ExecuteCommand(content::WebContents* web_contents,
|
||||||
|
int action,
|
||||||
|
const content::CustomContextMenuContext& context) {
|
||||||
|
web_contents->ExecuteCustomContextMenuCommand(action, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward declaration for nested recursive call.
|
||||||
|
v8::Local<v8::Value> MenuToV8(v8::Isolate* isolate,
|
||||||
|
content::WebContents* web_contents,
|
||||||
|
const content::CustomContextMenuContext& context,
|
||||||
|
const std::vector<content::MenuItem>& menu);
|
||||||
|
|
||||||
|
v8::Local<v8::Value> MenuItemToV8(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
content::WebContents* web_contents,
|
||||||
|
const content::CustomContextMenuContext& context,
|
||||||
|
const content::MenuItem& item) {
|
||||||
|
mate::Dictionary v8_item = mate::Dictionary::CreateEmpty(isolate);
|
||||||
|
switch (item.type) {
|
||||||
|
case content::MenuItem::CHECKABLE_OPTION:
|
||||||
|
case content::MenuItem::GROUP:
|
||||||
|
v8_item.Set("checked", item.checked);
|
||||||
|
case content::MenuItem::OPTION:
|
||||||
|
case content::MenuItem::SUBMENU:
|
||||||
|
v8_item.Set("label", item.label);
|
||||||
|
v8_item.Set("enabled", item.enabled);
|
||||||
|
default:
|
||||||
|
v8_item.Set("type", item.type);
|
||||||
|
}
|
||||||
|
if (item.type == content::MenuItem::SUBMENU)
|
||||||
|
v8_item.Set("submenu",
|
||||||
|
MenuToV8(isolate, web_contents, context, item.submenu));
|
||||||
|
else if (item.action > 0)
|
||||||
|
v8_item.Set("click",
|
||||||
|
base::Bind(ExecuteCommand, web_contents, item.action, context));
|
||||||
|
return v8_item.GetHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> MenuToV8(v8::Isolate* isolate,
|
||||||
|
content::WebContents* web_contents,
|
||||||
|
const content::CustomContextMenuContext& context,
|
||||||
|
const std::vector<content::MenuItem>& menu) {
|
||||||
|
std::vector<v8::Local<v8::Value>> v8_menu;
|
||||||
|
for (const auto& menu_item : menu)
|
||||||
|
v8_menu.push_back(MenuItemToV8(isolate, web_contents, context, menu_item));
|
||||||
|
return mate::ConvertToV8(isolate, v8_menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
template<>
|
|
||||||
struct Converter<content::MenuItem::Type> {
|
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
||||||
const content::MenuItem::Type& val) {
|
|
||||||
switch (val) {
|
|
||||||
case content::MenuItem::CHECKABLE_OPTION:
|
|
||||||
return StringToV8(isolate, "checkbox");
|
|
||||||
case content::MenuItem::SEPARATOR:
|
|
||||||
return StringToV8(isolate, "separator");
|
|
||||||
case content::MenuItem::SUBMENU:
|
|
||||||
return StringToV8(isolate, "submenu");
|
|
||||||
default:
|
|
||||||
return StringToV8(isolate, "normal");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> Converter<content::MenuItem>::ToV8(
|
v8::Local<v8::Value> Converter<content::MenuItem::Type>::ToV8(
|
||||||
v8::Isolate* isolate, const content::MenuItem& val) {
|
v8::Isolate* isolate, const content::MenuItem::Type& val) {
|
||||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
switch (val) {
|
||||||
dict.Set("id", val.action);
|
case content::MenuItem::CHECKABLE_OPTION:
|
||||||
dict.Set("type", val.type);
|
return StringToV8(isolate, "checkbox");
|
||||||
dict.Set("label", val.label);
|
case content::MenuItem::GROUP:
|
||||||
dict.Set("enabled", val.enabled);
|
return StringToV8(isolate, "radio");
|
||||||
dict.Set("checked", val.checked);
|
case content::MenuItem::SEPARATOR:
|
||||||
return mate::ConvertToV8(isolate, dict);
|
return StringToV8(isolate, "separator");
|
||||||
|
case content::MenuItem::SUBMENU:
|
||||||
|
return StringToV8(isolate, "submenu");
|
||||||
|
case content::MenuItem::OPTION:
|
||||||
|
default:
|
||||||
|
return StringToV8(isolate, "normal");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> Converter<content::ContextMenuParams>::ToV8(
|
v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
|
||||||
v8::Isolate* isolate, const content::ContextMenuParams& val) {
|
v8::Isolate* isolate, const ContextMenuParamsWithWebContents& val) {
|
||||||
|
const auto& params = val.first;
|
||||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||||
dict.Set("x", val.x);
|
dict.Set("x", params.x);
|
||||||
dict.Set("y", val.y);
|
dict.Set("y", params.y);
|
||||||
dict.Set("isPepperMenu", val.custom_context.is_pepper_menu);
|
if (params.custom_context.is_pepper_menu)
|
||||||
dict.Set("menuItems", val.custom_items);
|
dict.Set("menu", MenuToV8(isolate, val.second, params.custom_context,
|
||||||
|
params.custom_items));
|
||||||
return mate::ConvertToV8(isolate, dict);
|
return mate::ConvertToV8(isolate, dict);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,25 +5,31 @@
|
||||||
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
||||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "content/public/common/menu_item.h"
|
||||||
#include "native_mate/converter.h"
|
#include "native_mate/converter.h"
|
||||||
|
|
||||||
namespace content {
|
namespace content {
|
||||||
struct ContextMenuParams;
|
struct ContextMenuParams;
|
||||||
struct MenuItem;
|
class WebContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using ContextMenuParamsWithWebContents =
|
||||||
|
std::pair<content::ContextMenuParams, content::WebContents*>;
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct Converter<content::MenuItem> {
|
struct Converter<content::MenuItem::Type> {
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
const content::MenuItem& val);
|
const content::MenuItem::Type& val);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct Converter<content::ContextMenuParams> {
|
struct Converter<ContextMenuParamsWithWebContents> {
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
const content::ContextMenuParams& val);
|
const ContextMenuParamsWithWebContents& val);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
Loading…
Reference in a new issue