Listen to ElementSizeChanged event

This commit is contained in:
Cheng Zhao 2015-05-29 13:47:09 +08:00
parent e4bb456964
commit b5c9fe78f1
9 changed files with 104 additions and 8 deletions

View file

@ -62,7 +62,7 @@ struct Converter<atom::api::SetSizeParams> {
out->min_size.reset(new gfx::Size(size));
if (params.Get("max", &size))
out->max_size.reset(new gfx::Size(size));
if (params.Get("elementSize", &size))
if (params.Get("normal", &size))
out->normal_size.reset(new gfx::Size(size));
return true;
}

View file

@ -15,7 +15,6 @@ app.on('ready', function() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
resizable: false,
'auto-hide-menu-bar': true,
'use-content-size': true,
});

View file

@ -58,6 +58,8 @@ createGuest = (embedder, params) ->
@viewInstanceId = params.instanceId
@setSize
normal:
width: params.elementWidth, height: params.elementHeight
enableAutoSize: params.autosize
min:
width: params.minwidth, height: params.minheight

View file

@ -10,9 +10,11 @@
#define USE(WTF_FEATURE) (defined WTF_USE_##WTF_FEATURE && WTF_USE_##WTF_FEATURE) // NOLINT
#define ENABLE(WTF_FEATURE) (defined ENABLE_##WTF_FEATURE && ENABLE_##WTF_FEATURE) // NOLINT
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/renderer/api/atom_api_spell_check_client.h"
#include "content/public/renderer/render_frame.h"
#include "native_mate/callback.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "third_party/WebKit/public/web/WebDocument.h"
@ -78,6 +80,14 @@ v8::Local<v8::Value> WebFrame::RegisterEmbedderCustomElement(
return web_frame_->document().registerEmbedderCustomElement(name, options, c);
}
void WebFrame::RegisterElementResizeCallback(
int element_instance_id,
const GuestViewContainer::ResizeCallback& callback) {
auto guest_view_container = GuestViewContainer::FromID(element_instance_id);
if (guest_view_container)
guest_view_container->RegisterElementResizeCallback(callback);
}
void WebFrame::AttachGuest(int id) {
content::RenderFrame::FromWebFrame(web_frame_)->AttachGuest(id);
}
@ -106,6 +116,8 @@ mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder(
.SetMethod("getZoomFactor", &WebFrame::GetZoomFactor)
.SetMethod("registerEmbedderCustomElement",
&WebFrame::RegisterEmbedderCustomElement)
.SetMethod("registerElementResizeCallback",
&WebFrame::RegisterElementResizeCallback)
.SetMethod("attachGuest", &WebFrame::AttachGuest)
.SetMethod("setSpellCheckProvider", &WebFrame::SetSpellCheckProvider)
.SetMethod("registerUrlSchemeAsSecure",

View file

@ -7,6 +7,7 @@
#include <string>
#include "atom/renderer/guest_view_container.h"
#include "base/memory/scoped_ptr.h"
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
@ -42,6 +43,9 @@ class WebFrame : public mate::Wrappable {
v8::Local<v8::Value> RegisterEmbedderCustomElement(
const base::string16& name, v8::Local<v8::Object> options);
void RegisterElementResizeCallback(
int element_instance_id,
const GuestViewContainer::ResizeCallback& callback);
void AttachGuest(int element_instance_id);
// Set the provider that will be used by SpellCheckClient for spell check.

View file

@ -4,12 +4,54 @@
#include "atom/renderer/guest_view_container.h"
#include <map>
#include "base/lazy_instance.h"
namespace atom {
GuestViewContainer::GuestViewContainer(content::RenderFrame* render_frame) {
namespace {
using GuestViewContainerMap = std::map<int, GuestViewContainer*>;
static base::LazyInstance<GuestViewContainerMap> g_guest_view_container_map =
LAZY_INSTANCE_INITIALIZER;
} // namespace
GuestViewContainer::GuestViewContainer(content::RenderFrame* render_frame)
: render_frame_(render_frame) {
}
GuestViewContainer::~GuestViewContainer() {
if (element_instance_id_ > 0)
g_guest_view_container_map.Get().erase(element_instance_id_);
}
// static
GuestViewContainer* GuestViewContainer::FromID(int element_instance_id) {
GuestViewContainerMap* guest_view_containers =
g_guest_view_container_map.Pointer();
auto it = guest_view_containers->find(element_instance_id);
return it == guest_view_containers->end() ? nullptr : it->second;
}
void GuestViewContainer::RegisterElementResizeCallback(
const ResizeCallback& callback) {
element_resize_callback_ = callback;
}
void GuestViewContainer::SetElementInstanceID(int element_instance_id) {
element_instance_id_ = element_instance_id;
g_guest_view_container_map.Get().insert(
std::make_pair(element_instance_id, this));
}
void GuestViewContainer::DidResizeElement(const gfx::Size& old_size,
const gfx::Size& new_size) {
if (element_resize_callback_.is_null())
return;
element_resize_callback_.Run(old_size, new_size);
}
} // namespace atom

View file

@ -5,17 +5,38 @@
#ifndef ATOM_RENDERER_GUEST_VIEW_CONTAINER_H_
#define ATOM_RENDERER_GUEST_VIEW_CONTAINER_H_
#include "base/callback.h"
#include "content/public/renderer/browser_plugin_delegate.h"
#include "v8/include/v8.h"
namespace gfx {
class Size;
}
namespace atom {
class GuestViewContainer : public content::BrowserPluginDelegate {
public:
typedef base::Callback<void(const gfx::Size&, const gfx::Size&)>
ResizeCallback;
explicit GuestViewContainer(content::RenderFrame* render_frame);
~GuestViewContainer() override;
static GuestViewContainer* FromID(int element_instance_id);
void RegisterElementResizeCallback(const ResizeCallback& callback);
// content::BrowserPluginDelegate:
void SetElementInstanceID(int element_instance_id) final;
void DidResizeElement(const gfx::Size& old_size,
const gfx::Size& new_size) final;
private:
int element_instance_id_;
content::RenderFrame* render_frame_;
ResizeCallback element_resize_callback_;
DISALLOW_COPY_AND_ASSIGN(GuestViewContainer);
};

View file

@ -86,6 +86,9 @@ class WebViewImpl
@browserPluginNode.removeAttribute webViewConstants.ATTRIBUTE_INTERNALINSTANCEID
@internalInstanceId = parseInt newValue
# Track when the element resizes using the element resize callback.
webFrame.registerElementResizeCallback @internalInstanceId, @onElementResize.bind(this)
return unless @guestInstanceId
guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, @buildParams()
@ -120,6 +123,18 @@ class WebViewImpl
# changed.
@dispatchEvent webViewEvent
onElementResize: (oldSize, newSize) ->
# Dispatch the 'resize' event.
resizeEvent = new Event('resize', bubbles: true)
resizeEvent.oldWidth = oldSize.width
resizeEvent.oldHeight = oldSize.height
resizeEvent.newWidth = newSize.width
resizeEvent.newHeight = newSize.height
@dispatchEvent resizeEvent
if @guestInstanceId
guestViewInternal.setSize @guestInstanceId, normal: newSize
createGuest: ->
guestViewInternal.createGuest @buildParams(), (guestInstanceId) =>
@attachWindow guestInstanceId
@ -167,9 +182,10 @@ class WebViewImpl
# the on display:block.
css = window.getComputedStyle @webviewNode, null
elementRect = @webviewNode.getBoundingClientRect()
params.elementSize =
width: parseInt(elementRect.width) || parseInt(css.getPropertyValue('width'))
height: parseInt(elementRect.height) || parseInt(css.getPropertyValue('height'))
params.elementWidth = parseInt(elementRect.width) ||
parseInt(css.getPropertyValue('width'))
params.elementHeight = parseInt(elementRect.height) ||
parseInt(css.getPropertyValue('height'))
params
attachWindow: (guestInstanceId) ->

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit 1696237a3f444f0e33a8947749b4d70f6feb511b
Subproject commit ad207eeabb0185f74c017e70ca3411d522627ff0