如何在可可事件模型 NPAPI 插件中获取 NSView
我遵循了夜间 Webkit 构建中的 NetscapeCocoaPlugin 示例,并且能够构建使用 Cocoa 事件模型的 NPAPI 样式插件。
我现在的问题是如何在 NPP_SetWindow 中获取 NSView。
此 线程 中的海报说可以使用 [NSView focusView],但我还没有能够让它工作
我当前的功能如下所示:
NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
PluginObject *obj = instance->pdata;
obj->window = *window;
NSLog(@"Set Window called");
NSView* currentView = [NSView focusView];
[[NSColor redColor] set]; // Sets current drawing color.
NSRectFill(NSMakeRect(10, 10, 2, 20)); // Defines a rectangle and then fills it with the current drawing color.
[[NSColor colorWithCalibratedRed:0.7 green:0.9 blue:0.3 alpha:1.0] set]; // Sets a new color.
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(5, 0, 10, 10)] fill]; // Draws a circle in the new color.
[currentView setNeedsDisplay:YES];
return NPERR_NO_ERROR;
}
I've followed the NetscapeCocoaPlugin example from the nightly Webkit build, and I'm able to build a NPAPI style plugin that uses the Cocoa Event Model.
My question now, is how I can get the NSView inside NPP_SetWindow.
A poster in this thread, says that it's possible using [NSView focusView], but I haven't been able to get this to work
My current function looks like this:
NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
PluginObject *obj = instance->pdata;
obj->window = *window;
NSLog(@"Set Window called");
NSView* currentView = [NSView focusView];
[[NSColor redColor] set]; // Sets current drawing color.
NSRectFill(NSMakeRect(10, 10, 2, 20)); // Defines a rectangle and then fills it with the current drawing color.
[[NSColor colorWithCalibratedRed:0.7 green:0.9 blue:0.3 alpha:1.0] set]; // Sets a new color.
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(5, 0, 10, 10)] fill]; // Draws a circle in the new color.
[currentView setNeedsDisplay:YES];
return NPERR_NO_ERROR;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。曾经有一段时间,你可以使用 hack 来获取 NSView,但它从未得到支持,从来都不是一个好主意,并且不再可能,因为所有三个浏览器都已切换到使用进程外插件,这意味着你无法访问到 NSView。
您可以获取 CGContextRef,然后创建自己的屏幕外 NSWindow 和 NSView 并将它们渲染到 CGContextRef 中,但随后您还必须代理所有事件。 WebView 包装器 在 <一个仍然处于实验阶段的 href="http://www.firebreath.org" rel="nofollow">FireBreath 可以做到这一点,但这是相当痛苦的。最终我计划将其变成更通用的东西,以便 NSView 可以(有点)在插件中使用,但没有本地方法可以做到这一点。
这里有一篇关于 mac 绘图模型的优秀博客文章: http:// /www.escapedthoughts.com/weblog/geek/P110308-mac-npapi-plugins.writeback
You can't. There was a time when you could get the NSView using a hack, but it was never supported, never a good idea, and no longer possible because all three browsers have switched to using out of process plugins, which means you can't get access to the NSView.
You can get a CGContextRef and then create your own offscreen NSWindow and NSView and render them into the CGContextRef, but then you'd have to proxy all events as well. There is a WebView wrapper in FireBreath that is experimental still that does this, but it is quite a pain. Eventually I plan to turn it into something more generic so that an NSView can (kinda) be used in a plugin, but there is no native way to do so.
There is an excellent blog post about mac drawing models here: http://www.escapedthoughts.com/weblog/geek/P110308-mac-npapi-plugins.writeback