feat: add frame to context-menu event params (#30831)
* feat: add frame to context-menu event params * doc: rephrase frame description
This commit is contained in:
parent
52bacd38a9
commit
70c534fd14
5 changed files with 36 additions and 8 deletions
|
@ -651,6 +651,7 @@ Returns:
|
||||||
* `params` Object
|
* `params` Object
|
||||||
* `x` Integer - x coordinate.
|
* `x` Integer - x coordinate.
|
||||||
* `y` Integer - y coordinate.
|
* `y` Integer - y coordinate.
|
||||||
|
* `frame` WebFrameMain - Frame from which the context menu was invoked.
|
||||||
* `linkURL` String - URL of the link that encloses the node the context menu
|
* `linkURL` String - URL of the link that encloses the node the context menu
|
||||||
was invoked on.
|
was invoked on.
|
||||||
* `linkText` String - Text associated with the link. May be an empty
|
* `linkText` String - Text associated with the link. May be an empty
|
||||||
|
|
|
@ -1287,7 +1287,7 @@ void WebContents::RendererResponsive(
|
||||||
|
|
||||||
bool WebContents::HandleContextMenu(content::RenderFrameHost* render_frame_host,
|
bool WebContents::HandleContextMenu(content::RenderFrameHost* render_frame_host,
|
||||||
const content::ContextMenuParams& params) {
|
const content::ContextMenuParams& params) {
|
||||||
Emit("context-menu", std::make_pair(params, web_contents()));
|
Emit("context-menu", std::make_pair(params, render_frame_host));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include "shell/browser/web_contents_permission_helper.h"
|
#include "shell/browser/web_contents_permission_helper.h"
|
||||||
#include "shell/common/gin_converters/blink_converter.h"
|
#include "shell/common/gin_converters/blink_converter.h"
|
||||||
#include "shell/common/gin_converters/callback_converter.h"
|
#include "shell/common/gin_converters/callback_converter.h"
|
||||||
|
#include "shell/common/gin_converters/frame_converter.h"
|
||||||
#include "shell/common/gin_converters/gfx_converter.h"
|
#include "shell/common/gin_converters/gfx_converter.h"
|
||||||
#include "shell/common/gin_converters/gurl_converter.h"
|
#include "shell/common/gin_converters/gurl_converter.h"
|
||||||
#include "shell/common/gin_helper/dictionary.h"
|
#include "shell/common/gin_helper/dictionary.h"
|
||||||
|
@ -73,11 +74,13 @@ v8::Local<v8::Value> Converter<blink::mojom::MenuItem::Type>::ToV8(
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
|
v8::Local<v8::Value> Converter<ContextMenuParamsWithRenderFrameHost>::ToV8(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
const ContextMenuParamsWithWebContents& val) {
|
const ContextMenuParamsWithRenderFrameHost& val) {
|
||||||
const auto& params = val.first;
|
const auto& params = val.first;
|
||||||
|
content::RenderFrameHost* render_frame_host = val.second;
|
||||||
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
||||||
|
dict.SetGetter("frame", render_frame_host);
|
||||||
dict.Set("x", params.x);
|
dict.Set("x", params.x);
|
||||||
dict.Set("y", params.y);
|
dict.Set("y", params.y);
|
||||||
dict.Set("linkURL", params.link_url);
|
dict.Set("linkURL", params.link_url);
|
||||||
|
|
|
@ -17,11 +17,12 @@
|
||||||
namespace content {
|
namespace content {
|
||||||
struct ContextMenuParams;
|
struct ContextMenuParams;
|
||||||
struct NativeWebKeyboardEvent;
|
struct NativeWebKeyboardEvent;
|
||||||
|
class RenderFrameHost;
|
||||||
class WebContents;
|
class WebContents;
|
||||||
} // namespace content
|
} // namespace content
|
||||||
|
|
||||||
using ContextMenuParamsWithWebContents =
|
using ContextMenuParamsWithRenderFrameHost =
|
||||||
std::pair<content::ContextMenuParams, content::WebContents*>;
|
std::pair<content::ContextMenuParams, content::RenderFrameHost*>;
|
||||||
|
|
||||||
namespace gin {
|
namespace gin {
|
||||||
|
|
||||||
|
@ -32,9 +33,10 @@ struct Converter<blink::mojom::MenuItem::Type> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<ContextMenuParamsWithWebContents> {
|
struct Converter<ContextMenuParamsWithRenderFrameHost> {
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
static v8::Local<v8::Value> ToV8(
|
||||||
const ContextMenuParamsWithWebContents& val);
|
v8::Isolate* isolate,
|
||||||
|
const ContextMenuParamsWithRenderFrameHost& val);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
|
|
@ -2056,6 +2056,28 @@ describe('webContents module', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('context-menu event', () => {
|
||||||
|
afterEach(closeAllWindows);
|
||||||
|
it('emits when right-clicked in page', async () => {
|
||||||
|
const w = new BrowserWindow({ show: false });
|
||||||
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
||||||
|
|
||||||
|
const promise = emittedOnce(w.webContents, 'context-menu');
|
||||||
|
|
||||||
|
// Simulate right-click to create context-menu event.
|
||||||
|
const opts = { x: 0, y: 0, button: 'right' as any };
|
||||||
|
w.webContents.sendInputEvent({ ...opts, type: 'mouseDown' });
|
||||||
|
w.webContents.sendInputEvent({ ...opts, type: 'mouseUp' });
|
||||||
|
|
||||||
|
const [, params] = await promise;
|
||||||
|
|
||||||
|
expect(params.pageURL).to.equal(w.webContents.getURL());
|
||||||
|
expect(params.frame).to.be.an('object');
|
||||||
|
expect(params.x).to.be.a('number');
|
||||||
|
expect(params.y).to.be.a('number');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('emits a cancelable event before creating a child webcontents', async () => {
|
it('emits a cancelable event before creating a child webcontents', async () => {
|
||||||
const w = new BrowserWindow({
|
const w = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
|
|
Loading…
Reference in a new issue